Subversion Repositories gelsvn

Rev

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

Rev 79 Rev 178
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
				fscanf(file, "%f", &mesh->materials[nummaterials].shininess);
122
				fscanf(file, "%f", &mesh->materials[nummaterials].shininess);
123
				/* wavefront shininess is from [0, 1000], so scale for OpenGL */
123
				/* wavefront shininess is from [0, 1000], so scale for OpenGL */
124
				mesh->materials[nummaterials].shininess /= 1000.0;
124
				mesh->materials[nummaterials].shininess /= 1000.0;
125
				mesh->materials[nummaterials].shininess *= 128.0;
125
				mesh->materials[nummaterials].shininess *= 128.0;
126
				break;
126
				break;
127
			case 'K': 
127
			case 'K': 
128
				switch(buf[1]) 
128
				switch(buf[1]) 
129
					{
129
					{
130
					case 'd':
130
					case 'd':
131
						fscanf(file, "%f %f %f",
131
						fscanf(file, "%f %f %f",
132
									 &mesh->materials[nummaterials].diffuse[0],
132
									 &mesh->materials[nummaterials].diffuse[0],
133
									 &mesh->materials[nummaterials].diffuse[1],
133
									 &mesh->materials[nummaterials].diffuse[1],
134
									 &mesh->materials[nummaterials].diffuse[2]);
134
									 &mesh->materials[nummaterials].diffuse[2]);
135
						break;
135
						break;
136
					case 's':
136
					case 's':
137
						fscanf(file, "%f %f %f",
137
						fscanf(file, "%f %f %f",
138
									 &mesh->materials[nummaterials].specular[0],
138
									 &mesh->materials[nummaterials].specular[0],
139
									 &mesh->materials[nummaterials].specular[1],
139
									 &mesh->materials[nummaterials].specular[1],
140
									 &mesh->materials[nummaterials].specular[2]);
140
									 &mesh->materials[nummaterials].specular[2]);
141
						break;
141
						break;
142
					case 'a':
142
					case 'a':
143
						fscanf(file, "%f %f %f",
143
						fscanf(file, "%f %f %f",
144
									 &mesh->materials[nummaterials].ambient[0],
144
									 &mesh->materials[nummaterials].ambient[0],
145
									 &mesh->materials[nummaterials].ambient[1],
145
									 &mesh->materials[nummaterials].ambient[1],
146
									 &mesh->materials[nummaterials].ambient[2]);
146
									 &mesh->materials[nummaterials].ambient[2]);
147
						break;
147
						break;
148
					default:
148
					default:
149
						/* eat up rest of line */
149
						/* eat up rest of line */
150
						fgets(buf, sizeof(buf), file);
150
						fgets(buf, sizeof(buf), file);
151
						break;
151
						break;
152
					}
152
					}
153
				break;
153
				break;
154
			case 'm': // Map ... all maps are treated equally.
154
			case 'm': // Map ... all maps are treated equally.
155
				{
155
				{
156
					//fgets(buf, sizeof(buf), file);
-
 
157
					fscanf(file,"%s",buf);
156
					fscanf(file,"%s",buf);
158
					string texname = pathname + string(buf);
-
 
159
					int tm_idx = mesh->find_texmap(texname);
157
					mesh->materials[nummaterials].tex_path = pathname;
160
					if(tm_idx == -1)
-
 
161
						{
-
 
162
							Texmap tm;
-
 
163
							if(tm.load(texname))
-
 
164
								{
-
 
165
									tm_idx = mesh->texmaps.size();
-
 
166
									mesh->texmaps.push_back(tm);
-
 
167
								}
-
 
168
						}
-
 
169
					mesh->materials[nummaterials].tex_id = tm_idx;
158
					mesh->materials[nummaterials].tex_name = string(buf);
170
				}
159
				}
171
				break;
160
				break;
172
			default:
161
			default:
173
				/* eat up rest of line */
162
				/* eat up rest of line */
174
				fgets(buf, sizeof(buf), file);
163
				fgets(buf, sizeof(buf), file);
175
				break;
164
				break;
176
			}
165
			}
177
		}
166
		}
178
	}
167
	}
179
 
168
 
180
 
169
 
181
	void TriMeshObjLoader::load(const std::string& filename) 
170
	void TriMeshObjLoader::load(const std::string& filename) 
182
	{
171
	{
183
		pathname = get_path(filename);
172
		pathname = get_path(filename);
184
		FILE *fp = fopen(filename.data(), "r");
173
		FILE *fp = fopen(filename.data(), "r");
185
		if (fp==0) {
174
		if (fp==0) {
186
			cerr << "File " << filename << " does not exist" << endl;
175
			cerr << "File " << filename << " does not exist" << endl;
187
		}
176
		}
188
		mesh->materials.resize(1);
177
		mesh->materials.resize(1);
189
		
178
		
190
		char buf[256];
179
		char buf[256];
191
		Vec3f v_geo;
180
		Vec3f v_geo;
192
		Vec3f v_normals;
181
		Vec3f v_normals;
193
		Vec3f v_texcoords;
182
		Vec3f v_texcoords;
194
 
183
 
195
		Vec3i f_geo;
184
		Vec3i f_geo;
196
		Vec3i f_normals;
185
		Vec3i f_normals;
197
		Vec3i f_texcoords;
186
		Vec3i f_texcoords;
198
		int current_material=0;
187
		int current_material=0;
199
		int v,n,t;
188
		int v,n,t;
200
		while(fscanf(fp, "%s", buf) != EOF) 
189
		while(fscanf(fp, "%s", buf) != EOF) 
201
			{
190
			{
202
				switch(buf[0]) 
191
				switch(buf[0]) 
203
					{
192
					{
204
					case '#': // A comment
193
					case '#': // A comment
205
						fgets(buf, sizeof(buf), fp);
194
						fgets(buf, sizeof(buf), fp);
206
						break;
195
						break;
207
					case 'm':
196
					case 'm':
208
						fgets(buf, sizeof(buf), fp);
197
						fgets(buf, sizeof(buf), fp);
209
						sscanf(buf, "%s %s", buf, buf);
198
						sscanf(buf, "%s %s", buf, buf);
210
						read_material_library(buf);
199
						read_material_library(buf);
211
						break;
200
						break;
212
					case 'u':
201
					case 'u':
213
						fgets(buf, sizeof(buf), fp);
202
						fgets(buf, sizeof(buf), fp);
214
						sscanf(buf, "%s %s", buf, buf);
203
						sscanf(buf, "%s %s", buf, buf);
215
						current_material = mesh->find_material(buf);
204
						current_material = mesh->find_material(buf);
216
						break;
205
						break;
217
					case 'v': // v, vn, vt
206
					case 'v': // v, vn, vt
218
						switch(buf[1]) 
207
						switch(buf[1]) 
219
							{
208
							{
220
							case '\0': // vertex
209
							case '\0': // vertex
221
								fscanf(fp, "%f %f %f", &v_geo[0], &v_geo[1], &v_geo[2]);
210
								fscanf(fp, "%f %f %f", &v_geo[0], &v_geo[1], &v_geo[2]);
222
								mesh->geometry.add_vertex(v_geo);
211
								mesh->geometry.add_vertex(v_geo);
223
								break;
212
								break;
224
							case 'n': // normal
213
							case 'n': // normal
225
								fscanf(fp, "%f %f %f", &v_normals[0], &v_normals[1], &v_normals[2]);
214
								fscanf(fp, "%f %f %f", &v_normals[0], &v_normals[1], &v_normals[2]);
226
								mesh->normals.add_vertex(v_normals);
215
								mesh->normals.add_vertex(v_normals);
227
								break;
216
								break;
228
							case 't': // texcoord
217
							case 't': // texcoord
229
								fscanf(fp, "%f %f", &v_texcoords[0], &v_texcoords[1]);
218
								fscanf(fp, "%f %f", &v_texcoords[0], &v_texcoords[1]);
230
								v_texcoords[2]=1;
219
								v_texcoords[2]=1;
231
								mesh->texcoords.add_vertex(v_texcoords);
220
								mesh->texcoords.add_vertex(v_texcoords);
232
								break;
221
								break;
233
							}
222
							}
234
						break;
223
						break;
235
					case 'f':
224
					case 'f':
236
						v = n = t = 0;
225
						v = n = t = 0;
237
						fscanf(fp, "%s", buf);
226
						fscanf(fp, "%s", buf);
238
						// can be one of %d, %d//%d, %d/%d, %d/%d/%d 
227
						// can be one of %d, %d//%d, %d/%d, %d/%d/%d 
239
						if(sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3)
228
						if(sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3)
240
							{ // v/t/n
229
							{ // v/t/n
241
								
230
								
242
								f_geo[0]=get_vert(v); 
231
								f_geo[0]=get_vert(v); 
243
								f_texcoords[0]=get_texcoord(t);
232
								f_texcoords[0]=get_texcoord(t);
244
								f_normals[0]=get_normal(n);
233
								f_normals[0]=get_normal(n);
245
 
234
 
246
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
235
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
247
								f_geo[1]=get_vert(v); 
236
								f_geo[1]=get_vert(v); 
248
								f_texcoords[1]=get_texcoord(t);
237
								f_texcoords[1]=get_texcoord(t);
249
								f_normals[1]=get_normal(n);
238
								f_normals[1]=get_normal(n);
250
 
239
 
251
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
240
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
252
								f_geo[2]=get_vert(v); 
241
								f_geo[2]=get_vert(v); 
253
								f_texcoords[2]=get_texcoord(t);
242
								f_texcoords[2]=get_texcoord(t);
254
								f_normals[2]=get_normal(n);
243
								f_normals[2]=get_normal(n);
255
 
244
 
256
								int idx = mesh->geometry.add_face(f_geo);
245
								int idx = mesh->geometry.add_face(f_geo);
257
								mesh->normals.add_face(f_normals, idx);
246
								mesh->normals.add_face(f_normals, idx);
258
								mesh->texcoords.add_face(f_texcoords, idx);
247
								mesh->texcoords.add_face(f_texcoords, idx);
259
								mesh->mat_idx.push_back(current_material);
248
								mesh->mat_idx.push_back(current_material);
260
 
249
 
261
								// Load a general polygon and convert to triangles
250
								// Load a general polygon and convert to triangles
262
								while(fscanf(fp, "%d/%d/%d", &v, &t, &n)==3) 
251
								while(fscanf(fp, "%d/%d/%d", &v, &t, &n)==3) 
263
									{
252
									{
264
										f_geo[1]=f_geo[2];
253
										f_geo[1]=f_geo[2];
265
										f_normals[1]=f_normals[2];
254
										f_normals[1]=f_normals[2];
266
										f_texcoords[1]=f_texcoords[2];
255
										f_texcoords[1]=f_texcoords[2];
267
 
256
 
268
										f_geo[2]=get_vert(v);
257
										f_geo[2]=get_vert(v);
269
										f_normals[2]=get_normal(n);
258
										f_normals[2]=get_normal(n);
270
										f_texcoords[2]=get_texcoord(t);
259
										f_texcoords[2]=get_texcoord(t);
271
 
260
 
272
										int idx = mesh->geometry.add_face(f_geo);
261
										int idx = mesh->geometry.add_face(f_geo);
273
										mesh->normals.add_face(f_normals, idx);
262
										mesh->normals.add_face(f_normals, idx);
274
										mesh->texcoords.add_face(f_texcoords, idx);
263
										mesh->texcoords.add_face(f_texcoords, idx);
275
										mesh->mat_idx.push_back(current_material);
264
										mesh->mat_idx.push_back(current_material);
276
									}
265
									}
277
							} 
266
							} 
278
						else if (sscanf(buf, "%d//%d", &v, &n)==2)
267
						else if (sscanf(buf, "%d//%d", &v, &n)==2)
279
							{// v//n 
268
							{// v//n 
280
								f_geo[0]=get_vert(v);
269
								f_geo[0]=get_vert(v);
281
								f_normals[0]=get_normal(n);
270
								f_normals[0]=get_normal(n);
282
 
271
 
283
								fscanf(fp, "%d//%d", &v, &n); 
272
								fscanf(fp, "%d//%d", &v, &n); 
284
								f_geo[1]=get_vert(v);
273
								f_geo[1]=get_vert(v);
285
								f_normals[1]=get_normal(n);
274
								f_normals[1]=get_normal(n);
286
								
275
								
287
								fscanf(fp, "%d//%d", &v, &n); 
276
								fscanf(fp, "%d//%d", &v, &n); 
288
								f_geo[2]=get_vert(v);
277
								f_geo[2]=get_vert(v);
289
								f_normals[2]=get_normal(n);
278
								f_normals[2]=get_normal(n);
290
								
279
								
291
								int idx = mesh->geometry.add_face(f_geo);
280
								int idx = mesh->geometry.add_face(f_geo);
292
								mesh->normals.add_face(f_normals, idx);
281
								mesh->normals.add_face(f_normals, idx);
293
								mesh->mat_idx.push_back(current_material);
282
								mesh->mat_idx.push_back(current_material);
294
								
283
								
295
								// Load a general polygon and convert to triangles
284
								// Load a general polygon and convert to triangles
296
								while(fscanf(fp, "%d//%d", &v, &n)==2) 
285
								while(fscanf(fp, "%d//%d", &v, &n)==2) 
297
									{
286
									{
298
										f_geo[1]=f_geo[2];
287
										f_geo[1]=f_geo[2];
299
										f_normals[1]=f_normals[2];
288
										f_normals[1]=f_normals[2];
300
										f_geo[2]=get_vert(v);
289
										f_geo[2]=get_vert(v);
301
										int idx = mesh->geometry.add_face(f_geo);
290
										int idx = mesh->geometry.add_face(f_geo);
302
										mesh->normals.add_face(f_normals, idx);
291
										mesh->normals.add_face(f_normals, idx);
303
										mesh->mat_idx.push_back(current_material);
292
										mesh->mat_idx.push_back(current_material);
304
									}
293
									}
305
							} 
294
							} 
306
						else if (sscanf(buf, "%d/%d", &v, &t) == 2)
295
						else if (sscanf(buf, "%d/%d", &v, &t) == 2)
307
							{ // v/t 
296
							{ // v/t 
308
								f_geo[0]=get_vert(v);
297
								f_geo[0]=get_vert(v);
309
								f_texcoords[0]=get_texcoord(t);
298
								f_texcoords[0]=get_texcoord(t);
310
 
299
 
311
								fscanf(fp, "%d/%d", &v, &t); 
300
								fscanf(fp, "%d/%d", &v, &t); 
312
								f_geo[1]=get_vert(v);
301
								f_geo[1]=get_vert(v);
313
								f_texcoords[1]=get_texcoord(t);
302
								f_texcoords[1]=get_texcoord(t);
314
 
303
 
315
								fscanf(fp, "%d/%d", &v, &t); 
304
								fscanf(fp, "%d/%d", &v, &t); 
316
								f_geo[2]=get_vert(v);
305
								f_geo[2]=get_vert(v);
317
								f_texcoords[2]=get_texcoord(t);
306
								f_texcoords[2]=get_texcoord(t);
318
 
307
 
319
								int idx = mesh->geometry.add_face(f_geo);
308
								int idx = mesh->geometry.add_face(f_geo);
320
								mesh->texcoords.add_face(f_texcoords, idx);
309
								mesh->texcoords.add_face(f_texcoords, idx);
321
								mesh->mat_idx.push_back(current_material);
310
								mesh->mat_idx.push_back(current_material);
322
						
311
						
323
								// Load a general polygon and convert to triangles
312
								// Load a general polygon and convert to triangles
324
								while(fscanf(fp, "%d/%d", &v, &t)==2) 
313
								while(fscanf(fp, "%d/%d", &v, &t)==2) 
325
									{
314
									{
326
										f_geo[1]=f_geo[2];
315
										f_geo[1]=f_geo[2];
327
										f_texcoords[1]=f_texcoords[2];
316
										f_texcoords[1]=f_texcoords[2];
328
								
317
								
329
										f_geo[2]=get_vert(v);
318
										f_geo[2]=get_vert(v);
330
										f_texcoords[2]=get_texcoord(t);
319
										f_texcoords[2]=get_texcoord(t);
331
 
320
 
332
										int idx = mesh->geometry.add_face(f_geo);
321
										int idx = mesh->geometry.add_face(f_geo);
333
										mesh->texcoords.add_face(f_texcoords, idx);
322
										mesh->texcoords.add_face(f_texcoords, idx);
334
										mesh->mat_idx.push_back(current_material);
323
										mesh->mat_idx.push_back(current_material);
335
									}
324
									}
336
							} 
325
							} 
337
						else if (sscanf(buf, "%d", &v)==1)
326
						else if (sscanf(buf, "%d", &v)==1)
338
							{ // v 
327
							{ // v 
339
								f_geo[0]=get_vert(v);
328
								f_geo[0]=get_vert(v);
340
								
329
								
341
								fscanf(fp, "%d", &v);
330
								fscanf(fp, "%d", &v);
342
								f_geo[1]=get_vert(v);
331
								f_geo[1]=get_vert(v);
343
								
332
								
344
								fscanf(fp, "%d", &v);
333
								fscanf(fp, "%d", &v);
345
								f_geo[2]=get_vert(v);
334
								f_geo[2]=get_vert(v);
346
								
335
								
347
								mesh->geometry.add_face(f_geo);
336
								mesh->geometry.add_face(f_geo);
348
								mesh->mat_idx.push_back(current_material);
337
								mesh->mat_idx.push_back(current_material);
349
 
338
 
350
								// Load a general polygon and convert to triangles
339
								// Load a general polygon and convert to triangles
351
								while(fscanf(fp, "%d", &v)==1) 
340
								while(fscanf(fp, "%d", &v)==1) 
352
									{
341
									{
353
										f_geo[1]=f_geo[2];
342
										f_geo[1]=f_geo[2];
354
										f_geo[2]=get_vert(v);
343
										f_geo[2]=get_vert(v);
355
										mesh->geometry.add_face(f_geo);
344
										mesh->geometry.add_face(f_geo);
356
										mesh->mat_idx.push_back(current_material);
345
										mesh->mat_idx.push_back(current_material);
357
									}
346
									}
358
							}
347
							}
359
						break;
348
						break;
360
					default:
349
					default:
361
						fgets(buf, sizeof(buf), fp);
350
						fgets(buf, sizeof(buf), fp);
362
						break;
351
						break;
363
					}
352
					}
364
			}
353
			}
365
	}
354
	}
366
 
355
 
367
 
356
 
368
	void obj_load(const string& filename, TriMesh &mesh)
357
	void obj_load(const string& filename, TriMesh &mesh)
369
	{
358
	{
370
		TriMeshObjLoader loader(&mesh);
359
		TriMeshObjLoader loader(&mesh);
371
		loader.load(filename);
360
		loader.load(filename);
372
	}
361
	}
373
 
362
 
374
}
363
}
375
 
364