MD3FileData.h 8.3 KB

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