Subversion Repositories gelsvn

Rev

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