ObjFileData.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, 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;
  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. //! Transparency color
  159. aiColor3D transparent;
  160. //! PBR Roughness
  161. Maybe<ai_real> roughness;
  162. //! PBR Metallic
  163. Maybe<ai_real> metallic;
  164. //! PBR Metallic
  165. Maybe<aiColor3D> sheen;
  166. //! PBR Clearcoat Thickness
  167. Maybe<ai_real> clearcoat_thickness;
  168. //! PBR Clearcoat Rougness
  169. Maybe<ai_real> clearcoat_roughness;
  170. //! PBR Anisotropy
  171. ai_real anisotropy;
  172. //! bump map multipler (normal map scalar)(-bm)
  173. ai_real bump_multiplier;
  174. //! Constructor
  175. Material() :
  176. diffuse(0.6f, 0.6f, 0.6f),
  177. alpha(ai_real(1.0)),
  178. shineness(ai_real(0.0)),
  179. illumination_model(1),
  180. ior(ai_real(1.0)),
  181. transparent(1.0f, 1.0, 1.0),
  182. roughness(),
  183. metallic(),
  184. sheen(),
  185. clearcoat_thickness(),
  186. clearcoat_roughness(),
  187. anisotropy(ai_real(0.0)),
  188. bump_multiplier(ai_real(1.0)) {
  189. std::fill_n(clamp, static_cast<unsigned int>(TextureTypeCount), false);
  190. }
  191. // Destructor
  192. ~Material() = default;
  193. };
  194. // ------------------------------------------------------------------------------------------------
  195. //! \struct Mesh
  196. //! \brief Data structure to store a mesh
  197. // ------------------------------------------------------------------------------------------------
  198. struct Mesh {
  199. static const unsigned int NoMaterial = ~0u;
  200. /// The name for the mesh
  201. std::string m_name;
  202. /// Array with pointer to all stored faces
  203. std::vector<Face*> m_Faces;
  204. /// Assigned material
  205. Material *m_pMaterial;
  206. /// Number of stored indices.
  207. unsigned int m_uiNumIndices;
  208. /// Number of UV
  209. unsigned int m_uiUVCoordinates[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  210. /// Material index.
  211. unsigned int m_uiMaterialIndex;
  212. /// True, if normals are stored.
  213. bool m_hasNormals;
  214. /// Constructor
  215. explicit Mesh(const std::string &name) :
  216. m_name(name),
  217. m_pMaterial(nullptr),
  218. m_uiNumIndices(0),
  219. m_uiMaterialIndex(NoMaterial),
  220. m_hasNormals(false) {
  221. memset(m_uiUVCoordinates, 0, sizeof(unsigned int) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
  222. }
  223. /// Destructor
  224. ~Mesh() {
  225. for (std::vector<Face *>::iterator it = m_Faces.begin();
  226. it != m_Faces.end(); ++it) {
  227. delete *it;
  228. }
  229. }
  230. };
  231. // ------------------------------------------------------------------------------------------------
  232. //! \struct Model
  233. //! \brief Data structure to store all obj-specific model data
  234. // ------------------------------------------------------------------------------------------------
  235. struct Model {
  236. using GroupMap = std::map<std::string, std::vector<unsigned int> *>;
  237. using GroupMapIt = std::map<std::string, std::vector<unsigned int> *>::iterator;
  238. using ConstGroupMapIt = std::map<std::string, std::vector<unsigned int> *>::const_iterator;
  239. //! Model name
  240. std::string mModelName;
  241. //! List ob assigned objects
  242. std::vector<Object *> mObjects;
  243. //! Pointer to current object
  244. ObjFile::Object *mCurrentObject;
  245. //! Pointer to current material
  246. ObjFile::Material *mCurrentMaterial;
  247. //! Pointer to default material
  248. ObjFile::Material *mDefaultMaterial;
  249. //! Vector with all generated materials
  250. std::vector<std::string> mMaterialLib;
  251. //! Vector with all generated vertices
  252. std::vector<aiVector3D> mVertices;
  253. //! vector with all generated normals
  254. std::vector<aiVector3D> mNormals;
  255. //! vector with all vertex colors
  256. std::vector<aiVector3D> mVertexColors;
  257. //! Group map
  258. GroupMap mGroups;
  259. //! Group to face id assignment
  260. std::vector<unsigned int> *mGroupFaceIDs;
  261. //! Active group
  262. std::string mActiveGroup;
  263. //! Vector with generated texture coordinates
  264. std::vector<aiVector3D> mTextureCoord;
  265. //! Maximum dimension of texture coordinates
  266. unsigned int mTextureCoordDim;
  267. //! Current mesh instance
  268. Mesh *mCurrentMesh;
  269. //! Vector with stored meshes
  270. std::vector<Mesh *> mMeshes;
  271. //! Material map
  272. std::map<std::string, Material*> mMaterialMap;
  273. //! \brief The default class constructor
  274. Model() :
  275. mModelName(),
  276. mCurrentObject(nullptr),
  277. mCurrentMaterial(nullptr),
  278. mDefaultMaterial(nullptr),
  279. mGroupFaceIDs(nullptr),
  280. mActiveGroup(),
  281. mTextureCoordDim(0),
  282. mCurrentMesh(nullptr) {
  283. // empty
  284. }
  285. //! \brief The class destructor
  286. ~Model() {
  287. for (auto & it : mObjects) {
  288. delete it;
  289. }
  290. for (auto & Meshe : mMeshes) {
  291. delete Meshe;
  292. }
  293. for (auto & Group : mGroups) {
  294. delete Group.second;
  295. }
  296. for (auto & it : mMaterialMap) {
  297. delete it.second;
  298. }
  299. }
  300. };
  301. // ------------------------------------------------------------------------------------------------
  302. } // Namespace ObjFile
  303. } // Namespace Assimp
  304. #endif // OBJ_FILEDATA_H_INC