ObjFileData.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #ifndef OBJ_FILEDATA_H_INC
  34. #define OBJ_FILEDATA_H_INC
  35. #include <vector>
  36. #include <map>
  37. #include "../include/assimp/types.h"
  38. #include "../include/assimp/mesh.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 Data structure for a simple obj-face, describes discredit,l.ation and materials
  50. struct Face
  51. {
  52. typedef std::vector<unsigned int> IndexArray;
  53. //! Primitive type
  54. aiPrimitiveType 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. aiPrimitiveType pt = aiPrimitiveType_POLYGON) :
  71. m_PrimitiveType( pt ),
  72. m_pVertices( pVertices ),
  73. m_pNormals( pNormals ),
  74. m_pTexturCoords( pTexCoords ),
  75. m_pMaterial( 0L )
  76. {
  77. // empty
  78. }
  79. //! \brief Destructor
  80. ~Face()
  81. {
  82. delete m_pVertices;
  83. m_pVertices = NULL;
  84. delete m_pNormals;
  85. m_pNormals = NULL;
  86. delete m_pTexturCoords;
  87. m_pTexturCoords = NULL;
  88. }
  89. };
  90. // ------------------------------------------------------------------------------------------------
  91. //! \struct Object
  92. //! \brief Stores all objects of an objfile object definition
  93. struct Object
  94. {
  95. enum ObjectType
  96. {
  97. ObjType,
  98. GroupType
  99. };
  100. //! Object name
  101. std::string m_strObjName;
  102. //! Transformation matrix, stored in OpenGL format
  103. aiMatrix4x4 m_Transformation;
  104. //! All sub-objects referenced by this object
  105. std::vector<Object*> m_SubObjects;
  106. /// Assigned meshes
  107. std::vector<unsigned int> m_Meshes;
  108. //! \brief Default constructor
  109. Object() :
  110. m_strObjName("")
  111. {
  112. // empty
  113. }
  114. //! \brief Destructor
  115. ~Object()
  116. {
  117. for (std::vector<Object*>::iterator it = m_SubObjects.begin();
  118. it != m_SubObjects.end(); ++it)
  119. {
  120. delete *it;
  121. }
  122. m_SubObjects.clear();
  123. }
  124. };
  125. // ------------------------------------------------------------------------------------------------
  126. //! \struct Material
  127. //! \brief Data structure to store all material specific data
  128. struct Material
  129. {
  130. //! Name of material description
  131. aiString MaterialName;
  132. //! Texture names
  133. aiString texture;
  134. aiString textureSpecular;
  135. aiString textureAmbient;
  136. aiString textureEmissive;
  137. aiString textureBump;
  138. aiString textureNormal;
  139. aiString textureSpecularity;
  140. aiString textureOpacity;
  141. aiString textureDisp;
  142. enum TextureType
  143. {
  144. TextureDiffuseType = 0,
  145. TextureSpecularType,
  146. TextureAmbientType,
  147. TextureEmissiveType,
  148. TextureBumpType,
  149. TextureNormalType,
  150. TextureSpecularityType,
  151. TextureOpacityType,
  152. TextureDispType,
  153. TextureTypeCount
  154. };
  155. bool clamp[TextureTypeCount];
  156. //! Ambient color
  157. aiColor3D ambient;
  158. //! Diffuse color
  159. aiColor3D diffuse;
  160. //! Specular color
  161. aiColor3D specular;
  162. //! Emissive color
  163. aiColor3D emissive;
  164. //! Alpha value
  165. float alpha;
  166. //! Shineness factor
  167. float shineness;
  168. //! Illumination model
  169. int illumination_model;
  170. //! Index of refraction
  171. float ior;
  172. //! Constructor
  173. Material()
  174. : diffuse (0.6f,0.6f,0.6f)
  175. , alpha (1.f)
  176. , shineness (0.0f)
  177. , illumination_model (1)
  178. , ior (1.f)
  179. {
  180. // empty
  181. for (size_t i = 0; i < TextureTypeCount; ++i)
  182. {
  183. clamp[i] = false;
  184. }
  185. }
  186. // Destructor
  187. ~Material()
  188. {
  189. // empty
  190. }
  191. };
  192. // ------------------------------------------------------------------------------------------------
  193. //! \struct Mesh
  194. //! \brief Data structure to store a mesh
  195. struct Mesh
  196. {
  197. static const unsigned int NoMaterial = ~0u;
  198. /// Array with pointer to all stored faces
  199. std::vector<Face*> m_Faces;
  200. /// Assigned material
  201. Material *m_pMaterial;
  202. /// Number of stored indices.
  203. unsigned int m_uiNumIndices;
  204. /// Number of UV
  205. unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
  206. /// Material index.
  207. unsigned int m_uiMaterialIndex;
  208. /// True, if normals are stored.
  209. bool m_hasNormals;
  210. /// Constructor
  211. Mesh() :
  212. m_pMaterial(NULL),
  213. m_uiNumIndices(0),
  214. m_uiMaterialIndex( NoMaterial ),
  215. m_hasNormals(false)
  216. {
  217. memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
  218. }
  219. /// Destructor
  220. ~Mesh()
  221. {
  222. for (std::vector<Face*>::iterator it = m_Faces.begin();
  223. it != m_Faces.end(); ++it)
  224. {
  225. delete *it;
  226. }
  227. }
  228. };
  229. // ------------------------------------------------------------------------------------------------
  230. //! \struct Model
  231. //! \brief Data structure to store all obj-specific model datas
  232. struct Model
  233. {
  234. typedef std::map<std::string, std::vector<unsigned int>* > GroupMap;
  235. typedef std::map<std::string, std::vector<unsigned int>* >::iterator GroupMapIt;
  236. typedef std::map<std::string, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt;
  237. //! Model name
  238. std::string m_ModelName;
  239. //! List ob assigned objects
  240. std::vector<Object*> m_Objects;
  241. //! Pointer to current object
  242. ObjFile::Object *m_pCurrent;
  243. //! Pointer to current material
  244. ObjFile::Material *m_pCurrentMaterial;
  245. //! Pointer to default material
  246. ObjFile::Material *m_pDefaultMaterial;
  247. //! Vector with all generated materials
  248. std::vector<std::string> m_MaterialLib;
  249. //! Vector with all generated group
  250. std::vector<std::string> m_GroupLib;
  251. //! Vector with all generated vertices
  252. std::vector<aiVector3D> m_Vertices;
  253. //! vector with all generated normals
  254. std::vector<aiVector3D> m_Normals;
  255. //! Group map
  256. GroupMap m_Groups;
  257. //! Group to face id assignment
  258. std::vector<unsigned int> *m_pGroupFaceIDs;
  259. //! Active group
  260. std::string m_strActiveGroup;
  261. //! Vector with generated texture coordinates
  262. std::vector<aiVector3D> m_TextureCoord;
  263. //! Current mesh instance
  264. Mesh *m_pCurrentMesh;
  265. //! Vector with stored meshes
  266. std::vector<Mesh*> m_Meshes;
  267. //! Material map
  268. std::map<std::string, Material*> m_MaterialMap;
  269. //! \brief The default class constructor
  270. Model() :
  271. m_ModelName(""),
  272. m_pCurrent(NULL),
  273. m_pCurrentMaterial(NULL),
  274. m_pDefaultMaterial(NULL),
  275. m_pGroupFaceIDs(NULL),
  276. m_strActiveGroup(""),
  277. m_pCurrentMesh(NULL)
  278. {
  279. // empty
  280. }
  281. //! \brief The class destructor
  282. ~Model()
  283. {
  284. // Clear all stored object instances
  285. for (std::vector<Object*>::iterator it = m_Objects.begin();
  286. it != m_Objects.end(); ++it) {
  287. delete *it;
  288. }
  289. m_Objects.clear();
  290. // Clear all stored mesh instances
  291. for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
  292. it != m_Meshes.end(); ++it) {
  293. delete *it;
  294. }
  295. m_Meshes.clear();
  296. for(GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) {
  297. delete it->second;
  298. }
  299. m_Groups.clear();
  300. for ( std::map<std::string, Material*>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it ) {
  301. delete it->second;
  302. }
  303. }
  304. };
  305. // ------------------------------------------------------------------------------------------------
  306. } // Namespace ObjFile
  307. } // Namespace Assimp
  308. #endif