MILKSHAPEMODEL.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 6k
Category:

OpenCV

Development Platform:

Visual C++

  1. // MilkshapeModel.cpp: implementation of the MilkshapeModel class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "FighterTest.h"
  6. #include "MilkshapeModel.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. #include <fstream.h>
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. MilkshapeModel::MilkshapeModel()
  17. {
  18. }
  19. MilkshapeModel::~MilkshapeModel()
  20. {
  21. }
  22. /* 
  23. MS3D STRUCTURES 
  24. */
  25. // byte-align structures
  26. #ifdef _MSC_VER
  27. # pragma pack( push, packing )
  28. # pragma pack( 1 )
  29. # define PACK_STRUCT
  30. #elif defined( __GNUC__ )
  31. # define PACK_STRUCT __attribute__((packed))
  32. #else
  33. # error you must byte-align these structures with the appropriate compiler directives
  34. #endif
  35. typedef unsigned char byte;
  36. typedef unsigned short word;
  37. // File header
  38. struct MS3DHeader
  39. {
  40. char m_ID[10];
  41. int m_version;
  42. } PACK_STRUCT;
  43. // Vertex information
  44. struct MS3DVertex
  45. {
  46. byte m_flags;
  47. float m_vertex[3];
  48. char m_boneID;
  49. byte m_refCount;
  50. } PACK_STRUCT;
  51. // Triangle information
  52. struct MS3DTriangle
  53. {
  54. word m_flags;
  55. word m_vertexIndices[3];
  56. float m_vertexNormals[3][3];
  57. float m_s[3], m_t[3];
  58. byte m_smoothingGroup;
  59. byte m_groupIndex;
  60. } PACK_STRUCT;
  61. // Material information
  62. struct MS3DMaterial
  63. {
  64.     char m_name[32];
  65.     float m_ambient[4];
  66.     float m_diffuse[4];
  67.     float m_specular[4];
  68.     float m_emissive[4];
  69.     float m_shininess; // 0.0f - 128.0f
  70.     float m_transparency; // 0.0f - 1.0f
  71.     byte m_mode; // 0, 1, 2 is unused now
  72.     char m_texture[128];
  73.     char m_alphamap[128];
  74. } PACK_STRUCT;
  75. // Joint information
  76. struct MS3DJoint
  77. {
  78. byte m_flags;
  79. char m_name[32];
  80. char m_parentName[32];
  81. float m_rotation[3];
  82. float m_translation[3];
  83. word m_numRotationKeyframes;
  84. word m_numTranslationKeyframes;
  85. } PACK_STRUCT;
  86. // Keyframe data
  87. struct MS3DKeyframe
  88. {
  89. float m_time;
  90. float m_parameter[3];
  91. } PACK_STRUCT;
  92. // Default alignment
  93. #ifdef _MSC_VER
  94. # pragma pack( pop, packing )
  95. #endif
  96. #undef PACK_STRUCT
  97. bool MilkshapeModel::loadModelData( const char *filename )
  98. {
  99. ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate );
  100. if ( inputFile.fail())
  101. return false; // "Couldn't open the model file."
  102. inputFile.seekg( 0, ios::end );
  103. long fileSize = inputFile.tellg();
  104. inputFile.seekg( 0, ios::beg );
  105. byte *pBuffer = new byte[fileSize];
  106. inputFile.read( pBuffer, fileSize );
  107. inputFile.close();
  108. const byte *pPtr = pBuffer;
  109. MS3DHeader *pHeader = ( MS3DHeader* )pPtr;
  110. pPtr += sizeof( MS3DHeader );
  111. if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
  112. return false; // "Not a valid Milkshape3D model file."
  113. if ( pHeader->m_version < 3 || pHeader->m_version > 4 )
  114. return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );
  115. int nVertices = *( word* )pPtr; 
  116. m_numVertices = nVertices;
  117. m_pVertices = new Vertex[nVertices];
  118. pPtr += sizeof( word );
  119. int i;
  120. for ( i = 0; i < nVertices; i++ )
  121. {
  122. MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
  123. m_pVertices[i].m_boneID = pVertex->m_boneID;
  124. memcpy( m_pVertices[i].m_location, pVertex->m_vertex, sizeof( float )*3 );
  125. pPtr += sizeof( MS3DVertex );
  126. }
  127. int nTriangles = *( word* )pPtr;
  128. m_numTriangles = nTriangles;
  129. m_pTriangles = new Triangle[nTriangles];
  130. pPtr += sizeof( word );
  131. for ( i = 0; i < nTriangles; i++ )
  132. {
  133. MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
  134. int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
  135. float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
  136. memcpy( m_pTriangles[i].m_vertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
  137. memcpy( m_pTriangles[i].m_s, pTriangle->m_s, sizeof( float )*3 );
  138. memcpy( m_pTriangles[i].m_t, t, sizeof( float )*3 );
  139. memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices, sizeof( int )*3 );
  140. pPtr += sizeof( MS3DTriangle );
  141. }
  142. int nGroups = *( word* )pPtr;
  143. m_numMeshes = nGroups;
  144. m_pMeshes = new Mesh[nGroups];
  145. pPtr += sizeof( word );
  146. for ( i = 0; i < nGroups; i++ )
  147. {
  148. pPtr += sizeof( byte ); // flags
  149. pPtr += 32; // name
  150. word nTriangles = *( word* )pPtr;
  151. pPtr += sizeof( word );
  152. int *pTriangleIndices = new int[nTriangles];
  153. for ( int j = 0; j < nTriangles; j++ )
  154. {
  155. pTriangleIndices[j] = *( word* )pPtr;
  156. pPtr += sizeof( word );
  157. }
  158. char materialIndex = *( char* )pPtr;
  159. pPtr += sizeof( char );
  160. m_pMeshes[i].m_materialIndex = materialIndex;
  161. m_pMeshes[i].m_numTriangles = nTriangles;
  162. m_pMeshes[i].m_pTriangleIndices = pTriangleIndices;
  163. }
  164. int nMaterials = *( word* )pPtr;
  165. m_numMaterials = nMaterials;
  166. m_pMaterials = new Material[nMaterials];
  167. pPtr += sizeof( word );
  168. for ( i = 0; i < nMaterials; i++ )
  169. {
  170. MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
  171. memcpy( m_pMaterials[i].m_ambient, pMaterial->m_ambient, sizeof( float )*4 );
  172. memcpy( m_pMaterials[i].m_diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
  173. memcpy( m_pMaterials[i].m_specular, pMaterial->m_specular, sizeof( float )*4 );
  174. memcpy( m_pMaterials[i].m_emissive, pMaterial->m_emissive, sizeof( float )*4 );
  175. m_pMaterials[i].m_shininess = pMaterial->m_shininess;
  176. m_pMaterials[i].m_pTextureFilename = new char[strlen( pMaterial->m_texture )+1];
  177. strcpy( m_pMaterials[i].m_pTextureFilename, pMaterial->m_texture );
  178. pPtr += sizeof( MS3DMaterial );
  179. }
  180. reloadTextures();
  181. delete[] pBuffer;
  182. return true;
  183. }