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.

233 lines
8.9 KiB

  1. //======= Copyright � 1996-2006, Valve Corporation, All rights reserved. ======
  2. //
  3. // Mesh Manipulation Utilities
  4. //
  5. //=============================================================================
  6. #ifndef DMMESHUTILS_H
  7. #define DMMESHUTILS_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #include "movieobjects/dmedag.h"
  12. #include "movieobjects/dmmeshcomp.h"
  13. #include "movieobjects/dmevertexdata.h"
  14. #include "tier1/utlstring.h"
  15. #include "tier1/UtlStringMap.h"
  16. #include "tier1/utlvector.h"
  17. class CDmeMesh;
  18. class CDmeModel;
  19. class CDmMeshComp::CEdge;
  20. class CDmeCombinationOperator;
  21. class CDmePreset;
  22. class CDmePresetGroup;
  23. class CDmeFaceSet;
  24. class CMesh;
  25. bool SaveMeshesToDMX( CUtlVector<CMesh*> &inputMeshes, const char *pDMXFile, bool bForce2DTexcoords = true );
  26. bool LoadMeshesFromDMX( CUtlVector<CMesh*> &outputMeshes, const char *pDMXFile );
  27. bool LoadMeshes( CUtlVector<CMesh*> &outputMeshes, CDmeModel *pModel, float flScale, int *pBoneRemap = NULL );
  28. bool LoadCollisionMeshes( CUtlVector< CMesh* > &outputMeshes, CUtlVector< CDmeDag* > &outputDags, CDmeModel *pModel, float flScale );
  29. bool ConvertMeshFromDMX( CMesh *pMeshOut, CDmeMesh *pDmeMesh );
  30. void ConvertMeshToDMX( CDmeMesh *pDmeMeshOut, CMesh *pMeshIn, bool bForce2DTexcoords );
  31. //-----------------------------------------------------------------------------
  32. // Mesh Utility function class (more like a namespace)
  33. //-----------------------------------------------------------------------------
  34. class CDmMeshUtils
  35. {
  36. public:
  37. static bool RemoveLargeAxisAlignedPlanarFaces( CDmeMesh *pMesh );
  38. static bool RemoveFacesWithMaterial( CDmeMesh *pMesh, const char *pMaterialName );
  39. static bool RemoveFacesWithMoreThanNVerts( CDmeMesh *pMesh, const int nVertexCount );
  40. enum Axis_t
  41. {
  42. kXAxis,
  43. kYAxis,
  44. kZAxis
  45. };
  46. static bool Mirror( CDmeMesh *pMesh, int axis = kXAxis );
  47. static bool RemapMaterial( CDmeMesh *pMesh, const CUtlString &src, const CUtlString &dst );
  48. static bool RemapMaterial( CDmeMesh *pMesh, const int nMaterialIndex, const CUtlString &dst );
  49. // Merge the specified mesh onto the first mesh under pRoot that fits it
  50. static bool Merge( CDmeMesh *pSrcMesh, CDmElement *pRoot );
  51. static bool Merge( CDmeMesh *pSrcMesh, CDmeMesh *pDstMesh, int nSkinningJointIndex, CUtlMap< int, int > *pJointMap = NULL );
  52. static bool CreateDeltasFromPresets( CDmeMesh *pMesh, CDmeVertexData *pPassedDst, const CUtlStringMap< CUtlString > &presetMap, bool bPurge, const CUtlVector< CUtlString > *pPurgeAllButThese = NULL );
  53. static bool PurgeUnusedDeltas( CDmeMesh *pMesh );
  54. enum WrinkleOp
  55. {
  56. kReplace,
  57. kAdd
  58. };
  59. static bool CreateWrinkleDeltaFromBaseState( CDmeVertexDeltaData *pDelta, float flScale = 1.0f, WrinkleOp wrinkleOp = kReplace, CDmeMesh *pMesh = NULL, CDmeVertexData *pBind = NULL, CDmeVertexData *pCurrent = NULL, bool bUseNormalForSign = false );
  60. static int FindMergeSocket(
  61. const CUtlVector< CUtlVector< CDmMeshComp::CEdge * > > &srcBorderEdgesList,
  62. CDmeMesh *pDstMesh );
  63. static bool Merge(
  64. CDmMeshComp &srcComp,
  65. const CUtlVector< CDmMeshComp::CEdge * > &edgeList,
  66. CDmeMesh *pDstMesh );
  67. static bool NewMerge(
  68. CDmeMesh *pDstMesh,
  69. CDmeMesh *pSrcMesh,
  70. const CUtlVector< CDmMeshComp::CEdge * > &edgeList,
  71. float flTolerance = 1.0e-4,
  72. float flTexCoordTolerance = 1.0e-6 );
  73. static CDmeModel *GetDmeModelFromMesh( CDmeMesh *pDmeMesh );
  74. // Does a depth first walk of the DmeDag hierarchy specified until a DmeDag with the
  75. // requested name is found, returns that dag if found, NULL if not found or on error
  76. static CDmeDag *FindDagByNameInHierarchy( CDmeDag *pSearchDag, const char *pszSearchName );
  77. // Adds the joint to the specified model by finding the common ancestor
  78. // Duplicates the required hierarchy of the source joint
  79. static CDmeDag *AddJointToModel( CDmeModel *pDstModel, CDmeDag *pSrcJoint );
  80. static bool MergeMeshAndSkeleton( CDmeMesh *pDstMesh, CDmeMesh *pSrcMesh );
  81. protected:
  82. static const int *BuildDataMirrorMap( CDmeVertexData *pBase, int axis, CDmeVertexData::StandardFields_t standardField, CUtlVector< int > &dataMirrorMap );
  83. static bool MirrorVertices( CDmeMesh *pMesh, CDmeVertexData *pBase, int axis, CUtlVector< int > &mirrorMap );
  84. static bool MirrorVertices( CDmeVertexData *pBase, int axis, int nOldVertexCount, int nMirrorCount, const CUtlVector< int > &mirrorMap, const CUtlVector< int > &posMirrorMap, const CUtlVector< int > &normalMirrorMap, const CUtlVector< int > &uvMirrorMap );
  85. static void MirrorVertices( CDmeVertexData *pBase, FieldIndex_t fieldIndex, int nOldVertexCount, int nMirrorCount, const CUtlVector< int > &baseIndices, const CUtlVector< int > &mirrorMap );
  86. static bool MirrorDelta( CDmeVertexDeltaData *pDelta, int axis, const CUtlVector< int > &posMirrorMap, const CUtlVector< int > &normalMirrorMap, const CUtlVector< int > &uvMirrorMap );
  87. static bool PurgeUnusedData( CDmeMesh *pMesh );
  88. static void CreateDeltasFromPresetGroup( CDmePresetGroup *pPresetGroup, CDmeCombinationOperator * pComboOp, const CUtlVector< CUtlString > * pPurgeAllButThese, CDmeMesh * pMesh, CDmeVertexData * pDst, CUtlStringMap< CUtlString > &conflictingNames, CUtlStringMap< CDmePreset * > &presetMap );
  89. static void PurgeUnreferencedDeltas( CDmeMesh *pMesh, CUtlStringMap< CDmePreset * > &presetMap, const CUtlVector< CUtlString > *pPurgeAllButThese, CDmeCombinationOperator *pComboOp );
  90. };
  91. //-----------------------------------------------------------------------------
  92. // Iterate over all of the faces in a mesh. Use it like this:
  93. //
  94. // for ( CDmMeshFaceIt fIt( pMesh ); !fIt.IsDone(); fIt.Next() )
  95. // {
  96. // }
  97. //-----------------------------------------------------------------------------
  98. class CDmMeshFaceIt
  99. {
  100. public:
  101. // Constructs a new face iterator for the specified mesh
  102. CDmMeshFaceIt( const CDmeMesh *pMesh, const CDmeVertexData *pVertexData = NULL );
  103. // Resets the iterator to the start of the specified mesh or for another iteration of the
  104. // current mesh if the specified mesh is NULL
  105. bool Reset( const CDmeMesh *pMesh = NULL, const CDmeVertexData *pVertexData = NULL );
  106. // Returns the total number of faces in the mesh
  107. int Count() const;
  108. // The number of vertices in the face
  109. int VertexCount() const;
  110. // Is the iterator at the end of the mesh?
  111. bool IsDone() const;
  112. // Move the iterator to the next face
  113. bool Next();
  114. // Returns opposite sense of IsDone(), i.e. !IsDone() so true if iterator still has stuff to do
  115. operator bool() const { return !IsDone(); }
  116. // Prefix ++ which just calls Next() for syntax sugar
  117. CDmMeshFaceIt &operator ++() { Next(); return *this; }
  118. // Postfix ++ which just calls Next() for syntax sugar
  119. CDmMeshFaceIt operator ++( int ) { const CDmMeshFaceIt thisCopy( *this ); ++( *this ); return thisCopy; }
  120. // Gets the vertex indices for the vertices of this face
  121. bool GetVertexIndices( int *pIndices, int nIndices ) const;
  122. // Gets the vertex indices for the vertices of this face
  123. bool GetVertexIndices( CUtlVector< int > &vertexIndices ) const;
  124. // Gets the mesh relative vertex index given the face relative vertex index
  125. int GetVertexIndex( int nFaceRelativeVertexIndex ) const;
  126. // Gets the specified vertex data for the vertices of this face (if it exists) and the type matches
  127. template < class T_t >
  128. bool GetVertexData(
  129. CUtlVector< T_t > &vertexData,
  130. CDmeVertexDataBase::StandardFields_t nStandardField,
  131. CDmeVertexData *pPassedBase = NULL ) const;
  132. protected:
  133. bool SetFaceSet();
  134. const CDmeMesh *m_pMesh; // Current Mesh
  135. const CDmeVertexData *m_pVertexData; // Current Vertex Data
  136. int m_nFaceSetCount; // Number of face sets in current mesh: m_pMesh->FaceSetCount();
  137. int m_nFaceSetIndex; // Index of current face set: [ 0, m_nFaceSetCount ]
  138. const CDmeFaceSet *m_pFaceSet; // Current face set: m_pMesh->GetFaceSet( m_nFaceSetIndex )
  139. int m_nFaceSetIndexCount; // Number of indices in current face set: m_pFaceSet->NumIndices();
  140. int m_nFaceSetIndexIndex; // Index of current face set's indices: [ 0, m_nFaceSetIndexCount ]
  141. int m_nFaceCount; // Number of faces in current mesh
  142. int m_nFaceIndex; // The current face in the iteration [ 0, m_nFaceCount ]
  143. };
  144. //-----------------------------------------------------------------------------
  145. //
  146. //-----------------------------------------------------------------------------
  147. template < class T_t >
  148. inline bool CDmMeshFaceIt::GetVertexData(
  149. CUtlVector< T_t > &vertexData,
  150. CDmeVertexDataBase::StandardFields_t nStandardField,
  151. CDmeVertexData *pPassedBase /* = NULL */ ) const
  152. {
  153. vertexData.RemoveAll();
  154. if ( IsDone() )
  155. return false;
  156. CDmeVertexData *pBase = pPassedBase ? pPassedBase : m_pMesh->GetCurrentBaseState();
  157. if ( !pBase )
  158. return false;
  159. const FieldIndex_t nFieldIndex = pBase->FindFieldIndex( nStandardField );
  160. if ( nFieldIndex < 0 )
  161. return false;
  162. CDmAttribute *pDataAttr = pBase->GetVertexData( nFieldIndex );
  163. if ( pDataAttr->GetType() != CDmAttributeInfo< CUtlVector< T_t > >().AttributeType() )
  164. return false;
  165. const CDmrArrayConst< T_t > data( pDataAttr );
  166. const CUtlVector< int > &indices( pBase->GetVertexIndexData( nFieldIndex ) );
  167. }
  168. #endif // DMMESHUTILS_H