Subversion Repositories gelsvn

Rev

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