Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
12 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // A class representing vertex data
  4. //
  5. //=============================================================================
  6. #ifndef DMEVERTEXDATA_H
  7. #define DMEVERTEXDATA_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattribute.h"
  13. #include "datamodel/dmattributevar.h"
  14. #include "mathlib/vector.h"
  15. #include "Color.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class Vector;
  20. class Vector4D;
  21. class Color;
  22. //-----------------------------------------------------------------------------
  23. // Used to represent fields
  24. //-----------------------------------------------------------------------------
  25. typedef int FieldIndex_t;
  26. class CDmeVertexDataBase : public CDmElement
  27. {
  28. DEFINE_ELEMENT( CDmeVertexDataBase, CDmElement );
  29. public:
  30. // NOTE: If you add fields to this, add to g_pStandardFieldNames in dmevertexdata.cpp
  31. enum StandardFields_t
  32. {
  33. FIELD_POSITION,
  34. FIELD_NORMAL,
  35. FIELD_TANGENT,
  36. FIELD_TEXCOORD,
  37. FIELD_COLOR,
  38. FIELD_JOINT_WEIGHTS,
  39. FIELD_JOINT_INDICES,
  40. FIELD_BALANCE, // Used by left/right delta states
  41. FIELD_MORPH_SPEED, // Used to author morph speeds
  42. FIELD_WRINKLE, // Used to author morphed wrinklemaps
  43. FIELD_WEIGHT, // Weight is just the different between the base position and the delta position
  44. FIELD_CLOTH_ENABLE,
  45. STANDARD_FIELD_COUNT,
  46. };
  47. // resolve internal data from changed attributes
  48. virtual void Resolve();
  49. // Returns the number of joints per vertex
  50. int JointCount() const;
  51. // Vertex accessors
  52. int VertexCount() const;
  53. const Vector& GetPosition( int nVertexIndex ) const;
  54. const Vector& GetNormal( int nVertexIndex ) const;
  55. const Vector2D& GetTexCoord( int nVertexIndex ) const;
  56. const Vector4D& GetTangent( int nVertexIndex ) const;
  57. const Color& GetColor( int nVertexIndex ) const;
  58. const float *GetJointWeights( int nVertexIndex ) const;
  59. const float *GetJointPositionWeights( int nPositionIndex ) const;
  60. const int *GetJointIndices( int nVertexIndex ) const;
  61. const int *GetJointPositionIndices( int nPositionIndex ) const;
  62. float GetBalance( int nVertexIndex ) const;
  63. float GetMorphSpeed( int nVertexIndex ) const;
  64. float GetWrinkle( int nVertexIndex ) const;
  65. float GetWeight( int nVertexIndex ) const;
  66. // Returns indices into the various fields
  67. int GetPositionIndex( int nVertexIndex ) const;
  68. int GetNormalIndex( int nVertexIndex ) const;
  69. int GetTangentIndex( int nVertexIndex ) const;
  70. int GetTexCoordIndex( int nVertexIndex ) const;
  71. int GetColorIndex( int nVertexIndex ) const;
  72. int GetBalanceIndex( int nVertexIndex ) const;
  73. int GetMorphSpeedIndex( int nVertexIndex ) const;
  74. int GetWrinkleIndex( int nVertexIndex ) const;
  75. int GetWeightIndex( int nVertexIndex ) const;
  76. // Creates a new vertex field. NOTE: This cannot be used to create joint weights + indices
  77. template< class T >
  78. FieldIndex_t CreateField( const char *pFieldName );
  79. FieldIndex_t CreateField( const char *pFieldName, DmAttributeType_t type );
  80. FieldIndex_t CreateField( StandardFields_t fieldId );
  81. // Use this to create vertex fields for joint weights + indices
  82. void CreateJointWeightsAndIndices( int nJointCount, FieldIndex_t *pJointWeightsField, FieldIndex_t *pJointIndicesField );
  83. // Returns the field index of a particular field
  84. FieldIndex_t FindFieldIndex( const char *pFieldName ) const;
  85. FieldIndex_t FindFieldIndex( StandardFields_t nFieldIndex ) const;
  86. // Adds a new vertex, returns the vertex index
  87. // NOTE: This will also add vertex indices for DmeMeshDeltaData
  88. int AddVertexData( FieldIndex_t nFieldIndex, int nCount );
  89. // Sets vertex data
  90. void SetVertexData( FieldIndex_t nFieldIndex, int nFirstVertex, int nCount, DmAttributeType_t valueType, const void *pData );
  91. void SetVertexIndices( FieldIndex_t nFieldIndex, int nFirstIndex, int nCount, const int *pIndices );
  92. // Removes all vertex data associated with a particular field
  93. void RemoveAllVertexData( FieldIndex_t nFieldIndex );
  94. // Returns arbitrary vertex + index data
  95. CDmAttribute* GetVertexData( FieldIndex_t nFieldIndex );
  96. const CDmAttribute* GetVertexData( FieldIndex_t nFieldIndex ) const;
  97. CDmAttribute* GetIndexData( FieldIndex_t nFieldIndex );
  98. const CDmAttribute* GetIndexData( FieldIndex_t nFieldIndex ) const;
  99. // Returns well-known vertex data
  100. const CUtlVector<Vector> &GetPositionData( ) const;
  101. const CUtlVector<Vector> &GetNormalData( ) const;
  102. const CUtlVector<Vector4D> &GetTangentData( ) const;
  103. const CUtlVector<Vector2D> &GetTextureCoordData( ) const;
  104. const CUtlVector<Color> &GetColorData( ) const;
  105. const float *GetJointWeightData( int nDataIndex ) const;
  106. const int *GetJointIndexData( int nDataIndex ) const;
  107. const CUtlVector<float> &GetBalanceData( ) const;
  108. const CUtlVector<float> &GetMorphSpeedData( ) const;
  109. const CUtlVector<float> &GetWrinkleData( ) const;
  110. const CUtlVector<float> &GetWeightData( ) const;
  111. // Returns well-known index data
  112. const CUtlVector<int> &GetVertexIndexData( FieldIndex_t nFieldIndex ) const;
  113. const CUtlVector<int> &GetVertexIndexData( StandardFields_t fieldId ) const;
  114. // Do we have skinning data?
  115. bool HasSkinningData() const;
  116. // Do we have cloth_enable data?
  117. bool HasClothData();
  118. // Do we need tangent data? (Utility method for applications to know if they should call ComputeDefaultTangentData)
  119. bool NeedsTangentData() const;
  120. // Should we flip the V coordinates?
  121. bool IsVCoordinateFlipped() const;
  122. void FlipVCoordinate( bool bFlip );
  123. // Returns an inverse map from vertex data index to vertex index
  124. const CUtlVector< int > &FindVertexIndicesFromDataIndex( FieldIndex_t nFieldIndex, int nDataIndex );
  125. const CUtlVector< int > &FindVertexIndicesFromDataIndex( StandardFields_t nFieldIndex, int nDataIndex );
  126. int FieldCount() const;
  127. const char *FieldName( int i ) const;
  128. void CopyFrom( CDmeVertexDataBase *pSrc );
  129. void CopyTo( CDmeVertexDataBase *pDst ) const;
  130. // Reskins the vertex data to new bones
  131. // The joint index remap maps an initial bone index to a new bone index
  132. void Reskin( const int *pJointTransformIndexRemap );
  133. protected:
  134. struct FieldInfo_t
  135. {
  136. CUtlString m_Name;
  137. CDmAttribute *m_pVertexData;
  138. CDmAttribute* m_pIndexData;
  139. CUtlVector< CUtlVector< int > > m_InverseMap;
  140. bool m_bInverseMapDirty;
  141. };
  142. // Derived classes must inherit
  143. virtual bool IsVertexDeltaData() const { Assert(0); return false; }
  144. // Computes the vertex count ( min of the index buffers )
  145. void ComputeFieldInfo();
  146. // Computes the vertex count ( min of the index buffers )
  147. void ComputeVertexCount();
  148. // Updates info for fast lookups for well-known fields
  149. void UpdateStandardFieldInfo( int nFieldIndex, const char *pFieldName, DmAttributeType_t attrType );
  150. // Adds a field to the vertex format
  151. void FindOrAddVertexField( const char *pFieldName );
  152. // Returns the index of a particular field
  153. int GetFieldIndex( int nVertexIndex, StandardFields_t nFieldIndex ) const;
  154. // List of names of attributes containing vertex data
  155. CDmaStringArray m_VertexFormat;
  156. CDmaVar< int > m_nJointCount;
  157. CDmaVar< bool > m_bFlipVCoordinates;
  158. CUtlVector< FieldInfo_t > m_FieldInfo;
  159. FieldIndex_t m_pStandardFieldIndex[STANDARD_FIELD_COUNT];
  160. int m_nVertexCount;
  161. };
  162. //-----------------------------------------------------------------------------
  163. // Creates a particular vertex data field + associated index field
  164. //-----------------------------------------------------------------------------
  165. template< class T >
  166. inline FieldIndex_t CDmeVertexDataBase::CreateField( const char *pFieldName )
  167. {
  168. return CreateField( pFieldName, CDmAttributeInfo< CUtlVector<T> >::AttributeType() );
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Returns a standard field index
  172. //-----------------------------------------------------------------------------
  173. inline FieldIndex_t CDmeVertexDataBase::FindFieldIndex( StandardFields_t nFieldIndex ) const
  174. {
  175. return m_pStandardFieldIndex[ nFieldIndex ];
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Vertex field accessors
  179. //-----------------------------------------------------------------------------
  180. inline int CDmeVertexDataBase::VertexCount() const
  181. {
  182. return m_nVertexCount;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Returns the number of joints per vertex
  186. //-----------------------------------------------------------------------------
  187. inline int CDmeVertexDataBase::JointCount() const
  188. {
  189. return m_nJointCount;
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Should we flip the V coordinates?
  193. //-----------------------------------------------------------------------------
  194. inline bool CDmeVertexDataBase::IsVCoordinateFlipped() const
  195. {
  196. return m_bFlipVCoordinates;
  197. }
  198. inline void CDmeVertexDataBase::FlipVCoordinate( bool bFlip )
  199. {
  200. m_bFlipVCoordinates = bFlip;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Returns arbitrary vertex data
  204. //-----------------------------------------------------------------------------
  205. inline CDmAttribute* CDmeVertexDataBase::GetVertexData( FieldIndex_t nFieldIndex )
  206. {
  207. return m_FieldInfo[ nFieldIndex ].m_pVertexData;
  208. }
  209. inline const CDmAttribute* CDmeVertexDataBase::GetVertexData( FieldIndex_t nFieldIndex ) const
  210. {
  211. return m_FieldInfo[ nFieldIndex ].m_pVertexData;
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Returns arbitrary index data
  215. //-----------------------------------------------------------------------------
  216. inline CDmAttribute* CDmeVertexDataBase::GetIndexData( FieldIndex_t nFieldIndex )
  217. {
  218. return m_FieldInfo[ nFieldIndex ].m_pIndexData;
  219. }
  220. inline const CDmAttribute* CDmeVertexDataBase::GetIndexData( FieldIndex_t nFieldIndex ) const
  221. {
  222. return m_FieldInfo[ nFieldIndex ].m_pIndexData;
  223. }
  224. //-----------------------------------------------------------------------------
  225. // Utility method for getting at various vertex field indices
  226. //-----------------------------------------------------------------------------
  227. inline int CDmeVertexDataBase::GetFieldIndex( int nVertexIndex, StandardFields_t nFieldId ) const
  228. {
  229. Assert( nVertexIndex < m_nVertexCount );
  230. FieldIndex_t nFieldIndex = m_pStandardFieldIndex[nFieldId];
  231. if ( nFieldIndex < 0 )
  232. return -1;
  233. CDmrArrayConst<int> indices( GetIndexData( nFieldIndex ) );
  234. return indices[ nVertexIndex ];
  235. }
  236. //-----------------------------------------------------------------------------
  237. //
  238. // Vertex Data for base states
  239. //
  240. //-----------------------------------------------------------------------------
  241. class CDmeVertexData : public CDmeVertexDataBase
  242. {
  243. DEFINE_ELEMENT( CDmeVertexData, CDmeVertexDataBase );
  244. public:
  245. // Adds a new vertex; creates a new entry in all vertex data fields
  246. int AddVertexIndices( int nCount );
  247. private:
  248. virtual bool IsVertexDeltaData() const { return false; }
  249. };
  250. //-----------------------------------------------------------------------------
  251. //
  252. // Vertex Data for delta states
  253. //
  254. //-----------------------------------------------------------------------------
  255. class CDmeVertexDeltaData : public CDmeVertexDataBase
  256. {
  257. DEFINE_ELEMENT( CDmeVertexDeltaData, CDmeVertexDataBase );
  258. public:
  259. // Computes wrinkle data from position deltas
  260. // NOTE: Pass in negative scales to get 'compression', positive to get 'expansion'
  261. void GenerateWrinkleDelta( CDmeVertexData *pBindState, float flScale, bool bOverwrite, bool bUseNormalForSign = false );
  262. // Updates existing data or generates new data
  263. void UpdateWrinkleDelta( CDmeVertexData *pBindState, float flOldScale, float flNewScale );
  264. // Computes a float map which is the distance between the base and delta position
  265. // The maximum distance any vertex is moved is returned
  266. float GenerateWeightDelta( CDmeVertexData *pBindState );
  267. CDmaVar< bool > m_bCorrected;
  268. CDmaVar< bool > m_bRenderVerts;
  269. private:
  270. virtual bool IsVertexDeltaData() const { return true; }
  271. // Computes max positional delta length
  272. float ComputeMaxDeflection( );
  273. };
  274. #endif // DMEVERTEXDATA_H