ASEParser.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. /** Represents an ASE file node. Base class for mesh, light and cameras */
  154. struct BaseNode
  155. {
  156. enum Type {Light, Camera, Mesh, Dummy} mType;
  157. //! Constructor. Creates a default name for the node
  158. BaseNode(Type _mType)
  159. : mType (_mType)
  160. , mProcessed (false)
  161. {
  162. // generate a default name for the node
  163. static int iCnt = 0;
  164. char szTemp[128]; // should be sufficiently large
  165. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  166. mName = szTemp;
  167. }
  168. //! Name of the mesh
  169. std::string mName;
  170. //! Name of the parent of the node
  171. //! "" if there is no parent ...
  172. std::string mParent;
  173. //! Transformation matrix of the node
  174. aiMatrix4x4 mTransform;
  175. //! Specifies which axes transformations a node inherits
  176. //! from its parent ...
  177. InheritanceInfo inherit;
  178. bool mProcessed;
  179. };
  180. // ---------------------------------------------------------------------------
  181. /** Helper structure to represent an ASE file mesh */
  182. struct Mesh : public MeshWithSmoothingGroups<ASE::Face>, public BaseNode
  183. {
  184. //! Constructor.
  185. Mesh()
  186. : BaseNode (BaseNode::Mesh)
  187. , bSkip (false)
  188. {
  189. // use 2 texture vertex components by default
  190. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  191. this->mNumUVComponents[c] = 2;
  192. // setup the default material index by default
  193. iMaterialIndex = Face::DEFAULT_MATINDEX;
  194. }
  195. //! List of all texture coordinate sets
  196. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  197. //! List of all vertex color sets.
  198. std::vector<aiColor4D> mVertexColors;
  199. //! List of all bone vertices
  200. std::vector<BoneVertex> mBoneVertices;
  201. //! List of all bones
  202. std::vector<Bone> mBones;
  203. //! Animation channels for the node
  204. Animation mAnim;
  205. //! Material index of the mesh
  206. unsigned int iMaterialIndex;
  207. //! Number of vertex components for each UVW set
  208. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  209. //! used internally
  210. bool bSkip;
  211. };
  212. // ---------------------------------------------------------------------------
  213. /** Helper structure to represent an ASE light source */
  214. struct Light : public BaseNode
  215. {
  216. enum LightType
  217. {
  218. OMNI
  219. };
  220. //! Constructor.
  221. Light()
  222. : BaseNode (BaseNode::Light)
  223. , mLightType (OMNI)
  224. , mColor (1.f,1.f,1.f)
  225. , mIntensity (1.f) // light is white by default
  226. {
  227. }
  228. LightType mLightType;
  229. aiColor3D mColor;
  230. float mIntensity;
  231. };
  232. // ---------------------------------------------------------------------------
  233. /** Helper structure to represent an ASE camera */
  234. struct Camera : public BaseNode
  235. {
  236. //! Constructor
  237. Camera()
  238. : BaseNode (BaseNode::Camera)
  239. , mFOV (0.75f) // in radians
  240. , mNear (0.1f)
  241. , mFar (1000.f) // could be zero
  242. {
  243. }
  244. float mFOV, mNear, mFar;
  245. };
  246. // ---------------------------------------------------------------------------------
  247. /** \brief Class to parse ASE files
  248. */
  249. class Parser
  250. {
  251. private:
  252. Parser() {}
  253. public:
  254. //! Construct a parser from a given input file which is
  255. //! guaranted to be terminated with zero.
  256. Parser (const char* szFile);
  257. // -------------------------------------------------------------------
  258. //! Parses the file into the parsers internal representation
  259. void Parse();
  260. private:
  261. // -------------------------------------------------------------------
  262. //! Parse the *SCENE block in a file
  263. void ParseLV1SceneBlock();
  264. // -------------------------------------------------------------------
  265. //! Parse the *MATERIAL_LIST block in a file
  266. void ParseLV1MaterialListBlock();
  267. // -------------------------------------------------------------------
  268. //! Parse a *GEOMOBJECT block in a file
  269. //! \param mesh Mesh object to be filled
  270. void ParseLV1GeometryObjectBlock(Mesh& mesh);
  271. // -------------------------------------------------------------------
  272. //! Parse a *LIGHTOBJECT block in a file
  273. //! \param light Light object to be filled
  274. void ParseLV1LightObjectBlock(Light& mesh);
  275. // -------------------------------------------------------------------
  276. //! Parse a *CAMERAOBJECT block in a file
  277. //! \param cam Camera object to be filled
  278. void ParseLV1CameraObjectBlock(Camera& cam);
  279. // -------------------------------------------------------------------
  280. //! Parse the shared parts of the *GEOMOBJECT, *LIGHTOBJECT and
  281. //! *CAMERAOBJECT chunks.
  282. //! \param mesh ..
  283. //! \return true = token parsed, get next please
  284. bool ParseSharedNodeInfo(ASE::BaseNode& mesh);
  285. // -------------------------------------------------------------------
  286. //! Parse a *MATERIAL blocks in a material list
  287. //! \param mat Material structure to be filled
  288. void ParseLV2MaterialBlock(Material& mat);
  289. // -------------------------------------------------------------------
  290. //! Parse a *NODE_TM block in a file
  291. //! \param mesh Node (!) object to be filled
  292. void ParseLV2NodeTransformBlock(BaseNode& mesh);
  293. // -------------------------------------------------------------------
  294. //! Parse a *TM_ANIMATION block in a file
  295. //! \param mesh Mesh object to be filled
  296. void ParseLV2AnimationBlock(Mesh& mesh);
  297. void ParseLV3PosAnimationBlock(Mesh& mesh);
  298. void ParseLV3RotAnimationBlock(Mesh& mesh);
  299. // -------------------------------------------------------------------
  300. //! Parse a *MESH block in a file
  301. //! \param mesh Mesh object to be filled
  302. void ParseLV2MeshBlock(Mesh& mesh);
  303. // -------------------------------------------------------------------
  304. //! Parse a *LIGHT_SETTINGS block in a file
  305. //! \param light Light object to be filled
  306. void ParseLV2LightSettingsBlock(Light& light);
  307. // -------------------------------------------------------------------
  308. //! Parse a *CAMERA_SETTINGS block in a file
  309. //! \param cam Camera object to be filled
  310. void ParseLV2CameraSettingsBlock(Camera& cam);
  311. // -------------------------------------------------------------------
  312. //! Parse the *MAP_XXXXXX blocks in a material
  313. //! \param map Texture structure to be filled
  314. void ParseLV3MapBlock(Texture& map);
  315. // -------------------------------------------------------------------
  316. //! Parse a *MESH_VERTEX_LIST block in a file
  317. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  318. //! Otherwise zero. This is used to check the consistency of the file.
  319. //! A warning is sent to the logger if the validations fails.
  320. //! \param mesh Mesh object to be filled
  321. void ParseLV3MeshVertexListBlock(
  322. unsigned int iNumVertices,Mesh& mesh);
  323. // -------------------------------------------------------------------
  324. //! Parse a *MESH_FACE_LIST block in a file
  325. //! \param iNumFaces Value of *MESH_NUMFACES, if present.
  326. //! Otherwise zero. This is used to check the consistency of the file.
  327. //! A warning is sent to the logger if the validations fails.
  328. //! \param mesh Mesh object to be filled
  329. void ParseLV3MeshFaceListBlock(
  330. unsigned int iNumFaces,Mesh& mesh);
  331. // -------------------------------------------------------------------
  332. //! Parse a *MESH_TVERT_LIST block in a file
  333. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  334. //! Otherwise zero. This is used to check the consistency of the file.
  335. //! A warning is sent to the logger if the validations fails.
  336. //! \param mesh Mesh object to be filled
  337. //! \param iChannel Output UVW channel
  338. void ParseLV3MeshTListBlock(
  339. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  340. // -------------------------------------------------------------------
  341. //! Parse a *MESH_TFACELIST block in a file
  342. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  343. //! Otherwise zero. This is used to check the consistency of the file.
  344. //! A warning is sent to the logger if the validations fails.
  345. //! \param mesh Mesh object to be filled
  346. //! \param iChannel Output UVW channel
  347. void ParseLV3MeshTFaceListBlock(
  348. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  349. // -------------------------------------------------------------------
  350. //! Parse an additional mapping channel
  351. //! (specified via *MESH_MAPPINGCHANNEL)
  352. //! \param iChannel Channel index to be filled
  353. //! \param mesh Mesh object to be filled
  354. void ParseLV3MappingChannel(
  355. unsigned int iChannel, Mesh& mesh);
  356. // -------------------------------------------------------------------
  357. //! Parse a *MESH_CVERTLIST block in a file
  358. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  359. //! Otherwise zero. This is used to check the consistency of the file.
  360. //! A warning is sent to the logger if the validations fails.
  361. //! \param mesh Mesh object to be filled
  362. void ParseLV3MeshCListBlock(
  363. unsigned int iNumVertices, Mesh& mesh);
  364. // -------------------------------------------------------------------
  365. //! Parse a *MESH_CFACELIST block in a file
  366. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  367. //! Otherwise zero. This is used to check the consistency of the file.
  368. //! A warning is sent to the logger if the validations fails.
  369. //! \param mesh Mesh object to be filled
  370. void ParseLV3MeshCFaceListBlock(
  371. unsigned int iNumFaces, Mesh& mesh);
  372. // -------------------------------------------------------------------
  373. //! Parse a *MESH_NORMALS block in a file
  374. //! \param mesh Mesh object to be filled
  375. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  376. // -------------------------------------------------------------------
  377. //! Parse a *MESH_WEIGHTSblock in a file
  378. //! \param mesh Mesh object to be filled
  379. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  380. // -------------------------------------------------------------------
  381. //! Parse the bone list of a file
  382. //! \param mesh Mesh object to be filled
  383. //! \param iNumBones Number of bones in the mesh
  384. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  385. // -------------------------------------------------------------------
  386. //! Parse the bone vertices list of a file
  387. //! \param mesh Mesh object to be filled
  388. //! \param iNumVertices Number of vertices to be parsed
  389. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  390. // -------------------------------------------------------------------
  391. //! Parse a *MESH_FACE block in a file
  392. //! \param out receive the face data
  393. void ParseLV4MeshFace(ASE::Face& out);
  394. // -------------------------------------------------------------------
  395. //! Parse a *MESH_VERT block in a file
  396. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  397. //! \param apOut Output buffer (3 floats)
  398. //! \param rIndexOut Output index
  399. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  400. // -------------------------------------------------------------------
  401. //! Parse a *MESH_VERT block in a file
  402. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  403. //! \param apOut Output buffer (3 floats)
  404. void ParseLV4MeshFloatTriple(float* apOut);
  405. // -------------------------------------------------------------------
  406. //! Parse a *MESH_TFACE block in a file
  407. //! (also works for MESH_CFACE)
  408. //! \param apOut Output buffer (3 ints)
  409. //! \param rIndexOut Output index
  410. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  411. // -------------------------------------------------------------------
  412. //! Parse a *MESH_TFACE block in a file
  413. //! (also works for MESH_CFACE)
  414. //! \param apOut Output buffer (3 ints)
  415. void ParseLV4MeshLongTriple(unsigned int* apOut);
  416. // -------------------------------------------------------------------
  417. //! Parse a single float element
  418. //! \param fOut Output float
  419. void ParseLV4MeshFloat(float& fOut);
  420. // -------------------------------------------------------------------
  421. //! Parse a single int element
  422. //! \param iOut Output integer
  423. void ParseLV4MeshLong(unsigned int& iOut);
  424. // -------------------------------------------------------------------
  425. //! Skip everything to the next: '*' or '\0'
  426. bool SkipToNextToken();
  427. // -------------------------------------------------------------------
  428. //! Skip the current section until the token after the closing }.
  429. //! This function handles embedded subsections correctly
  430. bool SkipSection();
  431. // -------------------------------------------------------------------
  432. //! Output a warning to the logger
  433. //! \param szWarn Warn message
  434. void LogWarning(const char* szWarn);
  435. // -------------------------------------------------------------------
  436. //! Output a message to the logger
  437. //! \param szWarn Message
  438. void LogInfo(const char* szWarn);
  439. // -------------------------------------------------------------------
  440. //! Output an error to the logger
  441. //! \param szWarn Error message
  442. void LogError(const char* szWarn);
  443. // -------------------------------------------------------------------
  444. //! Parse a string, enclosed in double quotation marks
  445. //! \param out Output string
  446. //! \param szName Name of the enclosing element -> used in error
  447. //! messages.
  448. //! \return false if an error occured
  449. bool ParseString(std::string& out,const char* szName);
  450. public:
  451. //! Pointer to current data
  452. const char* m_szFile;
  453. //! background color to be passed to the viewer
  454. //! QNAN if none was found
  455. aiColor3D m_clrBackground;
  456. //! Base ambient color to be passed to all materials
  457. //! QNAN if none was found
  458. aiColor3D m_clrAmbient;
  459. //! List of all materials found in the file
  460. std::vector<Material> m_vMaterials;
  461. //! List of all meshes found in the file
  462. std::vector<Mesh> m_vMeshes;
  463. //! List of all lights found in the file
  464. std::vector<Light> m_vLights;
  465. //! List of all cameras found in the file
  466. std::vector<Camera> m_vCameras;
  467. //! Current line in the file
  468. unsigned int iLineNumber;
  469. //! First frame
  470. unsigned int iFirstFrame;
  471. //! Last frame
  472. unsigned int iLastFrame;
  473. //! Frame speed - frames per second
  474. unsigned int iFrameSpeed;
  475. //! Ticks per frame
  476. unsigned int iTicksPerFrame;
  477. //! true if the last character read was an end-line character
  478. bool bLastWasEndLine;
  479. };
  480. } // Namespace ASE
  481. } // Namespace ASSIMP
  482. #endif // !! include guard