CHAI3D  3.0.0
CFileModelOBJ.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2015, 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.0.0 $Rev: 1639 $
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  unsigned int m_vertexCount;
187  unsigned int m_texCoordCount;
188  unsigned int m_normalCount;
189  unsigned int m_faceCount;
190  unsigned int m_materialCount;
191 };
192 
193 // Information about a surface face.
194 struct cFace
195 {
196  unsigned int m_numVertices;
197  unsigned int m_materialIndex;
198 
199  // Which 'g ...' group does this face belong to? -1 indicates no group.
200  int m_groupIndex;
201 
202  int *m_pVertexIndices;
203  cVector3d *m_pVertices;
204  cColorf *m_pColors;
205  int *m_pNormalIndices;
206  cVector3d *m_pNormals;
207  int *m_pTextureIndices;
208  cVector3d *m_pTexCoords;
209 };
210 
211 // Information about a material property
212 struct cMaterialInfo
213 {
214  char m_name[1024];
215  char m_texture[C_OBJ_SIZE_PATH];
216  int m_textureID;
217  float m_diffuse[3];
218  float m_ambient[3];
219  float m_specular[3];
220  float m_emmissive[3];
221  float m_alpha;
222  float m_shininess;
223 
224  cMaterialInfo() {
225  m_name[0] = '\0';
226  m_texture[0] = '\0';
227  m_textureID = -1;
228  m_diffuse[0] = m_diffuse[1] = m_diffuse[2] = 0.8f;
229  m_ambient[0] = m_ambient[1] = m_ambient[2] = 0.8f;
230  m_specular[0] = m_specular[1] = m_specular[2] = 0.3f;
231  m_emmissive[0] = m_emmissive[1] = m_emmissive[2] = 0.0f;
232  m_shininess = 0;
233  m_alpha = 1.0f;
234  }
235 };
236 
237 
238 //==============================================================================
246 //==============================================================================
247 class cOBJModel
248 {
249  //--------------------------------------------------------------------------
250  // CONSTRUCTOR & DESTRUCTOR:
251  //--------------------------------------------------------------------------
252 
253  public:
254 
256  cOBJModel();
257 
259  ~cOBJModel();
260 
261  //--------------------------------------------------------------------------
262  // METHODS:
263  //--------------------------------------------------------------------------
264 
266  bool LoadModel(const char szFileName[]);
267 
268 
269  //--------------------------------------------------------------------------
270  // MEMBERS:
271  //--------------------------------------------------------------------------
272 
274  cVector3d* m_pVertices;
275 
277  cColorf* m_pColors;
278 
280  cFace* m_pFaces;
281 
283  cVector3d* m_pNormals;
284 
286  cVector3d* m_pTexCoords;
287 
289  cMaterialInfo* m_pMaterials;
290 
292  cOBJFileInfo m_OBJInfo;
293 
295  std::vector<char*> m_groupNames;
296 
297 
298  //--------------------------------------------------------------------------
299  // METHODS:
300  //--------------------------------------------------------------------------
301 
302  private:
303 
305  void readNextString(char a_string[], int a_size, FILE *hStream);
306 
308  void getTokenParameter(char a_string[], const unsigned int a_strSize, FILE *a_hFile);
309 
311  void makePath(char a_fileAndPath[]);
312 
314  bool loadMaterialLib(const char a_fFileName[], cMaterialInfo *a_pMaterials,
315  unsigned int *a_curMaterialIndex, char a_basePath[]);
316 
318  void parseFaceString(char a_faceString[], cFace *a_faceOut, const cVector3d *a_pVertices,
319  const cVector3d *a_pNormals, const cVector3d *a_pTexCoords, const unsigned int a_materialIndex);
320 
322  void getFileInfo(FILE *a_hStream, cOBJFileInfo *a_stat, const char a_constBasePath[]);
323 };
324 
326 unsigned int getVertexIndex(cMesh* a_Mesh,
327  cOBJModel* a_model,
328  vertexIndexSet_uint_map* a_VertexMap,
329  vertexIndexSet& vis);
330 
331 //------------------------------------------------------------------------------
332 #endif // DOXYGEN_SHOULD_SKIP_THIS
333 //------------------------------------------------------------------------------
334 
335 //------------------------------------------------------------------------------
336 } // namespace chai3d
337 //------------------------------------------------------------------------------
338 
339 //------------------------------------------------------------------------------
340 #endif
341 //------------------------------------------------------------------------------
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

CHAI3D 3.0.0 documentation
Please address any questions to support@chai3d.org
(C) 2003-2015 - CHAI3D
All Rights Reserved.