ASEParser.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 "PlyParser.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 Dot3DS::Face
  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. : vScaling(1.0f,1.0f,1.0f)
  159. , vPosition(std::numeric_limits<float>::quiet_NaN(),0.0f,0.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
  182. {
  183. //! Constructor. Creates a default name for the mesh
  184. Mesh() : bSkip(false)
  185. {
  186. static int iCnt = 0;
  187. char szTemp[128];
  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. //! vertex positions
  202. std::vector<aiVector3D> mPositions;
  203. //! List of all faces loaded
  204. std::vector<ASE::Face> mFaces;
  205. //! List of all texture coordinate sets
  206. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  207. //! List of all vertex color sets.
  208. std::vector<aiColor4D> mVertexColors;
  209. //! List of normal vectors
  210. std::vector<aiVector3D> mNormals;
  211. //! List of all bone vertices
  212. std::vector<BoneVertex> mBoneVertices;
  213. //! List of all bones
  214. std::vector<Bone> mBones;
  215. //! Transformation matrix of the mesh
  216. aiMatrix4x4 mTransform;
  217. //! Animation channels for the node
  218. Animation mAnim;
  219. //! Material index of the mesh
  220. unsigned int iMaterialIndex;
  221. //! Number of vertex components for each UVW set
  222. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  223. //! used internally
  224. bool bSkip;
  225. //! Specifies which axes transformations a node inherits
  226. //! from its parent ...
  227. InheritanceInfo inherit;
  228. };
  229. // ---------------------------------------------------------------------------------
  230. /** \brief Class to parse ASE files
  231. */
  232. class Parser
  233. {
  234. private:
  235. Parser() {}
  236. public:
  237. //! Construct a parser from a given input file which is
  238. //! guaranted to be terminated with zero.
  239. Parser (const char* szFile);
  240. // -------------------------------------------------------------------
  241. //! Parses the file into the parsers internal representation
  242. void Parse();
  243. private:
  244. // -------------------------------------------------------------------
  245. //! Parse the *SCENE block in a file
  246. void ParseLV1SceneBlock();
  247. // -------------------------------------------------------------------
  248. //! Parse the *MATERIAL_LIST block in a file
  249. void ParseLV1MaterialListBlock();
  250. // -------------------------------------------------------------------
  251. //! Parse a *GEOMOBJECT block in a file
  252. //! \param mesh Mesh object to be filled
  253. void ParseLV1GeometryObjectBlock(Mesh& mesh);
  254. // -------------------------------------------------------------------
  255. //! Parse a *MATERIAL blocks in a material list
  256. //! \param mat Material structure to be filled
  257. void ParseLV2MaterialBlock(Material& mat);
  258. // -------------------------------------------------------------------
  259. //! Parse a *NODE_TM block in a file
  260. //! \param mesh Mesh object to be filled
  261. void ParseLV2NodeTransformBlock(Mesh& mesh);
  262. // -------------------------------------------------------------------
  263. //! Parse a *TM_ANIMATION block in a file
  264. //! \param mesh Mesh object to be filled
  265. void ParseLV2AnimationBlock(Mesh& mesh);
  266. void ParseLV3PosAnimationBlock(Mesh& mesh);
  267. void ParseLV3RotAnimationBlock(Mesh& mesh);
  268. // -------------------------------------------------------------------
  269. //! Parse a *MESH block in a file
  270. //! \param mesh Mesh object to be filled
  271. void ParseLV2MeshBlock(Mesh& mesh);
  272. // -------------------------------------------------------------------
  273. //! Parse the *MAP_XXXXXX blocks in a material
  274. //! \param map Texture structure to be filled
  275. void ParseLV3MapBlock(Texture& map);
  276. // -------------------------------------------------------------------
  277. //! Parse a *MESH_VERTEX_LIST block in a file
  278. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  279. //! Otherwise zero. This is used to check the consistency of the file.
  280. //! A warning is sent to the logger if the validations fails.
  281. //! \param mesh Mesh object to be filled
  282. void ParseLV3MeshVertexListBlock(
  283. unsigned int iNumVertices,Mesh& mesh);
  284. // -------------------------------------------------------------------
  285. //! Parse a *MESH_FACE_LIST block in a file
  286. //! \param iNumFaces Value of *MESH_NUMFACES, if present.
  287. //! Otherwise zero. This is used to check the consistency of the file.
  288. //! A warning is sent to the logger if the validations fails.
  289. //! \param mesh Mesh object to be filled
  290. void ParseLV3MeshFaceListBlock(
  291. unsigned int iNumFaces,Mesh& mesh);
  292. // -------------------------------------------------------------------
  293. //! Parse a *MESH_TVERT_LIST block in a file
  294. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  295. //! Otherwise zero. This is used to check the consistency of the file.
  296. //! A warning is sent to the logger if the validations fails.
  297. //! \param mesh Mesh object to be filled
  298. //! \param iChannel Output UVW channel
  299. void ParseLV3MeshTListBlock(
  300. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  301. // -------------------------------------------------------------------
  302. //! Parse a *MESH_TFACELIST block in a file
  303. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  304. //! Otherwise zero. This is used to check the consistency of the file.
  305. //! A warning is sent to the logger if the validations fails.
  306. //! \param mesh Mesh object to be filled
  307. //! \param iChannel Output UVW channel
  308. void ParseLV3MeshTFaceListBlock(
  309. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  310. // -------------------------------------------------------------------
  311. //! Parse an additional mapping channel
  312. //! (specified via *MESH_MAPPINGCHANNEL)
  313. //! \param iChannel Channel index to be filled
  314. //! \param mesh Mesh object to be filled
  315. void ParseLV3MappingChannel(
  316. unsigned int iChannel, Mesh& mesh);
  317. // -------------------------------------------------------------------
  318. //! Parse a *MESH_CVERTLIST block in a file
  319. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  320. //! Otherwise zero. This is used to check the consistency of the file.
  321. //! A warning is sent to the logger if the validations fails.
  322. //! \param mesh Mesh object to be filled
  323. void ParseLV3MeshCListBlock(
  324. unsigned int iNumVertices, Mesh& mesh);
  325. // -------------------------------------------------------------------
  326. //! Parse a *MESH_CFACELIST block in a file
  327. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  328. //! Otherwise zero. This is used to check the consistency of the file.
  329. //! A warning is sent to the logger if the validations fails.
  330. //! \param mesh Mesh object to be filled
  331. void ParseLV3MeshCFaceListBlock(
  332. unsigned int iNumFaces, Mesh& mesh);
  333. // -------------------------------------------------------------------
  334. //! Parse a *MESH_NORMALS block in a file
  335. //! \param mesh Mesh object to be filled
  336. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  337. // -------------------------------------------------------------------
  338. //! Parse a *MESH_WEIGHTSblock in a file
  339. //! \param mesh Mesh object to be filled
  340. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  341. // -------------------------------------------------------------------
  342. //! Parse the bone list of a file
  343. //! \param mesh Mesh object to be filled
  344. //! \param iNumBones Number of bones in the mesh
  345. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  346. // -------------------------------------------------------------------
  347. //! Parse the bone vertices list of a file
  348. //! \param mesh Mesh object to be filled
  349. //! \param iNumVertices Number of vertices to be parsed
  350. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  351. // -------------------------------------------------------------------
  352. //! Parse a *MESH_FACE block in a file
  353. //! \param out receive the face data
  354. void ParseLV4MeshFace(ASE::Face& out);
  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. //! \param rIndexOut Output index
  360. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  361. // -------------------------------------------------------------------
  362. //! Parse a *MESH_VERT block in a file
  363. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  364. //! \param apOut Output buffer (3 floats)
  365. void ParseLV4MeshFloatTriple(float* apOut);
  366. // -------------------------------------------------------------------
  367. //! Parse a *MESH_TFACE block in a file
  368. //! (also works for MESH_CFACE)
  369. //! \param apOut Output buffer (3 ints)
  370. //! \param rIndexOut Output index
  371. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  372. // -------------------------------------------------------------------
  373. //! Parse a *MESH_TFACE block in a file
  374. //! (also works for MESH_CFACE)
  375. //! \param apOut Output buffer (3 ints)
  376. void ParseLV4MeshLongTriple(unsigned int* apOut);
  377. // -------------------------------------------------------------------
  378. //! Parse a single float element
  379. //! \param fOut Output float
  380. void ParseLV4MeshFloat(float& fOut);
  381. // -------------------------------------------------------------------
  382. //! Parse a single int element
  383. //! \param iOut Output integer
  384. void ParseLV4MeshLong(unsigned int& iOut);
  385. // -------------------------------------------------------------------
  386. //! Skip the opening bracket at the beginning of a complex statement
  387. bool SkipOpeningBracket();
  388. // -------------------------------------------------------------------
  389. //! Skip everything to the next: '*' or '\0'
  390. bool SkipToNextToken();
  391. // -------------------------------------------------------------------
  392. //! Skip the current section until the token after the closing }.
  393. //! This function handles embedded subsections correctly
  394. bool SkipSection();
  395. // -------------------------------------------------------------------
  396. //! Output a warning to the logger
  397. //! \param szWarn Warn message
  398. void LogWarning(const char* szWarn);
  399. // -------------------------------------------------------------------
  400. //! Output a message to the logger
  401. //! \param szWarn Message
  402. void LogInfo(const char* szWarn);
  403. // -------------------------------------------------------------------
  404. //! Output an error to the logger
  405. //! \param szWarn Error message
  406. void LogError(const char* szWarn);
  407. // -------------------------------------------------------------------
  408. //! Parse a string, enclosed in double quotation marks
  409. //! \param out Output string
  410. //! \param szName Name of the enclosing element -> used in error
  411. //! messages.
  412. //! \return false if an error occured
  413. bool ParseString(std::string& out,const char* szName);
  414. public:
  415. //! Pointer to current data
  416. const char* m_szFile;
  417. //! background color to be passed to the viewer
  418. //! QNAN if none was found
  419. aiColor3D m_clrBackground;
  420. //! Base ambient color to be passed to all materials
  421. //! QNAN if none was found
  422. aiColor3D m_clrAmbient;
  423. //! List of all materials found in the file
  424. std::vector<Material> m_vMaterials;
  425. //! List of all meshes found in the file
  426. std::vector<Mesh> m_vMeshes;
  427. //! Current line in the file
  428. unsigned int iLineNumber;
  429. //! First frame
  430. unsigned int iFirstFrame;
  431. //! Last frame
  432. unsigned int iLastFrame;
  433. //! Frame speed - frames per second
  434. unsigned int iFrameSpeed;
  435. //! Ticks per frame
  436. unsigned int iTicksPerFrame;
  437. //! true if the last character read was an end-line character
  438. bool bLastWasEndLine;
  439. };
  440. };};
  441. #endif // !! include guard