ObjFileData.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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 mPrimitiveType;
  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. mPrimitiveType(pt), m_vertices(), m_normals(), m_texturCoords(), m_pMaterial(nullptr) {
  65. // empty
  66. }
  67. //! \brief Destructor
  68. ~Face() = default;
  69. };
  70. // ------------------------------------------------------------------------------------------------
  71. //! \struct Object
  72. //! \brief Stores all objects of an obj-file object definition
  73. // ------------------------------------------------------------------------------------------------
  74. struct Object {
  75. enum ObjectType {
  76. ObjType,
  77. GroupType
  78. };
  79. //! Object name
  80. std::string m_strObjName;
  81. //! Transformation matrix, stored in OpenGL format
  82. aiMatrix4x4 m_Transformation;
  83. //! All sub-objects referenced by this object
  84. std::vector<Object *> m_SubObjects;
  85. /// Assigned meshes
  86. std::vector<unsigned int> m_Meshes;
  87. //! \brief Default constructor
  88. Object() = default;
  89. //! \brief Destructor
  90. ~Object() {
  91. for (std::vector<Object *>::iterator it = m_SubObjects.begin(); it != m_SubObjects.end(); ++it) {
  92. delete *it;
  93. }
  94. }
  95. };
  96. // ------------------------------------------------------------------------------------------------
  97. //! \struct Material
  98. //! \brief Data structure to store all material specific data
  99. // ------------------------------------------------------------------------------------------------
  100. struct Material {
  101. //! Name of material description
  102. aiString MaterialName;
  103. //! Texture names
  104. aiString texture;
  105. aiString textureSpecular;
  106. aiString textureAmbient;
  107. aiString textureEmissive;
  108. aiString textureBump;
  109. aiString textureNormal;
  110. aiString textureReflection[6];
  111. aiString textureSpecularity;
  112. aiString textureOpacity;
  113. aiString textureDisp;
  114. aiString textureRoughness;
  115. aiString textureMetallic;
  116. aiString textureSheen;
  117. aiString textureRMA;
  118. enum TextureType {
  119. TextureDiffuseType = 0,
  120. TextureSpecularType,
  121. TextureAmbientType,
  122. TextureEmissiveType,
  123. TextureBumpType,
  124. TextureNormalType,
  125. TextureReflectionSphereType,
  126. TextureReflectionCubeTopType,
  127. TextureReflectionCubeBottomType,
  128. TextureReflectionCubeFrontType,
  129. TextureReflectionCubeBackType,
  130. TextureReflectionCubeLeftType,
  131. TextureReflectionCubeRightType,
  132. TextureSpecularityType,
  133. TextureOpacityType,
  134. TextureDispType,
  135. TextureRoughnessType,
  136. TextureMetallicType,
  137. TextureSheenType,
  138. TextureRMAType,
  139. TextureTypeCount
  140. };
  141. bool clamp[TextureTypeCount];
  142. //! Ambient color
  143. aiColor3D ambient;
  144. //! Diffuse color
  145. aiColor3D diffuse = aiColor3D(0.6f, 0.6f, 0.6f);
  146. //! Specular color
  147. aiColor3D specular;
  148. //! Emissive color
  149. aiColor3D emissive;
  150. //! Alpha value
  151. ai_real alpha = ai_real(1.0);
  152. //! Shineness factor
  153. ai_real shineness = ai_real(0.0);
  154. //! Illumination model
  155. int illumination_model = 1;
  156. //! Index of refraction
  157. ai_real ior = ai_real(1.0);
  158. //! Transparency color
  159. aiColor3D transparent = aiColor3D(1.0f, 1.0f, 1.0f);
  160. //! Ambient occlusion
  161. Maybe<ai_real> ambient_occlusion;
  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 Sheen: an additional grazing component, primarily intended for cloth.
  169. Maybe<ai_real> sheen_grazing;
  170. //! PBR Sheen Tint: amount to tint sheen towards base color.
  171. Maybe<ai_real> sheen_tint;
  172. //! PBR Clearcoat
  173. Maybe<ai_real> clearcoat;
  174. //! PBR Clearcoat Thickness
  175. Maybe<ai_real> clearcoat_thickness;
  176. //! PBR Clearcoat Rougness
  177. Maybe<ai_real> clearcoat_roughness;
  178. //! PBR clearcoatGloss: controls clearcoat glossiness (0 = a “satin” appearance, 1 = a “gloss” appearance).
  179. Maybe<ai_real> clearcoat_gloss;
  180. //! PBR Anisotropy
  181. ai_real anisotropy = ai_real(0.0);
  182. //! PBR Anisotropy Rotation
  183. Maybe<ai_real> anisotropy_rotation;
  184. //! PBR Subsurface Scattering
  185. Maybe<ai_real> subsurface_scattering;
  186. //! PBR Specular Tint: a concession for artistic control that tints incident specular towards the base color.
  187. Maybe<ai_real> specular_tint;
  188. // See: https://disneyanimation.com/publications/physically-based-shading-at-disney/
  189. //! bump map multiplier (normal map scalar)(-bm)
  190. ai_real bump_multiplier = ai_real(1.0);
  191. //! Constructor
  192. Material() {
  193. std::fill_n(clamp, static_cast<unsigned int>(TextureTypeCount), false);
  194. }
  195. // Destructor
  196. ~Material() = default;
  197. };
  198. // ------------------------------------------------------------------------------------------------
  199. //! \struct Mesh
  200. //! \brief Data structure to store a mesh
  201. // ------------------------------------------------------------------------------------------------
  202. struct Mesh {
  203. static const unsigned int NoMaterial = ~0u;
  204. /// The name for the mesh
  205. std::string m_name;
  206. /// Array with pointer to all stored faces
  207. std::vector<Face*> m_Faces;
  208. /// Assigned material
  209. Material *m_pMaterial;
  210. /// Number of stored indices.
  211. unsigned int m_uiNumIndices;
  212. /// Number of UV
  213. unsigned int m_uiUVCoordinates[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  214. /// Material index.
  215. unsigned int m_uiMaterialIndex;
  216. /// True, if normals are stored.
  217. bool m_hasNormals;
  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 mModelName;
  245. //! List ob assigned objects
  246. std::vector<Object *> mObjects;
  247. //! Pointer to current object
  248. ObjFile::Object *mCurrentObject;
  249. //! Pointer to current material
  250. ObjFile::Material *mCurrentMaterial;
  251. //! Pointer to default material
  252. ObjFile::Material *mDefaultMaterial;
  253. //! Vector with all generated materials
  254. std::vector<std::string> mMaterialLib;
  255. //! Vector with all generated vertices
  256. std::vector<aiVector3D> mVertices;
  257. //! vector with all generated normals
  258. std::vector<aiVector3D> mNormals;
  259. //! vector with all vertex colors
  260. std::vector<aiVector3D> mVertexColors;
  261. //! Group map
  262. GroupMap mGroups;
  263. //! Group to face id assignment
  264. std::vector<unsigned int> *mGroupFaceIDs;
  265. //! Active group
  266. std::string mActiveGroup;
  267. //! Vector with generated texture coordinates
  268. std::vector<aiVector3D> mTextureCoord;
  269. //! Maximum dimension of texture coordinates
  270. unsigned int mTextureCoordDim;
  271. //! Current mesh instance
  272. Mesh *mCurrentMesh;
  273. //! Vector with stored meshes
  274. std::vector<Mesh *> mMeshes;
  275. //! Material map
  276. std::map<std::string, Material*> mMaterialMap;
  277. //! \brief The default class constructor
  278. Model() :
  279. mModelName(),
  280. mCurrentObject(nullptr),
  281. mCurrentMaterial(nullptr),
  282. mDefaultMaterial(nullptr),
  283. mGroupFaceIDs(nullptr),
  284. mActiveGroup(),
  285. mTextureCoordDim(0),
  286. mCurrentMesh(nullptr) {
  287. // empty
  288. }
  289. //! \brief The class destructor
  290. ~Model() {
  291. for (auto & it : mObjects) {
  292. delete it;
  293. }
  294. for (auto & Meshe : mMeshes) {
  295. delete Meshe;
  296. }
  297. for (auto & Group : mGroups) {
  298. delete Group.second;
  299. }
  300. for (auto & it : mMaterialMap) {
  301. delete it.second;
  302. }
  303. }
  304. };
  305. // ------------------------------------------------------------------------------------------------
  306. } // Namespace ObjFile
  307. } // Namespace Assimp
  308. #endif // OBJ_FILEDATA_H_INC