ObjFileData.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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() = default;
  94. //! \brief Destructor
  95. ~Object() {
  96. for ( std::vector<Object*>::iterator it = m_SubObjects.begin(); it != m_SubObjects.end(); ++it) {
  97. delete *it;
  98. }
  99. }
  100. };
  101. // ------------------------------------------------------------------------------------------------
  102. //! \struct Material
  103. //! \brief Data structure to store all material specific data
  104. // ------------------------------------------------------------------------------------------------
  105. struct Material {
  106. //! Name of material description
  107. aiString MaterialName;
  108. //! Texture names
  109. aiString texture;
  110. aiString textureSpecular;
  111. aiString textureAmbient;
  112. aiString textureEmissive;
  113. aiString textureBump;
  114. aiString textureNormal;
  115. aiString textureReflection[6];
  116. aiString textureSpecularity;
  117. aiString textureOpacity;
  118. aiString textureDisp;
  119. enum TextureType {
  120. TextureDiffuseType = 0,
  121. TextureSpecularType,
  122. TextureAmbientType,
  123. TextureEmissiveType,
  124. TextureBumpType,
  125. TextureNormalType,
  126. TextureReflectionSphereType,
  127. TextureReflectionCubeTopType,
  128. TextureReflectionCubeBottomType,
  129. TextureReflectionCubeFrontType,
  130. TextureReflectionCubeBackType,
  131. TextureReflectionCubeLeftType,
  132. TextureReflectionCubeRightType,
  133. TextureSpecularityType,
  134. TextureOpacityType,
  135. TextureDispType,
  136. TextureTypeCount
  137. };
  138. bool clamp[TextureTypeCount];
  139. //! Ambient color
  140. aiColor3D ambient;
  141. //! Diffuse color
  142. aiColor3D diffuse;
  143. //! Specular color
  144. aiColor3D specular;
  145. //! Emissive color
  146. aiColor3D emissive;
  147. //! Alpha value
  148. ai_real alpha;
  149. //! Shineness factor
  150. ai_real shineness;
  151. //! Illumination model
  152. int illumination_model;
  153. //! Index of refraction
  154. ai_real ior;
  155. //! Transparency color
  156. aiColor3D transparent;
  157. //! Constructor
  158. Material()
  159. : diffuse ( ai_real( 0.6 ), ai_real( 0.6 ), ai_real( 0.6 ) )
  160. , alpha (ai_real( 1.0 ) )
  161. , shineness ( ai_real( 0.0) )
  162. , illumination_model (1)
  163. , ior ( ai_real( 1.0 ) )
  164. , transparent( ai_real( 1.0), ai_real (1.0), ai_real(1.0)) {
  165. std::fill_n(clamp, static_cast<unsigned int>(TextureTypeCount), false);
  166. }
  167. // Destructor
  168. ~Material() = default;
  169. };
  170. // ------------------------------------------------------------------------------------------------
  171. //! \struct Mesh
  172. //! \brief Data structure to store a mesh
  173. // ------------------------------------------------------------------------------------------------
  174. struct Mesh {
  175. static const unsigned int NoMaterial = ~0u;
  176. /// The name for the mesh
  177. std::string m_name;
  178. /// Array with pointer to all stored faces
  179. std::vector<Face*> m_Faces;
  180. /// Assigned material
  181. Material *m_pMaterial;
  182. /// Number of stored indices.
  183. unsigned int m_uiNumIndices;
  184. /// Number of UV
  185. unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
  186. /// Material index.
  187. unsigned int m_uiMaterialIndex;
  188. /// True, if normals are stored.
  189. bool m_hasNormals;
  190. /// True, if vertex colors are stored.
  191. bool m_hasVertexColors;
  192. /// Constructor
  193. explicit Mesh( const std::string &name )
  194. : m_name( name )
  195. , m_pMaterial(NULL)
  196. , m_uiNumIndices(0)
  197. , m_uiMaterialIndex( NoMaterial )
  198. , m_hasNormals(false) {
  199. memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
  200. }
  201. /// Destructor
  202. ~Mesh() {
  203. for (std::vector<Face*>::iterator it = m_Faces.begin();
  204. it != m_Faces.end(); ++it)
  205. {
  206. delete *it;
  207. }
  208. }
  209. };
  210. // ------------------------------------------------------------------------------------------------
  211. //! \struct Model
  212. //! \brief Data structure to store all obj-specific model datas
  213. // ------------------------------------------------------------------------------------------------
  214. struct Model {
  215. typedef std::map<std::string, std::vector<unsigned int>* > GroupMap;
  216. typedef std::map<std::string, std::vector<unsigned int>* >::iterator GroupMapIt;
  217. typedef std::map<std::string, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt;
  218. //! Model name
  219. std::string m_ModelName;
  220. //! List ob assigned objects
  221. std::vector<Object*> m_Objects;
  222. //! Pointer to current object
  223. ObjFile::Object *m_pCurrent;
  224. //! Pointer to current material
  225. ObjFile::Material *m_pCurrentMaterial;
  226. //! Pointer to default material
  227. ObjFile::Material *m_pDefaultMaterial;
  228. //! Vector with all generated materials
  229. std::vector<std::string> m_MaterialLib;
  230. //! Vector with all generated vertices
  231. std::vector<aiVector3D> m_Vertices;
  232. //! vector with all generated normals
  233. std::vector<aiVector3D> m_Normals;
  234. //! vector with all vertex colors
  235. std::vector<aiVector3D> m_VertexColors;
  236. //! Group map
  237. GroupMap m_Groups;
  238. //! Group to face id assignment
  239. std::vector<unsigned int> *m_pGroupFaceIDs;
  240. //! Active group
  241. std::string m_strActiveGroup;
  242. //! Vector with generated texture coordinates
  243. std::vector<aiVector3D> m_TextureCoord;
  244. //! Maximum dimension of texture coordinates
  245. unsigned int m_TextureCoordDim;
  246. //! Current mesh instance
  247. Mesh *m_pCurrentMesh;
  248. //! Vector with stored meshes
  249. std::vector<Mesh*> m_Meshes;
  250. //! Material map
  251. std::map<std::string, Material*> m_MaterialMap;
  252. //! \brief The default class constructor
  253. Model() :
  254. m_ModelName(""),
  255. m_pCurrent(NULL),
  256. m_pCurrentMaterial(NULL),
  257. m_pDefaultMaterial(NULL),
  258. m_pGroupFaceIDs(NULL),
  259. m_strActiveGroup(""),
  260. m_TextureCoordDim(0),
  261. m_pCurrentMesh(NULL)
  262. {
  263. // empty
  264. }
  265. //! \brief The class destructor
  266. ~Model() {
  267. // Clear all stored object instances
  268. for (std::vector<Object*>::iterator it = m_Objects.begin();
  269. it != m_Objects.end(); ++it) {
  270. delete *it;
  271. }
  272. m_Objects.clear();
  273. // Clear all stored mesh instances
  274. for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
  275. it != m_Meshes.end(); ++it) {
  276. delete *it;
  277. }
  278. m_Meshes.clear();
  279. for(GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) {
  280. delete it->second;
  281. }
  282. m_Groups.clear();
  283. for ( std::map<std::string, Material*>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it ) {
  284. delete it->second;
  285. }
  286. }
  287. };
  288. // ------------------------------------------------------------------------------------------------
  289. } // Namespace ObjFile
  290. } // Namespace Assimp
  291. #endif // OBJ_FILEDATA_H_INC