ObjFileData.h 11 KB

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