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