CFileModelOBJ.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, 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.2.0 $Rev: 2146 $
42 */
43 //==============================================================================
44 
45 //------------------------------------------------------------------------------
46 #ifndef CFileModelOBJH
47 #define CFileModelOBJH
48 //------------------------------------------------------------------------------
49 #include "world/CMultiMesh.h"
50 //------------------------------------------------------------------------------
51 #include <map>
52 //------------------------------------------------------------------------------
53 
54 //------------------------------------------------------------------------------
55 namespace chai3d {
56 //------------------------------------------------------------------------------
57 
58 //==============================================================================
66 //==============================================================================
67 
68 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 
75 
77 bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename);
78 
80 bool cSaveFileOBJ(cMultiMesh* a_object, const std::string& a_filename);
81 
83 
84 
85 //------------------------------------------------------------------------------
92 //------------------------------------------------------------------------------
94 
95 
96 //------------------------------------------------------------------------------
97 #ifndef DOXYGEN_SHOULD_SKIP_THIS
98 //------------------------------------------------------------------------------
99 
100 //==============================================================================
101 // INTERNAL DEFINITIONS FOR OBJ FILE LOADER:
102 //==============================================================================
103 
105 struct vertexIndexSet
106 {
107  int vIndex;
108  int nIndex;
109  int tIndex;
110 
111  vertexIndexSet()
112  {
113  vIndex = nIndex = tIndex = 0;
114  }
115  vertexIndexSet(int vIndex, int nIndex, int tIndex)
116  {
117  this->vIndex = vIndex;
118  this->nIndex = nIndex;
119  this->tIndex = tIndex;
120  }
121  vertexIndexSet(int vIndex)
122  {
123  this->vIndex = vIndex;
124  this->nIndex = 0;
125  this->tIndex = 0;
126  }
127 };
128 
129 //------------------------------------------------------------------------------
130 
131 struct 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 (v1.nIndex < v2.nIndex) return 1;
138  if (v2.nIndex < v1.nIndex) return 0;
139  if (v1.tIndex < v2.tIndex) return 1;
140  return 0;
141  }
142 };
143 
144 //------------------------------------------------------------------------------
145 
146 typedef std::map<vertexIndexSet,unsigned int,ltVertexIndexSet> vertexIndexSet_uint_map;
147 
148 //------------------------------------------------------------------------------
149 
150 //==============================================================================
151 // INTERNAL IMPLEMENTATION
152 //==============================================================================
153 
154 // OBJ maximum length of a path
155 #define C_OBJ_SIZE_PATH 255
156 
157 // OBJ File string identifiers
158 #define C_OBJ_VERTEX_ID "v"
159 #define C_OBJ_TEXCOORD_ID "vt"
160 #define C_OBJ_NORMAL_ID "vn"
161 #define C_OBJ_FACE_ID "f"
162 #define C_OBJ_COMMENT_ID "#"
163 #define C_OBJ_MTL_LIB_ID "mtllib"
164 #define C_OBJ_USE_MTL_ID "usemtl"
165 #define C_OBJ_NAME_ID "g"
166 
167 // MTL File string identifiers
168 #define C_OBJ_NEW_MTL_ID "newmtl"
169 #define C_OBJ_MTL_TEXTURE_ID "map_Kd"
170 #define C_OBJ_MTL_AMBIENT_ID "Ka"
171 #define C_OBJ_MTL_DIFFUSE_ID "Kd"
172 #define C_OBJ_MTL_SPECULAR_ID "Ks"
173 #define C_OBJ_MTL_SHININESS_ID "Ns"
174 #define C_OBJ_MTL_ALPHA_ID "Tr"
175 #define C_OBJ_MTL_ALPHA_ID_ALT "d"
176 
177 // Maximum size of a string that could be read out of the OBJ file
178 #define C_OBJ_MAX_STR_SIZE 1024
179 
180 // Maximum number of vertices a that a single face can have
181 #define C_OBJ_MAX_VERTICES 256
182 
183 // Image File information.
184 struct cOBJFileInfo
185 {
186  cOBJFileInfo()
187  {
188  init();
189  }
190 
191  void init()
192  {
193  m_vertexCount = 0;
194  m_texCoordCount = 0;
195  m_normalCount = 0;
196  m_faceCount = 0;
197  m_materialCount = 0;
198  }
199 
200  unsigned int m_vertexCount;
201  unsigned int m_texCoordCount;
202  unsigned int m_normalCount;
203  unsigned int m_faceCount;
204  unsigned int m_materialCount;
205 };
206 
207 // Information about a surface face.
208 struct cFace
209 {
210  unsigned int m_numVertices;
211  unsigned int m_materialIndex;
212  int m_groupIndex;
213  int* m_pVertexIndices;
214  cVector3d* m_pVertices;
215  cColorf* m_pColors;
216  int* m_pNormalIndices;
217  cVector3d* m_pNormals;
218  int* m_pTextureIndices;
219  cVector3d* m_pTexCoords;
220 
221  cFace()
222  {
223  init();
224  }
225 
226  void init()
227  {
228  m_numVertices = 0;
229  m_materialIndex = -1;
230  m_groupIndex = -1;
231  m_pVertexIndices = NULL;
232  m_pVertices = NULL;
233  m_pColors = NULL;
234  m_pNormalIndices = NULL;
235  m_pNormals = NULL;
236  m_pTextureIndices = NULL;
237  m_pTexCoords = NULL;
238  }
239 };
240 
241 // Information about a material property
242 struct cMaterialInfo
243 {
244  char m_name[1024];
245  char m_texture[C_OBJ_SIZE_PATH];
246  int m_textureID;
247  float m_diffuse[3];
248  float m_ambient[3];
249  float m_specular[3];
250  float m_emmissive[3];
251  float m_alpha;
252  float m_shininess;
253 
254  cMaterialInfo()
255  {
256  init();
257  }
258 
259  void init()
260  {
261  m_name[0] = '\0';
262  m_texture[0] = '\0';
263  m_textureID = 0;
264  m_diffuse[0] = m_diffuse[1] = m_diffuse[2] = 0.8f;
265  m_ambient[0] = m_ambient[1] = m_ambient[2] = 0.5f;
266  m_specular[0] = m_specular[1] = m_specular[2] = 0.3f;
267  m_emmissive[0] = m_emmissive[1] = m_emmissive[2] = 0.0f;
268  m_shininess = 0;
269  m_alpha = 1.0f;
270  }
271 };
272 
273 
274 //==============================================================================
282 //==============================================================================
283 class cOBJModel
284 {
285  //--------------------------------------------------------------------------
286  // CONSTRUCTOR & DESTRUCTOR:
287  //--------------------------------------------------------------------------
288 
289  public:
290 
292  cOBJModel();
293 
295  ~cOBJModel();
296 
297  //--------------------------------------------------------------------------
298  // METHODS:
299  //--------------------------------------------------------------------------
300 
302  bool LoadModel(const char szFileName[]);
303 
304 
305  //--------------------------------------------------------------------------
306  // MEMBERS:
307  //--------------------------------------------------------------------------
308 
310  cVector3d* m_pVertices;
311 
313  cColorf* m_pColors;
314 
316  cFace* m_pFaces;
317 
319  cVector3d* m_pNormals;
320 
322  cVector3d* m_pTexCoords;
323 
325  cMaterialInfo* m_pMaterials;
326 
328  cOBJFileInfo m_OBJInfo;
329 
331  std::vector<char*> m_groupNames;
332 
333 
334  //--------------------------------------------------------------------------
335  // METHODS:
336  //--------------------------------------------------------------------------
337 
338  private:
339 
341  void readNextString(char *a_str, int a_size, FILE *a_hStream);
342 
344  void getTokenParameter(char a_str[], const unsigned int a_strSize, FILE *a_hFile);
345 
347  void makePath(char a_fileAndPath[]);
348 
350  bool loadMaterialLib(const char a_fFileName[], cMaterialInfo *a_pMaterials,
351  unsigned int *a_curMaterialIndex, char a_basePath[]);
352 
354  void parseFaceString(char a_faceString[], cFace *a_faceOut, const cVector3d *a_pVertices,
355  const cVector3d *a_pNormals, const cVector3d *a_pTexCoords, const unsigned int a_materialIndex);
356 
358  void getFileInfo(FILE *a_hStream, cOBJFileInfo *a_stat, const char a_constBasePath[]);
359 };
360 
362 unsigned int getVertexIndex(cMesh* a_Mesh,
363  cOBJModel* a_model,
364  vertexIndexSet_uint_map* a_VertexMap,
365  vertexIndexSet& vis);
366 
367 //------------------------------------------------------------------------------
368 #endif // DOXYGEN_SHOULD_SKIP_THIS
369 //------------------------------------------------------------------------------
370 
371 //------------------------------------------------------------------------------
372 } // namespace chai3d
373 //------------------------------------------------------------------------------
374 
375 //------------------------------------------------------------------------------
376 #endif
377 //------------------------------------------------------------------------------
Implements a 3D multi-mesh object.
bool cSaveFileOBJ(cMultiMesh *a_object, const std::string &a_filename)
This function saves an OBJ model file.
Definition: CFileModelOBJ.cpp:381
bool cLoadFileOBJ(cMultiMesh *a_object, const std::string &a_filename)
This function loads an OBJ model file.
Definition: CFileModelOBJ.cpp:81
Definition: CAudioBuffer.cpp:56
bool g_objLoaderShouldGenerateExtraVertices
Definition: CFileModelOBJ.cpp:65