Oe_import_ms3d.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. animace
  3. */
  4. #include "mss_on.h"
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <direct.h>
  9. #include <float.h>
  10. #include <assert.h>
  11. #include "objekt_editor_all.h"
  12. #include "oe_import_ms3d.h"
  13. #include "dbgwnd.h"
  14. EDIT_KONTEJNER *ms3d_import(byte * p_file, byte * p_dir)
  15. {
  16. EDIT_KONTEJNER *p_kont;
  17. FILE *f;
  18. byte *p_buffer;
  19. byte *p_akt;
  20. struct MS3DHeader *pHeader;
  21. int velikost;
  22. _chdir(p_dir);
  23. f = fopen(p_file, "rb");
  24. if (!f)
  25. return (NULL);
  26. fseek(f, 0, SEEK_END);
  27. velikost = ftell(f);
  28. fseek(f, 0, SEEK_SET);
  29. p_buffer = malloc(sizeof(byte) * velikost);
  30. mtest(p_buffer);
  31. fread(p_buffer, sizeof(byte), velikost, f);
  32. p_akt = p_buffer;
  33. pHeader = (struct MS3DHeader *) p_akt;
  34. p_akt += sizeof(struct MS3DHeader);
  35. if (strncmp(pHeader->m_ID, "MS3D000000", 10) != 0) {
  36. kprintf(TRUE, "Neodpovida hlavicka - nejedna se o MilkShape soubor.");
  37. return (NULL);
  38. }
  39. if (pHeader->m_version < 3 || pHeader->m_version > 4) {
  40. kprintf(TRUE, "Spatna verze souboru (%d)", pHeader->m_version);
  41. return (NULL); // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );
  42. }
  43. p_kont = vyrob_kontejner();
  44. return (p_kont);
  45. }
  46. /*
  47. bool MilkshapeModel::loadModelData( const char *filename )
  48. {
  49. int nVertices = *( word* )pPtr;
  50. m_numVertices = nVertices;
  51. m_pVertices = new Vertex[nVertices];
  52. pPtr += sizeof( word );
  53. int i;
  54. for ( i = 0; i < nVertices; i++ )
  55. {
  56. MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
  57. m_pVertices[i].m_boneID = pVertex->m_boneID;
  58. memcpy( m_pVertices[i].m_location, pVertex->m_vertex, sizeof( float )*3 );
  59. pPtr += sizeof( MS3DVertex );
  60. }
  61. int nTriangles = *( word* )pPtr;
  62. m_numTriangles = nTriangles;
  63. m_pTriangles = new Triangle[nTriangles];
  64. pPtr += sizeof( word );
  65. for ( i = 0; i < nTriangles; i++ )
  66. {
  67. MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
  68. int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
  69. float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
  70. memcpy( m_pTriangles[i].m_vertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
  71. memcpy( m_pTriangles[i].m_s, pTriangle->m_s, sizeof( float )*3 );
  72. memcpy( m_pTriangles[i].m_t, t, sizeof( float )*3 );
  73. memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices, sizeof( int )*3 );
  74. pPtr += sizeof( MS3DTriangle );
  75. }
  76. int nGroups = *( word* )pPtr;
  77. m_numMeshes = nGroups;
  78. m_pMeshes = new Mesh[nGroups];
  79. pPtr += sizeof( word );
  80. for ( i = 0; i < nGroups; i++ )
  81. {
  82. pPtr += sizeof( byte ); // flags
  83. pPtr += 32; // name
  84. word nTriangles = *( word* )pPtr;
  85. pPtr += sizeof( word );
  86. int *pTriangleIndices = new int[nTriangles];
  87. for ( int j = 0; j < nTriangles; j++ )
  88. {
  89. pTriangleIndices[j] = *( word* )pPtr;
  90. pPtr += sizeof( word );
  91. }
  92. char materialIndex = *( char* )pPtr;
  93. pPtr += sizeof( char );
  94. m_pMeshes[i].m_materialIndex = materialIndex;
  95. m_pMeshes[i].m_numTriangles = nTriangles;
  96. m_pMeshes[i].m_pTriangleIndices = pTriangleIndices;
  97. }
  98. int nMaterials = *( word* )pPtr;
  99. m_numMaterials = nMaterials;
  100. m_pMaterials = new Material[nMaterials];
  101. pPtr += sizeof( word );
  102. for ( i = 0; i < nMaterials; i++ )
  103. {
  104. MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
  105. memcpy( m_pMaterials[i].m_ambient, pMaterial->m_ambient, sizeof( float )*4 );
  106. memcpy( m_pMaterials[i].m_diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
  107. memcpy( m_pMaterials[i].m_specular, pMaterial->m_specular, sizeof( float )*4 );
  108. memcpy( m_pMaterials[i].m_emissive, pMaterial->m_emissive, sizeof( float )*4 );
  109. m_pMaterials[i].m_shininess = pMaterial->m_shininess;
  110. m_pMaterials[i].m_pTextureFilename = new char[strlen( pMaterial->m_texture )+1];
  111. strcpy( m_pMaterials[i].m_pTextureFilename, pMaterial->m_texture );
  112. pPtr += sizeof( MS3DMaterial );
  113. }
  114. reloadTextures();
  115. // { NEW }
  116. // Load Skeletal Animation Stuff
  117. float animFPS = *( float* )pPtr;
  118. pPtr += sizeof( float );
  119. // skip currentTime
  120. pPtr += sizeof( float );
  121. int totalFrames = *( int* )pPtr;
  122. pPtr += sizeof( int );
  123. m_totalTime = totalFrames*1000.0/animFPS;
  124. m_numJoints = *( word* )pPtr;
  125. pPtr += sizeof( word );
  126. m_pJoints = new Joint[m_numJoints];
  127. struct JointNameListRec
  128. {
  129. int m_jointIndex;
  130. const char *m_pName;
  131. };
  132. const byte *pTempPtr = pPtr;
  133. JointNameListRec *pNameList = new JointNameListRec[m_numJoints];
  134. for ( i = 0; i < m_numJoints; i++ )
  135. {
  136. MS3DJoint *pJoint = ( MS3DJoint* )pTempPtr;
  137. pTempPtr += sizeof( MS3DJoint );
  138. pTempPtr += sizeof( MS3DKeyframe )*( pJoint->m_numRotationKeyframes+pJoint->m_numTranslationKeyframes );
  139. pNameList[i].m_jointIndex = i;
  140. pNameList[i].m_pName = pJoint->m_name;
  141. }
  142. for ( i = 0; i < m_numJoints; i++ )
  143. {
  144. MS3DJoint *pJoint = ( MS3DJoint* )pPtr;
  145. pPtr += sizeof( MS3DJoint );
  146. int j, parentIndex = -1;
  147. if ( strlen( pJoint->m_parentName ) > 0 )
  148. {
  149. for ( j = 0; j < m_numJoints; j++ )
  150. {
  151. if ( stricmp( pNameList[j].m_pName, pJoint->m_parentName ) == 0 )
  152. {
  153. parentIndex = pNameList[j].m_jointIndex;
  154. break;
  155. }
  156. }
  157. if ( parentIndex == -1 ) // Unable to find parent bone in MS3D file
  158. return false;
  159. }
  160. memcpy( m_pJoints[i].m_localRotation, pJoint->m_rotation, sizeof( float )*3 );
  161. memcpy( m_pJoints[i].m_localTranslation, pJoint->m_translation, sizeof( float )*3 );
  162. m_pJoints[i].m_parent = parentIndex;
  163. m_pJoints[i].m_numRotationKeyframes = pJoint->m_numRotationKeyframes;
  164. m_pJoints[i].m_pRotationKeyframes = new Keyframe[pJoint->m_numRotationKeyframes];
  165. m_pJoints[i].m_numTranslationKeyframes = pJoint->m_numTranslationKeyframes;
  166. m_pJoints[i].m_pTranslationKeyframes = new Keyframe[pJoint->m_numTranslationKeyframes];
  167. for ( j = 0; j < pJoint->m_numRotationKeyframes; j++ )
  168. {
  169. MS3DKeyframe *pKeyframe = ( MS3DKeyframe* )pPtr;
  170. pPtr += sizeof( MS3DKeyframe );
  171. setJointKeyframe( i, j, pKeyframe->m_time*1000.0f, pKeyframe->m_parameter, true );
  172. }
  173. for ( j = 0; j < pJoint->m_numTranslationKeyframes; j++ )
  174. {
  175. MS3DKeyframe *pKeyframe = ( MS3DKeyframe* )pPtr;
  176. pPtr += sizeof( MS3DKeyframe );
  177. setJointKeyframe( i, j, pKeyframe->m_time*1000.0f, pKeyframe->m_parameter, false );
  178. }
  179. }
  180. delete[] pNameList;
  181. setupJoints();
  182. // { end NEW }
  183. delete[] pBuffer;
  184. return true;
  185. }
  186. */