ObjFileData.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /// Constructor
  217. explicit Mesh( const std::string &name )
  218. : m_name( name )
  219. , m_pMaterial(NULL)
  220. , m_uiNumIndices(0)
  221. , m_uiMaterialIndex( NoMaterial )
  222. , m_hasNormals(false) {
  223. memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
  224. }
  225. /// Destructor
  226. ~Mesh()
  227. {
  228. for (std::vector<Face*>::iterator it = m_Faces.begin();
  229. it != m_Faces.end(); ++it)
  230. {
  231. delete *it;
  232. }
  233. }
  234. };
  235. // ------------------------------------------------------------------------------------------------
  236. //! \struct Model
  237. //! \brief Data structure to store all obj-specific model datas
  238. struct Model
  239. {
  240. typedef std::map<std::string, std::vector<unsigned int>* > GroupMap;
  241. typedef std::map<std::string, std::vector<unsigned int>* >::iterator GroupMapIt;
  242. typedef std::map<std::string, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt;
  243. //! Model name
  244. std::string m_ModelName;
  245. //! List ob assigned objects
  246. std::vector<Object*> m_Objects;
  247. //! Pointer to current object
  248. ObjFile::Object *m_pCurrent;
  249. //! Pointer to current material
  250. ObjFile::Material *m_pCurrentMaterial;
  251. //! Pointer to default material
  252. ObjFile::Material *m_pDefaultMaterial;
  253. //! Vector with all generated materials
  254. std::vector<std::string> m_MaterialLib;
  255. //! Vector with all generated group
  256. std::vector<std::string> m_GroupLib;
  257. //! Vector with all generated vertices
  258. std::vector<aiVector3D> m_Vertices;
  259. //! vector with all generated normals
  260. std::vector<aiVector3D> m_Normals;
  261. //! Group map
  262. GroupMap m_Groups;
  263. //! Group to face id assignment
  264. std::vector<unsigned int> *m_pGroupFaceIDs;
  265. //! Active group
  266. std::string m_strActiveGroup;
  267. //! Vector with generated texture coordinates
  268. std::vector<aiVector3D> m_TextureCoord;
  269. //! Current mesh instance
  270. Mesh *m_pCurrentMesh;
  271. //! Vector with stored meshes
  272. std::vector<Mesh*> m_Meshes;
  273. //! Material map
  274. std::map<std::string, Material*> m_MaterialMap;
  275. //! \brief The default class constructor
  276. Model() :
  277. m_ModelName(""),
  278. m_pCurrent(NULL),
  279. m_pCurrentMaterial(NULL),
  280. m_pDefaultMaterial(NULL),
  281. m_pGroupFaceIDs(NULL),
  282. m_strActiveGroup(""),
  283. m_pCurrentMesh(NULL)
  284. {
  285. // empty
  286. }
  287. //! \brief The class destructor
  288. ~Model()
  289. {
  290. // Clear all stored object instances
  291. for (std::vector<Object*>::iterator it = m_Objects.begin();
  292. it != m_Objects.end(); ++it) {
  293. delete *it;
  294. }
  295. m_Objects.clear();
  296. // Clear all stored mesh instances
  297. for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
  298. it != m_Meshes.end(); ++it) {
  299. delete *it;
  300. }
  301. m_Meshes.clear();
  302. for(GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) {
  303. delete it->second;
  304. }
  305. m_Groups.clear();
  306. for ( std::map<std::string, Material*>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it ) {
  307. delete it->second;
  308. }
  309. }
  310. };
  311. // ------------------------------------------------------------------------------------------------
  312. } // Namespace ObjFile
  313. } // Namespace Assimp
  314. #endif