Subversion Repositories gelsvn

Rev

Rev 178 | Rev 421 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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