MD3FileData.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp 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 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 Md3FileData.h
  34. *
  35. * @brief Defines helper data structures for importing MD3 files.
  36. * http://linux.ucla.edu/~phaethon/q3/formats/md3format.html
  37. */
  38. #ifndef AI_MD3FILEHELPER_H_INC
  39. #define AI_MD3FILEHELPER_H_INC
  40. #include <string>
  41. #include <vector>
  42. #include <sstream>
  43. #include <stdint.h>
  44. #include <assimp/types.h>
  45. #include <assimp/mesh.h>
  46. #include <assimp/anim.h>
  47. #include <assimp/Compiler/pushpack1.h>
  48. namespace Assimp {
  49. namespace MD3 {
  50. // to make it easier for us, we test the magic word against both "endianesses"
  51. #define AI_MD3_MAGIC_NUMBER_BE AI_MAKE_MAGIC("IDP3")
  52. #define AI_MD3_MAGIC_NUMBER_LE AI_MAKE_MAGIC("3PDI")
  53. // common limitations
  54. #define AI_MD3_VERSION 15
  55. #define AI_MD3_MAXQPATH 64
  56. #define AI_MD3_MAXFRAME 16
  57. #define AI_MD3_MAX_FRAMES 1024
  58. #define AI_MD3_MAX_TAGS 16
  59. #define AI_MD3_MAX_SURFACES 32
  60. #define AI_MD3_MAX_SHADERS 256
  61. #define AI_MD3_MAX_VERTS 4096
  62. #define AI_MD3_MAX_TRIANGLES 8192
  63. // master scale factor for all vertices in a MD3 model
  64. #define AI_MD3_XYZ_SCALE (1.0f/64.0f)
  65. // -------------------------------------------------------------------------------
  66. /** @brief Data structure for the MD3 main header
  67. */
  68. struct Header
  69. {
  70. //! magic number
  71. uint32_t IDENT;
  72. //! file format version
  73. uint32_t VERSION;
  74. //! original name in .pak archive
  75. char NAME[ AI_MD3_MAXQPATH ];
  76. //! unknown
  77. int32_t FLAGS;
  78. //! number of frames in the file
  79. uint32_t NUM_FRAMES;
  80. //! number of tags in the file
  81. uint32_t NUM_TAGS;
  82. //! number of surfaces in the file
  83. uint32_t NUM_SURFACES;
  84. //! number of skins in the file
  85. uint32_t NUM_SKINS;
  86. //! offset of the first frame
  87. uint32_t OFS_FRAMES;
  88. //! offset of the first tag
  89. uint32_t OFS_TAGS;
  90. //! offset of the first surface
  91. uint32_t OFS_SURFACES;
  92. //! end of file
  93. uint32_t OFS_EOF;
  94. } PACK_STRUCT;
  95. // -------------------------------------------------------------------------------
  96. /** @brief Data structure for the frame header
  97. */
  98. struct Frame
  99. {
  100. //! minimum bounds
  101. aiVector3D min;
  102. //! maximum bounds
  103. aiVector3D max;
  104. //! local origin for this frame
  105. aiVector3D origin;
  106. //! radius of bounding sphere
  107. ai_real radius;
  108. //! name of frame
  109. char name[ AI_MD3_MAXFRAME ];
  110. } PACK_STRUCT;
  111. // -------------------------------------------------------------------------------
  112. /** @brief Data structure for the tag header
  113. */
  114. struct Tag
  115. {
  116. //! name of the tag
  117. char NAME[ AI_MD3_MAXQPATH ];
  118. //! Local tag origin and orientation
  119. aiVector3D origin;
  120. ai_real orientation[3][3];
  121. } PACK_STRUCT;
  122. // -------------------------------------------------------------------------------
  123. /** @brief Data structure for the surface header
  124. */
  125. struct Surface
  126. {
  127. //! magic number
  128. int32_t IDENT;
  129. //! original name of the surface
  130. char NAME[ AI_MD3_MAXQPATH ];
  131. //! unknown
  132. int32_t FLAGS;
  133. //! number of frames in the surface
  134. uint32_t NUM_FRAMES;
  135. //! number of shaders in the surface
  136. uint32_t NUM_SHADER;
  137. //! number of vertices in the surface
  138. uint32_t NUM_VERTICES;
  139. //! number of triangles in the surface
  140. uint32_t NUM_TRIANGLES;
  141. //! offset to the triangle data
  142. uint32_t OFS_TRIANGLES;
  143. //! offset to the shader data
  144. uint32_t OFS_SHADERS;
  145. //! offset to the texture coordinate data
  146. uint32_t OFS_ST;
  147. //! offset to the vertex/normal data
  148. uint32_t OFS_XYZNORMAL;
  149. //! offset to the end of the Surface object
  150. int32_t OFS_END;
  151. } PACK_STRUCT;
  152. // -------------------------------------------------------------------------------
  153. /** @brief Data structure for a shader defined in there
  154. */
  155. struct Shader
  156. {
  157. //! filename of the shader
  158. char NAME[ AI_MD3_MAXQPATH ];
  159. //! index of the shader
  160. uint32_t SHADER_INDEX;
  161. } PACK_STRUCT;
  162. // -------------------------------------------------------------------------------
  163. /** @brief Data structure for a triangle
  164. */
  165. struct Triangle
  166. {
  167. //! triangle indices
  168. uint32_t INDEXES[3];
  169. } PACK_STRUCT;
  170. // -------------------------------------------------------------------------------
  171. /** @brief Data structure for an UV coord
  172. */
  173. struct TexCoord
  174. {
  175. //! UV coordinates
  176. ai_real U,V;
  177. } PACK_STRUCT;
  178. // -------------------------------------------------------------------------------
  179. /** @brief Data structure for a vertex
  180. */
  181. struct Vertex
  182. {
  183. //! X/Y/Z coordinates
  184. int16_t X,Y,Z;
  185. //! encoded normal vector
  186. uint16_t NORMAL;
  187. } PACK_STRUCT;
  188. #include "./../include/assimp/Compiler/poppack1.h"
  189. // -------------------------------------------------------------------------------
  190. /** @brief Unpack a Q3 16 bit vector to its full float3 representation
  191. *
  192. * @param p_iNormal Input normal vector in latitude/longitude form
  193. * @param p_afOut Pointer to an array of three floats to receive the result
  194. *
  195. * @note This has been taken from q3 source (misc_model.c)
  196. */
  197. inline void LatLngNormalToVec3(uint16_t p_iNormal, ai_real* p_afOut)
  198. {
  199. ai_real lat = (ai_real)(( p_iNormal >> 8u ) & 0xff);
  200. ai_real lng = (ai_real)(( p_iNormal & 0xff ));
  201. const ai_real invVal( ai_real( 1.0 ) / ai_real( 128.0 ) );
  202. lat *= ai_real( 3.141926 ) * invVal;
  203. lng *= ai_real( 3.141926 ) * invVal;
  204. p_afOut[ 0 ] = std::cos(lat) * std::sin(lng);
  205. p_afOut[ 1 ] = std::sin(lat) * std::sin(lng);
  206. p_afOut[ 2 ] = std::cos(lng);
  207. }
  208. // -------------------------------------------------------------------------------
  209. /** @brief Pack a Q3 normal into 16bit latitute/longitude representation
  210. * @param p_vIn Input vector
  211. * @param p_iOut Output normal
  212. *
  213. * @note This has been taken from q3 source (mathlib.c)
  214. */
  215. inline void Vec3NormalToLatLng( const aiVector3D& p_vIn, uint16_t& p_iOut )
  216. {
  217. // check for singularities
  218. if ( 0.0f == p_vIn[0] && 0.0f == p_vIn[1] )
  219. {
  220. if ( p_vIn[2] > 0.0f )
  221. {
  222. ((unsigned char*)&p_iOut)[0] = 0;
  223. ((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 0
  224. }
  225. else
  226. {
  227. ((unsigned char*)&p_iOut)[0] = 128;
  228. ((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 128
  229. }
  230. }
  231. else
  232. {
  233. int a, b;
  234. a = int(57.2957795f * ( std::atan2( p_vIn[1], p_vIn[0] ) ) * (255.0f / 360.0f ));
  235. a &= 0xff;
  236. b = int(57.2957795f * ( std::acos( p_vIn[2] ) ) * ( 255.0f / 360.0f ));
  237. b &= 0xff;
  238. ((unsigned char*)&p_iOut)[0] = b; // longitude
  239. ((unsigned char*)&p_iOut)[1] = a; // latitude
  240. }
  241. }
  242. }
  243. }
  244. #endif // !! AI_MD3FILEHELPER_H_INC