ASEParser.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /** @file Defines the helper data structures for importing ASE files */
  34. #ifndef AI_ASEFILEHELPER_H_INC
  35. #define AI_ASEFILEHELPER_H_INC
  36. // STL/CRT headers
  37. #include <string>
  38. #include <vector>
  39. #include <list>
  40. // public ASSIMP headers
  41. #include "../include/aiTypes.h"
  42. #include "../include/aiMesh.h"
  43. #include "../include/aiAnim.h"
  44. // for some helper routines like IsSpace()
  45. #include "ParsingUtils.h"
  46. #include "qnan.h"
  47. // ASE is quite similar to 3ds. We can reuse some structures
  48. #include "3DSLoader.h"
  49. namespace Assimp
  50. {
  51. // http://wiki.beyondunreal.com/Legacy:ASE_File_Format
  52. namespace ASE
  53. {
  54. using namespace Dot3DS;
  55. // ---------------------------------------------------------------------------
  56. /** Helper structure representing an ASE material */
  57. struct Material : public Dot3DS::Material
  58. {
  59. //! Default constructor
  60. Material() : pcInstance(NULL), bNeed (false)
  61. {}
  62. //! Contains all sub materials of this material
  63. std::vector<Material> avSubMaterials;
  64. //! MaterialHelper object
  65. MaterialHelper* pcInstance;
  66. //! Can we remove this material?
  67. bool bNeed;
  68. };
  69. // ---------------------------------------------------------------------------
  70. /** Helper structure to represent an ASE file face */
  71. struct Face : public FaceWithSmoothingGroup
  72. {
  73. //! Default constructor. Initializes everything with 0
  74. Face()
  75. {
  76. mColorIndices[0] = mColorIndices[1] = mColorIndices[2] = 0;
  77. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  78. {
  79. amUVIndices[i][0] = amUVIndices[i][1] = amUVIndices[i][2] = 0;
  80. }
  81. iMaterial = DEFAULT_MATINDEX;
  82. iFace = 0;
  83. }
  84. //! special value to indicate that no material index has
  85. //! been assigned to a face. The default material index
  86. //! will replace this value later.
  87. static const unsigned int DEFAULT_MATINDEX = 0xFFFFFFFF;
  88. //! Indices into each list of texture coordinates
  89. unsigned int amUVIndices[AI_MAX_NUMBER_OF_TEXTURECOORDS][3];
  90. //! Index into the list of vertex colors
  91. unsigned int mColorIndices[3];
  92. //! (Sub)Material index to be assigned to this face
  93. unsigned int iMaterial;
  94. //! Index of the face. It is not specified whether it is
  95. //! a requirement of the file format that all faces are
  96. //! written in sequential order, so we have to expect this case
  97. unsigned int iFace;
  98. };
  99. // ---------------------------------------------------------------------------
  100. /** Helper structure to represent an ASE file bone */
  101. struct Bone
  102. {
  103. //! Constructor
  104. Bone()
  105. {
  106. static int iCnt = 0;
  107. char szTemp[128];
  108. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  109. mName = szTemp;
  110. }
  111. //! Name of the bone
  112. std::string mName;
  113. };
  114. // ---------------------------------------------------------------------------
  115. /** Helper structure to represent an ASE file bone vertex */
  116. struct BoneVertex
  117. {
  118. //! Bone and corresponding vertex weight.
  119. //! -1 for unrequired bones ....
  120. std::vector<std::pair<int,float> > mBoneWeights;
  121. //! Position of the bone vertex.
  122. //! MUST be identical to the vertex position
  123. //aiVector3D mPosition;
  124. };
  125. // ---------------------------------------------------------------------------
  126. /** Helper structure to represent an ASE file animation */
  127. struct Animation
  128. {
  129. //! List of rotation keyframes
  130. std::vector< aiQuatKey > akeyRotations;
  131. //! List of position keyframes
  132. std::vector< aiVectorKey > akeyPositions;
  133. };
  134. // ---------------------------------------------------------------------------
  135. /** Helper structure to represent the inheritance information of an ASE node */
  136. struct InheritanceInfo
  137. {
  138. //! Default constructor
  139. InheritanceInfo()
  140. {
  141. // set the inheritance flag for all axes by default to true
  142. for (unsigned int i = 0; i < 3;++i)
  143. abInheritPosition[i] = abInheritRotation[i] = abInheritScaling[i] = true;
  144. }
  145. //! Inherit the parent's position?, axis order is x,y,z
  146. bool abInheritPosition[3];
  147. //! Inherit the parent's rotation?, axis order is x,y,z
  148. bool abInheritRotation[3];
  149. //! Inherit the parent's scaling?, axis order is x,y,z
  150. bool abInheritScaling[3];
  151. };
  152. // ---------------------------------------------------------------------------
  153. /** Stores a decomposed transformation matrix */
  154. struct DecompTransform
  155. {
  156. //! Construction from a reference to an existing matrix
  157. DecompTransform(aiMatrix4x4& ref)
  158. : vPosition(std::numeric_limits<float>::quiet_NaN(),0.0f,0.0f)
  159. , vScaling(1.0f,1.0f,1.0f)
  160. , mMatrix(ref)
  161. {}
  162. //! Translational component
  163. mutable aiVector3D vPosition;
  164. //! Rotational component
  165. mutable aiQuaternion qRotation;
  166. //! Scaling component
  167. mutable aiVector3D vScaling;
  168. //! Reference to the matrix being decomposed
  169. const aiMatrix4x4& mMatrix;
  170. //! Decomposes the matrix if this has not yet been done
  171. inline void NeedDecomposedMatrixNOW() const
  172. {
  173. if (is_qnan(vPosition.x))
  174. {
  175. mMatrix.Decompose(vScaling,qRotation,vPosition);
  176. }
  177. }
  178. };
  179. // ---------------------------------------------------------------------------
  180. /** Helper structure to represent an ASE file mesh */
  181. struct Mesh : public MeshWithSmoothingGroups<ASE::Face>
  182. {
  183. //! Constructor. Creates a default name for the mesh
  184. Mesh() : bSkip(false)
  185. {
  186. static int iCnt = 0;
  187. char szTemp[128]; // should be sufficiently large
  188. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  189. mName = szTemp;
  190. // use 2 texture vertex components by default
  191. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  192. this->mNumUVComponents[c] = 2;
  193. // setup the default material index by default
  194. iMaterialIndex = Face::DEFAULT_MATINDEX;
  195. }
  196. //! Name of the mesh
  197. std::string mName;
  198. //! Name of the parent of the mesh
  199. //! "" if there is no parent ...
  200. std::string mParent;
  201. //! List of all texture coordinate sets
  202. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  203. //! List of all vertex color sets.
  204. std::vector<aiColor4D> mVertexColors;
  205. //! List of all bone vertices
  206. std::vector<BoneVertex> mBoneVertices;
  207. //! List of all bones
  208. std::vector<Bone> mBones;
  209. //! Transformation matrix of the mesh
  210. aiMatrix4x4 mTransform;
  211. //! Animation channels for the node
  212. Animation mAnim;
  213. //! Material index of the mesh
  214. unsigned int iMaterialIndex;
  215. //! Number of vertex components for each UVW set
  216. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  217. //! used internally
  218. bool bSkip;
  219. //! Specifies which axes transformations a node inherits
  220. //! from its parent ...
  221. InheritanceInfo inherit;
  222. };
  223. // ---------------------------------------------------------------------------------
  224. /** \brief Class to parse ASE files
  225. */
  226. class Parser
  227. {
  228. private:
  229. Parser() {}
  230. public:
  231. //! Construct a parser from a given input file which is
  232. //! guaranted to be terminated with zero.
  233. Parser (const char* szFile);
  234. // -------------------------------------------------------------------
  235. //! Parses the file into the parsers internal representation
  236. void Parse();
  237. private:
  238. // -------------------------------------------------------------------
  239. //! Parse the *SCENE block in a file
  240. void ParseLV1SceneBlock();
  241. // -------------------------------------------------------------------
  242. //! Parse the *MATERIAL_LIST block in a file
  243. void ParseLV1MaterialListBlock();
  244. // -------------------------------------------------------------------
  245. //! Parse a *GEOMOBJECT block in a file
  246. //! \param mesh Mesh object to be filled
  247. void ParseLV1GeometryObjectBlock(Mesh& mesh);
  248. // -------------------------------------------------------------------
  249. //! Parse a *MATERIAL blocks in a material list
  250. //! \param mat Material structure to be filled
  251. void ParseLV2MaterialBlock(Material& mat);
  252. // -------------------------------------------------------------------
  253. //! Parse a *NODE_TM block in a file
  254. //! \param mesh Mesh object to be filled
  255. void ParseLV2NodeTransformBlock(Mesh& mesh);
  256. // -------------------------------------------------------------------
  257. //! Parse a *TM_ANIMATION block in a file
  258. //! \param mesh Mesh object to be filled
  259. void ParseLV2AnimationBlock(Mesh& mesh);
  260. void ParseLV3PosAnimationBlock(Mesh& mesh);
  261. void ParseLV3RotAnimationBlock(Mesh& mesh);
  262. // -------------------------------------------------------------------
  263. //! Parse a *MESH block in a file
  264. //! \param mesh Mesh object to be filled
  265. void ParseLV2MeshBlock(Mesh& mesh);
  266. // -------------------------------------------------------------------
  267. //! Parse the *MAP_XXXXXX blocks in a material
  268. //! \param map Texture structure to be filled
  269. void ParseLV3MapBlock(Texture& map);
  270. // -------------------------------------------------------------------
  271. //! Parse a *MESH_VERTEX_LIST block in a file
  272. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  273. //! Otherwise zero. This is used to check the consistency of the file.
  274. //! A warning is sent to the logger if the validations fails.
  275. //! \param mesh Mesh object to be filled
  276. void ParseLV3MeshVertexListBlock(
  277. unsigned int iNumVertices,Mesh& mesh);
  278. // -------------------------------------------------------------------
  279. //! Parse a *MESH_FACE_LIST block in a file
  280. //! \param iNumFaces Value of *MESH_NUMFACES, if present.
  281. //! Otherwise zero. This is used to check the consistency of the file.
  282. //! A warning is sent to the logger if the validations fails.
  283. //! \param mesh Mesh object to be filled
  284. void ParseLV3MeshFaceListBlock(
  285. unsigned int iNumFaces,Mesh& mesh);
  286. // -------------------------------------------------------------------
  287. //! Parse a *MESH_TVERT_LIST block in a file
  288. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  289. //! Otherwise zero. This is used to check the consistency of the file.
  290. //! A warning is sent to the logger if the validations fails.
  291. //! \param mesh Mesh object to be filled
  292. //! \param iChannel Output UVW channel
  293. void ParseLV3MeshTListBlock(
  294. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  295. // -------------------------------------------------------------------
  296. //! Parse a *MESH_TFACELIST block in a file
  297. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  298. //! Otherwise zero. This is used to check the consistency of the file.
  299. //! A warning is sent to the logger if the validations fails.
  300. //! \param mesh Mesh object to be filled
  301. //! \param iChannel Output UVW channel
  302. void ParseLV3MeshTFaceListBlock(
  303. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  304. // -------------------------------------------------------------------
  305. //! Parse an additional mapping channel
  306. //! (specified via *MESH_MAPPINGCHANNEL)
  307. //! \param iChannel Channel index to be filled
  308. //! \param mesh Mesh object to be filled
  309. void ParseLV3MappingChannel(
  310. unsigned int iChannel, Mesh& mesh);
  311. // -------------------------------------------------------------------
  312. //! Parse a *MESH_CVERTLIST block in a file
  313. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  314. //! Otherwise zero. This is used to check the consistency of the file.
  315. //! A warning is sent to the logger if the validations fails.
  316. //! \param mesh Mesh object to be filled
  317. void ParseLV3MeshCListBlock(
  318. unsigned int iNumVertices, Mesh& mesh);
  319. // -------------------------------------------------------------------
  320. //! Parse a *MESH_CFACELIST block in a file
  321. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  322. //! Otherwise zero. This is used to check the consistency of the file.
  323. //! A warning is sent to the logger if the validations fails.
  324. //! \param mesh Mesh object to be filled
  325. void ParseLV3MeshCFaceListBlock(
  326. unsigned int iNumFaces, Mesh& mesh);
  327. // -------------------------------------------------------------------
  328. //! Parse a *MESH_NORMALS block in a file
  329. //! \param mesh Mesh object to be filled
  330. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  331. // -------------------------------------------------------------------
  332. //! Parse a *MESH_WEIGHTSblock in a file
  333. //! \param mesh Mesh object to be filled
  334. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  335. // -------------------------------------------------------------------
  336. //! Parse the bone list of a file
  337. //! \param mesh Mesh object to be filled
  338. //! \param iNumBones Number of bones in the mesh
  339. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  340. // -------------------------------------------------------------------
  341. //! Parse the bone vertices list of a file
  342. //! \param mesh Mesh object to be filled
  343. //! \param iNumVertices Number of vertices to be parsed
  344. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  345. // -------------------------------------------------------------------
  346. //! Parse a *MESH_FACE block in a file
  347. //! \param out receive the face data
  348. void ParseLV4MeshFace(ASE::Face& out);
  349. // -------------------------------------------------------------------
  350. //! Parse a *MESH_VERT block in a file
  351. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  352. //! \param apOut Output buffer (3 floats)
  353. //! \param rIndexOut Output index
  354. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  355. // -------------------------------------------------------------------
  356. //! Parse a *MESH_VERT block in a file
  357. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  358. //! \param apOut Output buffer (3 floats)
  359. void ParseLV4MeshFloatTriple(float* apOut);
  360. // -------------------------------------------------------------------
  361. //! Parse a *MESH_TFACE block in a file
  362. //! (also works for MESH_CFACE)
  363. //! \param apOut Output buffer (3 ints)
  364. //! \param rIndexOut Output index
  365. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  366. // -------------------------------------------------------------------
  367. //! Parse a *MESH_TFACE block in a file
  368. //! (also works for MESH_CFACE)
  369. //! \param apOut Output buffer (3 ints)
  370. void ParseLV4MeshLongTriple(unsigned int* apOut);
  371. // -------------------------------------------------------------------
  372. //! Parse a single float element
  373. //! \param fOut Output float
  374. void ParseLV4MeshFloat(float& fOut);
  375. // -------------------------------------------------------------------
  376. //! Parse a single int element
  377. //! \param iOut Output integer
  378. void ParseLV4MeshLong(unsigned int& iOut);
  379. // -------------------------------------------------------------------
  380. //! Skip everything to the next: '*' or '\0'
  381. bool SkipToNextToken();
  382. // -------------------------------------------------------------------
  383. //! Skip the current section until the token after the closing }.
  384. //! This function handles embedded subsections correctly
  385. bool SkipSection();
  386. // -------------------------------------------------------------------
  387. //! Output a warning to the logger
  388. //! \param szWarn Warn message
  389. void LogWarning(const char* szWarn);
  390. // -------------------------------------------------------------------
  391. //! Output a message to the logger
  392. //! \param szWarn Message
  393. void LogInfo(const char* szWarn);
  394. // -------------------------------------------------------------------
  395. //! Output an error to the logger
  396. //! \param szWarn Error message
  397. void LogError(const char* szWarn);
  398. // -------------------------------------------------------------------
  399. //! Parse a string, enclosed in double quotation marks
  400. //! \param out Output string
  401. //! \param szName Name of the enclosing element -> used in error
  402. //! messages.
  403. //! \return false if an error occured
  404. bool ParseString(std::string& out,const char* szName);
  405. public:
  406. //! Pointer to current data
  407. const char* m_szFile;
  408. //! background color to be passed to the viewer
  409. //! QNAN if none was found
  410. aiColor3D m_clrBackground;
  411. //! Base ambient color to be passed to all materials
  412. //! QNAN if none was found
  413. aiColor3D m_clrAmbient;
  414. //! List of all materials found in the file
  415. std::vector<Material> m_vMaterials;
  416. //! List of all meshes found in the file
  417. std::vector<Mesh> m_vMeshes;
  418. //! Current line in the file
  419. unsigned int iLineNumber;
  420. //! First frame
  421. unsigned int iFirstFrame;
  422. //! Last frame
  423. unsigned int iLastFrame;
  424. //! Frame speed - frames per second
  425. unsigned int iFrameSpeed;
  426. //! Ticks per frame
  427. unsigned int iTicksPerFrame;
  428. //! true if the last character read was an end-line character
  429. bool bLastWasEndLine;
  430. };
  431. } // Namespace ASE
  432. } // Namespace ASSIMP
  433. #endif // !! include guard