Subversion Repositories gelsvn

Rev

Rev 145 | 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);
156 jab 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;
176
		}
177
		mesh->materials.resize(1);
178
 
179
		char buf[256];
180
		Vec3f v_geo;
181
		Vec3f v_normals;
182
		Vec3f v_texcoords;
183
 
184
		Vec3i f_geo;
185
		Vec3i f_normals;
186
		Vec3i f_texcoords;
187
		int current_material=0;
188
		int v,n,t;
189
		while(fscanf(fp, "%s", buf) != EOF) 
190
			{
191
				switch(buf[0]) 
192
					{
193
					case '#': // A comment
194
						fgets(buf, sizeof(buf), fp);
195
						break;
196
					case 'm':
197
						fgets(buf, sizeof(buf), fp);
198
						sscanf(buf, "%s %s", buf, buf);
199
						read_material_library(buf);
200
						break;
201
					case 'u':
202
						fgets(buf, sizeof(buf), fp);
203
						sscanf(buf, "%s %s", buf, buf);
204
						current_material = mesh->find_material(buf);
205
						break;
206
					case 'v': // v, vn, vt
207
						switch(buf[1]) 
208
							{
209
							case '\0': // vertex
210
								fscanf(fp, "%f %f %f", &v_geo[0], &v_geo[1], &v_geo[2]);
211
								mesh->geometry.add_vertex(v_geo);
212
								break;
213
							case 'n': // normal
214
								fscanf(fp, "%f %f %f", &v_normals[0], &v_normals[1], &v_normals[2]);
215
								mesh->normals.add_vertex(v_normals);
216
								break;
217
							case 't': // texcoord
218
								fscanf(fp, "%f %f", &v_texcoords[0], &v_texcoords[1]);
219
								v_texcoords[2]=1;
220
								mesh->texcoords.add_vertex(v_texcoords);
221
								break;
222
							}
223
						break;
224
					case 'f':
225
						v = n = t = 0;
226
						fscanf(fp, "%s", buf);
227
						// can be one of %d, %d//%d, %d/%d, %d/%d/%d 
228
						if(sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3)
229
							{ // v/t/n
230
 
231
								f_geo[0]=get_vert(v); 
232
								f_texcoords[0]=get_texcoord(t);
233
								f_normals[0]=get_normal(n);
234
 
235
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
236
								f_geo[1]=get_vert(v); 
237
								f_texcoords[1]=get_texcoord(t);
238
								f_normals[1]=get_normal(n);
239
 
240
								fscanf(fp, "%d/%d/%d", &v, &t, &n); 
241
								f_geo[2]=get_vert(v); 
242
								f_texcoords[2]=get_texcoord(t);
243
								f_normals[2]=get_normal(n);
244
 
245
								int idx = mesh->geometry.add_face(f_geo);
246
								mesh->normals.add_face(f_normals, idx);
247
								mesh->texcoords.add_face(f_texcoords, idx);
248
								mesh->mat_idx.push_back(current_material);
249
 
250
								// Load a general polygon and convert to triangles
251
								while(fscanf(fp, "%d/%d/%d", &v, &t, &n)==3) 
252
									{
253
										f_geo[1]=f_geo[2];
254
										f_normals[1]=f_normals[2];
255
										f_texcoords[1]=f_texcoords[2];
256
 
257
										f_geo[2]=get_vert(v);
258
										f_normals[2]=get_normal(n);
259
										f_texcoords[2]=get_texcoord(t);
260
 
261
										int idx = mesh->geometry.add_face(f_geo);
262
										mesh->normals.add_face(f_normals, idx);
263
										mesh->texcoords.add_face(f_texcoords, idx);
264
										mesh->mat_idx.push_back(current_material);
265
									}
266
							} 
267
						else if (sscanf(buf, "%d//%d", &v, &n)==2)
268
							{// v//n 
269
								f_geo[0]=get_vert(v);
270
								f_normals[0]=get_normal(n);
271
 
272
								fscanf(fp, "%d//%d", &v, &n); 
273
								f_geo[1]=get_vert(v);
274
								f_normals[1]=get_normal(n);
275
 
276
								fscanf(fp, "%d//%d", &v, &n); 
277
								f_geo[2]=get_vert(v);
278
								f_normals[2]=get_normal(n);
279
 
280
								int idx = mesh->geometry.add_face(f_geo);
281
								mesh->normals.add_face(f_normals, idx);
282
								mesh->mat_idx.push_back(current_material);
283
 
284
								// Load a general polygon and convert to triangles
285
								while(fscanf(fp, "%d//%d", &v, &n)==2) 
286
									{
287
										f_geo[1]=f_geo[2];
288
										f_normals[1]=f_normals[2];
289
										f_geo[2]=get_vert(v);
290
										int idx = mesh->geometry.add_face(f_geo);
291
										mesh->normals.add_face(f_normals, idx);
292
										mesh->mat_idx.push_back(current_material);
293
									}
294
							} 
295
						else if (sscanf(buf, "%d/%d", &v, &t) == 2)
296
							{ // v/t 
297
								f_geo[0]=get_vert(v);
298
								f_texcoords[0]=get_texcoord(t);
299
 
300
								fscanf(fp, "%d/%d", &v, &t); 
301
								f_geo[1]=get_vert(v);
302
								f_texcoords[1]=get_texcoord(t);
303
 
304
								fscanf(fp, "%d/%d", &v, &t); 
305
								f_geo[2]=get_vert(v);
306
								f_texcoords[2]=get_texcoord(t);
307
 
308
								int idx = mesh->geometry.add_face(f_geo);
309
								mesh->texcoords.add_face(f_texcoords, idx);
310
								mesh->mat_idx.push_back(current_material);
311
 
312
								// Load a general polygon and convert to triangles
313
								while(fscanf(fp, "%d/%d", &v, &t)==2) 
314
									{
315
										f_geo[1]=f_geo[2];
316
										f_texcoords[1]=f_texcoords[2];
317
 
318
										f_geo[2]=get_vert(v);
319
										f_texcoords[2]=get_texcoord(t);
320
 
321
										int idx = mesh->geometry.add_face(f_geo);
322
										mesh->texcoords.add_face(f_texcoords, idx);
323
										mesh->mat_idx.push_back(current_material);
324
									}
325
							} 
326
						else if (sscanf(buf, "%d", &v)==1)
327
							{ // v 
328
								f_geo[0]=get_vert(v);
329
 
330
								fscanf(fp, "%d", &v);
331
								f_geo[1]=get_vert(v);
332
 
333
								fscanf(fp, "%d", &v);
334
								f_geo[2]=get_vert(v);
335
 
336
								mesh->geometry.add_face(f_geo);
337
								mesh->mat_idx.push_back(current_material);
338
 
339
								// Load a general polygon and convert to triangles
340
								while(fscanf(fp, "%d", &v)==1) 
341
									{
342
										f_geo[1]=f_geo[2];
343
										f_geo[2]=get_vert(v);
344
										mesh->geometry.add_face(f_geo);
345
										mesh->mat_idx.push_back(current_material);
346
									}
347
							}
348
						break;
349
					default:
350
						fgets(buf, sizeof(buf), fp);
351
						break;
352
					}
353
			}
354
	}
355
 
356
 
357
	void obj_load(const string& filename, TriMesh &mesh)
358
	{
359
		TriMeshObjLoader loader(&mesh);
360
		loader.load(filename);
361
	}
362
 
363
}