/
scene_parser.C
524 lines (470 loc) · 15.5 KB
/
scene_parser.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include <stdio.h>
#include <string.h>
#include "scene_parser.h"
#include "matrix.h"
#include "camera.h"
#include "light.h"
#include "material.h"
#include "object3d.h"
#include "group.h"
#include "sphere.h"
#include "plane.h"
#include "triangle.h"
#include "transform.h"
#define DegreesToRadians(x) ((M_PI * x) / 180.0f)
// ====================================================================
// ====================================================================
// CONSTRUCTOR & DESTRUCTOR
SceneParser::SceneParser(const char* filename) {
// initialize some reasonable default values
group = NULL;
camera = NULL;
background_color = Vec3f(0.5,0.5,0.5);
ambient_light = Vec3f(0,0,0);
num_lights = 0;
lights = NULL;
num_materials = 0;
materials = NULL;
current_material = NULL;
// parse the file
assert(filename != NULL);
const char *ext = &filename[strlen(filename)-4];
assert(!strcmp(ext,".txt"));
file = fopen(filename,"r");
assert (file != NULL);
parseFile();
fclose(file);
file = NULL;
// if no lights are specified, set ambient light to white
// (do solid color ray casting)
if (num_lights == 0) {
printf ("WARNING: No lights specified\n");
ambient_light = Vec3f(1,1,1);
}
}
SceneParser::~SceneParser() {
if (group != NULL)
delete group;
if (camera != NULL)
delete camera;
int i;
for (i = 0; i < num_materials; i++) {
delete materials[i]; }
delete [] materials;
for (i = 0; i < num_lights; i++) {
delete lights[i]; }
delete [] lights;
}
// ====================================================================
// ====================================================================
void SceneParser::parseFile() {
//
// at the top level, the scene can have a camera,
// background color and a group of objects
// (we add lights and other things in future assignments)
//
char token[MAX_PARSER_TOKEN_LENGTH];
while (getToken(token)) {
if (!strcmp(token, "OrthographicCamera")) {
parseOrthographicCamera();
} else if (!strcmp(token, "PerspectiveCamera")) {
parsePerspectiveCamera();
} else if (!strcmp(token, "Background")) {
parseBackground();
} else if (!strcmp(token, "Lights")) {
parseLights();
} else if (!strcmp(token, "Materials")) {
parseMaterials();
} else if (!strcmp(token, "Group")) {
group = parseGroup();
} else {
printf ("Unknown token in parseFile: '%s'\n", token);
exit(0);
}
}
}
// ====================================================================
// ====================================================================
void SceneParser::parseOrthographicCamera() {
char token[MAX_PARSER_TOKEN_LENGTH];
// read in the camera parameters
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "center"));
Vec3f center = readVec3f();
getToken(token); assert (!strcmp(token, "direction"));
Vec3f direction = readVec3f();
getToken(token); assert (!strcmp(token, "up"));
Vec3f up = readVec3f();
getToken(token); assert (!strcmp(token, "size"));
float size = readFloat();
getToken(token); assert (!strcmp(token, "}"));
camera = new OrthographicCamera(center,direction,up,size);
}
void SceneParser::parsePerspectiveCamera() {
char token[MAX_PARSER_TOKEN_LENGTH];
// read in the camera parameters
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "center"));
Vec3f center = readVec3f();
getToken(token); assert (!strcmp(token, "direction"));
Vec3f direction = readVec3f();
getToken(token); assert (!strcmp(token, "up"));
Vec3f up = readVec3f();
getToken(token); assert (!strcmp(token, "angle"));
float angle_degrees = readFloat();
float angle_radians = DegreesToRadians(angle_degrees);
getToken(token); assert (!strcmp(token, "}"));
camera = new PerspectiveCamera(center,direction,up,angle_radians);
}
void SceneParser::parseBackground() {
char token[MAX_PARSER_TOKEN_LENGTH];
// read in the background color
getToken(token); assert (!strcmp(token, "{"));
while (1) {
getToken(token);
if (!strcmp(token, "}")) {
break;
} else if (!strcmp(token, "color")) {
background_color = readVec3f();
} else if (!strcmp(token, "ambientLight")) {
ambient_light = readVec3f();
} else {
printf ("Unknown token in parseBackground: '%s'\n", token);
assert(0);
}
}
}
// ====================================================================
// ====================================================================
void SceneParser::parseLights() {
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
// read in the number of objects
getToken(token); assert (!strcmp(token, "numLights"));
num_lights = readInt();
lights = new Light*[num_lights];
// read in the objects
int count = 0;
while (num_lights > count) {
getToken(token);
if (!strcmp(token, "DirectionalLight")) {
lights[count] = parseDirectionalLight();
} else {
printf ("Unknown token in parseLight: '%s'\n", token);
exit(0);
}
count++;
}
getToken(token); assert (!strcmp(token, "}"));
}
Light* SceneParser::parseDirectionalLight() {
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "direction"));
Vec3f direction = readVec3f();
getToken(token); assert (!strcmp(token, "color"));
Vec3f color = readVec3f();
getToken(token); assert (!strcmp(token, "}"));
return new DirectionalLight(direction,color);
}
// ====================================================================
// ====================================================================
void SceneParser::parseMaterials() {
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
// read in the number of objects
getToken(token); assert (!strcmp(token, "numMaterials"));
num_materials = readInt();
materials = new Material*[num_materials];
// read in the objects
int count = 0;
while (num_materials > count) {
getToken(token);
if (!strcmp(token, "Material")) {
materials[count] = parseMaterial();
} else {
printf ("Unknown token in parseMaterial: '%s'\n", token);
exit(0);
}
count++;
}
getToken(token); assert (!strcmp(token, "}"));
}
Material* SceneParser::parseMaterial() {
char token[MAX_PARSER_TOKEN_LENGTH];
Vec3f diffuseColor(1,1,1);
getToken(token); assert (!strcmp(token, "{"));
while (1) {
getToken(token);
if (!strcmp(token, "diffuseColor")) {
diffuseColor = readVec3f();
} else {
assert (!strcmp(token, "}"));
break;
}
}
Material *answer = new Material(diffuseColor);
return answer;
}
// ====================================================================
// ====================================================================
Object3D* SceneParser::parseObject(char token[MAX_PARSER_TOKEN_LENGTH]) {
Object3D *answer = NULL;
if (!strcmp(token, "Group")) {
answer = (Object3D*)parseGroup();
} else if (!strcmp(token, "Sphere")) {
answer = (Object3D*)parseSphere();
} else if (!strcmp(token, "Plane")) {
answer = (Object3D*)parsePlane();
} else if (!strcmp(token, "Triangle")) {
answer = (Object3D*)parseTriangle();
} else if (!strcmp(token, "TriangleMesh")) {
answer = (Object3D*)parseTriangleMesh();
} else if (!strcmp(token, "Transform")) {
answer = (Object3D*)parseTransform();
} else {
printf ("Unknown token in parseObject: '%s'\n", token);
exit(0);
}
return answer;
}
// ====================================================================
// ====================================================================
Group* SceneParser::parseGroup() {
//
// each group starts with an integer that specifies
// the number of objects in the group
//
// the material index sets the material of all objects which follow,
// until the next material index (scoping for the materials is very
// simple, and essentially ignores any tree hierarchy)
//
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
// read in the number of objects
getToken(token); assert (!strcmp(token, "numObjects"));
int num_objects = readInt();
Group *answer = new Group(num_objects);
// read in the objects
int count = 0;
while (num_objects > count) {
getToken(token);
if (!strcmp(token, "MaterialIndex")) {
// change the current material
int index = readInt();
assert (index >= 0 && index <= getNumMaterials());
current_material = getMaterial(index);
} else {
Object3D *object = parseObject(token);
assert (object != NULL);
answer->addObject(count,object);
count++;
}
}
getToken(token); assert (!strcmp(token, "}"));
// return the group
return answer;
}
// ====================================================================
// ====================================================================
Sphere* SceneParser::parseSphere() {
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "center"));
Vec3f center = readVec3f();
getToken(token); assert (!strcmp(token, "radius"));
float radius = readFloat();
getToken(token); assert (!strcmp(token, "}"));
assert (current_material != NULL);
return new Sphere(center,radius,current_material);
}
Plane* SceneParser::parsePlane() {
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "normal"));
Vec3f normal = readVec3f();
getToken(token); assert (!strcmp(token, "offset"));
float offset = readFloat();
getToken(token); assert (!strcmp(token, "}"));
assert (current_material != NULL);
return new Plane(normal,offset,current_material);
}
Triangle* SceneParser::parseTriangle() {
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token); assert (!strcmp(token, "{"));
getToken(token);
assert (!strcmp(token, "vertex0"));
Vec3f v0 = readVec3f();
getToken(token);
assert (!strcmp(token, "vertex1"));
Vec3f v1 = readVec3f();
getToken(token);
assert (!strcmp(token, "vertex2"));
Vec3f v2 = readVec3f();
getToken(token); assert (!strcmp(token, "}"));
assert (current_material != NULL);
return new Triangle(v0,v1,v2,current_material);
}
Group* SceneParser::parseTriangleMesh() {
char token[MAX_PARSER_TOKEN_LENGTH];
char filename[MAX_PARSER_TOKEN_LENGTH];
// get the filename
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "obj_file"));
getToken(filename);
getToken(token); assert (!strcmp(token, "}"));
const char *ext = &filename[strlen(filename)-4];
assert(!strcmp(ext,".obj"));
// read it once, get counts
FILE *file = fopen(filename,"r");
assert (file != NULL);
int vcount = 0; int fcount = 0;
while (1) {
int c = fgetc(file);
if (c == EOF) { break;
} else if (c == 'v') {
assert(fcount == 0); float v0,v1,v2;
fscanf (file,"%f %f %f",&v0,&v1,&v2);
vcount++;
} else if (c == 'f') {
int f0,f1,f2;
fscanf (file,"%d %d %d",&f0,&f1,&f2);
fcount++;
} // otherwise, must be whitespace
}
fclose(file);
// make arrays
Vec3f *verts = new Vec3f[vcount];
Group *answer = new Group(fcount);
// read it again, save it
file = fopen(filename,"r");
assert (file != NULL);
int new_vcount = 0; int new_fcount = 0;
while (1) {
int c = fgetc(file);
if (c == EOF) { break;
} else if (c == 'v') {
assert(new_fcount == 0); float v0,v1,v2;
fscanf (file,"%f %f %f",&v0,&v1,&v2);
verts[new_vcount] = Vec3f(v0,v1,v2);
new_vcount++;
} else if (c == 'f') {
assert (vcount == new_vcount);
int f0,f1,f2;
fscanf (file,"%d %d %d",&f0,&f1,&f2);
// indexed starting at 1...
assert (f0 > 0 && f0 <= vcount);
assert (f1 > 0 && f1 <= vcount);
assert (f2 > 0 && f2 <= vcount);
assert (current_material != NULL);
Triangle *t = new Triangle(verts[f0-1],verts[f1-1],verts[f2-1],current_material);
answer->addObject(new_fcount,t);
new_fcount++;
} // otherwise, must be whitespace
}
delete [] verts;
assert (fcount == new_fcount);
assert (vcount == new_vcount);
fclose(file);
return answer;
}
Transform* SceneParser::parseTransform() {
char token[MAX_PARSER_TOKEN_LENGTH];
Matrix matrix; matrix.SetToIdentity();
Object3D *object = NULL;
getToken(token); assert (!strcmp(token, "{"));
// read in transformations:
// apply to the LEFT side of the current matrix (so the first
// transform in the list is the last applied to the object)
getToken(token);
while (1) {
if (!strcmp(token,"Scale")) {
matrix *= Matrix::MakeScale(readVec3f());
} else if (!strcmp(token,"UniformScale")) {
float s = readFloat();
matrix *= Matrix::MakeScale(Vec3f(s,s,s));
} else if (!strcmp(token,"Translate")) {
matrix *= Matrix::MakeTranslation(readVec3f());
} else if (!strcmp(token,"XRotate")) {
matrix *= Matrix::MakeXRotation(DegreesToRadians(readFloat()));
} else if (!strcmp(token,"YRotate")) {
matrix *= Matrix::MakeYRotation(DegreesToRadians(readFloat()));
} else if (!strcmp(token,"ZRotate")) {
matrix *= Matrix::MakeZRotation(DegreesToRadians(readFloat()));
} else if (!strcmp(token,"Rotate")) {
getToken(token); assert (!strcmp(token, "{"));
Vec3f axis = readVec3f();
float degrees = readFloat();
matrix *= Matrix::MakeAxisRotation(axis,DegreesToRadians(degrees));
getToken(token); assert (!strcmp(token, "}"));
} else if (!strcmp(token,"Matrix")) {
Matrix matrix2; matrix2.SetToIdentity();
getToken(token); assert (!strcmp(token, "{"));
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
float v = readFloat();
matrix2.Set(i,j,v); } }
getToken(token); assert (!strcmp(token, "}"));
matrix = matrix2 * matrix;
} else {
// otherwise this must be an object,
// and there are no more transformations
object = parseObject(token);
break;
}
getToken(token);
}
assert(object != NULL);
getToken(token); assert (!strcmp(token, "}"));
return new Transform(matrix, object);
}
// ====================================================================
// ====================================================================
int SceneParser::getToken(char token[MAX_PARSER_TOKEN_LENGTH]) {
// for simplicity, tokens must be separated by whitespace
assert (file != NULL);
int success = fscanf(file,"%s ",token);
if (success == EOF) {
token[0] = '\0';
return 0;
}
return 1;
}
Vec3f SceneParser::readVec3f() {
float x,y,z;
int count = fscanf(file,"%f %f %f",&x,&y,&z);
if (count != 3) {
printf ("Error trying to read 3 floats to make a Vec3f\n");
assert (0);
}
return Vec3f(x,y,z);
}
Vec2f SceneParser::readVec2f() {
float u,v;
int count = fscanf(file,"%f %f",&u,&v);
if (count != 2) {
printf ("Error trying to read 2 floats to make a Vec2f\n");
assert (0);
}
return Vec2f(u,v);
}
float SceneParser::readFloat() {
float answer;
int count = fscanf(file,"%f",&answer);
if (count != 1) {
printf ("Error trying to read 1 float\n");
assert (0);
}
return answer;
}
int SceneParser::readInt() {
int answer;
int count = fscanf(file,"%d",&answer);
if (count != 1) {
printf ("Error trying to read 1 int\n");
assert (0);
}
return answer;
}
// ====================================================================
// ====================================================================