You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

316 lines
7.6 KiB

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