ObjFileData.h 10 KB

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