2
0

ASEParser.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. namespace ASE {
  51. using namespace D3DS;
  52. // ---------------------------------------------------------------------------
  53. /** Helper structure representing an ASE material */
  54. struct Material : public D3DS::Material
  55. {
  56. //! Default constructor
  57. Material() : pcInstance(NULL), bNeed (false)
  58. {}
  59. //! Contains all sub materials of this material
  60. std::vector<Material> avSubMaterials;
  61. //! MaterialHelper object
  62. MaterialHelper* pcInstance;
  63. //! Can we remove this material?
  64. bool bNeed;
  65. };
  66. // ---------------------------------------------------------------------------
  67. /** Helper structure to represent an ASE file face */
  68. struct Face : public FaceWithSmoothingGroup
  69. {
  70. //! Default constructor. Initializes everything with 0
  71. Face()
  72. {
  73. mColorIndices[0] = mColorIndices[1] = mColorIndices[2] = 0;
  74. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  75. {
  76. amUVIndices[i][0] = amUVIndices[i][1] = amUVIndices[i][2] = 0;
  77. }
  78. iMaterial = DEFAULT_MATINDEX;
  79. iFace = 0;
  80. }
  81. //! special value to indicate that no material index has
  82. //! been assigned to a face. The default material index
  83. //! will replace this value later.
  84. static const unsigned int DEFAULT_MATINDEX = 0xFFFFFFFF;
  85. //! Indices into each list of texture coordinates
  86. unsigned int amUVIndices[AI_MAX_NUMBER_OF_TEXTURECOORDS][3];
  87. //! Index into the list of vertex colors
  88. unsigned int mColorIndices[3];
  89. //! (Sub)Material index to be assigned to this face
  90. unsigned int iMaterial;
  91. //! Index of the face. It is not specified whether it is
  92. //! a requirement of the file format that all faces are
  93. //! written in sequential order, so we have to expect this case
  94. unsigned int iFace;
  95. };
  96. // ---------------------------------------------------------------------------
  97. /** Helper structure to represent an ASE file bone */
  98. struct Bone
  99. {
  100. //! Constructor
  101. Bone()
  102. {
  103. static int iCnt = 0;
  104. char szTemp[128];
  105. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  106. mName = szTemp;
  107. }
  108. //! Name of the bone
  109. std::string mName;
  110. };
  111. // ---------------------------------------------------------------------------
  112. /** Helper structure to represent an ASE file bone vertex */
  113. struct BoneVertex
  114. {
  115. //! Bone and corresponding vertex weight.
  116. //! -1 for unrequired bones ....
  117. std::vector<std::pair<int,float> > mBoneWeights;
  118. //! Position of the bone vertex.
  119. //! MUST be identical to the vertex position
  120. //aiVector3D mPosition;
  121. };
  122. // ---------------------------------------------------------------------------
  123. /** Helper structure to represent an ASE file animation */
  124. struct Animation
  125. {
  126. enum Type
  127. {
  128. TRACK = 0x0,
  129. BEZIER = 0x1,
  130. TCB = 0x2
  131. } mRotationType, mScalingType, mPositionType;
  132. Animation()
  133. : mRotationType (TRACK)
  134. , mScalingType (TRACK)
  135. , mPositionType (TRACK)
  136. {}
  137. /** ONLY ONE OF THESE SETS IS USED
  138. *
  139. * Bezier and TCB channels are converted to a normal
  140. * trac later.
  141. */
  142. //! List of track rotation keyframes
  143. std::vector< aiQuatKey > akeyRotations;
  144. //! List of track position keyframes
  145. std::vector< aiVectorKey > akeyPositions;
  146. //! List of track scaling keyframes
  147. std::vector< aiVectorKey > akeyScaling;
  148. };
  149. // ---------------------------------------------------------------------------
  150. /** Helper structure to represent the inheritance information of an ASE node */
  151. struct InheritanceInfo
  152. {
  153. //! Default constructor
  154. InheritanceInfo()
  155. {
  156. // set the inheritance flag for all axes by default to true
  157. for (unsigned int i = 0; i < 3;++i)
  158. abInheritPosition[i] = abInheritRotation[i] = abInheritScaling[i] = true;
  159. }
  160. //! Inherit the parent's position?, axis order is x,y,z
  161. bool abInheritPosition[3];
  162. //! Inherit the parent's rotation?, axis order is x,y,z
  163. bool abInheritRotation[3];
  164. //! Inherit the parent's scaling?, axis order is x,y,z
  165. bool abInheritScaling[3];
  166. };
  167. // ---------------------------------------------------------------------------
  168. /** Represents an ASE file node. Base class for mesh, light and cameras */
  169. struct BaseNode
  170. {
  171. enum Type {Light, Camera, Mesh, Dummy} mType;
  172. //! Constructor. Creates a default name for the node
  173. BaseNode(Type _mType)
  174. : mType (_mType)
  175. , mProcessed (false)
  176. {
  177. // generate a default name for the node
  178. static int iCnt = 0;
  179. char szTemp[128]; // should be sufficiently large
  180. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  181. mName = szTemp;
  182. }
  183. //! Name of the mesh
  184. std::string mName;
  185. //! Name of the parent of the node
  186. //! "" if there is no parent ...
  187. std::string mParent;
  188. //! Transformation matrix of the node
  189. aiMatrix4x4 mTransform;
  190. //! Target position (target lights and cameras)
  191. aiVector3D mTargetPosition;
  192. //! Specifies which axes transformations a node inherits
  193. //! from its parent ...
  194. InheritanceInfo inherit;
  195. //! Animation channels for the node
  196. Animation mAnim;
  197. //! Needed for lights and cameras: target animation channel
  198. //! Should contain position keys only.
  199. Animation mTargetAnim;
  200. bool mProcessed;
  201. };
  202. // ---------------------------------------------------------------------------
  203. /** Helper structure to represent an ASE file mesh */
  204. struct Mesh : public MeshWithSmoothingGroups<ASE::Face>, public BaseNode
  205. {
  206. //! Constructor.
  207. Mesh()
  208. : BaseNode (BaseNode::Mesh)
  209. , bSkip (false)
  210. {
  211. // use 2 texture vertex components by default
  212. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  213. this->mNumUVComponents[c] = 2;
  214. // setup the default material index by default
  215. iMaterialIndex = Face::DEFAULT_MATINDEX;
  216. }
  217. //! List of all texture coordinate sets
  218. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  219. //! List of all vertex color sets.
  220. std::vector<aiColor4D> mVertexColors;
  221. //! List of all bone vertices
  222. std::vector<BoneVertex> mBoneVertices;
  223. //! List of all bones
  224. std::vector<Bone> mBones;
  225. //! Material index of the mesh
  226. unsigned int iMaterialIndex;
  227. //! Number of vertex components for each UVW set
  228. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  229. //! used internally
  230. bool bSkip;
  231. };
  232. // ---------------------------------------------------------------------------
  233. /** Helper structure to represent an ASE light source */
  234. struct Light : public BaseNode
  235. {
  236. enum LightType
  237. {
  238. OMNI,
  239. TARGET,
  240. FREE,
  241. DIRECTIONAL
  242. };
  243. //! Constructor.
  244. Light()
  245. : BaseNode (BaseNode::Light)
  246. , mLightType (OMNI)
  247. , mColor (1.f,1.f,1.f)
  248. , mIntensity (1.f) // light is white by default
  249. , mAngle (45.f)
  250. , mFalloff (0.f)
  251. {
  252. }
  253. LightType mLightType;
  254. aiColor3D mColor;
  255. float mIntensity;
  256. float mAngle; // in degrees
  257. float mFalloff;
  258. };
  259. // ---------------------------------------------------------------------------
  260. /** Helper structure to represent an ASE camera */
  261. struct Camera : public BaseNode
  262. {
  263. enum CameraType
  264. {
  265. FREE,
  266. TARGET
  267. };
  268. //! Constructor
  269. Camera()
  270. : BaseNode (BaseNode::Camera)
  271. , mFOV (0.75f) // in radians
  272. , mNear (0.1f)
  273. , mFar (1000.f) // could be zero
  274. , mCameraType (FREE)
  275. {
  276. }
  277. float mFOV, mNear, mFar;
  278. CameraType mCameraType;
  279. };
  280. // ---------------------------------------------------------------------------
  281. /** Helper structure to represent an ASE helper object (dummy) */
  282. struct Dummy : public BaseNode
  283. {
  284. //! Constructor
  285. Dummy()
  286. : BaseNode (BaseNode::Dummy)
  287. {
  288. }
  289. };
  290. // ---------------------------------------------------------------------------------
  291. /** \brief Class to parse ASE files
  292. */
  293. class Parser
  294. {
  295. private:
  296. Parser() {}
  297. public:
  298. //! Construct a parser from a given input file which is
  299. //! guaranted to be terminated with zero.
  300. Parser (const char* szFile);
  301. // -------------------------------------------------------------------
  302. //! Parses the file into the parsers internal representation
  303. void Parse();
  304. private:
  305. // -------------------------------------------------------------------
  306. //! Parse the *SCENE block in a file
  307. void ParseLV1SceneBlock();
  308. // -------------------------------------------------------------------
  309. //! Parse the *MATERIAL_LIST block in a file
  310. void ParseLV1MaterialListBlock();
  311. // -------------------------------------------------------------------
  312. //! Parse a *<xxx>OBJECT block in a file
  313. //! \param mesh Node to be filled
  314. void ParseLV1ObjectBlock(BaseNode& mesh);
  315. // -------------------------------------------------------------------
  316. //! Parse a *MATERIAL blocks in a material list
  317. //! \param mat Material structure to be filled
  318. void ParseLV2MaterialBlock(Material& mat);
  319. // -------------------------------------------------------------------
  320. //! Parse a *NODE_TM block in a file
  321. //! \param mesh Node (!) object to be filled
  322. void ParseLV2NodeTransformBlock(BaseNode& mesh);
  323. // -------------------------------------------------------------------
  324. //! Parse a *TM_ANIMATION block in a file
  325. //! \param mesh Mesh object to be filled
  326. void ParseLV2AnimationBlock(BaseNode& mesh);
  327. void ParseLV3PosAnimationBlock(ASE::Animation& anim);
  328. void ParseLV3ScaleAnimationBlock(ASE::Animation& anim);
  329. void ParseLV3RotAnimationBlock(ASE::Animation& anim);
  330. // -------------------------------------------------------------------
  331. //! Parse a *MESH block in a file
  332. //! \param mesh Mesh object to be filled
  333. void ParseLV2MeshBlock(Mesh& mesh);
  334. // -------------------------------------------------------------------
  335. //! Parse a *LIGHT_SETTINGS block in a file
  336. //! \param light Light object to be filled
  337. void ParseLV2LightSettingsBlock(Light& light);
  338. // -------------------------------------------------------------------
  339. //! Parse a *CAMERA_SETTINGS block in a file
  340. //! \param cam Camera object to be filled
  341. void ParseLV2CameraSettingsBlock(Camera& cam);
  342. // -------------------------------------------------------------------
  343. //! Parse the *MAP_XXXXXX blocks in a material
  344. //! \param map Texture structure to be filled
  345. void ParseLV3MapBlock(Texture& map);
  346. // -------------------------------------------------------------------
  347. //! Parse a *MESH_VERTEX_LIST block in a file
  348. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  349. //! Otherwise zero. This is used to check the consistency of the file.
  350. //! A warning is sent to the logger if the validations fails.
  351. //! \param mesh Mesh object to be filled
  352. void ParseLV3MeshVertexListBlock(
  353. unsigned int iNumVertices,Mesh& mesh);
  354. // -------------------------------------------------------------------
  355. //! Parse a *MESH_FACE_LIST block in a file
  356. //! \param iNumFaces Value of *MESH_NUMFACES, if present.
  357. //! Otherwise zero. This is used to check the consistency of the file.
  358. //! A warning is sent to the logger if the validations fails.
  359. //! \param mesh Mesh object to be filled
  360. void ParseLV3MeshFaceListBlock(
  361. unsigned int iNumFaces,Mesh& mesh);
  362. // -------------------------------------------------------------------
  363. //! Parse a *MESH_TVERT_LIST block in a file
  364. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  365. //! Otherwise zero. This is used to check the consistency of the file.
  366. //! A warning is sent to the logger if the validations fails.
  367. //! \param mesh Mesh object to be filled
  368. //! \param iChannel Output UVW channel
  369. void ParseLV3MeshTListBlock(
  370. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  371. // -------------------------------------------------------------------
  372. //! Parse a *MESH_TFACELIST block in a file
  373. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  374. //! Otherwise zero. This is used to check the consistency of the file.
  375. //! A warning is sent to the logger if the validations fails.
  376. //! \param mesh Mesh object to be filled
  377. //! \param iChannel Output UVW channel
  378. void ParseLV3MeshTFaceListBlock(
  379. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  380. // -------------------------------------------------------------------
  381. //! Parse an additional mapping channel
  382. //! (specified via *MESH_MAPPINGCHANNEL)
  383. //! \param iChannel Channel index to be filled
  384. //! \param mesh Mesh object to be filled
  385. void ParseLV3MappingChannel(
  386. unsigned int iChannel, Mesh& mesh);
  387. // -------------------------------------------------------------------
  388. //! Parse a *MESH_CVERTLIST block in a file
  389. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  390. //! Otherwise zero. This is used to check the consistency of the file.
  391. //! A warning is sent to the logger if the validations fails.
  392. //! \param mesh Mesh object to be filled
  393. void ParseLV3MeshCListBlock(
  394. unsigned int iNumVertices, Mesh& mesh);
  395. // -------------------------------------------------------------------
  396. //! Parse a *MESH_CFACELIST block in a file
  397. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  398. //! Otherwise zero. This is used to check the consistency of the file.
  399. //! A warning is sent to the logger if the validations fails.
  400. //! \param mesh Mesh object to be filled
  401. void ParseLV3MeshCFaceListBlock(
  402. unsigned int iNumFaces, Mesh& mesh);
  403. // -------------------------------------------------------------------
  404. //! Parse a *MESH_NORMALS block in a file
  405. //! \param mesh Mesh object to be filled
  406. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  407. // -------------------------------------------------------------------
  408. //! Parse a *MESH_WEIGHTSblock in a file
  409. //! \param mesh Mesh object to be filled
  410. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  411. // -------------------------------------------------------------------
  412. //! Parse the bone list of a file
  413. //! \param mesh Mesh object to be filled
  414. //! \param iNumBones Number of bones in the mesh
  415. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  416. // -------------------------------------------------------------------
  417. //! Parse the bone vertices list of a file
  418. //! \param mesh Mesh object to be filled
  419. //! \param iNumVertices Number of vertices to be parsed
  420. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  421. // -------------------------------------------------------------------
  422. //! Parse a *MESH_FACE block in a file
  423. //! \param out receive the face data
  424. void ParseLV4MeshFace(ASE::Face& out);
  425. // -------------------------------------------------------------------
  426. //! Parse a *MESH_VERT block in a file
  427. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  428. //! \param apOut Output buffer (3 floats)
  429. //! \param rIndexOut Output index
  430. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  431. // -------------------------------------------------------------------
  432. //! Parse a *MESH_VERT block in a file
  433. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  434. //! \param apOut Output buffer (3 floats)
  435. void ParseLV4MeshFloatTriple(float* apOut);
  436. // -------------------------------------------------------------------
  437. //! Parse a *MESH_TFACE block in a file
  438. //! (also works for MESH_CFACE)
  439. //! \param apOut Output buffer (3 ints)
  440. //! \param rIndexOut Output index
  441. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  442. // -------------------------------------------------------------------
  443. //! Parse a *MESH_TFACE block in a file
  444. //! (also works for MESH_CFACE)
  445. //! \param apOut Output buffer (3 ints)
  446. void ParseLV4MeshLongTriple(unsigned int* apOut);
  447. // -------------------------------------------------------------------
  448. //! Parse a single float element
  449. //! \param fOut Output float
  450. void ParseLV4MeshFloat(float& fOut);
  451. // -------------------------------------------------------------------
  452. //! Parse a single int element
  453. //! \param iOut Output integer
  454. void ParseLV4MeshLong(unsigned int& iOut);
  455. // -------------------------------------------------------------------
  456. //! Skip everything to the next: '*' or '\0'
  457. bool SkipToNextToken();
  458. // -------------------------------------------------------------------
  459. //! Skip the current section until the token after the closing }.
  460. //! This function handles embedded subsections correctly
  461. bool SkipSection();
  462. // -------------------------------------------------------------------
  463. //! Output a warning to the logger
  464. //! \param szWarn Warn message
  465. void LogWarning(const char* szWarn);
  466. // -------------------------------------------------------------------
  467. //! Output a message to the logger
  468. //! \param szWarn Message
  469. void LogInfo(const char* szWarn);
  470. // -------------------------------------------------------------------
  471. //! Output an error to the logger
  472. //! \param szWarn Error message
  473. void LogError(const char* szWarn);
  474. // -------------------------------------------------------------------
  475. //! Parse a string, enclosed in double quotation marks
  476. //! \param out Output string
  477. //! \param szName Name of the enclosing element -> used in error
  478. //! messages.
  479. //! \return false if an error occured
  480. bool ParseString(std::string& out,const char* szName);
  481. public:
  482. //! Pointer to current data
  483. const char* m_szFile;
  484. //! background color to be passed to the viewer
  485. //! QNAN if none was found
  486. aiColor3D m_clrBackground;
  487. //! Base ambient color to be passed to all materials
  488. //! QNAN if none was found
  489. aiColor3D m_clrAmbient;
  490. //! List of all materials found in the file
  491. std::vector<Material> m_vMaterials;
  492. //! List of all meshes found in the file
  493. std::vector<Mesh> m_vMeshes;
  494. //! List of all dummies found in the file
  495. std::vector<Dummy> m_vDummies;
  496. //! List of all lights found in the file
  497. std::vector<Light> m_vLights;
  498. //! List of all cameras found in the file
  499. std::vector<Camera> m_vCameras;
  500. //! Current line in the file
  501. unsigned int iLineNumber;
  502. //! First frame
  503. unsigned int iFirstFrame;
  504. //! Last frame
  505. unsigned int iLastFrame;
  506. //! Frame speed - frames per second
  507. unsigned int iFrameSpeed;
  508. //! Ticks per frame
  509. unsigned int iTicksPerFrame;
  510. //! true if the last character read was an end-line character
  511. bool bLastWasEndLine;
  512. };
  513. } // Namespace ASE
  514. } // Namespace ASSIMP
  515. #endif // !! include guard