ObjFileData.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 obj-file 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. TextureDiffuseType = 0,
  142. TextureSpecularType,
  143. TextureAmbientType,
  144. TextureEmissiveType,
  145. TextureBumpType,
  146. TextureNormalType,
  147. TextureReflectionSphereType,
  148. TextureReflectionCubeTopType,
  149. TextureReflectionCubeBottomType,
  150. TextureReflectionCubeFrontType,
  151. TextureReflectionCubeBackType,
  152. TextureReflectionCubeLeftType,
  153. TextureReflectionCubeRightType,
  154. TextureSpecularityType,
  155. TextureOpacityType,
  156. TextureDispType,
  157. TextureTypeCount
  158. };
  159. bool clamp[TextureTypeCount];
  160. //! Ambient color
  161. aiColor3D ambient;
  162. //! Diffuse color
  163. aiColor3D diffuse;
  164. //! Specular color
  165. aiColor3D specular;
  166. //! Emissive color
  167. aiColor3D emissive;
  168. //! Alpha value
  169. ai_real alpha;
  170. //! Shineness factor
  171. ai_real shineness;
  172. //! Illumination model
  173. int illumination_model;
  174. //! Index of refraction
  175. ai_real ior;
  176. //! Constructor
  177. Material()
  178. : diffuse ( ai_real( 0.6 ), ai_real( 0.6 ), ai_real( 0.6 ) )
  179. , alpha (ai_real( 1.0 ) )
  180. , shineness ( ai_real( 0.0) )
  181. , illumination_model (1)
  182. , ior ( ai_real( 1.0 ) )
  183. {
  184. // empty
  185. for (size_t i = 0; i < TextureTypeCount; ++i)
  186. {
  187. clamp[i] = false;
  188. }
  189. }
  190. // Destructor
  191. ~Material()
  192. {
  193. // empty
  194. }
  195. };
  196. // ------------------------------------------------------------------------------------------------
  197. //! \struct Mesh
  198. //! \brief Data structure to store a mesh
  199. struct Mesh {
  200. static const unsigned int NoMaterial = ~0u;
  201. /// The name for the mesh
  202. std::string m_name;
  203. /// Array with pointer to all stored faces
  204. std::vector<Face*> m_Faces;
  205. /// Assigned material
  206. Material *m_pMaterial;
  207. /// Number of stored indices.
  208. unsigned int m_uiNumIndices;
  209. /// Number of UV
  210. unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
  211. /// Material index.
  212. unsigned int m_uiMaterialIndex;
  213. /// True, if normals are stored.
  214. bool m_hasNormals;
  215. /// True, if vertex colors are stored.
  216. bool m_hasVertexColors;
  217. /// Constructor
  218. explicit Mesh( const std::string &name )
  219. : m_name( name )
  220. , m_pMaterial(NULL)
  221. , m_uiNumIndices(0)
  222. , m_uiMaterialIndex( NoMaterial )
  223. , m_hasNormals(false) {
  224. memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
  225. }
  226. /// Destructor
  227. ~Mesh()
  228. {
  229. for (std::vector<Face*>::iterator it = m_Faces.begin();
  230. it != m_Faces.end(); ++it)
  231. {
  232. delete *it;
  233. }
  234. }
  235. };
  236. // ------------------------------------------------------------------------------------------------
  237. //! \struct Model
  238. //! \brief Data structure to store all obj-specific model datas
  239. struct Model
  240. {
  241. typedef std::map<std::string, std::vector<unsigned int>* > GroupMap;
  242. typedef std::map<std::string, std::vector<unsigned int>* >::iterator GroupMapIt;
  243. typedef std::map<std::string, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt;
  244. //! Model name
  245. std::string m_ModelName;
  246. //! List ob assigned objects
  247. std::vector<Object*> m_Objects;
  248. //! Pointer to current object
  249. ObjFile::Object *m_pCurrent;
  250. //! Pointer to current material
  251. ObjFile::Material *m_pCurrentMaterial;
  252. //! Pointer to default material
  253. ObjFile::Material *m_pDefaultMaterial;
  254. //! Vector with all generated materials
  255. std::vector<std::string> m_MaterialLib;
  256. //! Vector with all generated vertices
  257. std::vector<aiVector3D> m_Vertices;
  258. //! vector with all generated normals
  259. std::vector<aiVector3D> m_Normals;
  260. //! vector with all vertex colors
  261. std::vector<aiVector3D> m_VertexColors;
  262. //! Group map
  263. GroupMap m_Groups;
  264. //! Group to face id assignment
  265. std::vector<unsigned int> *m_pGroupFaceIDs;
  266. //! Active group
  267. std::string m_strActiveGroup;
  268. //! Vector with generated texture coordinates
  269. std::vector<aiVector3D> m_TextureCoord;
  270. //! Current mesh instance
  271. Mesh *m_pCurrentMesh;
  272. //! Vector with stored meshes
  273. std::vector<Mesh*> m_Meshes;
  274. //! Material map
  275. std::map<std::string, Material*> m_MaterialMap;
  276. //! \brief The default class constructor
  277. Model() :
  278. m_ModelName(""),
  279. m_pCurrent(NULL),
  280. m_pCurrentMaterial(NULL),
  281. m_pDefaultMaterial(NULL),
  282. m_pGroupFaceIDs(NULL),
  283. m_strActiveGroup(""),
  284. m_pCurrentMesh(NULL)
  285. {
  286. // empty
  287. }
  288. //! \brief The class destructor
  289. ~Model()
  290. {
  291. // Clear all stored object instances
  292. for (std::vector<Object*>::iterator it = m_Objects.begin();
  293. it != m_Objects.end(); ++it) {
  294. delete *it;
  295. }
  296. m_Objects.clear();
  297. // Clear all stored mesh instances
  298. for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
  299. it != m_Meshes.end(); ++it) {
  300. delete *it;
  301. }
  302. m_Meshes.clear();
  303. for(GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) {
  304. delete it->second;
  305. }
  306. m_Groups.clear();
  307. for ( std::map<std::string, Material*>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it ) {
  308. delete it->second;
  309. }
  310. }
  311. };
  312. // ------------------------------------------------------------------------------------------------
  313. } // Namespace ObjFile
  314. } // Namespace Assimp
  315. #endif