ObjFileData.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifndef OBJ_FILEDATA_H_INC
  34. #define OBJ_FILEDATA_H_INC
  35. #include <vector>
  36. #include <map>
  37. #include "../include/aiTypes.h"
  38. #include "../include/aiMesh.h"
  39. namespace Assimp
  40. {
  41. namespace ObjFile
  42. {
  43. // ------------------------------------------------------------------------------------------------
  44. struct Object;
  45. struct Face;
  46. struct Material;
  47. // ------------------------------------------------------------------------------------------------
  48. //! \struct Face
  49. //! \brief Datastructure for a simple obj-face, descripes discredisation and materials
  50. struct Face
  51. {
  52. typedef std::vector<unsigned int> IndexArray;
  53. //! Primitive type
  54. int m_PrimitiveType;
  55. //! Vertex indices
  56. IndexArray *m_pVertices;
  57. //! Normal indices
  58. IndexArray *m_pNormals;
  59. //! Texture coordinates indices
  60. IndexArray *m_pTexturCoords;
  61. //! Pointer to assigned material
  62. Material *m_pMaterial;
  63. //! \brief Default constructor
  64. //! \param pVertices Pointer to assigned vertex indexbuffer
  65. //! \param pNormals Pointer to assigned normals indexbuffer
  66. //! \param pTexCoords Pointer to assigned texture indexbuffer
  67. Face( std::vector<unsigned int> *pVertices,
  68. std::vector<unsigned int> *pNormals,
  69. std::vector<unsigned int> *pTexCoords) :
  70. m_PrimitiveType( 2 ),
  71. m_pVertices( pVertices ),
  72. m_pNormals( pNormals ),
  73. m_pTexturCoords( pTexCoords ),
  74. m_pMaterial( 0L )
  75. {
  76. // empty
  77. }
  78. //! \brief Destructor
  79. ~Face()
  80. {
  81. delete m_pVertices;
  82. m_pVertices = NULL;
  83. delete m_pNormals;
  84. m_pNormals = NULL;
  85. delete m_pTexturCoords;
  86. m_pTexturCoords = NULL;
  87. }
  88. };
  89. // ------------------------------------------------------------------------------------------------
  90. //! \struct Object
  91. //! \brief Stores all objects of an objfile object definition
  92. struct Object
  93. {
  94. //! Obejct name
  95. std::string m_strObjName;
  96. //! Assigend face instances
  97. std::vector<Face*> m_Faces;
  98. //! Transformation matrix, stored in OpenGL format
  99. aiMatrix4x4 m_Transformation;
  100. //! All subobjects references by this object
  101. std::vector<Object*> m_SubObjects;
  102. //! \brief Default constructor
  103. Object() :
  104. m_strObjName("")
  105. {
  106. // empty
  107. }
  108. //! \brief Destructor
  109. ~Object()
  110. {
  111. for (std::vector<Object*>::iterator it = m_SubObjects.begin();
  112. it != m_SubObjects.end(); ++it)
  113. {
  114. delete *it;
  115. }
  116. m_SubObjects.clear();
  117. }
  118. };
  119. // ------------------------------------------------------------------------------------------------
  120. //! \struct Material
  121. //! \brief Data structure to store all material specific data
  122. struct Material
  123. {
  124. //! Name of material description
  125. aiString MaterialName;
  126. //! Texture names
  127. aiString texture;
  128. aiString textureSpecular;
  129. aiString textureAmbient;
  130. aiString textureBump;
  131. aiString textureSpecularity;
  132. aiString textureOpacity;
  133. //! Ambient color
  134. aiColor3D ambient;
  135. //! Diffuse color
  136. aiColor3D diffuse;
  137. //! Speculao color
  138. aiColor3D specular;
  139. //! Alpha value
  140. float alpha;
  141. //! Shineness factor
  142. float shineness;
  143. //! Illumination model
  144. int illumination_model;
  145. //! Index of refraction
  146. float ior;
  147. //! Constructor
  148. Material()
  149. : diffuse (0.6f,0.6f,0.6f)
  150. , alpha (1.f)
  151. , shineness (0.0f)
  152. , illumination_model (1)
  153. , ior (1.f)
  154. {
  155. // empty
  156. }
  157. // Destructor
  158. ~Material()
  159. {
  160. // empty
  161. }
  162. };
  163. // ------------------------------------------------------------------------------------------------
  164. //! \struct Mesh
  165. //! \brief Data structure to store a mesh
  166. struct Mesh
  167. {
  168. /// Array with pointer to all stored faces
  169. std::vector<Face*> m_Faces;
  170. /// Assigned material
  171. Material *m_pMaterial;
  172. /// Number of stored indices.
  173. unsigned int m_uiNumIndices;
  174. /// Number of UV
  175. unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
  176. /// Material index.
  177. unsigned int m_uiMaterialIndex;
  178. /// True, if normals are stored.
  179. bool m_hasNormals;
  180. /// Constructor
  181. Mesh() :
  182. m_pMaterial(NULL),
  183. m_uiNumIndices(0),
  184. m_uiMaterialIndex(0),
  185. m_hasNormals(false)
  186. {
  187. memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
  188. }
  189. /// Destructor
  190. ~Mesh()
  191. {
  192. for (std::vector<Face*>::iterator it = m_Faces.begin();
  193. it != m_Faces.end(); ++it)
  194. {
  195. delete *it;
  196. }
  197. }
  198. };
  199. // ------------------------------------------------------------------------------------------------
  200. //! \struct Model
  201. //! \brief Data structure to store all obj-specific model datas
  202. struct Model
  203. {
  204. typedef std::map<std::string*, std::vector<unsigned int>* > GroupMap;
  205. typedef std::map<std::string*, std::vector<unsigned int>* >::iterator GroupMapIt;
  206. typedef std::map<std::string*, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt;
  207. //! Model name
  208. std::string m_ModelName;
  209. //! List ob assigned objects
  210. std::vector<Object*> m_Objects;
  211. //! Pointer to current object
  212. ObjFile::Object *m_pCurrent;
  213. //! Pointer to current material
  214. ObjFile::Material *m_pCurrentMaterial;
  215. //! Pointer to default material
  216. ObjFile::Material *m_pDefaultMaterial;
  217. //! Vector with all generated materials
  218. std::vector<std::string> m_MaterialLib;
  219. //! Vector with all generated group
  220. std::vector<std::string> m_GroupLib;
  221. //! Vector with all generated vertices
  222. std::vector<aiVector3D> m_Vertices;
  223. //! vector with all generated normals
  224. std::vector<aiVector3D> m_Normals;
  225. //! Groupmap
  226. GroupMap m_Groups;
  227. //! Group to face id assignment
  228. std::vector<unsigned int> *m_pGroupFaceIDs;
  229. //! Active group
  230. std::string m_strActiveGroup;
  231. //! Vector with generated texture coordinates
  232. std::vector<aiVector2D> m_TextureCoord;
  233. //! Current mesh instance
  234. Mesh *m_pCurrentMesh;
  235. //! Vector with stored meshes
  236. std::vector<Mesh*> m_Meshes;
  237. //! Material map
  238. std::map<std::string, Material*> m_MaterialMap;
  239. //! \brief Default constructor
  240. Model() :
  241. m_ModelName(""),
  242. m_pCurrent(NULL),
  243. m_pCurrentMaterial(NULL),
  244. m_pDefaultMaterial(NULL),
  245. m_strActiveGroup(""),
  246. m_pCurrentMesh(NULL)
  247. {
  248. // empty
  249. }
  250. //! \brief Destructor
  251. ~Model()
  252. {
  253. // Clear all stored object instances
  254. for (std::vector<Object*>::iterator it = m_Objects.begin();
  255. it != m_Objects.end(); ++it)
  256. {
  257. delete *it;
  258. }
  259. m_Objects.clear();
  260. // Clear all stored mesh instances
  261. for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
  262. it != m_Meshes.end(); ++it)
  263. {
  264. delete *it;
  265. }
  266. m_Meshes.clear();
  267. for(GroupMapIt it = m_Groups.begin();
  268. it != m_Groups.end(); ++it)
  269. {
  270. delete it->second;
  271. }
  272. m_Groups.clear();
  273. }
  274. };
  275. // ------------------------------------------------------------------------------------------------
  276. } // Namespace ObjFile
  277. } // Namespace Assimp
  278. #endif