ASEParser.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include <string>
  37. #include <vector>
  38. #include <list>
  39. #include <sstream>
  40. #include "../include/aiTypes.h"
  41. #include "../include/aiMesh.h"
  42. #include "../include/aiAnim.h"
  43. // for some helper routines like IsSpace()
  44. #include "PlyParser.h"
  45. // ASE is quite similar to 3ds. We can reuse some structures
  46. #include "3DSLoader.h"
  47. namespace Assimp
  48. {
  49. // http://wiki.beyondunreal.com/Legacy:ASE_File_Format
  50. namespace ASE
  51. {
  52. using namespace Dot3DS;
  53. // ---------------------------------------------------------------------------
  54. /** Helper structure representing an ASE material */
  55. struct Material : public Dot3DS::Material
  56. {
  57. //! Default constructor
  58. Material() : pcInstance(NULL), bNeed (false)
  59. {}
  60. //! Ambient texture channel
  61. Texture sTexAmbient;
  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. std::stringstream ss(mName);
  108. ss << "%%_UNNAMED_" << iCnt++ << "_%%";
  109. ss.flush();
  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 mesh */
  127. struct Mesh
  128. {
  129. //! Constructor. Creates a default name for the mesh
  130. Mesh() : bSkip(false)
  131. {
  132. static int iCnt = 0;
  133. std::stringstream ss(mName);
  134. ss << "%%_UNNAMED_" << iCnt++ << "_%%";
  135. ss.flush();
  136. // use 2 texture vertex components by default
  137. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  138. this->mNumUVComponents[c] = 2;
  139. // setup the default material index by default
  140. iMaterialIndex = Face::DEFAULT_MATINDEX;
  141. }
  142. //! Name of the mesh
  143. std::string mName;
  144. //! Name of the parent of the mesh
  145. //! "" if there is no parent ...
  146. std::string mParent;
  147. //! vertex positions
  148. std::vector<aiVector3D> mPositions;
  149. //! List of all faces loaded
  150. std::vector<ASE::Face> mFaces;
  151. //! List of all texture coordinate sets
  152. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  153. //! List of all vertex color sets.
  154. std::vector<aiColor4D> mVertexColors;
  155. //! List of normal vectors
  156. std::vector<aiVector3D> mNormals;
  157. //! List of all bone vertices
  158. std::vector<BoneVertex> mBoneVertices;
  159. //! List of all bones
  160. std::vector<Bone> mBones;
  161. //! Transformation matrix of the mesh
  162. aiMatrix4x4 mTransform;
  163. //! Material index of the mesh
  164. unsigned int iMaterialIndex;
  165. //! Number of vertex components for each UVW set
  166. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  167. //! used internally
  168. bool bSkip;
  169. };
  170. // ---------------------------------------------------------------------------------
  171. /** \brief Class to parse ASE files
  172. */
  173. class Parser
  174. {
  175. private:
  176. Parser() {}
  177. public:
  178. //! Construct a parser from a given input file which is
  179. //! guaranted to be terminated with zero.
  180. Parser (const char* szFile);
  181. // -------------------------------------------------------------------
  182. //! Parses the file into the parsers internal representation
  183. void Parse();
  184. private:
  185. // -------------------------------------------------------------------
  186. //! Parse the *SCENE block in a file
  187. void ParseLV1SceneBlock();
  188. // -------------------------------------------------------------------
  189. //! Parse the *MATERIAL_LIST block in a file
  190. void ParseLV1MaterialListBlock();
  191. // -------------------------------------------------------------------
  192. //! Parse a *GEOMOBJECT block in a file
  193. //! \param mesh Mesh object to be filled
  194. void ParseLV1GeometryObjectBlock(Mesh& mesh);
  195. // -------------------------------------------------------------------
  196. //! Parse a *MATERIAL blocks in a material list
  197. //! \param mat Material structure to be filled
  198. void ParseLV2MaterialBlock(Material& mat);
  199. // -------------------------------------------------------------------
  200. //! Parse a *NODE_TM block in a file
  201. //! \param mesh Mesh object to be filled
  202. void ParseLV2NodeTransformBlock(Mesh& mesh);
  203. // -------------------------------------------------------------------
  204. //! Parse a *MESH block in a file
  205. //! \param mesh Mesh object to be filled
  206. void ParseLV2MeshBlock(Mesh& mesh);
  207. // -------------------------------------------------------------------
  208. //! Parse the *MAP_XXXXXX blocks in a material
  209. //! \param map Texture structure to be filled
  210. void ParseLV3MapBlock(Texture& map);
  211. // -------------------------------------------------------------------
  212. //! Parse a *MESH_VERTEX_LIST block in a file
  213. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  214. //! Otherwise zero. This is used to check the consistency of the file.
  215. //! A warning is sent to the logger if the validations fails.
  216. //! \param mesh Mesh object to be filled
  217. void ParseLV3MeshVertexListBlock(
  218. unsigned int iNumVertices,Mesh& mesh);
  219. // -------------------------------------------------------------------
  220. //! Parse a *MESH_FACE_LIST block in a file
  221. //! \param iNumFaces Value of *MESH_NUMFACES, if present.
  222. //! Otherwise zero. This is used to check the consistency of the file.
  223. //! A warning is sent to the logger if the validations fails.
  224. //! \param mesh Mesh object to be filled
  225. void ParseLV3MeshFaceListBlock(
  226. unsigned int iNumFaces,Mesh& mesh);
  227. // -------------------------------------------------------------------
  228. //! Parse a *MESH_TVERT_LIST block in a file
  229. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  230. //! Otherwise zero. This is used to check the consistency of the file.
  231. //! A warning is sent to the logger if the validations fails.
  232. //! \param mesh Mesh object to be filled
  233. //! \param iChannel Output UVW channel
  234. void ParseLV3MeshTListBlock(
  235. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  236. // -------------------------------------------------------------------
  237. //! Parse a *MESH_TFACELIST block in a file
  238. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  239. //! Otherwise zero. This is used to check the consistency of the file.
  240. //! A warning is sent to the logger if the validations fails.
  241. //! \param mesh Mesh object to be filled
  242. //! \param iChannel Output UVW channel
  243. void ParseLV3MeshTFaceListBlock(
  244. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  245. // -------------------------------------------------------------------
  246. //! Parse an additional mapping channel
  247. //! (specified via *MESH_MAPPINGCHANNEL)
  248. //! \param iChannel Channel index to be filled
  249. //! \param mesh Mesh object to be filled
  250. void ParseLV3MappingChannel(
  251. unsigned int iChannel, Mesh& mesh);
  252. // -------------------------------------------------------------------
  253. //! Parse a *MESH_CVERTLIST block in a file
  254. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  255. //! Otherwise zero. This is used to check the consistency of the file.
  256. //! A warning is sent to the logger if the validations fails.
  257. //! \param mesh Mesh object to be filled
  258. void ParseLV3MeshCListBlock(
  259. unsigned int iNumVertices, Mesh& mesh);
  260. // -------------------------------------------------------------------
  261. //! Parse a *MESH_CFACELIST block in a file
  262. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  263. //! Otherwise zero. This is used to check the consistency of the file.
  264. //! A warning is sent to the logger if the validations fails.
  265. //! \param mesh Mesh object to be filled
  266. void ParseLV3MeshCFaceListBlock(
  267. unsigned int iNumFaces, Mesh& mesh);
  268. // -------------------------------------------------------------------
  269. //! Parse a *MESH_NORMALS block in a file
  270. //! \param mesh Mesh object to be filled
  271. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  272. // -------------------------------------------------------------------
  273. //! Parse a *MESH_WEIGHTSblock in a file
  274. //! \param mesh Mesh object to be filled
  275. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  276. // -------------------------------------------------------------------
  277. //! Parse the bone list of a file
  278. //! \param mesh Mesh object to be filled
  279. //! \param iNumBones Number of bones in the mesh
  280. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  281. // -------------------------------------------------------------------
  282. //! Parse the bone vertices list of a file
  283. //! \param mesh Mesh object to be filled
  284. //! \param iNumVertices Number of vertices to be parsed
  285. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  286. // -------------------------------------------------------------------
  287. //! Parse a *MESH_FACE block in a file
  288. //! \param out receive the face data
  289. void ParseLV4MeshFace(ASE::Face& out);
  290. // -------------------------------------------------------------------
  291. //! Parse a *MESH_VERT block in a file
  292. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  293. //! \param apOut Output buffer (3 floats)
  294. //! \param rIndexOut Output index
  295. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  296. // -------------------------------------------------------------------
  297. //! Parse a *MESH_VERT block in a file
  298. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  299. //! \param apOut Output buffer (3 floats)
  300. void ParseLV4MeshFloatTriple(float* apOut);
  301. // -------------------------------------------------------------------
  302. //! Parse a *MESH_TFACE block in a file
  303. //! (also works for MESH_CFACE)
  304. //! \param apOut Output buffer (3 ints)
  305. //! \param rIndexOut Output index
  306. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  307. // -------------------------------------------------------------------
  308. //! Parse a *MESH_TFACE block in a file
  309. //! (also works for MESH_CFACE)
  310. //! \param apOut Output buffer (3 ints)
  311. void ParseLV4MeshLongTriple(unsigned int* apOut);
  312. // -------------------------------------------------------------------
  313. //! Parse a single float element
  314. //! \param fOut Output float
  315. void ParseLV4MeshFloat(float& fOut);
  316. // -------------------------------------------------------------------
  317. //! Parse a single int element
  318. //! \param iOut Output integer
  319. void ParseLV4MeshLong(unsigned int& iOut);
  320. // -------------------------------------------------------------------
  321. //! Skip the opening bracket at the beginning of a complex statement
  322. bool SkipOpeningBracket();
  323. // -------------------------------------------------------------------
  324. //! Skip everything to the next: '*' or '\0'
  325. bool SkipToNextToken();
  326. // -------------------------------------------------------------------
  327. //! Skip the current section until the token after the closing }.
  328. //! This function handles embedded subsections correctly
  329. bool SkipSection();
  330. // -------------------------------------------------------------------
  331. //! Output a warning to the logger
  332. //! \param szWarn Warn message
  333. void LogWarning(const char* szWarn);
  334. // -------------------------------------------------------------------
  335. //! Output an error to the logger
  336. //! \param szWarn Error message
  337. void LogError(const char* szWarn);
  338. // -------------------------------------------------------------------
  339. //! Parse a string, enclosed in double quotation marks
  340. //! \param out Output string
  341. //! \param szName Name of the enclosing element -> used in error
  342. //! messages.
  343. //! \return false if an error occured
  344. bool Parser::ParseString(std::string& out,const char* szName);
  345. public:
  346. //! Pointer to current data
  347. const char* m_szFile;
  348. //! background color to be passed to the viewer
  349. //! QNAN if none was found
  350. aiColor3D m_clrBackground;
  351. //! Base ambient color to be passed to all materials
  352. //! QNAN if none was found
  353. aiColor3D m_clrAmbient;
  354. //! List of all materials found in the file
  355. std::vector<Material> m_vMaterials;
  356. //! List of all meshes found in the file
  357. std::vector<Mesh> m_vMeshes;
  358. //! Current line in the file
  359. unsigned int iLineNumber;
  360. };
  361. };};
  362. #endif // !! include guard