ObjFileData.h 10 KB

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