Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

670 рядки
20 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @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/assimp/types.h"
  42. #include "../include/assimp/mesh.h"
  43. #include "../include/assimp/anim.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. //! aiMaterial object
  62. aiMaterial* 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. // Generate a default name for the bone
  105. char szTemp[128];
  106. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  107. mName = szTemp;
  108. }
  109. //! Construction from an existing name
  110. Bone( const std::string& name)
  111. : mName (name)
  112. {}
  113. //! Name of the bone
  114. std::string mName;
  115. };
  116. // ---------------------------------------------------------------------------
  117. /** Helper structure to represent an ASE file bone vertex */
  118. struct BoneVertex
  119. {
  120. //! Bone and corresponding vertex weight.
  121. //! -1 for unrequired bones ....
  122. std::vector<std::pair<int,float> > mBoneWeights;
  123. //! Position of the bone vertex.
  124. //! MUST be identical to the vertex position
  125. //aiVector3D mPosition;
  126. };
  127. // ---------------------------------------------------------------------------
  128. /** Helper structure to represent an ASE file animation */
  129. struct Animation
  130. {
  131. enum Type
  132. {
  133. TRACK = 0x0,
  134. BEZIER = 0x1,
  135. TCB = 0x2
  136. } mRotationType, mScalingType, mPositionType;
  137. Animation()
  138. : mRotationType (TRACK)
  139. , mScalingType (TRACK)
  140. , mPositionType (TRACK)
  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. // Set mTargetPosition to qnan
  183. const float qnan = get_qnan();
  184. mTargetPosition.x = qnan;
  185. }
  186. //! Name of the mesh
  187. std::string mName;
  188. //! Name of the parent of the node
  189. //! "" if there is no parent ...
  190. std::string mParent;
  191. //! Transformation matrix of the node
  192. aiMatrix4x4 mTransform;
  193. //! Target position (target lights and cameras)
  194. aiVector3D mTargetPosition;
  195. //! Specifies which axes transformations a node inherits
  196. //! from its parent ...
  197. InheritanceInfo inherit;
  198. //! Animation channels for the node
  199. Animation mAnim;
  200. //! Needed for lights and cameras: target animation channel
  201. //! Should contain position keys only.
  202. Animation mTargetAnim;
  203. bool mProcessed;
  204. };
  205. // ---------------------------------------------------------------------------
  206. /** Helper structure to represent an ASE file mesh */
  207. struct Mesh : public MeshWithSmoothingGroups<ASE::Face>, public BaseNode
  208. {
  209. //! Constructor.
  210. Mesh()
  211. : BaseNode (BaseNode::Mesh)
  212. , bSkip (false)
  213. {
  214. // use 2 texture vertex components by default
  215. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  216. this->mNumUVComponents[c] = 2;
  217. // setup the default material index by default
  218. iMaterialIndex = Face::DEFAULT_MATINDEX;
  219. }
  220. //! List of all texture coordinate sets
  221. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  222. //! List of all vertex color sets.
  223. std::vector<aiColor4D> mVertexColors;
  224. //! List of all bone vertices
  225. std::vector<BoneVertex> mBoneVertices;
  226. //! List of all bones
  227. std::vector<Bone> mBones;
  228. //! Material index of the mesh
  229. unsigned int iMaterialIndex;
  230. //! Number of vertex components for each UVW set
  231. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  232. //! used internally
  233. bool bSkip;
  234. };
  235. // ---------------------------------------------------------------------------
  236. /** Helper structure to represent an ASE light source */
  237. struct Light : public BaseNode
  238. {
  239. enum LightType
  240. {
  241. OMNI,
  242. TARGET,
  243. FREE,
  244. DIRECTIONAL
  245. };
  246. //! Constructor.
  247. Light()
  248. : BaseNode (BaseNode::Light)
  249. , mLightType (OMNI)
  250. , mColor (1.f,1.f,1.f)
  251. , mIntensity (1.f) // light is white by default
  252. , mAngle (45.f)
  253. , mFalloff (0.f)
  254. {
  255. }
  256. LightType mLightType;
  257. aiColor3D mColor;
  258. float mIntensity;
  259. float mAngle; // in degrees
  260. float mFalloff;
  261. };
  262. // ---------------------------------------------------------------------------
  263. /** Helper structure to represent an ASE camera */
  264. struct Camera : public BaseNode
  265. {
  266. enum CameraType
  267. {
  268. FREE,
  269. TARGET
  270. };
  271. //! Constructor
  272. Camera()
  273. : BaseNode (BaseNode::Camera)
  274. , mFOV (0.75f) // in radians
  275. , mNear (0.1f)
  276. , mFar (1000.f) // could be zero
  277. , mCameraType (FREE)
  278. {
  279. }
  280. float mFOV, mNear, mFar;
  281. CameraType mCameraType;
  282. };
  283. // ---------------------------------------------------------------------------
  284. /** Helper structure to represent an ASE helper object (dummy) */
  285. struct Dummy : public BaseNode
  286. {
  287. //! Constructor
  288. Dummy()
  289. : BaseNode (BaseNode::Dummy)
  290. {
  291. }
  292. };
  293. // Parameters to Parser::Parse()
  294. #define AI_ASE_NEW_FILE_FORMAT 200
  295. #define AI_ASE_OLD_FILE_FORMAT 110
  296. // Internally we're a little bit more tolerant
  297. #define AI_ASE_IS_NEW_FILE_FORMAT() (iFileFormat >= 200)
  298. #define AI_ASE_IS_OLD_FILE_FORMAT() (iFileFormat < 200)
  299. // -------------------------------------------------------------------------------
  300. /** \brief Class to parse ASE files
  301. */
  302. class Parser
  303. {
  304. private:
  305. Parser() {}
  306. public:
  307. // -------------------------------------------------------------------
  308. //! Construct a parser from a given input file which is
  309. //! guaranted to be terminated with zero.
  310. //! @param szFile Input file
  311. //! @param fileFormatDefault Assumed file format version. If the
  312. //! file format is specified in the file the new value replaces
  313. //! the default value.
  314. Parser (const char* szFile, unsigned int fileFormatDefault);
  315. // -------------------------------------------------------------------
  316. //! Parses the file into the parsers internal representation
  317. void Parse();
  318. private:
  319. // -------------------------------------------------------------------
  320. //! Parse the *SCENE block in a file
  321. void ParseLV1SceneBlock();
  322. // -------------------------------------------------------------------
  323. //! Parse the *MESH_SOFTSKINVERTS block in a file
  324. void ParseLV1SoftSkinBlock();
  325. // -------------------------------------------------------------------
  326. //! Parse the *MATERIAL_LIST block in a file
  327. void ParseLV1MaterialListBlock();
  328. // -------------------------------------------------------------------
  329. //! Parse a *<xxx>OBJECT block in a file
  330. //! \param mesh Node to be filled
  331. void ParseLV1ObjectBlock(BaseNode& mesh);
  332. // -------------------------------------------------------------------
  333. //! Parse a *MATERIAL blocks in a material list
  334. //! \param mat Material structure to be filled
  335. void ParseLV2MaterialBlock(Material& mat);
  336. // -------------------------------------------------------------------
  337. //! Parse a *NODE_TM block in a file
  338. //! \param mesh Node (!) object to be filled
  339. void ParseLV2NodeTransformBlock(BaseNode& mesh);
  340. // -------------------------------------------------------------------
  341. //! Parse a *TM_ANIMATION block in a file
  342. //! \param mesh Mesh object to be filled
  343. void ParseLV2AnimationBlock(BaseNode& mesh);
  344. void ParseLV3PosAnimationBlock(ASE::Animation& anim);
  345. void ParseLV3ScaleAnimationBlock(ASE::Animation& anim);
  346. void ParseLV3RotAnimationBlock(ASE::Animation& anim);
  347. // -------------------------------------------------------------------
  348. //! Parse a *MESH block in a file
  349. //! \param mesh Mesh object to be filled
  350. void ParseLV2MeshBlock(Mesh& mesh);
  351. // -------------------------------------------------------------------
  352. //! Parse a *LIGHT_SETTINGS block in a file
  353. //! \param light Light object to be filled
  354. void ParseLV2LightSettingsBlock(Light& light);
  355. // -------------------------------------------------------------------
  356. //! Parse a *CAMERA_SETTINGS block in a file
  357. //! \param cam Camera object to be filled
  358. void ParseLV2CameraSettingsBlock(Camera& cam);
  359. // -------------------------------------------------------------------
  360. //! Parse the *MAP_XXXXXX blocks in a material
  361. //! \param map Texture structure to be filled
  362. void ParseLV3MapBlock(Texture& map);
  363. // -------------------------------------------------------------------
  364. //! Parse a *MESH_VERTEX_LIST block in a file
  365. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  366. //! Otherwise zero. This is used to check the consistency of the file.
  367. //! A warning is sent to the logger if the validations fails.
  368. //! \param mesh Mesh object to be filled
  369. void ParseLV3MeshVertexListBlock(
  370. unsigned int iNumVertices,Mesh& mesh);
  371. // -------------------------------------------------------------------
  372. //! Parse a *MESH_FACE_LIST block in a file
  373. //! \param iNumFaces Value of *MESH_NUMFACES, 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. void ParseLV3MeshFaceListBlock(
  378. unsigned int iNumFaces,Mesh& mesh);
  379. // -------------------------------------------------------------------
  380. //! Parse a *MESH_TVERT_LIST block in a file
  381. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  382. //! Otherwise zero. This is used to check the consistency of the file.
  383. //! A warning is sent to the logger if the validations fails.
  384. //! \param mesh Mesh object to be filled
  385. //! \param iChannel Output UVW channel
  386. void ParseLV3MeshTListBlock(
  387. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  388. // -------------------------------------------------------------------
  389. //! Parse a *MESH_TFACELIST block in a file
  390. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  391. //! Otherwise zero. This is used to check the consistency of the file.
  392. //! A warning is sent to the logger if the validations fails.
  393. //! \param mesh Mesh object to be filled
  394. //! \param iChannel Output UVW channel
  395. void ParseLV3MeshTFaceListBlock(
  396. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  397. // -------------------------------------------------------------------
  398. //! Parse an additional mapping channel
  399. //! (specified via *MESH_MAPPINGCHANNEL)
  400. //! \param iChannel Channel index to be filled
  401. //! \param mesh Mesh object to be filled
  402. void ParseLV3MappingChannel(
  403. unsigned int iChannel, Mesh& mesh);
  404. // -------------------------------------------------------------------
  405. //! Parse a *MESH_CVERTLIST block in a file
  406. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  407. //! Otherwise zero. This is used to check the consistency of the file.
  408. //! A warning is sent to the logger if the validations fails.
  409. //! \param mesh Mesh object to be filled
  410. void ParseLV3MeshCListBlock(
  411. unsigned int iNumVertices, Mesh& mesh);
  412. // -------------------------------------------------------------------
  413. //! Parse a *MESH_CFACELIST block in a file
  414. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  415. //! Otherwise zero. This is used to check the consistency of the file.
  416. //! A warning is sent to the logger if the validations fails.
  417. //! \param mesh Mesh object to be filled
  418. void ParseLV3MeshCFaceListBlock(
  419. unsigned int iNumFaces, Mesh& mesh);
  420. // -------------------------------------------------------------------
  421. //! Parse a *MESH_NORMALS block in a file
  422. //! \param mesh Mesh object to be filled
  423. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  424. // -------------------------------------------------------------------
  425. //! Parse a *MESH_WEIGHTSblock in a file
  426. //! \param mesh Mesh object to be filled
  427. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  428. // -------------------------------------------------------------------
  429. //! Parse the bone list of a file
  430. //! \param mesh Mesh object to be filled
  431. //! \param iNumBones Number of bones in the mesh
  432. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  433. // -------------------------------------------------------------------
  434. //! Parse the bone vertices list of a file
  435. //! \param mesh Mesh object to be filled
  436. //! \param iNumVertices Number of vertices to be parsed
  437. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  438. // -------------------------------------------------------------------
  439. //! Parse a *MESH_FACE block in a file
  440. //! \param out receive the face data
  441. void ParseLV4MeshFace(ASE::Face& out);
  442. // -------------------------------------------------------------------
  443. //! Parse a *MESH_VERT block in a file
  444. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  445. //! \param apOut Output buffer (3 floats)
  446. //! \param rIndexOut Output index
  447. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  448. // -------------------------------------------------------------------
  449. //! Parse a *MESH_VERT block in a file
  450. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  451. //! \param apOut Output buffer (3 floats)
  452. void ParseLV4MeshFloatTriple(float* apOut);
  453. // -------------------------------------------------------------------
  454. //! Parse a *MESH_TFACE block in a file
  455. //! (also works for MESH_CFACE)
  456. //! \param apOut Output buffer (3 ints)
  457. //! \param rIndexOut Output index
  458. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  459. // -------------------------------------------------------------------
  460. //! Parse a *MESH_TFACE block in a file
  461. //! (also works for MESH_CFACE)
  462. //! \param apOut Output buffer (3 ints)
  463. void ParseLV4MeshLongTriple(unsigned int* apOut);
  464. // -------------------------------------------------------------------
  465. //! Parse a single float element
  466. //! \param fOut Output float
  467. void ParseLV4MeshFloat(float& fOut);
  468. // -------------------------------------------------------------------
  469. //! Parse a single int element
  470. //! \param iOut Output integer
  471. void ParseLV4MeshLong(unsigned int& iOut);
  472. // -------------------------------------------------------------------
  473. //! Skip everything to the next: '*' or '\0'
  474. bool SkipToNextToken();
  475. // -------------------------------------------------------------------
  476. //! Skip the current section until the token after the closing }.
  477. //! This function handles embedded subsections correctly
  478. bool SkipSection();
  479. // -------------------------------------------------------------------
  480. //! Output a warning to the logger
  481. //! \param szWarn Warn message
  482. void LogWarning(const char* szWarn);
  483. // -------------------------------------------------------------------
  484. //! Output a message to the logger
  485. //! \param szWarn Message
  486. void LogInfo(const char* szWarn);
  487. // -------------------------------------------------------------------
  488. //! Output an error to the logger
  489. //! \param szWarn Error message
  490. void LogError(const char* szWarn);
  491. // -------------------------------------------------------------------
  492. //! Parse a string, enclosed in double quotation marks
  493. //! \param out Output string
  494. //! \param szName Name of the enclosing element -> used in error
  495. //! messages.
  496. //! \return false if an error occured
  497. bool ParseString(std::string& out,const char* szName);
  498. public:
  499. //! Pointer to current data
  500. const char* filePtr;
  501. //! background color to be passed to the viewer
  502. //! QNAN if none was found
  503. aiColor3D m_clrBackground;
  504. //! Base ambient color to be passed to all materials
  505. //! QNAN if none was found
  506. aiColor3D m_clrAmbient;
  507. //! List of all materials found in the file
  508. std::vector<Material> m_vMaterials;
  509. //! List of all meshes found in the file
  510. std::vector<Mesh> m_vMeshes;
  511. //! List of all dummies found in the file
  512. std::vector<Dummy> m_vDummies;
  513. //! List of all lights found in the file
  514. std::vector<Light> m_vLights;
  515. //! List of all cameras found in the file
  516. std::vector<Camera> m_vCameras;
  517. //! Current line in the file
  518. unsigned int iLineNumber;
  519. //! First frame
  520. unsigned int iFirstFrame;
  521. //! Last frame
  522. unsigned int iLastFrame;
  523. //! Frame speed - frames per second
  524. unsigned int iFrameSpeed;
  525. //! Ticks per frame
  526. unsigned int iTicksPerFrame;
  527. //! true if the last character read was an end-line character
  528. bool bLastWasEndLine;
  529. //! File format version
  530. unsigned int iFileFormat;
  531. };
  532. } // Namespace ASE
  533. } // Namespace ASSIMP
  534. #endif // !! include guard