ASEParser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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()
  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. };
  168. // ---------------------------------------------------------------------------------
  169. /** \brief Class to parse ASE files
  170. */
  171. class Parser
  172. {
  173. private:
  174. Parser() {}
  175. public:
  176. //! Construct a parser from a given input file which is
  177. //! guaranted to be terminated with zero.
  178. Parser (const char* szFile);
  179. // -------------------------------------------------------------------
  180. //! Parses the file into the parsers internal representation
  181. void Parse();
  182. private:
  183. // -------------------------------------------------------------------
  184. //! Parse the *SCENE block in a file
  185. void ParseLV1SceneBlock();
  186. // -------------------------------------------------------------------
  187. //! Parse the *MATERIAL_LIST block in a file
  188. void ParseLV1MaterialListBlock();
  189. // -------------------------------------------------------------------
  190. //! Parse a *GEOMOBJECT block in a file
  191. //! \param mesh Mesh object to be filled
  192. void ParseLV1GeometryObjectBlock(Mesh& mesh);
  193. // -------------------------------------------------------------------
  194. //! Parse a *MATERIAL blocks in a material list
  195. //! \param mat Material structure to be filled
  196. void ParseLV2MaterialBlock(Material& mat);
  197. // -------------------------------------------------------------------
  198. //! Parse a *NODE_TM block in a file
  199. //! \param mesh Mesh object to be filled
  200. void ParseLV2NodeTransformBlock(Mesh& mesh);
  201. // -------------------------------------------------------------------
  202. //! Parse a *MESH block in a file
  203. //! \param mesh Mesh object to be filled
  204. void ParseLV2MeshBlock(Mesh& mesh);
  205. // -------------------------------------------------------------------
  206. //! Parse the *MAP_XXXXXX blocks in a material
  207. //! \param map Texture structure to be filled
  208. void ParseLV3MapBlock(Texture& map);
  209. // -------------------------------------------------------------------
  210. //! Parse a *MESH_VERTEX_LIST block in a file
  211. //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
  212. //! Otherwise zero. This is used to check the consistency of the file.
  213. //! A warning is sent to the logger if the validations fails.
  214. //! \param mesh Mesh object to be filled
  215. void ParseLV3MeshVertexListBlock(
  216. unsigned int iNumVertices,Mesh& mesh);
  217. // -------------------------------------------------------------------
  218. //! Parse a *MESH_FACE_LIST block in a file
  219. //! \param iNumFaces Value of *MESH_NUMFACES, if present.
  220. //! Otherwise zero. This is used to check the consistency of the file.
  221. //! A warning is sent to the logger if the validations fails.
  222. //! \param mesh Mesh object to be filled
  223. void ParseLV3MeshFaceListBlock(
  224. unsigned int iNumFaces,Mesh& mesh);
  225. // -------------------------------------------------------------------
  226. //! Parse a *MESH_TVERT_LIST block in a file
  227. //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
  228. //! Otherwise zero. This is used to check the consistency of the file.
  229. //! A warning is sent to the logger if the validations fails.
  230. //! \param mesh Mesh object to be filled
  231. //! \param iChannel Output UVW channel
  232. void ParseLV3MeshTListBlock(
  233. unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
  234. // -------------------------------------------------------------------
  235. //! Parse a *MESH_TFACELIST block in a file
  236. //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
  237. //! Otherwise zero. This is used to check the consistency of the file.
  238. //! A warning is sent to the logger if the validations fails.
  239. //! \param mesh Mesh object to be filled
  240. //! \param iChannel Output UVW channel
  241. void ParseLV3MeshTFaceListBlock(
  242. unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
  243. // -------------------------------------------------------------------
  244. //! Parse an additional mapping channel
  245. //! (specified via *MESH_MAPPINGCHANNEL)
  246. //! \param iChannel Channel index to be filled
  247. //! \param mesh Mesh object to be filled
  248. void ParseLV3MappingChannel(
  249. unsigned int iChannel, Mesh& mesh);
  250. // -------------------------------------------------------------------
  251. //! Parse a *MESH_CVERTLIST block in a file
  252. //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
  253. //! Otherwise zero. This is used to check the consistency of the file.
  254. //! A warning is sent to the logger if the validations fails.
  255. //! \param mesh Mesh object to be filled
  256. void ParseLV3MeshCListBlock(
  257. unsigned int iNumVertices, Mesh& mesh);
  258. // -------------------------------------------------------------------
  259. //! Parse a *MESH_CFACELIST block in a file
  260. //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
  261. //! Otherwise zero. This is used to check the consistency of the file.
  262. //! A warning is sent to the logger if the validations fails.
  263. //! \param mesh Mesh object to be filled
  264. void ParseLV3MeshCFaceListBlock(
  265. unsigned int iNumFaces, Mesh& mesh);
  266. // -------------------------------------------------------------------
  267. //! Parse a *MESH_NORMALS block in a file
  268. //! \param mesh Mesh object to be filled
  269. void ParseLV3MeshNormalListBlock(Mesh& mesh);
  270. // -------------------------------------------------------------------
  271. //! Parse a *MESH_WEIGHTSblock in a file
  272. //! \param mesh Mesh object to be filled
  273. void ParseLV3MeshWeightsBlock(Mesh& mesh);
  274. // -------------------------------------------------------------------
  275. //! Parse the bone list of a file
  276. //! \param mesh Mesh object to be filled
  277. //! \param iNumBones Number of bones in the mesh
  278. void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
  279. // -------------------------------------------------------------------
  280. //! Parse the bone vertices list of a file
  281. //! \param mesh Mesh object to be filled
  282. //! \param iNumVertices Number of vertices to be parsed
  283. void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
  284. // -------------------------------------------------------------------
  285. //! Parse a *MESH_FACE block in a file
  286. //! \param out receive the face data
  287. void ParseLV4MeshFace(ASE::Face& out);
  288. // -------------------------------------------------------------------
  289. //! Parse a *MESH_VERT block in a file
  290. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  291. //! \param apOut Output buffer (3 floats)
  292. //! \param rIndexOut Output index
  293. void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
  294. // -------------------------------------------------------------------
  295. //! Parse a *MESH_VERT block in a file
  296. //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
  297. //! \param apOut Output buffer (3 floats)
  298. void ParseLV4MeshFloatTriple(float* apOut);
  299. // -------------------------------------------------------------------
  300. //! Parse a *MESH_TFACE block in a file
  301. //! (also works for MESH_CFACE)
  302. //! \param apOut Output buffer (3 ints)
  303. //! \param rIndexOut Output index
  304. void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
  305. // -------------------------------------------------------------------
  306. //! Parse a *MESH_TFACE block in a file
  307. //! (also works for MESH_CFACE)
  308. //! \param apOut Output buffer (3 ints)
  309. void ParseLV4MeshLongTriple(unsigned int* apOut);
  310. // -------------------------------------------------------------------
  311. //! Parse a single float element
  312. //! \param fOut Output float
  313. void ParseLV4MeshFloat(float& fOut);
  314. // -------------------------------------------------------------------
  315. //! Parse a single int element
  316. //! \param iOut Output integer
  317. void ParseLV4MeshLong(unsigned int& iOut);
  318. // -------------------------------------------------------------------
  319. //! Skip the opening bracket at the beginning of a complex statement
  320. bool SkipOpeningBracket();
  321. // -------------------------------------------------------------------
  322. //! Skip everything to the next: '*' or '\0'
  323. bool SkipToNextToken();
  324. // -------------------------------------------------------------------
  325. //! Skip the current section until the token after the closing }.
  326. //! This function handles embedded subsections correctly
  327. bool SkipSection();
  328. // -------------------------------------------------------------------
  329. //! Output a warning to the logger
  330. //! \param szWarn Warn message
  331. void LogWarning(const char* szWarn);
  332. // -------------------------------------------------------------------
  333. //! Output an error to the logger
  334. //! \param szWarn Error message
  335. void LogError(const char* szWarn);
  336. // -------------------------------------------------------------------
  337. //! Parse a string, enclosed in double quotation marks
  338. //! \param out Output string
  339. //! \param szName Name of the enclosing element -> used in error
  340. //! messages.
  341. //! \return false if an error occured
  342. bool Parser::ParseString(std::string& out,const char* szName);
  343. public:
  344. //! Pointer to current data
  345. const char* m_szFile;
  346. //! background color to be passed to the viewer
  347. //! QNAN if none was found
  348. aiColor3D m_clrBackground;
  349. //! Base ambient color to be passed to all materials
  350. //! QNAN if none was found
  351. aiColor3D m_clrAmbient;
  352. //! List of all materials found in the file
  353. std::vector<Material> m_vMaterials;
  354. //! List of all meshes found in the file
  355. std::vector<Mesh> m_vMeshes;
  356. //! Current line in the file
  357. unsigned int iLineNumber;
  358. };
  359. };};
  360. #endif // !! include guard