ObjFileData.h 9.7 KB

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