My Project
Loading...
Searching...
No Matches
CFileModelOBJ.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, CHAI3D
5 (www.chai3d.org)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of CHAI3D nor the names of its contributors may
22 be used to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 \author <http://www.chai3d.org>
39 \author Tim Schroder
40 \author Francois Conti
41 \version 3.3.0
42*/
43//==============================================================================
44
45//------------------------------------------------------------------------------
46#ifndef CFileModelOBJH
47#define CFileModelOBJH
48//------------------------------------------------------------------------------
49#include "world/CMultiMesh.h"
50//------------------------------------------------------------------------------
51#include <map>
52//------------------------------------------------------------------------------
53
54//------------------------------------------------------------------------------
55namespace chai3d {
56//------------------------------------------------------------------------------
57
58//==============================================================================
66//==============================================================================
67
68//------------------------------------------------------------------------------
72//------------------------------------------------------------------------------
73
75
77bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename);
78
80bool cSaveFileOBJ(cMultiMesh* a_object, const std::string& a_filename);
81
83
84
85//------------------------------------------------------------------------------
92//------------------------------------------------------------------------------
96//------------------------------------------------------------------------------
97#ifndef DOXYGEN_SHOULD_SKIP_THIS
98//------------------------------------------------------------------------------
99
100//==============================================================================
101// INTERNAL DEFINITIONS FOR OBJ FILE LOADER:
102//==============================================================================
103
105struct vertexIndexSet
106{
107 int vIndex;
108 int nIndex;
109 int tIndex;
110
111 vertexIndexSet()
112 {
113 vIndex = nIndex = tIndex = 0;
114 }
115 vertexIndexSet(int a_vIndex, int a_nIndex, int a_tIndex)
116 {
117 vIndex = a_vIndex;
118 nIndex = a_nIndex;
119 tIndex = a_tIndex;
120 }
121 vertexIndexSet(int a_vIndex)
122 {
123 vIndex = a_vIndex;
124 nIndex = 0;
125 tIndex = 0;
126 }
127};
128
129//------------------------------------------------------------------------------
130
131struct ltVertexIndexSet
132{
133 bool operator()(vertexIndexSet v1, vertexIndexSet v2) const
134 {
135 if (v1.vIndex < v2.vIndex) return 1;
136 if (v2.vIndex < v1.vIndex) return 0;
137 if (!g_objLoaderUnifyVerticesWithSamePosition)
138 {
139 if (v1.nIndex < v2.nIndex) return 1;
140 if (v2.nIndex < v1.nIndex) return 0;
141 if (v1.tIndex < v2.tIndex) return 1;
142 }
143 return 0;
144 }
145};
146
147//------------------------------------------------------------------------------
148
149typedef std::map<vertexIndexSet,unsigned int,ltVertexIndexSet> vertexIndexSet_uint_map;
150
151//------------------------------------------------------------------------------
152
153//==============================================================================
154// INTERNAL IMPLEMENTATION
155//==============================================================================
156
157// OBJ maximum length of a path
158#define C_OBJ_SIZE_PATH 255
159
160// OBJ File string identifiers
161#define C_OBJ_VERTEX_ID "v"
162#define C_OBJ_TEXCOORD_ID "vt"
163#define C_OBJ_NORMAL_ID "vn"
164#define C_OBJ_FACE_ID "f"
165#define C_OBJ_COMMENT_ID "#"
166#define C_OBJ_MTL_LIB_ID "mtllib"
167#define C_OBJ_USE_MTL_ID "usemtl"
168#define C_OBJ_NAME_ID "g"
169
170// MTL File string identifiers
171#define C_OBJ_NEW_MTL_ID "newmtl"
172#define C_OBJ_MTL_TEXTURE_ID "map_Kd"
173#define C_OBJ_MTL_AMBIENT_ID "Ka"
174#define C_OBJ_MTL_DIFFUSE_ID "Kd"
175#define C_OBJ_MTL_SPECULAR_ID "Ks"
176#define C_OBJ_MTL_SHININESS_ID "Ns"
177#define C_OBJ_MTL_ALPHA_ID "Tr"
178#define C_OBJ_MTL_ALPHA_ID_ALT "d"
179
180// Maximum size of a string that could be read out of the OBJ file
181#define C_OBJ_MAX_STR_SIZE 1024
182
183// Maximum number of vertices a that a single face can have
184#define C_OBJ_MAX_VERTICES 256
185
186// Image File information.
187struct cOBJFileInfo
188{
189 cOBJFileInfo()
190 {
191 init();
192 }
193
194 void init()
195 {
196 m_vertexCount = 0;
197 m_texCoordCount = 0;
198 m_normalCount = 0;
199 m_faceCount = 0;
200 m_materialCount = 0;
201 }
202
203 unsigned int m_vertexCount;
204 unsigned int m_texCoordCount;
205 unsigned int m_normalCount;
206 unsigned int m_faceCount;
207 unsigned int m_materialCount;
208};
209
210// Information about a surface face.
211struct cFace
212{
213 unsigned int m_numVertices;
214 unsigned int m_materialIndex;
215 int m_groupIndex;
216 int* m_pVertexIndices;
217 cVector3d* m_pVertices;
218 cColorf* m_pColors;
219 int* m_pNormalIndices;
220 cVector3d* m_pNormals;
221 int* m_pTextureIndices;
222 cVector3d* m_pTexCoords;
223
224 cFace()
225 {
226 init();
227 }
228
229 void init()
230 {
231 m_numVertices = 0;
232 m_materialIndex = 0;
233 m_groupIndex = -1;
234 m_pVertexIndices = NULL;
235 m_pVertices = NULL;
236 m_pColors = NULL;
237 m_pNormalIndices = NULL;
238 m_pNormals = NULL;
239 m_pTextureIndices = NULL;
240 m_pTexCoords = NULL;
241 }
242};
243
244// Information about a material property
245struct cMaterialInfo
246{
247 char m_name[1024];
248 char m_texture[C_OBJ_SIZE_PATH];
249 int m_textureID;
250 float m_diffuse[3];
251 float m_ambient[3];
252 float m_specular[3];
253 float m_emmissive[3];
254 float m_alpha;
255 float m_shininess;
256
257 cMaterialInfo()
258 {
259 init();
260 }
261
262 void init()
263 {
264 m_name[0] = '\0';
265 m_texture[0] = '\0';
266 m_textureID = 0;
267 m_diffuse[0] = m_diffuse[1] = m_diffuse[2] = 0.8f;
268 m_ambient[0] = m_ambient[1] = m_ambient[2] = 0.5f;
269 m_specular[0] = m_specular[1] = m_specular[2] = 0.3f;
270 m_emmissive[0] = m_emmissive[1] = m_emmissive[2] = 0.0f;
271 m_shininess = 0;
272 m_alpha = 1.0f;
273 }
274};
275
276
277//==============================================================================
285//==============================================================================
286class cOBJModel
287{
288 //--------------------------------------------------------------------------
289 // CONSTRUCTOR & DESTRUCTOR:
290 //--------------------------------------------------------------------------
291
292 public:
293
295 cOBJModel();
296
298 ~cOBJModel();
299
300 //--------------------------------------------------------------------------
301 // METHODS:
302 //--------------------------------------------------------------------------
303
305 bool LoadModel(const char szFileName[]);
306
307
308 //--------------------------------------------------------------------------
309 // MEMBERS:
310 //--------------------------------------------------------------------------
311
313 cVector3d* m_pVertices;
314
316 cColorf* m_pColors;
317
319 cFace* m_pFaces;
320
322 cVector3d* m_pNormals;
323
325 cVector3d* m_pTexCoords;
326
328 cMaterialInfo* m_pMaterials;
329
331 cOBJFileInfo m_OBJInfo;
332
334 std::vector<char*> m_groupNames;
335
336
337 //--------------------------------------------------------------------------
338 // METHODS:
339 //--------------------------------------------------------------------------
340
341 private:
342
344 void readNextString(char *a_str, int a_size, FILE *a_hStream);
345
347 void getTokenParameter(char a_str[], const unsigned int a_strSize, FILE *a_hFile);
348
350 void makePath(char a_fileAndPath[]);
351
353 bool loadMaterialLib(const char a_fFileName[], cMaterialInfo *a_pMaterials,
354 unsigned int *a_curMaterialIndex, char a_basePath[]);
355
357 void parseFaceString(char a_faceString[], cFace *a_faceOut, const cVector3d *a_pVertices,
358 const cVector3d *a_pNormals, const cVector3d *a_pTexCoords, const unsigned int a_materialIndex);
359
361 void getFileInfo(FILE *a_hStream, cOBJFileInfo *a_stat, const char a_constBasePath[]);
362};
363
365unsigned int getVertexIndex(cMesh* a_Mesh,
366 cOBJModel* a_model,
367 vertexIndexSet_uint_map* a_VertexMap,
368 vertexIndexSet& vis);
369
370//------------------------------------------------------------------------------
371#endif // DOXYGEN_SHOULD_SKIP_THIS
372//------------------------------------------------------------------------------
373
374//------------------------------------------------------------------------------
375} // namespace chai3d
376//------------------------------------------------------------------------------
377
378//------------------------------------------------------------------------------
379#endif
380//------------------------------------------------------------------------------
Implements a 3D multi-mesh object.
Definition CAudioBuffer.cpp:57
bool g_objLoaderMinimizeNumberOfMeshes
Definition CFileModelOBJ.cpp:67
bool cLoadFileOBJ(cMultiMesh *a_object, const std::string &a_filename)
This function loads an OBJ model file.
Definition CFileModelOBJ.cpp:83
bool g_objLoaderShouldGenerateExtraVertices
Definition CFileModelOBJ.cpp:66
bool g_objLoaderUnifyVerticesWithSamePosition
Definition CFileModelOBJ.cpp:65
bool cSaveFileOBJ(cMultiMesh *a_object, const std::string &a_filename)
This function saves an OBJ model file.
Definition CFileModelOBJ.cpp:450