MDLFileData.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. //
  34. //! @file Definition of in-memory structures for the MDL file format.
  35. //
  36. // The specification has been taken from various sources on the internet.
  37. // http://tfc.duke.free.fr/coding/mdl-specs-en.html
  38. #ifndef AI_MDLFILEHELPER_H_INC
  39. #define AI_MDLFILEHELPER_H_INC
  40. #include <string>
  41. #include <vector>
  42. #include "../include/aiTypes.h"
  43. #include "../include/aiMesh.h"
  44. #include "../include/aiAnim.h"
  45. #include "../include/aiMaterial.h"
  46. // ugly compiler dependent packing stuff
  47. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  48. # pragma pack(push,1)
  49. # define PACK_STRUCT
  50. #elif defined( __GNUC__ )
  51. # define PACK_STRUCT __attribute__((packed))
  52. #else
  53. # error Compiler not supported. Never do this again.
  54. #endif
  55. namespace Assimp
  56. {
  57. namespace MDL
  58. {
  59. // magic bytes used in Quake 1 MDL meshes
  60. #define AI_MDL_MAGIC_NUMBER_BE 'IDPO'
  61. #define AI_MDL_MAGIC_NUMBER_LE 'OPDI'
  62. // magic bytes used in GameStudio A4 MDL meshes
  63. #define AI_MDL_MAGIC_NUMBER_BE_GS4 'MDL3'
  64. #define AI_MDL_MAGIC_NUMBER_LE_GS4 '3LDM'
  65. // magic bytes used in GameStudio A5+ MDL meshes
  66. #define AI_MDL_MAGIC_NUMBER_BE_GS5a 'MDL4'
  67. #define AI_MDL_MAGIC_NUMBER_LE_GS5a '4LDM'
  68. #define AI_MDL_MAGIC_NUMBER_BE_GS5b 'MDL5'
  69. #define AI_MDL_MAGIC_NUMBER_LE_GS5b '5LDM'
  70. // magic bytes used in GameStudio A6+ MDL meshes
  71. #define AI_MDL_MAGIC_NUMBER_BE_GS6 'MDL6'
  72. #define AI_MDL_MAGIC_NUMBER_LE_GS6 '6LDM'
  73. // magic bytes used in GameStudio A7+ MDL meshes
  74. #define AI_MDL_MAGIC_NUMBER_BE_GS7 'MDL7'
  75. #define AI_MDL_MAGIC_NUMBER_LE_GS7 '7LDM'
  76. // common limitations for Quake1 meshes. The loader does not check them,
  77. // but models should not exceed these limits.
  78. #if (!defined AI_MDL_VERSION)
  79. # define AI_MDL_VERSION 6
  80. #endif
  81. #if (!defined AI_MDL_MAX_FRAMES)
  82. # define AI_MDL_MAX_FRAMES 256
  83. #endif
  84. #if (!defined AI_MDL_MAX_UVS)
  85. # define AI_MDL_MAX_UVS 1024
  86. #endif
  87. #if (!defined AI_MDL_MAX_VERTS)
  88. # define AI_MDL_MAX_VERTS 1024
  89. #endif
  90. #if (!defined AI_MDL_MAX_TRIANGLES)
  91. # define AI_MDL_MAX_TRIANGLES 2048
  92. #endif
  93. // ---------------------------------------------------------------------------
  94. /** \struct Header
  95. * \brief Data structure for the MDL main header
  96. */
  97. // ---------------------------------------------------------------------------
  98. struct Header
  99. {
  100. //! magic number: "IDPO"
  101. int32_t ident;
  102. //! version number: 6
  103. int32_t version;
  104. //! scale factors for each axis
  105. aiVector3D scale;
  106. //! translation factors for each axis
  107. aiVector3D translate;
  108. //! bounding radius of the mesh
  109. float boundingradius;
  110. //! Position of the viewer's exe. Ignored
  111. aiVector3D vEyePos;
  112. //! Number of textures
  113. int32_t num_skins;
  114. //! Texture width in pixels
  115. int32_t skinwidth;
  116. //! Texture height in pixels
  117. int32_t skinheight;
  118. //! Number of vertices contained in the file
  119. int32_t num_verts;
  120. //! Number of triangles contained in the file
  121. int32_t num_tris;
  122. //! Number of frames contained in the file
  123. int32_t num_frames;
  124. //! 0 = synchron, 1 = random . Ignored
  125. int32_t synctype;
  126. //! State flag
  127. int32_t flags;
  128. //! ???
  129. float size;
  130. } PACK_STRUCT;
  131. // ---------------------------------------------------------------------------
  132. /** \struct Header_MDL7
  133. * \brief Data structure for the MDL 7 main header
  134. */
  135. // ---------------------------------------------------------------------------
  136. struct Header_MDL7
  137. {
  138. //! magic number: "MDL7"
  139. char ident[4];
  140. //! Version number. Ignored
  141. int32_t version;
  142. //! Number of bones in file
  143. int32_t bones_num;
  144. //! Number of groups in file
  145. int32_t groups_num;
  146. //! Size of data in the file
  147. int32_t data_size;
  148. //! Ignored. Used to store entity specific information
  149. int32_t entlump_size;
  150. //! Ignored. Used to store MED related data
  151. int32_t medlump_size;
  152. // -------------------------------------------------------
  153. // Sizes of some file parts
  154. uint16_t bone_stc_size;
  155. uint16_t skin_stc_size;
  156. uint16_t colorvalue_stc_size;
  157. uint16_t material_stc_size;
  158. uint16_t skinpoint_stc_size;
  159. uint16_t triangle_stc_size;
  160. uint16_t mainvertex_stc_size;
  161. uint16_t framevertex_stc_size;
  162. uint16_t bonetrans_stc_size;
  163. uint16_t frame_stc_size;
  164. } PACK_STRUCT;
  165. #define AI_MDL7_MAX_BONENAMESIZE 20
  166. // ---------------------------------------------------------------------------
  167. /** \struct Bone_MDL7
  168. * \brief Bone in a MDL7 file
  169. */
  170. // ---------------------------------------------------------------------------
  171. struct Bone_MDL7
  172. {
  173. uint16_t parent_index;
  174. uint8_t _unused_[2]; //
  175. float x,y,z;
  176. char name[AI_MDL7_MAX_BONENAMESIZE];
  177. };
  178. #define AI_MDL7_MAX_GROUPNAMESIZE 16
  179. // ---------------------------------------------------------------------------
  180. /** \struct Group_MDL7
  181. * \brief Group in a MDL7 file
  182. */
  183. // ---------------------------------------------------------------------------
  184. struct Group_MDL7
  185. {
  186. //! = '1' -> triangle based Mesh
  187. unsigned char typ;
  188. int8_t deformers;
  189. int8_t max_weights;
  190. int8_t _unused_;
  191. //! size of data for this group in bytes ( MD7_GROUP stc. included).
  192. int32_t groupdata_size;
  193. char name[AI_MDL7_MAX_GROUPNAMESIZE];
  194. //! Number of skins
  195. int32_t numskins;
  196. //! Number of texture coordinates
  197. int32_t num_stpts;
  198. //! Number of triangles
  199. int32_t numtris;
  200. //! Number of vertices
  201. int32_t numverts;
  202. //! Number of frames
  203. int32_t numframes;
  204. } PACK_STRUCT;
  205. #define AI_MDL7_SKINTYPE_MIPFLAG 0x08
  206. #define AI_MDL7_SKINTYPE_MATERIAL 0x10
  207. #define AI_MDL7_SKINTYPE_MATERIAL_ASCDEF 0x20
  208. #define AI_MDL7_SKINTYPE_RGBFLAG 0x80
  209. #define AI_MDL7_MAX_BONENAMESIZE 20
  210. // ---------------------------------------------------------------------------
  211. /** \struct Deformer_MDL7
  212. * \brief Deformer in a MDL7 file
  213. */
  214. // ---------------------------------------------------------------------------
  215. struct Deformer_MDL7
  216. {
  217. int8_t deformer_version; // 0
  218. int8_t deformer_typ; // 0 - bones
  219. int8_t _unused_[2];
  220. int32_t group_index;
  221. int32_t elements;
  222. int32_t deformerdata_size;
  223. } PACK_STRUCT;
  224. // ---------------------------------------------------------------------------
  225. /** \struct DeformerElement_MDL7
  226. * \brief Deformer element in a MDL7 file
  227. */
  228. // ---------------------------------------------------------------------------
  229. struct DeformerElement_MDL7
  230. {
  231. //! bei deformer_typ==0 (==bones) element_index == bone index
  232. int32_t element_index;
  233. char element_name[AI_MDL7_MAX_BONENAMESIZE];
  234. int32_t weights;
  235. } PACK_STRUCT;
  236. // ---------------------------------------------------------------------------
  237. /** \struct DeformerWeight_MDL7
  238. * \brief Deformer weight in a MDL7 file
  239. */
  240. // ---------------------------------------------------------------------------
  241. struct DeformerWeight_MDL7
  242. {
  243. //! for deformer_typ==0 (==bones) index == vertex index
  244. int32_t index;
  245. float weight;
  246. } PACK_STRUCT;
  247. // maximum length of texture file name
  248. #define AI_MDL7_MAX_TEXNAMESIZE 0x10
  249. // don't know why this was in the original headers ...
  250. // to be removed in future versions
  251. typedef int32_t MD7_MATERIAL_ASCDEFSIZE;
  252. // ---------------------------------------------------------------------------
  253. /** \struct Skin_MDL7
  254. * \brief Skin in a MDL7 file
  255. */
  256. // ---------------------------------------------------------------------------
  257. struct Skin_MDL7
  258. {
  259. uint8_t typ;
  260. int8_t _unused_[3];
  261. int32_t width;
  262. int32_t height;
  263. char texture_name[AI_MDL7_MAX_TEXNAMESIZE];
  264. } PACK_STRUCT;
  265. struct ColorValue_MDL7
  266. {
  267. float r,g,b,a;
  268. } PACK_STRUCT;
  269. // ---------------------------------------------------------------------------
  270. /** \struct Material_MDL7
  271. * \brief Material in a MDL7 file
  272. */
  273. // ---------------------------------------------------------------------------
  274. struct Material_MDL7
  275. {
  276. //! Diffuse base color of the material
  277. ColorValue_MDL7 Diffuse;
  278. //! Ambient base color of the material
  279. ColorValue_MDL7 Ambient;
  280. //! Specular base color of the material
  281. ColorValue_MDL7 Specular;
  282. //! Emissive base color of the material
  283. ColorValue_MDL7 Emissive;
  284. //! Phong power
  285. float Power;
  286. } PACK_STRUCT;
  287. // ---------------------------------------------------------------------------
  288. /** \struct Skin
  289. * \brief Skin data structure #1
  290. */
  291. // ---------------------------------------------------------------------------
  292. struct Skin
  293. {
  294. //! 0 = single (Skin), 1 = group (GroupSkin)
  295. //! For MDL3-5: Defines the type of the skin and there
  296. //! fore the size of the data to skip:
  297. //-------------------------------------------------------
  298. //! 2 for 565 RGB,
  299. //! 3 for 4444 ARGB,
  300. //! 10 for 565 mipmapped,
  301. //! 11 for 4444 mipmapped (bpp = 2),
  302. //! 12 for 888 RGB mipmapped (bpp = 3),
  303. //! 13 for 8888 ARGB mipmapped (bpp = 4)
  304. //-------------------------------------------------------
  305. int32_t group;
  306. //! Texture data
  307. uint8_t *data;
  308. } PACK_STRUCT;
  309. struct Skin_MDL5
  310. {
  311. int32_t size, width, height;
  312. uint8_t *data;
  313. } PACK_STRUCT;
  314. struct RGB565
  315. {
  316. uint16_t r : 5;
  317. uint16_t g : 6;
  318. uint16_t b : 5;
  319. } PACK_STRUCT;
  320. struct ARGB4
  321. {
  322. uint16_t a : 4;
  323. uint16_t r : 4;
  324. uint16_t g : 4;
  325. uint16_t b : 4;
  326. } PACK_STRUCT;
  327. // ---------------------------------------------------------------------------
  328. /** \struct GroupSkin
  329. * \brief Skin data structure #2 (group of pictures)
  330. */
  331. // ---------------------------------------------------------------------------
  332. struct GroupSkin
  333. {
  334. //! 0 = single (Skin), 1 = group (GroupSkin)
  335. int32_t group;
  336. //! Number of images
  337. int32_t nb;
  338. //! Time for each image
  339. float *time;
  340. //! Data of each image
  341. uint8_t **data;
  342. } PACK_STRUCT;
  343. // ---------------------------------------------------------------------------
  344. /** \struct TexCoord
  345. * \brief Texture coordinate data structure
  346. */
  347. // ---------------------------------------------------------------------------
  348. struct TexCoord
  349. {
  350. //! Is the vertex on the noundary between front and back piece?
  351. int32_t onseam;
  352. //! Texture coordinate in the tx direction
  353. int32_t s;
  354. //! Texture coordinate in the ty direction
  355. int32_t t;
  356. } PACK_STRUCT;
  357. struct TexCoord_MDL3
  358. {
  359. //! position, horizontally in range 0..skinwidth-1
  360. int16_t u;
  361. //! position, vertically in range 0..skinheight-1
  362. int16_t v;
  363. } PACK_STRUCT;
  364. struct TexCoord_MDL7
  365. {
  366. //! position, horizontally in range 0..1
  367. float u;
  368. //! position, vertically in range 0..1
  369. float v;
  370. } PACK_STRUCT;
  371. // ---------------------------------------------------------------------------
  372. /** \struct Triangle
  373. * \brief Triangle data structure
  374. */
  375. // ---------------------------------------------------------------------------
  376. struct Triangle
  377. {
  378. //! 0 = backface, 1 = frontface
  379. int32_t facesfront;
  380. //! Vertex indices
  381. int32_t vertex[3];
  382. } PACK_STRUCT;
  383. struct Triangle_MDL3
  384. {
  385. //! Index of 3 3D vertices in range 0..numverts
  386. uint16_t index_xyz[3];
  387. //! Index of 3 skin vertices in range 0..numskinverts
  388. uint16_t index_uv[3];
  389. } PACK_STRUCT;
  390. struct SkinSet_MDL7
  391. {
  392. //! Index into the UV coordinate list
  393. uint16_t st_index[3]; // size 6
  394. //! Material index
  395. int32_t material; // size 4
  396. } PACK_STRUCT;
  397. struct Triangle_MDL7
  398. {
  399. //! Vertex indices
  400. uint16_t v_index[3]; // size 6
  401. //! Two skinsets. The second will be used for multi-texturing
  402. SkinSet_MDL7 skinsets[2];
  403. } PACK_STRUCT;
  404. // Helper constants for Triangle::facesfront
  405. #if (!defined AI_MDL_BACKFACE)
  406. # define AI_MDL_BACKFACE 0x0
  407. #endif
  408. #if (!defined AI_MDL_FRONTFACE)
  409. # define AI_MDL_FRONTFACE 0x1
  410. #endif
  411. // ---------------------------------------------------------------------------
  412. /** \struct Vertex
  413. * \brief Vertex data structure
  414. */
  415. // ---------------------------------------------------------------------------
  416. struct Vertex
  417. {
  418. uint8_t v[3];
  419. uint8_t normalIndex;
  420. } PACK_STRUCT;
  421. struct Vertex_MDL4
  422. {
  423. uint16_t v[3];
  424. uint8_t normalIndex;
  425. uint8_t unused;
  426. } PACK_STRUCT;
  427. #define AI_MDL7_FRAMEVERTEX120503_STCSIZE 16
  428. #define AI_MDL7_FRAMEVERTEX030305_STCSIZE 26
  429. // ---------------------------------------------------------------------------
  430. /** \struct Vertex_MDL7
  431. * \brief Vertex data structure used in MDL7 files
  432. */
  433. // ---------------------------------------------------------------------------
  434. struct Vertex_MDL7
  435. {
  436. float x,y,z;
  437. uint16_t vertindex;
  438. union {
  439. uint16_t norm162index;
  440. float norm[3];
  441. };
  442. } PACK_STRUCT;
  443. // ---------------------------------------------------------------------------
  444. /** \struct BoneTransform_MDL7
  445. * \brief bone transformation matrix structure used in MDL7 files
  446. */
  447. // ---------------------------------------------------------------------------
  448. struct BoneTransform_MDL7
  449. {
  450. //! 4*3
  451. float m [4*4];
  452. //! the index of this vertex, 0.. header::bones_num - 1
  453. uint16_t bone_index;
  454. //! I HATE 3DGS AND THE SILLY DEVELOPER WHO DESIGNED
  455. //! THIS STUPID FILE FORMAT!
  456. int8_t _unused_[2];
  457. } PACK_STRUCT;
  458. #define AI_MDL7_MAX_FRAMENAMESIZE 16
  459. // ---------------------------------------------------------------------------
  460. /** \struct Frame_MDL7
  461. * \brief Frame data structure used by MDL7 files
  462. */
  463. // ---------------------------------------------------------------------------
  464. struct Frame_MDL7
  465. {
  466. char frame_name[AI_MDL7_MAX_FRAMENAMESIZE];
  467. uint32_t vertices_count;
  468. uint32_t transmatrix_count;
  469. };
  470. // ---------------------------------------------------------------------------
  471. /** \struct SimpleFrame
  472. * \brief Data structure for a simple frame
  473. */
  474. // ---------------------------------------------------------------------------
  475. struct SimpleFrame
  476. {
  477. //! Minimum vertex of the bounding box
  478. Vertex bboxmin;
  479. //! Maximum vertex of the bounding box
  480. Vertex bboxmax;
  481. //! Name of the frame
  482. char name[16];
  483. //! Vertex list of the frame
  484. Vertex *verts;
  485. } PACK_STRUCT;
  486. // ---------------------------------------------------------------------------
  487. /** \struct Frame
  488. * \brief Model frame data structure
  489. */
  490. // ---------------------------------------------------------------------------
  491. struct Frame
  492. {
  493. //! 0 = simple frame, !0 = group frame
  494. int32_t type;
  495. //! Frame data
  496. SimpleFrame frame;
  497. } PACK_STRUCT;
  498. struct SimpleFrame_MDLn_SP
  499. {
  500. //! Minimum vertex of the bounding box
  501. Vertex_MDL4 bboxmin;
  502. //! Maximum vertex of the bounding box
  503. Vertex_MDL4 bboxmax;
  504. //! Name of the frame
  505. char name[16];
  506. //! Vertex list of the frame
  507. Vertex_MDL4 *verts;
  508. } PACK_STRUCT;
  509. // ---------------------------------------------------------------------------
  510. /** \struct GroupFrame
  511. * \brief Data structure for a group of frames
  512. */
  513. // ---------------------------------------------------------------------------
  514. struct GroupFrame
  515. {
  516. //! 0 = simple frame, !0 = group frame
  517. int32_t type;
  518. //! Minimum vertex for all single frames
  519. Vertex min;
  520. //! Maximum vertex for all single frames
  521. Vertex max;
  522. //! Time for all single frames
  523. float *time;
  524. //! List of single frames
  525. SimpleFrame *frames;
  526. } PACK_STRUCT;
  527. // reset packing to the original value
  528. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  529. # pragma pack( pop )
  530. #endif
  531. #undef PACK_STRUCT
  532. // ---------------------------------------------------------------------------
  533. /** \struct IntFace_MDL7
  534. * \brief Internal data structure to temporarily represent a face
  535. */
  536. // ---------------------------------------------------------------------------
  537. struct IntFace_MDL7
  538. {
  539. // provide a constructor for our own convenience
  540. IntFace_MDL7()
  541. {
  542. // set everything to zero
  543. mIndices[0] = mIndices[1] = mIndices[2] = 0;
  544. iMatIndex[0] = iMatIndex[1] = 0;
  545. }
  546. //! Vertex indices
  547. uint32_t mIndices[3];
  548. //! Material index (maximally two channels, which are joined later)
  549. unsigned int iMatIndex[2];
  550. };
  551. // ---------------------------------------------------------------------------
  552. /** \struct IntMaterial_MDL7
  553. * \brief Internal data structure to temporarily represent a material
  554. * which has been created from two single materials along with the
  555. * original material indices.
  556. */
  557. // ---------------------------------------------------------------------------
  558. struct IntMaterial_MDL7
  559. {
  560. // provide a constructor for our own convenience
  561. IntMaterial_MDL7()
  562. {
  563. pcMat = NULL;
  564. iOldMatIndices[0] = iOldMatIndices[1] = 0;
  565. }
  566. //! Material instance
  567. MaterialHelper* pcMat;
  568. //! Old material indices
  569. unsigned int iOldMatIndices[2];
  570. };
  571. };}; // end namespaces
  572. #endif // !! AI_MDLFILEHELPER_H_INC