Subversion Repositories gelsvn

Rev

Rev 364 | Rev 436 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 364 Rev 421
1
// bdl, jab, feb 2005
1
// bdl, jab, feb 2005
2
// Inspired and partly lifted from Nate Robins' Obj loader
2
// Inspired and partly lifted from Nate Robins' Obj loader
3
 
3
 
4
#include "TriMesh.h"
4
#include "TriMesh.h"
5
#include <CGLA/Vec3f.h>
5
#include <CGLA/Vec3f.h>
6
#include <stdio.h>
6
#include <stdio.h>
7
#include <iostream>
7
#include <iostream>
8
 
8
 
9
using namespace std;
9
using namespace std;
10
using namespace CGLA;
10
using namespace CGLA;
11
 
11
 
12
namespace Geometry {
12
namespace Geometry {
13
 
13
 
14
	namespace
14
	namespace
15
	{
15
	{
16
		string get_path(const string& _filename)
16
		string get_path(const string& _filename)
17
		{
17
		{
18
			// Make sure we only have slashes and not backslashes
18
			// Make sure we only have slashes and not backslashes
19
			string filename = _filename;
19
			string filename = _filename;
20
			replace(filename.begin(),filename.end(),'\\','/');
20
			replace(filename.begin(),filename.end(),'\\','/');
21
			
21
			
22
			// Find the last occurrence of slash.
22
			// Find the last occurrence of slash.
23
			// Everything before is path.
23
			// Everything before is path.
24
			unsigned int n = filename.rfind("/");
24
			unsigned int n = filename.rfind("/");
25
			if(n > filename.size())
25
			if(n > filename.size())
26
				return "./";
26
				return "./";
27
			string pathname = "";
27
			string pathname = "";
28
			pathname.assign(filename,0,n);
28
			pathname.assign(filename,0,n);
29
			pathname.append("/");
29
			pathname.append("/");
30
			return pathname;
30
			return pathname;
31
		}
31
		}
32
	}
32
	}
33
	class TriMeshObjLoader
33
	class TriMeshObjLoader
34
	{
34
	{
35
		TriMesh *mesh;
35
		TriMesh *mesh;
36
		std::string pathname;
36
		std::string pathname;
37
 
37
 
38
		int get_vert(int i) {
38
		int get_vert(int i) {
39
			assert(i!=0);
39
			assert(i!=0);
40
			if (i<0) {
40
			if (i<0) {
41
				return mesh->geometry.no_vertices()+i;
41
				return mesh->geometry.no_vertices()+i;
42
			} else
42
			} else
43
				return i-1;
43
				return i-1;
44
		}
44
		}
45
 
45
 
46
		int get_normal(int i) {
46
		int get_normal(int i) {
47
			if (i<0) {
47
			if (i<0) {
48
				return mesh->normals.no_vertices()+i;
48
				return mesh->normals.no_vertices()+i;
49
			} else
49
			} else
50
				return i-1;
50
				return i-1;
51
		}
51
		}
52
 
52
 
53
		int get_texcoord(int i) {
53
		int get_texcoord(int i) {
54
			if (i<0) {
54
			if (i<0) {
55
				return mesh->texcoords.no_vertices()+i;
55
				return mesh->texcoords.no_vertices()+i;
56
			} else
56
			} else
57
				return i-1;
57
				return i-1;
58
		}
58
		}
59
		
59
		
60
		void read_material_library(const string& filename);
60
		void read_material_library(const string& filename);
61
 
61
 
62
	public:
62
	public:
63
 
63
 
64
		TriMeshObjLoader(TriMesh *_mesh): mesh(_mesh) {}
64
		TriMeshObjLoader(TriMesh *_mesh): mesh(_mesh) {}
65
		
65
		
66
		void load(const std::string& filename);
66
		void load(const std::string& filename);
67
	};
67
	};
68
	
68
	
69
	void TriMeshObjLoader::read_material_library(const string& filename)
69
	void TriMeshObjLoader::read_material_library(const string& filename)
70
	{
70
	{
71
		string fn = pathname + filename;
71
		string fn = pathname + filename;
72
		FILE* file = fopen(fn.data(), "r");
72
		FILE* file = fopen(fn.data(), "r");
73
		if (!file) 
73
		if (!file) 
74
			{
74
			{
75
				cerr << "Could not open " << filename << endl;
75
				cerr << "Could not open " << filename << endl;
76
				return;
76
				return;
77
			}
77
			}
78
			
78
			
79
		char  buf[128];
79
		char  buf[128];
80
		unsigned int nummaterials=1;
80
		unsigned int nummaterials=1;
81
			
81
			
82
		// count the number of materials in the file 
82
		// count the number of materials in the file 
83
		while(fscanf(file, "%s", buf) != EOF) 
83
		while(fscanf(file, "%s", buf) != EOF) 
84
			{
84
			{
85
				switch(buf[0]) 
85
				switch(buf[0]) 
86
					{
86
					{
87
					case '#':				/* comment */
87
					case '#':				/* comment */
88
						/* eat up rest of line */
88
						/* eat up rest of line */
89
						fgets(buf, sizeof(buf), file);
89
						fgets(buf, sizeof(buf), file);
90
						break;
90
						break;
91
					case 'n':				/* newmtl */
91
					case 'n':				/* newmtl */
92
						fgets(buf, sizeof(buf), file);
92
						fgets(buf, sizeof(buf), file);
93
						nummaterials++;
93
						nummaterials++;
94
						sscanf(buf, "%s %s", buf, buf);
94
						sscanf(buf, "%s %s", buf, buf);
95
						break;
95
						break;
96
					default:
96
					default:
97
						/* eat up rest of line */
97
						/* eat up rest of line */
98
						fgets(buf, sizeof(buf), file);
98
						fgets(buf, sizeof(buf), file);
99
						break;
99
						break;
100
					}
100
					}
101
			}
101
			}
102
		rewind(file);
102
		rewind(file);
103
		
103
		
104
		/* allocate memory for the materials */
104
		/* allocate memory for the materials */
105
		mesh->materials.resize(nummaterials);
105
		mesh->materials.resize(nummaterials);
106
 
106
 
107
		/* now, read in the data */
107
		/* now, read in the data */
108
		nummaterials = 0;
108
		nummaterials = 0;
109
		while(fscanf(file, "%s", buf) != EOF) {
109
		while(fscanf(file, "%s", buf) != EOF) {
110
			switch(buf[0]) {
110
			switch(buf[0]) {
111
			case '#':				/* comment */
111
			case '#':				/* comment */
112
				/* eat up rest of line */
112
				/* eat up rest of line */
113
				fgets(buf, sizeof(buf), file);
113
				fgets(buf, sizeof(buf), file);
114
				break;
114
				break;
115
			case 'n':				/* newmtl */
115
			case 'n':				/* newmtl */
116
				fgets(buf, sizeof(buf), file);
116
				fgets(buf, sizeof(buf), file);
117
				sscanf(buf, "%s %s", buf, buf);
117
				sscanf(buf, "%s %s", buf, buf);
118
				nummaterials++;
118
				nummaterials++;
119
				mesh->materials[nummaterials].name = buf;
119
				mesh->materials[nummaterials].name = buf;
120
				break;
120
				break;
121
			case 'N':
121
			case 'N':
-
 
122
        switch(buf[1])
-
 
123
          {
-
 
124
          case 's':
122
				fscanf(file, "%f", &mesh->materials[nummaterials].shininess);
125
				    fscanf(file, "%f", &mesh->materials[nummaterials].shininess);
123
				/* wavefront shininess is from [0, 1000], so scale for OpenGL */
126
				    /* wavefront shininess is from [0, 1000], so scale for OpenGL */
124
				mesh->materials[nummaterials].shininess /= 1000.0;
127
				    mesh->materials[nummaterials].shininess /= 1000.0;
125
				mesh->materials[nummaterials].shininess *= 128.0;
128
				    mesh->materials[nummaterials].shininess *= 128.0;
-
 
129
            break;
-
 
130
          case 'i':
-
 
131
				    fscanf(file, "%f", &mesh->materials[nummaterials].ior_in);
-
 
132
            break;
-
 
133
					default:
-
 
134
						/* eat up rest of line */
-
 
135
						fgets(buf, sizeof(buf), file);
-
 
136
						break;
-
 
137
          }
126
				break;
138
				break;
127
			case 'K': 
139
			case 'K': 
128
				switch(buf[1]) 
140
				switch(buf[1]) 
129
					{
141
					{
130
					case 'd':
142
					case 'd':
131
						fscanf(file, "%f %f %f",
143
						fscanf(file, "%f %f %f",
132
									 &mesh->materials[nummaterials].diffuse[0],
144
									 &mesh->materials[nummaterials].diffuse[0],
133
									 &mesh->materials[nummaterials].diffuse[1],
145
									 &mesh->materials[nummaterials].diffuse[1],
134
									 &mesh->materials[nummaterials].diffuse[2]);
146
									 &mesh->materials[nummaterials].diffuse[2]);
135
						break;
147
						break;
136
					case 's':
148
					case 's':
137
						fscanf(file, "%f %f %f",
149
						fscanf(file, "%f %f %f",
138
									 &mesh->materials[nummaterials].specular[0],
150
									 &mesh->materials[nummaterials].specular[0],
139
									 &mesh->materials[nummaterials].specular[1],
151
									 &mesh->materials[nummaterials].specular[1],
140
									 &mesh->materials[nummaterials].specular[2]);
152
									 &mesh->materials[nummaterials].specular[2]);
141
						break;
153
						break;
142
					case 'a':
154
					case 'a':
143
						fscanf(file, "%f %f %f",
155
						fscanf(file, "%f %f %f",
144
									 &mesh->materials[nummaterials].ambient[0],
156
									 &mesh->materials[nummaterials].ambient[0],
145
									 &mesh->materials[nummaterials].ambient[1],
157
									 &mesh->materials[nummaterials].ambient[1],
146
									 &mesh->materials[nummaterials].ambient[2]);
158
									 &mesh->materials[nummaterials].ambient[2]);
147
						break;
159
						break;
148
					default:
160
					default:
149
						/* eat up rest of line */
161
						/* eat up rest of line */
150
						fgets(buf, sizeof(buf), file);
162
						fgets(buf, sizeof(buf), file);
151
						break;
163
						break;
152
					}
164
					}
153
				break;
165
				break;
-
 
166
      case 'T':
-
 
167
				fscanf(file, "%f %f %f",
-
 
168
               &mesh->materials[nummaterials].transmission[0],
-
 
169
               &mesh->materials[nummaterials].transmission[1],
-
 
170
               &mesh->materials[nummaterials].transmission[2]);
-
 
171
        break;
-
 
172
      case 'i':
-
 
173
				fscanf(file, "%d", &mesh->materials[nummaterials].illum);
-
 
174
        break;
154
			case 'm': // Map ... all maps are treated equally.
175
			case 'm': // Map ... all maps are treated equally.
155
				{
176
				{
156
					fscanf(file,"%s",buf);
177
					fscanf(file,"%s",buf);
157
					mesh->materials[nummaterials].tex_path = pathname;
178
					mesh->materials[nummaterials].tex_path = pathname;
158
					mesh->materials[nummaterials].tex_name = string(buf);
179
					mesh->materials[nummaterials].tex_name = string(buf);
159
				}
180
				}
160
				break;
181
				break;
161
			default:
182
			default:
162
				/* eat up rest of line */
183
				/* eat up rest of line */
163
				fgets(buf, sizeof(buf), file);
184
				fgets(buf, sizeof(buf), file);
164
				break;
185
				break;
165
			}
186
			}
166
		}
187
		}
167
	}
188
	}
168
 
189
 
169
 
190
 
170
	void TriMeshObjLoader::load(const std::string& filename) 
191
	void TriMeshObjLoader::load(const std::string& filename) 
171
	{
192
	{
172
		pathname = get_path(filename);
193
		pathname = get_path(filename);
173
		FILE *fp = fopen(filename.data(), "r");
194
		FILE *fp = fopen(filename.data(), "r");
174
		if (fp==0) {
195
		if (fp==0) {
175
			cerr << "File " << filename << " does not exist" << endl;
196
			cerr << "File " << filename << " does not exist" << endl;
176
      exit(0);
197
      exit(0);
177
		}
198
		}
178
		mesh->materials.resize(1);
199
		mesh->materials.resize(1);
179
		
200
		
180
		char buf[256];
201
		char buf[256];
181
		Vec3f v_geo;
202
		Vec3f v_geo;
182
		Vec3f v_normals;
203
		Vec3f v_normals;
183
		Vec3f v_texcoords;
204
		Vec3f v_texcoords;
184
 
205
 
185
		Vec3i f_geo;
206
		Vec3i f_geo;
186
		Vec3i f_normals;
207
		Vec3i f_normals;
187
		Vec3i f_texcoords;
208
		Vec3i f_texcoords;
188
		int current_material=0;
209
		int current_material=0;
189
		int v,n,t;
210
		int v,n,t;
190
		while(fscanf(fp, "%s", buf) != EOF) 
211
		while(fscanf(fp, "%s", buf) != EOF) 
191
			{
212
			{
192
				switch(buf[0]) 
213
				switch(buf[0]) 
193
					{
214
					{
194
					case '#': // A comment
215
					case '#': // A comment
195
						fgets(buf, sizeof(buf), fp);
216
						fgets(buf, sizeof(buf), fp);
196
						break;
217
						break;
197
					case 'm':
218
					case 'm':
198
						fgets(buf, sizeof(buf), fp);
219
						fgets(buf, sizeof(buf), fp);
199
						sscanf(buf, "%s %s", buf, buf);
220
						sscanf(buf, "%s %s", buf, buf);
200
						read_material_library(buf);
221
						read_material_library(buf);
201
						break;
222
						break;
202
					case 'u':
223
					case 'u':
203
						fgets(buf, sizeof(buf), fp);
224
						fgets(buf, sizeof(buf), fp);
204
						sscanf(buf, "%s %s", buf, buf);
225
						sscanf(buf, "%s %s", buf, buf);
205
						current_material = mesh->find_material(buf);
226
						current_material = mesh->find_material(buf);
206
						break;
227
						break;
207
					case 'v': // v, vn, vt
228
					case 'v': // v, vn, vt
208
						switch(buf[1]) 
229
						switch(buf[1]) 
209
							{
230
							{
210
							case '\0': // vertex
231
							case '\0': // vertex
211
								fscanf(fp, "%f %f %f", &v_geo[0], &v_geo[1], &v_geo[2]);
232
								fscanf(fp, "%f %f %f", &v_geo[0], &v_geo[1], &v_geo[2]);
212
								mesh->geometry.add_vertex(v_geo);
233
								mesh->geometry.add_vertex(v_geo);
213
								break;
234
								break;
214
							case 'n': // normal
235
							case 'n': // normal
215
								fscanf(fp, "%f %f %f", &v_normals[0], &v_normals[1], &v_normals[2]);
236
								fscanf(fp, "%f %f %f", &v_normals[0], &v_normals[1], &v_normals[2]);
216
								mesh->normals.add_vertex(v_normals);
237
								mesh->normals.add_vertex(v_normals);
217
								break;
238
								break;
218
							case 't': // texcoord
239
							case 't': // texcoord
219
								fscanf(fp, "%f %f", &v_texcoords[0], &v_texcoords[1]);
240
								fscanf(fp, "%f %f", &v_texcoords[0], &v_texcoords[1]);
220
								v_texcoords[2]=1;
241
								v_texcoords[2]=1;
221
								mesh->texcoords.add_vertex(v_texcoords);
242
								mesh->texcoords.add_vertex(v_texcoords);
222
								break;
243
								break;
223
							}
244
							}
224
						break;
245
						break;
225
					case 'f':
246
					case 'f':
226
						v = n = t = 0;
247
						v = n = t = 0;
227
						fscanf(fp, "%s", buf);
248
						fscanf(fp, "%s", buf);
228
						// can be one of %d, %d//%d, %d/%d, %d/%d/%d 
249
						// can be one of %d, %d//%d, %d/%d, %d/%d/%d 
229
						if(sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3)
250
						if(sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3)
230
							{ // v/t/n
251
							{ // v/t/n
231
								
252
								
232
								f_geo[0]=get_vert(v); 
253
								f_geo[0]=get_vert(v); 
233
								f_texcoords[0]=get_texcoord(t);
254
								f_texcoords[0]=get_texcoord(t);
234
								f_normals[0]=get_normal(n);
255
								f_normals[0]=get_normal(n);
235
 
256
 
236
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
257
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
237
								f_geo[1]=get_vert(v); 
258
								f_geo[1]=get_vert(v); 
238
								f_texcoords[1]=get_texcoord(t);
259
								f_texcoords[1]=get_texcoord(t);
239
								f_normals[1]=get_normal(n);
260
								f_normals[1]=get_normal(n);
240
 
261
 
241
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
262
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
242
								f_geo[2]=get_vert(v); 
263
								f_geo[2]=get_vert(v); 
243
								f_texcoords[2]=get_texcoord(t);
264
								f_texcoords[2]=get_texcoord(t);
244
								f_normals[2]=get_normal(n);
265
								f_normals[2]=get_normal(n);
245
 
266
 
246
								int idx = mesh->geometry.add_face(f_geo);
267
								int idx = mesh->geometry.add_face(f_geo);
247
								mesh->normals.add_face(f_normals, idx);
268
								mesh->normals.add_face(f_normals, idx);
248
								mesh->texcoords.add_face(f_texcoords, idx);
269
								mesh->texcoords.add_face(f_texcoords, idx);
249
								mesh->mat_idx.push_back(current_material);
270
								mesh->mat_idx.push_back(current_material);
250
 
271
 
251
								// Load a general polygon and convert to triangles
272
								// Load a general polygon and convert to triangles
252
								while(fscanf(fp, "%d/%d/%d", &v, &t, &n)==3) 
273
								while(fscanf(fp, "%d/%d/%d", &v, &t, &n)==3) 
253
									{
274
									{
254
										f_geo[1]=f_geo[2];
275
										f_geo[1]=f_geo[2];
255
										f_normals[1]=f_normals[2];
276
										f_normals[1]=f_normals[2];
256
										f_texcoords[1]=f_texcoords[2];
277
										f_texcoords[1]=f_texcoords[2];
257
 
278
 
258
										f_geo[2]=get_vert(v);
279
										f_geo[2]=get_vert(v);
259
										f_normals[2]=get_normal(n);
280
										f_normals[2]=get_normal(n);
260
										f_texcoords[2]=get_texcoord(t);
281
										f_texcoords[2]=get_texcoord(t);
261
 
282
 
262
										int idx = mesh->geometry.add_face(f_geo);
283
										int idx = mesh->geometry.add_face(f_geo);
263
										mesh->normals.add_face(f_normals, idx);
284
										mesh->normals.add_face(f_normals, idx);
264
										mesh->texcoords.add_face(f_texcoords, idx);
285
										mesh->texcoords.add_face(f_texcoords, idx);
265
										mesh->mat_idx.push_back(current_material);
286
										mesh->mat_idx.push_back(current_material);
266
									}
287
									}
267
							} 
288
							} 
268
						else if (sscanf(buf, "%d//%d", &v, &n)==2)
289
						else if (sscanf(buf, "%d//%d", &v, &n)==2)
269
							{// v//n 
290
							{// v//n 
270
								f_geo[0]=get_vert(v);
291
								f_geo[0]=get_vert(v);
271
								f_normals[0]=get_normal(n);
292
								f_normals[0]=get_normal(n);
272
 
293
 
273
								fscanf(fp, "%d//%d", &v, &n); 
294
								fscanf(fp, "%d//%d", &v, &n); 
274
								f_geo[1]=get_vert(v);
295
								f_geo[1]=get_vert(v);
275
								f_normals[1]=get_normal(n);
296
								f_normals[1]=get_normal(n);
276
								
297
								
277
								fscanf(fp, "%d//%d", &v, &n); 
298
								fscanf(fp, "%d//%d", &v, &n); 
278
								f_geo[2]=get_vert(v);
299
								f_geo[2]=get_vert(v);
279
								f_normals[2]=get_normal(n);
300
								f_normals[2]=get_normal(n);
280
								
301
								
281
								int idx = mesh->geometry.add_face(f_geo);
302
								int idx = mesh->geometry.add_face(f_geo);
282
								mesh->normals.add_face(f_normals, idx);
303
								mesh->normals.add_face(f_normals, idx);
283
								mesh->mat_idx.push_back(current_material);
304
								mesh->mat_idx.push_back(current_material);
284
								
305
								
285
								// Load a general polygon and convert to triangles
306
								// Load a general polygon and convert to triangles
286
								while(fscanf(fp, "%d//%d", &v, &n)==2) 
307
								while(fscanf(fp, "%d//%d", &v, &n)==2) 
287
									{
308
									{
288
										f_geo[1]=f_geo[2];
309
										f_geo[1]=f_geo[2];
289
										f_normals[1]=f_normals[2];
310
										f_normals[1]=f_normals[2];
290
										f_geo[2]=get_vert(v);
311
										f_geo[2]=get_vert(v);
291
										int idx = mesh->geometry.add_face(f_geo);
312
										int idx = mesh->geometry.add_face(f_geo);
292
										mesh->normals.add_face(f_normals, idx);
313
										mesh->normals.add_face(f_normals, idx);
293
										mesh->mat_idx.push_back(current_material);
314
										mesh->mat_idx.push_back(current_material);
294
									}
315
									}
295
							} 
316
							} 
296
						else if (sscanf(buf, "%d/%d", &v, &t) == 2)
317
						else if (sscanf(buf, "%d/%d", &v, &t) == 2)
297
							{ // v/t 
318
							{ // v/t 
298
								f_geo[0]=get_vert(v);
319
								f_geo[0]=get_vert(v);
299
								f_texcoords[0]=get_texcoord(t);
320
								f_texcoords[0]=get_texcoord(t);
300
 
321
 
301
								fscanf(fp, "%d/%d", &v, &t); 
322
								fscanf(fp, "%d/%d", &v, &t); 
302
								f_geo[1]=get_vert(v);
323
								f_geo[1]=get_vert(v);
303
								f_texcoords[1]=get_texcoord(t);
324
								f_texcoords[1]=get_texcoord(t);
304
 
325
 
305
								fscanf(fp, "%d/%d", &v, &t); 
326
								fscanf(fp, "%d/%d", &v, &t); 
306
								f_geo[2]=get_vert(v);
327
								f_geo[2]=get_vert(v);
307
								f_texcoords[2]=get_texcoord(t);
328
								f_texcoords[2]=get_texcoord(t);
308
 
329
 
309
								int idx = mesh->geometry.add_face(f_geo);
330
								int idx = mesh->geometry.add_face(f_geo);
310
								mesh->texcoords.add_face(f_texcoords, idx);
331
								mesh->texcoords.add_face(f_texcoords, idx);
311
								mesh->mat_idx.push_back(current_material);
332
								mesh->mat_idx.push_back(current_material);
312
						
333
						
313
								// Load a general polygon and convert to triangles
334
								// Load a general polygon and convert to triangles
314
								while(fscanf(fp, "%d/%d", &v, &t)==2) 
335
								while(fscanf(fp, "%d/%d", &v, &t)==2) 
315
									{
336
									{
316
										f_geo[1]=f_geo[2];
337
										f_geo[1]=f_geo[2];
317
										f_texcoords[1]=f_texcoords[2];
338
										f_texcoords[1]=f_texcoords[2];
318
								
339
								
319
										f_geo[2]=get_vert(v);
340
										f_geo[2]=get_vert(v);
320
										f_texcoords[2]=get_texcoord(t);
341
										f_texcoords[2]=get_texcoord(t);
321
 
342
 
322
										int idx = mesh->geometry.add_face(f_geo);
343
										int idx = mesh->geometry.add_face(f_geo);
323
										mesh->texcoords.add_face(f_texcoords, idx);
344
										mesh->texcoords.add_face(f_texcoords, idx);
324
										mesh->mat_idx.push_back(current_material);
345
										mesh->mat_idx.push_back(current_material);
325
									}
346
									}
326
							} 
347
							} 
327
						else if (sscanf(buf, "%d", &v)==1)
348
						else if (sscanf(buf, "%d", &v)==1)
328
							{ // v 
349
							{ // v 
329
								f_geo[0]=get_vert(v);
350
								f_geo[0]=get_vert(v);
330
								
351
								
331
								fscanf(fp, "%d", &v);
352
								fscanf(fp, "%d", &v);
332
								f_geo[1]=get_vert(v);
353
								f_geo[1]=get_vert(v);
333
								
354
								
334
								fscanf(fp, "%d", &v);
355
								fscanf(fp, "%d", &v);
335
								f_geo[2]=get_vert(v);
356
								f_geo[2]=get_vert(v);
336
								
357
								
337
								mesh->geometry.add_face(f_geo);
358
								mesh->geometry.add_face(f_geo);
338
								mesh->mat_idx.push_back(current_material);
359
								mesh->mat_idx.push_back(current_material);
339
 
360
 
340
								// Load a general polygon and convert to triangles
361
								// Load a general polygon and convert to triangles
341
								while(fscanf(fp, "%d", &v)==1) 
362
								while(fscanf(fp, "%d", &v)==1) 
342
									{
363
									{
343
										f_geo[1]=f_geo[2];
364
										f_geo[1]=f_geo[2];
344
										f_geo[2]=get_vert(v);
365
										f_geo[2]=get_vert(v);
345
										mesh->geometry.add_face(f_geo);
366
										mesh->geometry.add_face(f_geo);
346
										mesh->mat_idx.push_back(current_material);
367
										mesh->mat_idx.push_back(current_material);
347
									}
368
									}
348
							}
369
							}
349
						break;
370
						break;
350
					default:
371
					default:
351
						fgets(buf, sizeof(buf), fp);
372
						fgets(buf, sizeof(buf), fp);
352
						break;
373
						break;
353
					}
374
					}
354
			}
375
			}
355
	}
376
	}
356
 
377
 
357
 
378
 
358
	void obj_load(const string& filename, TriMesh &mesh)
379
	void obj_load(const string& filename, TriMesh &mesh)
359
	{
380
	{
360
		TriMeshObjLoader loader(&mesh);
381
		TriMeshObjLoader loader(&mesh);
361
		loader.load(filename);
382
		loader.load(filename);
362
	}
383
	}
363
 
384
 
364
}
385
}
365
 
386