1 | /* |
2 | Open Asset Import Library (assimp) |
3 | ---------------------------------------------------------------------- |
4 | |
5 | Copyright (c) 2006-2017, assimp team |
6 | |
7 | All rights reserved. |
8 | |
9 | Redistribution and use of this software in source and binary forms, |
10 | with or without modification, are permitted provided that the |
11 | following conditions are met: |
12 | |
13 | * Redistributions of source code must retain the above |
14 | copyright notice, this list of conditions and the |
15 | following disclaimer. |
16 | |
17 | * Redistributions in binary form must reproduce the above |
18 | copyright notice, this list of conditions and the |
19 | following disclaimer in the documentation and/or other |
20 | materials provided with the distribution. |
21 | |
22 | * Neither the name of the assimp team, nor the names of its |
23 | contributors may be used to endorse or promote products |
24 | derived from this software without specific prior |
25 | written permission of the assimp team. |
26 | |
27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | |
39 | ---------------------------------------------------------------------- |
40 | */ |
41 | |
42 | #pragma once |
43 | #ifndef OBJ_FILEDATA_H_INC |
44 | #define OBJ_FILEDATA_H_INC |
45 | |
46 | #include <vector> |
47 | #include <map> |
48 | #include <assimp/types.h> |
49 | #include <assimp/mesh.h> |
50 | |
51 | namespace Assimp { |
52 | namespace ObjFile { |
53 | |
54 | struct Object; |
55 | struct Face; |
56 | struct Material; |
57 | |
58 | // ------------------------------------------------------------------------------------------------ |
59 | //! \struct Face |
60 | //! \brief Data structure for a simple obj-face, describes discredit,l.ation and materials |
61 | // ------------------------------------------------------------------------------------------------ |
62 | struct Face { |
63 | typedef std::vector<unsigned int> IndexArray; |
64 | |
65 | //! Primitive type |
66 | aiPrimitiveType m_PrimitiveType; |
67 | //! Vertex indices |
68 | IndexArray m_vertices; |
69 | //! Normal indices |
70 | IndexArray m_normals; |
71 | //! Texture coordinates indices |
72 | IndexArray m_texturCoords; |
73 | //! Pointer to assigned material |
74 | Material *m_pMaterial; |
75 | |
76 | //! \brief Default constructor |
77 | Face( aiPrimitiveType pt = aiPrimitiveType_POLYGON) |
78 | : m_PrimitiveType( pt ) |
79 | , m_vertices() |
80 | , m_normals() |
81 | , m_texturCoords() |
82 | , m_pMaterial( 0L ) { |
83 | // empty |
84 | } |
85 | |
86 | //! \brief Destructor |
87 | ~Face() { |
88 | // empty |
89 | } |
90 | }; |
91 | |
92 | // ------------------------------------------------------------------------------------------------ |
93 | //! \struct Object |
94 | //! \brief Stores all objects of an obj-file object definition |
95 | // ------------------------------------------------------------------------------------------------ |
96 | struct Object { |
97 | enum ObjectType { |
98 | ObjType, |
99 | GroupType |
100 | }; |
101 | |
102 | //! Object name |
103 | std::string m_strObjName; |
104 | //! Transformation matrix, stored in OpenGL format |
105 | aiMatrix4x4 m_Transformation; |
106 | //! All sub-objects referenced by this object |
107 | std::vector<Object*> m_SubObjects; |
108 | /// Assigned meshes |
109 | std::vector<unsigned int> m_Meshes; |
110 | |
111 | //! \brief Default constructor |
112 | Object() |
113 | : m_strObjName("" ) { |
114 | // empty |
115 | } |
116 | |
117 | //! \brief Destructor |
118 | ~Object() { |
119 | for ( std::vector<Object*>::iterator it = m_SubObjects.begin(); it != m_SubObjects.end(); ++it) { |
120 | delete *it; |
121 | } |
122 | } |
123 | }; |
124 | |
125 | // ------------------------------------------------------------------------------------------------ |
126 | //! \struct Material |
127 | //! \brief Data structure to store all material specific data |
128 | // ------------------------------------------------------------------------------------------------ |
129 | struct Material { |
130 | //! Name of material description |
131 | aiString MaterialName; |
132 | |
133 | //! Texture names |
134 | aiString texture; |
135 | aiString textureSpecular; |
136 | aiString textureAmbient; |
137 | aiString textureEmissive; |
138 | aiString textureBump; |
139 | aiString textureNormal; |
140 | aiString textureReflection[6]; |
141 | aiString textureSpecularity; |
142 | aiString textureOpacity; |
143 | aiString textureDisp; |
144 | |
145 | enum TextureType { |
146 | TextureDiffuseType = 0, |
147 | TextureSpecularType, |
148 | TextureAmbientType, |
149 | TextureEmissiveType, |
150 | TextureBumpType, |
151 | TextureNormalType, |
152 | TextureReflectionSphereType, |
153 | TextureReflectionCubeTopType, |
154 | TextureReflectionCubeBottomType, |
155 | TextureReflectionCubeFrontType, |
156 | TextureReflectionCubeBackType, |
157 | TextureReflectionCubeLeftType, |
158 | TextureReflectionCubeRightType, |
159 | TextureSpecularityType, |
160 | TextureOpacityType, |
161 | TextureDispType, |
162 | TextureTypeCount |
163 | }; |
164 | bool clamp[TextureTypeCount]; |
165 | |
166 | //! Ambient color |
167 | aiColor3D ambient; |
168 | //! Diffuse color |
169 | aiColor3D diffuse; |
170 | //! Specular color |
171 | aiColor3D specular; |
172 | //! Emissive color |
173 | aiColor3D emissive; |
174 | //! Alpha value |
175 | ai_real alpha; |
176 | //! Shineness factor |
177 | ai_real shineness; |
178 | //! Illumination model |
179 | int illumination_model; |
180 | //! Index of refraction |
181 | ai_real ior; |
182 | //! Transparency color |
183 | aiColor3D transparent; |
184 | |
185 | //! Constructor |
186 | Material() |
187 | : diffuse ( ai_real( 0.6 ), ai_real( 0.6 ), ai_real( 0.6 ) ) |
188 | , alpha (ai_real( 1.0 ) ) |
189 | , shineness ( ai_real( 0.0) ) |
190 | , illumination_model (1) |
191 | , ior ( ai_real( 1.0 ) ) |
192 | , transparent( ai_real( 1.0), ai_real (1.0), ai_real(1.0)) { |
193 | // empty |
194 | for (size_t i = 0; i < TextureTypeCount; ++i) { |
195 | clamp[ i ] = false; |
196 | } |
197 | } |
198 | |
199 | // Destructor |
200 | ~Material() { |
201 | // empty |
202 | } |
203 | }; |
204 | |
205 | // ------------------------------------------------------------------------------------------------ |
206 | //! \struct Mesh |
207 | //! \brief Data structure to store a mesh |
208 | // ------------------------------------------------------------------------------------------------ |
209 | struct Mesh { |
210 | static const unsigned int NoMaterial = ~0u; |
211 | /// The name for the mesh |
212 | std::string m_name; |
213 | /// Array with pointer to all stored faces |
214 | std::vector<Face*> m_Faces; |
215 | /// Assigned material |
216 | Material *m_pMaterial; |
217 | /// Number of stored indices. |
218 | unsigned int m_uiNumIndices; |
219 | /// Number of UV |
220 | unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ]; |
221 | /// Material index. |
222 | unsigned int m_uiMaterialIndex; |
223 | /// True, if normals are stored. |
224 | bool m_hasNormals; |
225 | /// True, if vertex colors are stored. |
226 | bool m_hasVertexColors; |
227 | |
228 | /// Constructor |
229 | explicit Mesh( const std::string &name ) |
230 | : m_name( name ) |
231 | , m_pMaterial(NULL) |
232 | , m_uiNumIndices(0) |
233 | , m_uiMaterialIndex( NoMaterial ) |
234 | , m_hasNormals(false) { |
235 | memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS); |
236 | } |
237 | |
238 | /// Destructor |
239 | ~Mesh() { |
240 | for (std::vector<Face*>::iterator it = m_Faces.begin(); |
241 | it != m_Faces.end(); ++it) |
242 | { |
243 | delete *it; |
244 | } |
245 | } |
246 | }; |
247 | |
248 | // ------------------------------------------------------------------------------------------------ |
249 | //! \struct Model |
250 | //! \brief Data structure to store all obj-specific model datas |
251 | // ------------------------------------------------------------------------------------------------ |
252 | struct Model { |
253 | typedef std::map<std::string, std::vector<unsigned int>* > GroupMap; |
254 | typedef std::map<std::string, std::vector<unsigned int>* >::iterator GroupMapIt; |
255 | typedef std::map<std::string, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt; |
256 | |
257 | //! Model name |
258 | std::string m_ModelName; |
259 | //! List ob assigned objects |
260 | std::vector<Object*> m_Objects; |
261 | //! Pointer to current object |
262 | ObjFile::Object *m_pCurrent; |
263 | //! Pointer to current material |
264 | ObjFile::Material *m_pCurrentMaterial; |
265 | //! Pointer to default material |
266 | ObjFile::Material *m_pDefaultMaterial; |
267 | //! Vector with all generated materials |
268 | std::vector<std::string> m_MaterialLib; |
269 | //! Vector with all generated vertices |
270 | std::vector<aiVector3D> m_Vertices; |
271 | //! vector with all generated normals |
272 | std::vector<aiVector3D> m_Normals; |
273 | //! vector with all vertex colors |
274 | std::vector<aiVector3D> m_VertexColors; |
275 | //! Group map |
276 | GroupMap m_Groups; |
277 | //! Group to face id assignment |
278 | std::vector<unsigned int> *m_pGroupFaceIDs; |
279 | //! Active group |
280 | std::string m_strActiveGroup; |
281 | //! Vector with generated texture coordinates |
282 | std::vector<aiVector3D> m_TextureCoord; |
283 | //! Current mesh instance |
284 | Mesh *m_pCurrentMesh; |
285 | //! Vector with stored meshes |
286 | std::vector<Mesh*> m_Meshes; |
287 | //! Material map |
288 | std::map<std::string, Material*> m_MaterialMap; |
289 | |
290 | //! \brief The default class constructor |
291 | Model() : |
292 | m_ModelName("" ), |
293 | m_pCurrent(NULL), |
294 | m_pCurrentMaterial(NULL), |
295 | m_pDefaultMaterial(NULL), |
296 | m_pGroupFaceIDs(NULL), |
297 | m_strActiveGroup("" ), |
298 | m_pCurrentMesh(NULL) |
299 | { |
300 | // empty |
301 | } |
302 | |
303 | //! \brief The class destructor |
304 | ~Model() { |
305 | // Clear all stored object instances |
306 | for (std::vector<Object*>::iterator it = m_Objects.begin(); |
307 | it != m_Objects.end(); ++it) { |
308 | delete *it; |
309 | } |
310 | m_Objects.clear(); |
311 | |
312 | // Clear all stored mesh instances |
313 | for (std::vector<Mesh*>::iterator it = m_Meshes.begin(); |
314 | it != m_Meshes.end(); ++it) { |
315 | delete *it; |
316 | } |
317 | m_Meshes.clear(); |
318 | |
319 | for(GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) { |
320 | delete it->second; |
321 | } |
322 | m_Groups.clear(); |
323 | |
324 | for ( std::map<std::string, Material*>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it ) { |
325 | delete it->second; |
326 | } |
327 | } |
328 | }; |
329 | |
330 | // ------------------------------------------------------------------------------------------------ |
331 | |
332 | } // Namespace ObjFile |
333 | } // Namespace Assimp |
334 | |
335 | #endif // OBJ_FILEDATA_H_INC |
336 | |