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.

211 lines
8.6 KiB

  1. //=========== Copyright � Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Mesh class dmx saving functions
  4. //
  5. //===========================================================================//
  6. #include "movieobjects/dmemodel.h"
  7. #include "movieobjects/dmemesh.h"
  8. #include "movieobjects/dmefaceset.h"
  9. #include "movieobjects/dmematerial.h"
  10. #include "movieobjects/dmeclip.h"
  11. #include "movieobjects/dmechannel.h"
  12. #include "movieobjects/dmeattachment.h"
  13. #include "movieobjects/dmeanimationlist.h"
  14. #include "movieobjects/dmecombinationoperator.h"
  15. #include "mdlobjects/dmebbox.h"
  16. #include "mdlobjects/dmelod.h"
  17. #include "mdlobjects/dmelodlist.h"
  18. #include "mdlobjects/dmebodygroup.h"
  19. #include "mdlobjects/dmebodygrouplist.h"
  20. #include "mdlobjects/dmehitbox.h"
  21. #include "mdlobjects/dmehitboxset.h"
  22. #include "mdlobjects/dmehitboxsetlist.h"
  23. #include "mdlobjects/dmesequence.h"
  24. #include "mdlobjects/dmesequencelist.h"
  25. #include "mdlobjects/dmecollisionmodel.h"
  26. #include "mdlobjects/dmecollisionjoints.h"
  27. #include "mdlobjects/dmeincludemodellist.h"
  28. #include "mdlobjects/dmedefinebone.h"
  29. #include "mdlobjects/dmedefinebonelist.h"
  30. #include "mdlobjects/dmematerialgroup.h"
  31. #include "mdlobjects/dmematerialgrouplist.h"
  32. #include "mdlobjects/dmeeyeball.h"
  33. #include "mdlobjects/dmeeyeballglobals.h"
  34. #include "mdlobjects/dmeboneweight.h"
  35. #include "mdlobjects/dmebonemask.h"
  36. #include "mdlobjects/dmebonemasklist.h"
  37. #include "mdlobjects/dmeik.h"
  38. #include "mdlobjects/dmeanimcmd.h"
  39. #include "mdlobjects/dmemotioncontrol.h"
  40. #include "mdlobjects/dmeposeparameter.h"
  41. #include "mdlobjects/dmeposeparameterlist.h"
  42. #include "mdlobjects/dmeanimblocksize.h"
  43. #include "meshutils/mesh.h"
  44. CDmeVertexDataBase::StandardFields_t g_AttribToField[] =
  45. {
  46. CDmeVertexData::FIELD_POSITION, //VERTEX_ELEMENT_POSITION = 0,
  47. CDmeVertexData::FIELD_POSITION, //VERTEX_ELEMENT_POSITION4D = 1,
  48. CDmeVertexData::FIELD_NORMAL, //VERTEX_ELEMENT_NORMAL = 2,
  49. CDmeVertexData::FIELD_NORMAL, //VERTEX_ELEMENT_NORMAL4D = 3,
  50. CDmeVertexData::FIELD_COLOR, //VERTEX_ELEMENT_COLOR = 4,
  51. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_SPECULAR = 5,
  52. CDmeVertexData::FIELD_TANGENT, //VERTEX_ELEMENT_TANGENT_S = 6,
  53. CDmeVertexData::FIELD_TANGENT, //VERTEX_ELEMENT_TANGENT_T = 7,
  54. CDmeVertexData::FIELD_WRINKLE, //VERTEX_ELEMENT_WRINKLE = 8,
  55. CDmeVertexData::FIELD_JOINT_INDICES, //VERTEX_ELEMENT_BONEINDICES = 9,
  56. CDmeVertexData::FIELD_JOINT_WEIGHTS, //VERTEX_ELEMENT_BONEWEIGHTS1 = 10,
  57. CDmeVertexData::FIELD_JOINT_WEIGHTS, //VERTEX_ELEMENT_BONEWEIGHTS2 = 11,
  58. CDmeVertexData::FIELD_JOINT_WEIGHTS, //VERTEX_ELEMENT_BONEWEIGHTS3 = 12,
  59. CDmeVertexData::FIELD_JOINT_WEIGHTS, //VERTEX_ELEMENT_BONEWEIGHTS4 = 13,
  60. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_USERDATA1 = 14,
  61. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_USERDATA2 = 15,
  62. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_USERDATA3 = 16,
  63. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_USERDATA4 = 17,
  64. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_0 = 18,
  65. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_1 = 19,
  66. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_2 = 20,
  67. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_3 = 21,
  68. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_4 = 22,
  69. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_5 = 23,
  70. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_6 = 24,
  71. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD1D_7 = 25,
  72. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_0 = 26,
  73. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_1 = 27,
  74. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_2 = 28,
  75. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_3 = 29,
  76. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_4 = 30,
  77. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_5 = 31,
  78. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_6 = 32,
  79. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD2D_7 = 33,
  80. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_0 = 34,
  81. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_1 = 35,
  82. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_2 = 36,
  83. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_3 = 37,
  84. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_4 = 38,
  85. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_5 = 39,
  86. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_6 = 40,
  87. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD3D_7 = 41,
  88. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_0 = 42,
  89. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_1 = 43,
  90. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_2 = 44,
  91. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_3 = 45,
  92. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_4 = 46,
  93. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_5 = 47,
  94. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_6 = 48,
  95. CDmeVertexData::FIELD_TEXCOORD, //VERTEX_ELEMENT_TEXCOORD4D_7 = 49,
  96. };
  97. DmAttributeType_t g_atForSizeFloats[] =
  98. {
  99. AT_FLOAT,
  100. AT_VECTOR2,
  101. AT_VECTOR3,
  102. AT_VECTOR4
  103. };
  104. void ConvertMeshToDMX( CDmeMesh *pDmeMeshOut, CMesh *pMeshIn, bool bForce2DTexcoords )
  105. {
  106. CDmeVertexData *pVertexData = pDmeMeshOut->FindOrCreateBaseState( "bind" );
  107. pDmeMeshOut->SetCurrentBaseState( "bind" );
  108. //pDmeMeshOut->SetBindBaseState( pVertexData );
  109. pVertexData->AddVertexIndices( pMeshIn->m_nIndexCount );
  110. pVertexData->FlipVCoordinate( false );
  111. // Add the vertices
  112. for ( int a=0; a<pMeshIn->m_nAttributeCount; ++a )
  113. {
  114. // translate CMesh attributes to dme mesh attributes
  115. CMeshVertexAttribute &attrib = pMeshIn->m_pAttributes[ a ];
  116. CDmeVertexDataBase::StandardFields_t field = g_AttribToField[ attrib.m_nType ];
  117. int nSize = GetVertexElementSize( attrib.m_nType, VERTEX_COMPRESSION_NONE );
  118. if ( bForce2DTexcoords && field == CDmeVertexData::FIELD_TEXCOORD )
  119. {
  120. nSize = 8;
  121. }
  122. int nFloats = nSize / sizeof( float );
  123. DmAttributeType_t at = g_atForSizeFloats[ nFloats - 1 ];
  124. // add a vertex field
  125. const FieldIndex_t pIndex( pVertexData->CreateField( field ) );
  126. pVertexData->AddVertexData( pIndex, pMeshIn->m_nVertexCount );
  127. float *pFieldData = new float[ pMeshIn->m_nVertexCount * nFloats ];
  128. float *pFieldStart = pFieldData;
  129. float *pVertStart = pMeshIn->m_pVerts + attrib.m_nOffsetFloats;
  130. for ( int v=0; v<pMeshIn->m_nVertexCount; ++v )
  131. {
  132. Q_memcpy( pFieldStart, pVertStart, nSize );
  133. pFieldStart += nFloats;
  134. pVertStart += pMeshIn->m_nVertexStrideFloats;
  135. }
  136. pVertexData->SetVertexData( pIndex, 0, pMeshIn->m_nVertexCount, at, pFieldData );
  137. delete []pFieldData;
  138. pVertexData->SetVertexIndices( pIndex, 0, pMeshIn->m_nIndexCount, (int*)pMeshIn->m_pIndices );
  139. }
  140. // Add a material
  141. CDmeMaterial *pMaterial = CreateElement< CDmeMaterial >( "material", pDmeMeshOut->GetFileId() );
  142. pMaterial->SetMaterial( pMeshIn->m_materialName );
  143. // Add a face set
  144. CDmeFaceSet *pFaceSet = CreateElement< CDmeFaceSet >( "faceSet", pDmeMeshOut->GetFileId() );
  145. int nFaceSetIndices = ( pMeshIn->m_nIndexCount / 3 ) * 4;
  146. pFaceSet->AddIndices( nFaceSetIndices );
  147. int nIndexCounter = 0;
  148. for ( int i=0; i<nFaceSetIndices; i+=4 )
  149. {
  150. pFaceSet->SetIndex( i, nIndexCounter++ );
  151. pFaceSet->SetIndex( i + 1, nIndexCounter++ );
  152. pFaceSet->SetIndex( i + 2, nIndexCounter++ );
  153. pFaceSet->SetIndex( i + 3, -1 );
  154. }
  155. //pFaceSet->SetIndices( 0, faceSetIndices.Count(), faceSetIndices.Base() );
  156. pFaceSet->SetMaterial( pMaterial );
  157. pDmeMeshOut->AddFaceSet( pFaceSet );
  158. }
  159. bool SaveMeshesToDMX( CUtlVector<CMesh*> &inputMeshes, const char *pDMXFile, bool bForce2DTexcoords )
  160. {
  161. CDisableUndoScopeGuard guard;
  162. DmFileId_t dmFileId = g_pDataModel->FindOrCreateFileId( pDMXFile );
  163. CDmElement *pRoot = CreateElement< CDmElement >( "root", dmFileId );
  164. CDmeModel *pModel = CreateElement< CDmeModel >( "model", dmFileId );
  165. pRoot->SetValue( "skeleton", pModel );
  166. pRoot->SetValue( "model", pModel );
  167. int nMeshes = inputMeshes.Count();
  168. for ( int m=0; m<nMeshes; ++m )
  169. {
  170. CDmeDag *pDmeDag = CreateElement< CDmeDag >( "obj", pRoot->GetFileId() );
  171. Assert( pDmeDag );
  172. CDmeMesh *pDmeMesh = CreateElement< CDmeMesh >( "obj", pRoot->GetFileId() );
  173. Assert( pDmeMesh );
  174. pDmeDag->SetShape( pDmeMesh );
  175. pModel->AddJoint( pDmeDag );
  176. pModel->AddChild( pDmeDag );
  177. ConvertMeshToDMX( pDmeMesh, inputMeshes[ m ], bForce2DTexcoords );
  178. }
  179. pModel->CaptureJointsToBaseState( "bind" );
  180. const char *pFileFormat = "model";
  181. if ( !g_pDataModel->SaveToFile( pDMXFile, NULL, NULL, pFileFormat, pRoot ) )
  182. {
  183. Warning( "SaveMeshesToDMX: SaveToFile \"%s\" failed!\n", pDMXFile );
  184. return false;
  185. }
  186. return true;
  187. }