ObjFileData.h 10 KB

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