-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSceneParser.cpp
More file actions
597 lines (541 loc) · 17.8 KB
/
SceneParser.cpp
File metadata and controls
597 lines (541 loc) · 17.8 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define _USE_MATH_DEFINES
#include <math.h>
#include "SceneParser.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)
SceneParser::SceneParser(const char* filename) {
// initialize some reasonable default values
group = NULL;
camera = NULL;
background_color = Vector3f(0.5,0.5,0.5);
ambient_light = Vector3f(0,0,0);
num_lights = 0;
lights = NULL;
num_materials = 0;
materials = NULL;
current_material = NULL;
cubemap = 0;
// parse the file
assert(filename != NULL);
const char *ext = &filename[strlen(filename)-4];
if(strcmp(ext,".txt")!=0){
printf("wrong file name extension\n");
exit(0);
}
file = fopen(filename,"r");
if (file == NULL){
printf("cannot open scene file\n");
exit(0);
}
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 = Vector3f(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, "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::parsePerspectiveCamera() {
char token[MAX_PARSER_TOKEN_LENGTH];
// read in the camera parameters
getToken(token); assert (!strcmp(token, "{"));
getToken(token); assert (!strcmp(token, "center"));
Vector3f center = readVector3f();
getToken(token); assert (!strcmp(token, "direction"));
Vector3f direction = readVector3f();
getToken(token); assert (!strcmp(token, "up"));
Vector3f up = readVector3f();
getToken(token); assert (!strcmp(token, "angle"));
float angle_degrees = readFloat();
float angle_radians = DegreesToRadians(angle_degrees);
getToken(token);
float focal_length = 1;
if (!strcmp(token, "focus"))
{
focal_length = readFloat();
getToken(token);
}
float fstop = 25;
if (!strcmp(token, "fstop"))
{
fstop = readFloat();
getToken(token);
}
int samples = 1;
if (!strcmp(token, "samples"))
{
samples = readInt();
getToken(token);
}
assert (!strcmp(token, "}"));
camera = new PerspectiveCamera(center,direction,up,angle_radians, focal_length, fstop, samples);
}
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 = readVector3f();
} else if (!strcmp(token, "ambientLight")) {
ambient_light = readVector3f();
} else if(strcmp(token,"cubeMap")==0){
cubemap = parseCubeMap();
}else {
printf ("Unknown token in parseBackground: '%s'\n", token);
assert(0);
}
}
}
CubeMap * SceneParser::parseCubeMap()
{
char token[MAX_PARSER_TOKEN_LENGTH];
getToken(token);
return new CubeMap(token);
}
// ====================================================================
// ====================================================================
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 if(strcmp(token, "PointLight")==0)
{
lights[count] = parsePointLight();
}
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"));
Vector3f direction = readVector3f();
getToken(token); assert (!strcmp(token, "color"));
Vector3f color = readVector3f();
getToken(token);
int samples = 1;
if (!strcmp(token, "samples"))
{
samples = readInt();
getToken(token);
}
assert (!strcmp(token, "}"));
return new DirectionalLight(direction,color, samples);
}
Light* SceneParser::parsePointLight() {
char token[MAX_PARSER_TOKEN_LENGTH];
Vector3f position,color;
float falloff =0;
getToken(token); assert (!strcmp(token, "{"));
while (1) {
getToken(token);
if (strcmp(token, "position")==0) {
position = readVector3f();
}else if (strcmp(token, "color")==0) {
color = readVector3f();
}else if(strcmp(token,"falloff")==0){
falloff = readFloat();
}else{
assert (!strcmp(token, "}"));
break;
}
}
return new PointLight(position,color,falloff);
}
// ====================================================================
// ====================================================================
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") ||
!strcmp(token, "PhongMaterial")) {
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];
char filename[MAX_PARSER_TOKEN_LENGTH];
filename[0] = 0;
Vector3f diffuseColor(1,1,1), specularColor(0,0,0);
float shininess=0;
float refractionIndex =0;
int roughness = 0;
getToken(token); assert (!strcmp(token, "{"));
Noise *noise =0;
while (1) {
getToken(token);
if (strcmp(token, "diffuseColor")==0) {
diffuseColor = readVector3f();
}
else if (strcmp(token, "specularColor")==0) {
specularColor = readVector3f();
}
else if (strcmp(token, "shininess")==0) {
shininess = readFloat();
}else if(strcmp(token, "refractionIndex")==0){
refractionIndex = readFloat();
}
else if (strcmp(token, "texture")==0) {
getToken(filename);
}
else if (strcmp(token, "roughness") == 0) {
roughness = readInt();
}
///unimplemented
else if (strcmp(token, "bump")==0) {
getToken(token);
}
else if(strcmp(token,"Noise")==0){
noise = parseNoise();
}
else {
assert (!strcmp(token, "}"));
break;
}
}
Material *answer = new Material(diffuseColor, specularColor, shininess,refractionIndex, roughness);
if(filename[0] !=0){
answer->loadTexture(filename);
}
if(noise != 0){
answer->setNoise(*noise);
delete noise;
}
return answer;
}
Noise * SceneParser::parseNoise()
{
char token[MAX_PARSER_TOKEN_LENGTH];
Vector3f color[2];
int colorIdx = 0;
int octaves=0;
float frequency = 1;
float amplitude = 1;
getToken(token); assert (!strcmp(token, "{"));
Noise *noise =0;
while (1) {
getToken(token);
if (strcmp(token, "color")==0) {
if(colorIdx > 1){
printf("Error parsing noise\n");
}else{
color[colorIdx]= readVector3f();
colorIdx++;
}
}
else if (strcmp(token, "octaves")==0) {
octaves= readInt();
}
else if (strcmp(token, "frequency")==0) {
frequency= readFloat();
}
else if (strcmp(token, "amplitude")==0) {
amplitude= readFloat();
}
else {
assert (!strcmp(token, "}"));
break;
}
}
return new Noise(octaves, color[0],color[1],frequency,amplitude);
}
// ====================================================================
// ====================================================================
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"));
Vector3f center = readVector3f();
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"));
Vector3f normal = readVector3f();
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"));
Vector3f v0 = readVector3f();
getToken(token);
assert (!strcmp(token, "vertex1"));
Vector3f v1 = readVector3f();
getToken(token);
assert (!strcmp(token, "vertex2"));
Vector3f v2 = readVector3f();
getToken(token); assert (!strcmp(token, "}"));
assert (current_material != NULL);
return new Triangle(v0,v1,v2,current_material);
}
Mesh* 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"));
Mesh *answer = new Mesh(filename,current_material);
return answer;
}
Transform* SceneParser::parseTransform() {
char token[MAX_PARSER_TOKEN_LENGTH];
Matrix4f matrix = Matrix4f::identity();
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")) {
Vector3f s = readVector3f();
matrix = matrix * Matrix4f::scaling( s[0], s[1], s[2] );
} else if (!strcmp(token,"UniformScale")) {
float s = readFloat();
matrix = matrix * Matrix4f::uniformScaling( s );
} else if (!strcmp(token,"Translate")) {
matrix = matrix * Matrix4f::translation( readVector3f() );
} else if (!strcmp(token,"XRotate")) {
matrix = matrix * Matrix4f::rotateX(DegreesToRadians(readFloat()));
} else if (!strcmp(token,"YRotate")) {
matrix = matrix * Matrix4f::rotateY(DegreesToRadians(readFloat()));
} else if (!strcmp(token,"ZRotate")) {
matrix = matrix * Matrix4f::rotateZ(DegreesToRadians(readFloat()));
} else if (!strcmp(token,"Rotate")) {
getToken(token); assert (!strcmp(token, "{"));
Vector3f axis = readVector3f();
float degrees = readFloat();
float radians = DegreesToRadians(degrees);
matrix = matrix * Matrix4f::rotation(axis,radians);
getToken(token); assert (!strcmp(token, "}"));
} else if (!strcmp(token,"Matrix4f")) {
Matrix4f matrix2 = Matrix4f::identity();
getToken(token); assert (!strcmp(token, "{"));
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
float v = readFloat();
matrix2( 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;
}
Vector3f SceneParser::readVector3f() {
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 Vector3f\n");
assert (0);
}
return Vector3f(x,y,z);
}
Vector2f 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 Vector2f(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;
}