ObjFileData.h 10 KB

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