M3Importer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. #ifndef AI_M3LOADER_H_INCLUDED
  34. #define AI_M3LOADER_H_INCLUDED
  35. #include <vector>
  36. namespace Assimp {
  37. namespace M3 {
  38. // ------------------------------------------------------------------------------------------------
  39. // The following data definitions are from http://code.google.com/p/libm3/, many thanks for that
  40. // help.
  41. // ------------------------------------------------------------------------------------------------
  42. typedef unsigned char uint8;
  43. typedef char int8;
  44. typedef unsigned short uint16;
  45. typedef short int16;
  46. typedef unsigned int uint32;
  47. typedef int int32;
  48. class Vec3D
  49. {
  50. public:
  51. float x,y,z;
  52. Vec3D(float x0 = 0.0f, float y0 = 0.0f, float z0 = 0.0f) : x(x0), y(y0), z(z0) {}
  53. Vec3D(const Vec3D& v) : x(v.x), y(v.y), z(v.z) {}
  54. void reset()
  55. {
  56. x = y = z = 0.0f;
  57. }
  58. Vec3D& operator= (const Vec3D &v)
  59. {
  60. x = v.x;
  61. y = v.y;
  62. z = v.z;
  63. return *this;
  64. }
  65. Vec3D operator+ (const Vec3D &v) const
  66. {
  67. Vec3D r(x+v.x,y+v.y,z+v.z);
  68. return r;
  69. }
  70. Vec3D operator- (const Vec3D &v) const
  71. {
  72. Vec3D r(x-v.x,y-v.y,z-v.z);
  73. return r;
  74. }
  75. float operator* (const Vec3D &v) const
  76. {
  77. return x*v.x + y*v.y + z*v.z;
  78. }
  79. Vec3D operator* (float d) const
  80. {
  81. Vec3D r(x*d,y*d,z*d);
  82. return r;
  83. }
  84. Vec3D operator/ (float d) const
  85. {
  86. Vec3D r(x/d,y/d,z/d);
  87. return r;
  88. }
  89. friend Vec3D operator* (float d, const Vec3D& v)
  90. {
  91. return v * d;
  92. }
  93. // Cross Product
  94. Vec3D operator% (const Vec3D &v) const
  95. {
  96. Vec3D r(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
  97. return r;
  98. }
  99. Vec3D& operator+= (const Vec3D &v)
  100. {
  101. x += v.x;
  102. y += v.y;
  103. z += v.z;
  104. return *this;
  105. }
  106. Vec3D& operator-= (const Vec3D &v)
  107. {
  108. x -= v.x;
  109. y -= v.y;
  110. z -= v.z;
  111. return *this;
  112. }
  113. Vec3D& operator*= (float d)
  114. {
  115. x *= d;
  116. y *= d;
  117. z *= d;
  118. return *this;
  119. }
  120. float lengthSquared() const
  121. {
  122. return x*x+y*y+z*z;
  123. }
  124. float length() const
  125. {
  126. return sqrtf(x*x+y*y+z*z);
  127. }
  128. Vec3D& normalize()
  129. {
  130. this->operator*= (1.0f/length());
  131. return *this;
  132. }
  133. Vec3D operator~ () const
  134. {
  135. Vec3D r(*this);
  136. r.normalize();
  137. return r;
  138. }
  139. operator float*()
  140. {
  141. return (float*)this;
  142. }
  143. };
  144. class Vec2D
  145. {
  146. public:
  147. float x,y;
  148. Vec2D(float x0 = 0.0f, float y0 = 0.0f) : x(x0), y(y0) {}
  149. Vec2D(const Vec2D& v) : x(v.x), y(v.y) {}
  150. Vec2D& operator= (const Vec2D &v)
  151. {
  152. x = v.x;
  153. y = v.y;
  154. return *this;
  155. }
  156. Vec2D operator+ (const Vec2D &v) const
  157. {
  158. Vec2D r(x+v.x,y+v.y);
  159. return r;
  160. }
  161. Vec2D operator- (const Vec2D &v) const
  162. {
  163. Vec2D r(x-v.x,y-v.y);
  164. return r;
  165. }
  166. float operator* (const Vec2D &v) const
  167. {
  168. return x*v.x + y*v.y;
  169. }
  170. Vec2D operator* (float d) const
  171. {
  172. Vec2D r(x*d,y*d);
  173. return r;
  174. }
  175. friend Vec2D operator* (float d, const Vec2D& v)
  176. {
  177. return v * d;
  178. }
  179. Vec2D& operator+= (const Vec2D &v)
  180. {
  181. x += v.x;
  182. y += v.y;
  183. return *this;
  184. }
  185. Vec2D& operator-= (const Vec2D &v)
  186. {
  187. x -= v.x;
  188. y -= v.y;
  189. return *this;
  190. }
  191. Vec2D& operator*= (float d)
  192. {
  193. x *= d;
  194. y *= d;
  195. return *this;
  196. }
  197. float lengthSquared() const
  198. {
  199. return x*x+y*y;
  200. }
  201. float length() const
  202. {
  203. return sqrtf(x*x+y*y);
  204. }
  205. Vec2D& normalize()
  206. {
  207. this->operator*= (1.0f/length());
  208. return *this;
  209. }
  210. Vec2D operator~ () const
  211. {
  212. Vec2D r(*this);
  213. r.normalize();
  214. return r;
  215. }
  216. operator float*()
  217. {
  218. return (float*)this;
  219. }
  220. };
  221. inline
  222. void rotate(float x0, float y0, float *x, float *y, float angle)
  223. {
  224. float xa = *x - x0, ya = *y - y0;
  225. *x = xa*cosf(angle) - ya*sinf(angle) + x0;
  226. *y = xa*sinf(angle) + ya*cosf(angle) + y0;
  227. }
  228. struct Reference
  229. {
  230. uint32 nEntries; // Code 0x00
  231. uint32 ref; // Code 0x04
  232. };
  233. struct ReferenceEntry
  234. {
  235. char id[ 4 ]; // Code 0x00
  236. uint32 offset; // Code 0x04
  237. uint32 nEntries; // Code 0x08
  238. uint32 type; // Code 0x0C
  239. };
  240. struct MD33
  241. {
  242. char id[4]; // Code 0x00
  243. uint32 ofsRefs; // Code 0x04
  244. uint32 nRefs; // Code 0x08
  245. Reference MODL; // Code 0x0C
  246. };
  247. enum ModelType
  248. {
  249. Type1 = 20,
  250. Type2 = 23
  251. };
  252. enum VertexFormat
  253. {
  254. Vertex_Standard,
  255. Vertex_Extended
  256. };
  257. struct MODL23
  258. {
  259. Reference name; // Code 0x00
  260. uint32 version; // Code 0x08
  261. Reference sequenceHeader; // Code 0x0C
  262. Reference sequenceData; // Code 0x14
  263. Reference sequenceLookup; // Code 0x1C
  264. uint32 d2; // Code 0x24
  265. uint32 d3; // Code 0x28
  266. uint32 d4; // Code 0x2C
  267. Reference STS; // Code 0x30
  268. Reference bones; // Code 0x38
  269. uint32 d5; // Code 0x40
  270. uint32 flags; // Code 0x44
  271. Reference vertexData; // Code 0x48
  272. Reference views; // Code 0x50
  273. Reference B; // Code 0x58
  274. Vec3D extents[2]; // Code 0x60
  275. float radius; // Code 0x78
  276. uint32 d7; // Code 0x7C
  277. uint32 d8; // Code 0x80
  278. uint32 d9; // Code 0x84
  279. uint32 d10; // Code 0x88
  280. uint32 d11; // Code 0x8C
  281. uint32 d12; // Code 0x90
  282. uint32 d13; // Code 0x94
  283. uint32 d14; // Code 0x98
  284. uint32 d15; // Code 0x9C
  285. uint32 d16; // Code 0xA0
  286. uint32 d17; // Code 0xA4
  287. uint32 d18; // Code 0xA8
  288. uint32 d19; // Code 0xAC
  289. Reference attachments; // Code 0xB0
  290. Reference attachmentLookup; // Code 0xB8
  291. Reference lights; // Code 0xC0
  292. Reference SHBX; // Code 0xC8
  293. Reference cameras; // Code 0xD0
  294. Reference D; // Code 0xD8
  295. Reference materialLookup; // Code 0xE0
  296. Reference materials; // Code 0xE8
  297. Reference DIS; // Code 0xF0
  298. Reference CMP; // Code 0xF8
  299. Reference TER; // Code 0x10
  300. Reference VOL; // Code 0x10
  301. uint32 d21; // Code 0x11
  302. uint32 d22; // Code 0x11
  303. Reference CREP; // Code 0x11
  304. Reference PAR; // Code 0x12
  305. Reference PARC; // Code 0x12
  306. Reference RIB; // Code 0x13
  307. Reference PROJ; // Code 0x13
  308. Reference FOR; // Code 0x14
  309. Reference WRP; // Code 0x14
  310. uint32 d24; // Code 0x15
  311. uint32 d25; // Code 0x15
  312. Reference PHRB; // Code 0x15
  313. uint32 d27; // Code 0x16
  314. uint32 d28; // Code 0x16
  315. uint32 d29; // Code 0x16
  316. uint32 d30; // Code 0x16
  317. uint32 d32; // Code 0x17
  318. uint32 d33; // Code 0x17
  319. Reference IKJT; // Code 0x17
  320. uint32 d35; // Code 0x18
  321. uint32 d36; // Code 0x18
  322. Reference PATU; // Code 0x18
  323. Reference TRGD; // Code 0x19
  324. Reference IREF; // Code 0x19
  325. Reference E; // Code 0x1A
  326. float matrix[4][4]; // Code 0x1A
  327. Vec3D extent[2]; // Code 0x1E
  328. float rad; // Code 0x20
  329. Reference SSGS; // Code 0x20
  330. Reference ATVL; // Code 0x20
  331. uint32 d61; // Code 0x21
  332. Reference F; // uint16, Code6 0x21
  333. Reference G; // uint16, Code 0x21
  334. Reference BBSC; // Code 0x22
  335. Reference TMD; // Code 0x22
  336. uint32 d62; // Code 0x23
  337. uint32 d63; // Code 0x23
  338. uint32 d64; // Code 0x23
  339. };
  340. struct MODL20
  341. {
  342. Reference name; // Code 0x00
  343. uint32 version; // Code 0x08
  344. Reference sequenceHeader; // Code 0x0C
  345. Reference sequenceData; // Code 0x14
  346. Reference sequenceLookup; // Code 0x1C
  347. uint32 d2; // Code 0x24
  348. uint32 d3; // Code 0x28
  349. uint32 d4; // Code 0x2C
  350. Reference STS; // Code 0x30
  351. Reference bones; // Code 0x38
  352. uint32 d5; // Code 0x44
  353. uint32 flags; // Code 0x44
  354. Reference vertexData; // uint8, Code 0x48
  355. Reference views; // Code 0x50
  356. Reference B; // uint16, Code 0x58
  357. Vec3D extents[2]; // Code 0x60
  358. float radius; // Code 0x78
  359. uint32 d7; // Code 0x7C
  360. uint32 d8; // Code 0x80
  361. uint32 d9; // Code 0x84
  362. uint32 d10; // Code 0x88
  363. uint32 d11; // Code 0x8C
  364. uint32 d12; // Code 0x90
  365. uint32 d13; // Code 0x94
  366. uint32 d14; // Code 0x98
  367. uint32 d15; // Code 0x9C
  368. uint32 d16; // Code 0xA0
  369. uint32 d17; // Code 0xA4
  370. uint32 d18; // Code 0xA8
  371. uint32 d19; // Code 0xAC
  372. Reference attachments; // Code 0xB0
  373. Reference attachmentLookup; // uint16, Code 0xB8
  374. Reference lights; // Code 0xC0
  375. Reference cameras; // Code 0xC8
  376. Reference D; // uint16, Code 0xD0
  377. Reference materialLookup; // Code 0xD8
  378. Reference materials; // Code 0xE0
  379. Reference DIS; // Code 0xE8
  380. Reference CMP; // Code 0xF0
  381. Reference TER; // Code 0xF8
  382. uint32 d20; // Code 0x10
  383. uint32 d21; // Code 0x10
  384. uint32 d22; // Code 0x10
  385. uint32 d23; // Code 0x10
  386. Reference CREP; // Code 0x11
  387. Reference PAR; // Code 0x11
  388. Reference PARC; // Code 0x12
  389. Reference RIB; // Code 0x12
  390. Reference PROJ; // Code 0x13
  391. Reference FOR; // Code 0x13
  392. uint32 d25; // Code 0x14
  393. uint32 d26; // Code 0x14
  394. uint32 d27; // Code 0x14
  395. uint32 d28; // Code 0x14
  396. Reference PHRB; // Code 0x15
  397. uint32 d30; // Code 0x15
  398. uint32 d31; // Code 0x15
  399. uint32 d32; // Code 0x16
  400. uint32 d33; // Code 0x16
  401. uint32 d34; // Code 0x16
  402. uint32 d35; // Code 0x16
  403. Reference IKJT; // Code 0x17
  404. uint32 d36; // Code 0x17
  405. uint32 d37; // Code 0x17
  406. Reference PATU; // Code 0x18
  407. Reference TRGD; // Code 0x18
  408. Reference IREF; // Code 0x19
  409. Reference E; // int32, Code 0x19
  410. float matrix[4][4]; // Code 0x1A
  411. Vec3D extent[2]; // Code 0x1E
  412. float rad; // Code 0x1F
  413. Reference SSGS; // Code 0x1F
  414. uint32 d38; // Code 0x20
  415. uint32 d39; // Code 0x20
  416. Reference BBSC; // Code 0x20
  417. uint32 d40; // Code 0x21
  418. uint32 d41; // Code 0x21
  419. uint32 d42; // Code 0x21
  420. uint32 d43; // Code 0x22
  421. uint32 d44; // Code 0x22
  422. };
  423. struct BONE
  424. {
  425. int32 d1; // Keybone?
  426. Reference name;
  427. uint32 flags;
  428. int16 parent;
  429. int16 s1;
  430. float floats[ 34 ];
  431. };
  432. struct VertexExt // 36 byte
  433. {
  434. Vec3D pos;
  435. uint8 boneWeight[ 4 ];
  436. uint8 boneIndex[ 4 ];
  437. uint8 normal[ 4 ]; //normal_x = (float)normal[0]/255.0f...
  438. int16 uv[ 2 ];
  439. uint32 d1;
  440. uint8 tangent[ 4 ];
  441. };
  442. struct Vertex // 32 byte
  443. {
  444. Vec3D pos;
  445. uint8 boneWeight[4];
  446. uint8 boneIndex[4];
  447. uint8 normal[4]; //normal_x = (float)normal[0]/255.0f...
  448. int16 uv[2];
  449. uint8 tangent[4];
  450. };
  451. struct MATM
  452. {
  453. uint32 d1;
  454. uint32 d2; // Index into MAT-table?
  455. };
  456. struct MAT
  457. {
  458. Reference name;
  459. int ukn1[ 8 ];
  460. float x, y; //always 1.0f
  461. Reference layers[13];
  462. int ukn2[15];
  463. };
  464. struct LAYR
  465. {
  466. int unk;
  467. Reference name;
  468. float unk2[85];
  469. };
  470. struct DIV
  471. {
  472. Reference faces; // Code 0x00
  473. Reference regions; // Code 0x08
  474. Reference BAT; // Code 0x10
  475. Reference MSEC; // Code 0x18
  476. };
  477. struct Region
  478. {
  479. uint32 unk;
  480. uint16 ofsVertices;
  481. uint16 nVertices;
  482. uint32 ofsIndices;
  483. uint32 nIndices; // reference into DIV.faces
  484. uint8 unknown[12];
  485. };
  486. struct CAM
  487. {
  488. int32 d1; // Code 0x00
  489. Reference name; // Code 0x04
  490. uint16 flags1; // Code 0x0C
  491. uint16 flags2; // Code 0x0E
  492. };
  493. struct EVNT
  494. {
  495. Reference name; // Code 0x00
  496. int16 unk1[4]; // Code 0x08
  497. float matrix[4][4]; // Code 0x10
  498. int32 unk2[4]; // Code 0x50
  499. };
  500. struct ATT
  501. {
  502. int32 unk; // Code 0x00
  503. Reference name; // Code 0x04
  504. int32 bone; // Code 0x0C
  505. };
  506. struct PHSH
  507. {
  508. float m[ 4 ][ 4 ];
  509. float f1;
  510. float f2;
  511. Reference refs[ 5 ];
  512. float f3;
  513. };
  514. struct SEQS
  515. {
  516. int32 d1; // Code 0x00
  517. int32 d2; // Code 0x04
  518. Reference name; // Code 0x08
  519. int32 d3; // Code 0x10
  520. uint32 length; // Code 0x14
  521. int32 d4; // Code 0x18
  522. uint32 flags; // Code 0x1C
  523. int32 unk[5]; // Code 0x20
  524. Vec3D extents[2]; // Code 0x34
  525. float radius; // Code 0x4C
  526. int32 d5; // Code 0x50
  527. int32 d6; // Code 0x54
  528. };
  529. struct STC
  530. {
  531. Reference name; // Code 0x00
  532. uint16 s1; // Code 0x08
  533. uint16 s2; // Code 0x0A
  534. uint16 s3; // Code 0x0C
  535. uint16 s4; // Code 0x0E
  536. Reference unk2; // uint32 // Code 0x12
  537. Reference unk3; // uint32 // Code 0x1A
  538. uint32 d3; // Code 0x22
  539. Reference evt; // Code 0x24
  540. Reference unk4[11]; // Seems to be transformation data // Code 0x2C
  541. Reference bnds; // Code 0x84
  542. };
  543. struct STS
  544. {
  545. Reference unk1; // uint32 // Code 0x00
  546. int32 unk[3]; // Code 0x08
  547. int16 s1; // Code 0x14
  548. int16 s2; // Code 0x16
  549. };
  550. struct STG
  551. {
  552. Reference name; // Code 0x00
  553. Reference stcID; // Code 0x08
  554. };
  555. struct SD
  556. {
  557. Reference timeline; // Code 0x00
  558. uint32 flags; // Code 0x08
  559. uint32 length; // Code 0x0C
  560. Reference data; // Code 0x10
  561. };
  562. struct BNDS
  563. {
  564. Vec3D extents1[2]; // Code 0x00
  565. float radius1; // Code 0x18
  566. Vec3D extents2[2]; // Code 0x1C
  567. float radius2; // Code 0x34
  568. };
  569. struct VEC2
  570. {
  571. float x, y;
  572. };
  573. struct VEC3
  574. {
  575. float x, y, z;
  576. };
  577. struct VEC4
  578. {
  579. float x, y, z, w;
  580. };
  581. struct QUAT
  582. {
  583. float x, y, z, w;
  584. };
  585. // ------------------------------------------------------------------------------------------------
  586. /** Loader to import M3-models.
  587. */
  588. // ------------------------------------------------------------------------------------------------
  589. class M3Importer : public BaseImporter
  590. {
  591. friend class Importer;
  592. public:
  593. /// @brief The default constructor.
  594. M3Importer();
  595. /// @brief The destructor.
  596. ~M3Importer();
  597. /// @brief Returns whether the class can handle the format of the given file.
  598. /// @remark See BaseImporter::CanRead() for details.
  599. bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig ) const;
  600. private:
  601. const aiImporterDesc* GetInfo () const;
  602. void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler );
  603. void convertToAssimp( const std::string& pFile, aiScene* pScene, DIV *pViews, Region *pRegions, uint16 *pFaces,
  604. const std::vector<aiVector3D> &vertices, const std::vector<aiVector3D> &uvCoords, const std::vector<aiVector3D> &normals );
  605. void createVertexData( aiMesh *pMesh, const std::vector<aiVector3D> &vertices, const std::vector<aiVector3D> &uvCoords,
  606. const std::vector<aiVector3D> &normals );
  607. aiNode *createNode( aiNode *pParent );
  608. template<typename T>
  609. T* GetEntries( Reference ref );
  610. private:
  611. MD33 *m_pHead;
  612. ReferenceEntry *m_pRefs;
  613. std::vector<unsigned char> m_Buffer;
  614. };
  615. // ------------------------------------------------------------------------------------------------
  616. template<typename T>
  617. inline
  618. T* M3Importer::GetEntries( Reference ref )
  619. {
  620. return (T*) ( &m_Buffer[ 0 ] + m_pRefs[ ref.ref ].offset );
  621. }
  622. // ------------------------------------------------------------------------------------------------
  623. } // Namespace M3
  624. } // Namespace Assimp
  625. #endif // AI_M3LOADER_H_INCLUDED