MD3FileData.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /** @file Defines the helper data structures for importing MD3 files */
  2. #ifndef AI_MD3FILEHELPER_H_INC
  3. #define AI_MD3FILEHELPER_H_INC
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. #include "../include/aiTypes.h"
  8. #include "../include/aiMesh.h"
  9. #include "../include/aiAnim.h"
  10. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  11. # pragma pack(push,1)
  12. # define PACK_STRUCT
  13. #elif defined( __GNUC__ )
  14. # define PACK_STRUCT __attribute__((packed))
  15. #else
  16. # error Compiler not supported
  17. #endif
  18. namespace Assimp
  19. {
  20. // http://linux.ucla.edu/~phaethon/q3/formats/md3format.html
  21. namespace MD3
  22. {
  23. #define AI_MD3_MAGIC_NUMBER_BE 'IDP3'
  24. #define AI_MD3_MAGIC_NUMBER_LE '3PDI'
  25. // common limitations
  26. #define AI_MD3_VERSION 15
  27. #define AI_MD3_MAXQPATH 64
  28. #define AI_MD3_MAX_FRAMES 1024
  29. #define AI_MD3_MAX_TAGS 16
  30. #define AI_MD3_MAX_SURFACES 32
  31. #define AI_MD3_MAX_SHADERS 256
  32. #define AI_MD3_MAX_VERTS 4096
  33. #define AI_MD3_MAX_TRIANGLES 8192
  34. // master scale factor for all vertices in a MD3 model
  35. #define AI_MD3_XYZ_SCALE (1.0f/64.0f)
  36. // ---------------------------------------------------------------------------
  37. /** \brief Data structure for the MD3 main header
  38. */
  39. // ---------------------------------------------------------------------------
  40. struct Header
  41. {
  42. // magic number
  43. int32_t IDENT;
  44. // file format version
  45. int32_t VERSION;
  46. // original name in .pak archive
  47. unsigned char NAME[ AI_MD3_MAXQPATH ];
  48. // unknown
  49. int32_t FLAGS;
  50. // number of frames in the file
  51. int32_t NUM_FRAMES;
  52. // number of tags in the file
  53. int32_t NUM_TAGS;
  54. // number of surfaces in the file
  55. int32_t NUM_SURFACES;
  56. // number of skins in the file
  57. int32_t NUM_SKINS;
  58. // offset of the first frame
  59. int32_t OFS_FRAMES;
  60. // offset of the first tag
  61. int32_t OFS_TAGS;
  62. // offset of the first surface
  63. int32_t OFS_SURFACES;
  64. // end of file
  65. int32_t OFS_EOF;
  66. } PACK_STRUCT;
  67. // ---------------------------------------------------------------------------
  68. /** \brief Data structure for the frame header
  69. */
  70. // ---------------------------------------------------------------------------
  71. struct Frame
  72. {
  73. // no need to define this, we won't need
  74. } PACK_STRUCT;
  75. // ---------------------------------------------------------------------------
  76. /** \brief Data structure for the tag header
  77. */
  78. // ---------------------------------------------------------------------------
  79. struct Tag
  80. {
  81. // no need to define this, we won't need
  82. } PACK_STRUCT;
  83. // ---------------------------------------------------------------------------
  84. /** \brief Data structure for the surface header
  85. */
  86. // ---------------------------------------------------------------------------
  87. struct Surface
  88. {
  89. // magic number
  90. int32_t IDENT;
  91. // original name of the surface
  92. unsigned char NAME[ AI_MD3_MAXQPATH ];
  93. // unknown
  94. int32_t FLAGS;
  95. // number of frames in the surface
  96. int32_t NUM_FRAMES;
  97. // number of shaders in the surface
  98. int32_t NUM_SHADER;
  99. // number of vertices in the surface
  100. int32_t NUM_VERTICES;
  101. // number of triangles in the surface
  102. int32_t NUM_TRIANGLES;
  103. // offset to the triangle data
  104. int32_t OFS_TRIANGLES;
  105. // offset to the shader data
  106. int32_t OFS_SHADERS;
  107. // offset to the texture coordinate data
  108. int32_t OFS_ST;
  109. // offset to the vertex/normal data
  110. int32_t OFS_XYZNORMAL;
  111. // offset to the end of the Surface object
  112. int32_t OFS_END;
  113. } PACK_STRUCT;
  114. // ---------------------------------------------------------------------------
  115. /** \brief Data structure for a shader
  116. */
  117. // ---------------------------------------------------------------------------
  118. struct Shader
  119. {
  120. // filename of the shader
  121. unsigned char NAME[ AI_MD3_MAXQPATH ];
  122. // index of the shader
  123. int32_t SHADER_INDEX;
  124. } PACK_STRUCT;
  125. // ---------------------------------------------------------------------------
  126. /** \brief Data structure for a triangle
  127. */
  128. // ---------------------------------------------------------------------------
  129. struct Triangle
  130. {
  131. // triangle indices
  132. int32_t INDEXES[3];
  133. } PACK_STRUCT;
  134. // ---------------------------------------------------------------------------
  135. /** \brief Data structure for an UV coord
  136. */
  137. // ---------------------------------------------------------------------------
  138. struct TexCoord
  139. {
  140. // UV coordinates
  141. float U,V;
  142. } PACK_STRUCT;
  143. // ---------------------------------------------------------------------------
  144. /** \brief Data structure for a vertex
  145. */
  146. // ---------------------------------------------------------------------------
  147. struct Vertex
  148. {
  149. // X/Y/Z coordinates
  150. int16_t X,Y,Z;
  151. // encoded normal vector
  152. int16_t NORMAL;
  153. } PACK_STRUCT;
  154. // reset packing to the original value
  155. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  156. # pragma pack( pop )
  157. #endif
  158. #undef PACK_STRUCT
  159. // ---------------------------------------------------------------------------
  160. /** \brief Unpack a Q3 16 bit vector to its full float3 representation
  161. *
  162. * \param p_iNormal Input normal vector in latitude/longitude form
  163. * \param p_afOut Pointer to an array of three floats to receive the result
  164. *
  165. * \note This has been taken from q3 source (misc_model.c)
  166. */
  167. // ---------------------------------------------------------------------------
  168. inline void LatLngNormalToVec3(uint16_t p_iNormal, float* p_afOut)
  169. {
  170. float lat = (float)(( p_iNormal >> 8 ) & 0xff);
  171. float lng = (float)(( p_iNormal & 0xff ));
  172. lat *= 3.141926f/128.0f;
  173. lng *= 3.141926f/128.0f;
  174. p_afOut[0] = cosf(lat) * sinf(lng);
  175. p_afOut[1] = sinf(lat) * sinf(lng);
  176. p_afOut[2] = cosf(lng);
  177. return;
  178. }
  179. // ---------------------------------------------------------------------------
  180. /** \brief Pack a Q3 normal into 16bit latitute/longitude representation
  181. * \param p_vIn Input vector
  182. * \param p_iOut Output normal
  183. *
  184. * \note This has been taken from q3 source (mathlib.c)
  185. */
  186. // ---------------------------------------------------------------------------
  187. inline void Vec3NormalToLatLng( const aiVector3D& p_vIn, uint16_t& p_iOut )
  188. {
  189. // check for singularities
  190. if ( 0.0f == p_vIn[0] && 0.0f == p_vIn[1] )
  191. {
  192. if ( p_vIn[2] > 0.0f )
  193. {
  194. ((unsigned char*)&p_iOut)[0] = 0;
  195. ((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 0
  196. }
  197. else
  198. {
  199. ((unsigned char*)&p_iOut)[0] = 128;
  200. ((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 128
  201. }
  202. }
  203. else
  204. {
  205. int a, b;
  206. a = int(57.2957795f * ( atan2f( p_vIn[1], p_vIn[0] ) ) * (255.0f / 360.0f ));
  207. a &= 0xff;
  208. b = int(57.2957795f * ( acosf( p_vIn[2] ) ) * ( 255.0f / 360.0f ));
  209. b &= 0xff;
  210. ((unsigned char*)&p_iOut)[0] = b; // longitude
  211. ((unsigned char*)&p_iOut)[1] = a; // lattitude
  212. }
  213. }
  214. };
  215. };
  216. #endif // !! AI_MD3FILEHELPER_H_INC