MD3FileData.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Defines the helper data structures for importing MD3 files.
  34. http://linux.ucla.edu/~phaethon/q3/formats/md3format.html
  35. */
  36. #ifndef AI_MD3FILEHELPER_H_INC
  37. #define AI_MD3FILEHELPER_H_INC
  38. #include <string>
  39. #include <vector>
  40. #include <sstream>
  41. #include "../include/aiTypes.h"
  42. #include "../include/aiMesh.h"
  43. #include "../include/aiAnim.h"
  44. #include "./../include/Compiler/pushpack1.h"
  45. namespace Assimp {
  46. namespace MD3 {
  47. // to make it easier for ourselfes, we test the magic word against both "endianesses"
  48. #define MD3_MAKE(string) ((uint32_t)((string[0] << 24) + (string[1] << 16) + (string[2] << 8) + string[3]))
  49. #define AI_MD3_MAGIC_NUMBER_BE MD3_MAKE("IDP3")
  50. #define AI_MD3_MAGIC_NUMBER_LE MD3_MAKE("3PDI")
  51. // common limitations
  52. #define AI_MD3_VERSION 15
  53. #define AI_MD3_MAXQPATH 64
  54. #define AI_MD3_MAX_FRAMES 1024
  55. #define AI_MD3_MAX_TAGS 16
  56. #define AI_MD3_MAX_SURFACES 32
  57. #define AI_MD3_MAX_SHADERS 256
  58. #define AI_MD3_MAX_VERTS 4096
  59. #define AI_MD3_MAX_TRIANGLES 8192
  60. // master scale factor for all vertices in a MD3 model
  61. #define AI_MD3_XYZ_SCALE (1.0f/64.0f)
  62. // ---------------------------------------------------------------------------
  63. /** \brief Data structure for the MD3 main header
  64. */
  65. // ---------------------------------------------------------------------------
  66. struct Header
  67. {
  68. //! magic number
  69. uint32_t IDENT;
  70. //! file format version
  71. uint32_t VERSION;
  72. //! original name in .pak archive
  73. unsigned char NAME[ AI_MD3_MAXQPATH ];
  74. //! unknown
  75. int32_t FLAGS;
  76. //! number of frames in the file
  77. uint32_t NUM_FRAMES;
  78. //! number of tags in the file
  79. uint32_t NUM_TAGS;
  80. //! number of surfaces in the file
  81. uint32_t NUM_SURFACES;
  82. //! number of skins in the file
  83. uint32_t NUM_SKINS;
  84. //! offset of the first frame
  85. uint32_t OFS_FRAMES;
  86. //! offset of the first tag
  87. uint32_t OFS_TAGS;
  88. //! offset of the first surface
  89. uint32_t OFS_SURFACES;
  90. //! end of file
  91. uint32_t OFS_EOF;
  92. } PACK_STRUCT;
  93. // ---------------------------------------------------------------------------
  94. /** \brief Data structure for the frame header
  95. */
  96. // ---------------------------------------------------------------------------
  97. struct Frame
  98. {
  99. // no need to define this, we won't need
  100. } PACK_STRUCT;
  101. // ---------------------------------------------------------------------------
  102. /** \brief Data structure for the tag header
  103. */
  104. // ---------------------------------------------------------------------------
  105. struct Tag
  106. {
  107. // no need to define this, we won't need
  108. } PACK_STRUCT;
  109. // ---------------------------------------------------------------------------
  110. /** \brief Data structure for the surface header
  111. */
  112. // ---------------------------------------------------------------------------
  113. struct Surface
  114. {
  115. //! magic number
  116. int32_t IDENT;
  117. //! original name of the surface
  118. unsigned char NAME[ AI_MD3_MAXQPATH ];
  119. //! unknown
  120. int32_t FLAGS;
  121. //! number of frames in the surface
  122. uint32_t NUM_FRAMES;
  123. //! number of shaders in the surface
  124. uint32_t NUM_SHADER;
  125. //! number of vertices in the surface
  126. uint32_t NUM_VERTICES;
  127. //! number of triangles in the surface
  128. uint32_t NUM_TRIANGLES;
  129. //! offset to the triangle data
  130. uint32_t OFS_TRIANGLES;
  131. //! offset to the shader data
  132. uint32_t OFS_SHADERS;
  133. //! offset to the texture coordinate data
  134. uint32_t OFS_ST;
  135. //! offset to the vertex/normal data
  136. uint32_t OFS_XYZNORMAL;
  137. //! offset to the end of the Surface object
  138. int32_t OFS_END;
  139. } PACK_STRUCT;
  140. // ---------------------------------------------------------------------------
  141. /** \brief Data structure for a shader
  142. */
  143. // ---------------------------------------------------------------------------
  144. struct Shader
  145. {
  146. //! filename of the shader
  147. unsigned char NAME[ AI_MD3_MAXQPATH ];
  148. //! index of the shader
  149. uint32_t SHADER_INDEX;
  150. } PACK_STRUCT;
  151. // ---------------------------------------------------------------------------
  152. /** \brief Data structure for a triangle
  153. */
  154. // ---------------------------------------------------------------------------
  155. struct Triangle
  156. {
  157. //! triangle indices
  158. uint32_t INDEXES[3];
  159. } PACK_STRUCT;
  160. // ---------------------------------------------------------------------------
  161. /** \brief Data structure for an UV coord
  162. */
  163. // ---------------------------------------------------------------------------
  164. struct TexCoord
  165. {
  166. //! UV coordinates
  167. float U,V;
  168. } PACK_STRUCT;
  169. // ---------------------------------------------------------------------------
  170. /** \brief Data structure for a vertex
  171. */
  172. // ---------------------------------------------------------------------------
  173. struct Vertex
  174. {
  175. //! X/Y/Z coordinates
  176. int16_t X,Y,Z;
  177. //! encoded normal vector
  178. uint16_t NORMAL;
  179. } PACK_STRUCT;
  180. #include "./../include/Compiler/poppack1.h"
  181. // ---------------------------------------------------------------------------
  182. /** \brief Unpack a Q3 16 bit vector to its full float3 representation
  183. *
  184. * \param p_iNormal Input normal vector in latitude/longitude form
  185. * \param p_afOut Pointer to an array of three floats to receive the result
  186. *
  187. * \note This has been taken from q3 source (misc_model.c)
  188. */
  189. // ---------------------------------------------------------------------------
  190. inline void LatLngNormalToVec3(uint16_t p_iNormal, float* p_afOut)
  191. {
  192. float lat = (float)(( p_iNormal >> 8u ) & 0xff);
  193. float lng = (float)(( p_iNormal & 0xff ));
  194. lat *= 3.141926f/128.0f;
  195. lng *= 3.141926f/128.0f;
  196. p_afOut[0] = cosf(lat) * sinf(lng);
  197. p_afOut[1] = sinf(lat) * sinf(lng);
  198. p_afOut[2] = cosf(lng);
  199. return;
  200. }
  201. // ---------------------------------------------------------------------------
  202. /** \brief Pack a Q3 normal into 16bit latitute/longitude representation
  203. * \param p_vIn Input vector
  204. * \param p_iOut Output normal
  205. *
  206. * \note This has been taken from q3 source (mathlib.c)
  207. */
  208. // ---------------------------------------------------------------------------
  209. inline void Vec3NormalToLatLng( const aiVector3D& p_vIn, uint16_t& p_iOut )
  210. {
  211. // check for singularities
  212. if ( 0.0f == p_vIn[0] && 0.0f == p_vIn[1] )
  213. {
  214. if ( p_vIn[2] > 0.0f )
  215. {
  216. ((unsigned char*)&p_iOut)[0] = 0;
  217. ((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 0
  218. }
  219. else
  220. {
  221. ((unsigned char*)&p_iOut)[0] = 128;
  222. ((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 128
  223. }
  224. }
  225. else
  226. {
  227. int a, b;
  228. a = int(57.2957795f * ( atan2f( p_vIn[1], p_vIn[0] ) ) * (255.0f / 360.0f ));
  229. a &= 0xff;
  230. b = int(57.2957795f * ( acosf( p_vIn[2] ) ) * ( 255.0f / 360.0f ));
  231. b &= 0xff;
  232. ((unsigned char*)&p_iOut)[0] = b; // longitude
  233. ((unsigned char*)&p_iOut)[1] = a; // lattitude
  234. }
  235. }
  236. }
  237. }
  238. #endif // !! AI_MD3FILEHELPER_H_INC