-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.h
436 lines (400 loc) · 18.7 KB
/
parsing.h
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
#ifndef __PARSING_H__
#define __PARSING_H__
#include <vector>
#include <iostream>
#include <fstream>
#include <cmath>
#include <time.h>
#include <math.h>
#include <Eigen/Dense>
#include "util.h"
using Eigen::Affine;
using Eigen::Vector3f;
using Eigen::Quaternionf;
using Eigen::Transform;
using Eigen::Translation;
using Eigen::AngleAxisf;
using namespace std;
bool parseObjFile(string objFileName, vector<Object*>& objects, const Transform<float, 3, Affine>& currentTransform, const Material& currentMaterial, bool flipNormals = false)
{
ifstream objFile;
objFile.open(objFileName + string(".obj"), ios::binary | ios::in);
if (!objFile.is_open()) {
cout << "Error: could not open \"" << objFileName << ".obj\". Make sure the file exists." << endl;
return false;
}
vector<Vector3f> vertices;
vector<Vector3f> normals;
int lineNo = 0;
string currentLine;
while (!objFile.eof()) {
getline(objFile, currentLine);
lineNo++;
trim(currentLine);
if (!currentLine.size())
continue;
if (currentLine[0] == '#')
continue;
vector<string> tokens = split(currentLine);
string identifier = tokens[0];
tokens.erase(tokens.begin());
if (identifier == "v") {
float x, y, z;
if (tokens.size() < 3) {
printf("Warning: not enough parameters for vertex on line %d of %s.obj.\n", lineNo, objFileName.c_str());
continue;
}
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse vertex on line %d of %s.obj.\n", lineNo, objFileName.c_str());
continue;
}
vertices.push_back(Vector3f(x, y, z));
} else if (identifier == "vn") {
float x, y, z;
if (tokens.size() < 3) {
printf("Warning: not enough parameters for vertex normal on line %d of %s.obj.\n", lineNo, objFileName.c_str());
continue;
}
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse vertex normal on line %d of %s.obj.\n", lineNo, objFileName.c_str());
continue;
}
normals.push_back(Vector3f(x, y, z));
} else if (identifier == "f") {
int v1, v2, v3, vn1 = 0, vn2 = 0, vn3 = 0;
if (tokens.size() < 3) {
printf("Warning: not enough parameters for face on line %d of %s.obj.\n", lineNo, objFileName.c_str());
continue;
}
if (!parseIntPair(tokens[0], v1, vn1) || !parseIntPair(tokens[1], v2, vn2) || !parseIntPair(tokens[2], v3, vn3)) {
printf("Warning: could not parse face on line %d of %s.obj.\n", lineNo, objFileName.c_str());
continue;
}
v1--;
v2--;
v3--;
vn1 = vn1 ? (vn1 - 1) : v1;
vn2 = vn2 ? (vn2 - 1) : v2;
vn3 = vn3 ? (vn3 - 1) : v3;
if (v1 >= vertices.size() || v1 < 0) {
printf("Warning: invalid vertex %d on line %d of %s.obj.\n", v1, lineNo, objFileName.c_str());
continue;
}
if (v2 >= vertices.size() || v2 < 0) {
printf("Warning: invalid vertex %d on line %d of %s.obj.\n", v2, lineNo, objFileName.c_str());
continue;
}
if (v3 >= vertices.size() || v3 < 0) {
printf("Warning: invalid vertex %d on line %d of %s.obj.\n", v3, lineNo, objFileName.c_str());
continue;
}
if (normals.size() && (vn1 >= normals.size() || vn1 < 0)) {
printf("Warning: invalid normal %d on line %d of %s.obj.\n", vn1, lineNo, objFileName.c_str());
continue;
}
if (normals.size() && (vn2 >= normals.size() || vn2 < 0)) {
printf("Warning: invalid normal %d on line %d of %s.obj.\n", vn2, lineNo, objFileName.c_str());
continue;
}
if (normals.size() && (vn3 >= normals.size() || vn3 < 0)) {
printf("Warning: invalid normal %d on line %d of %s.obj.\n", vn3, lineNo, objFileName.c_str());
continue;
}
Triangle* triangle;
if (normals.size() == 0)
triangle = flipNormals ? new Triangle(vertices[v3], vertices[v2], vertices[v1]) : new Triangle(vertices[v1], vertices[v2], vertices[v3]);
else
triangle = flipNormals ? new Triangle(vertices[v3], vertices[v2], vertices[v1], normals[vn3], normals[vn2], normals[vn1])
: new Triangle(vertices[v1], vertices[v2], vertices[v3], normals[vn1], normals[vn2], normals[vn3]);
triangle->setTransform(currentTransform);
triangle->setMaterial(currentMaterial);
objects.push_back(triangle);
}
}
return true;
}
bool parseSceneFile(int argc, char* argv[], Camera& camera, vector<Light*>& lights, vector<Object*>& objects, SkyBox& environment, int& width, int& height, int& antiAliasing, int& reflectionDepth)
{
string inputFileName = sceneFileNameFromArgs(argc, argv);
if (!inputFileName.size())
return false;
ifstream sceneFile;
sceneFile.open(inputFileName, ios::binary | ios::in);
if (!sceneFile.is_open()) {
cout << "Error: could not open \"" << inputFileName << "\". Make sure the file exists." << endl;
return false;
}
string currentLine;
unsigned int lineNo = 0;
Transform<float, 3, Affine> currentTransform = Transform<float, 3, Affine>();
resetTransformToIdentity(currentTransform);
bool isTransformSet = false;
Material currentMaterial;
while (!sceneFile.eof()) {
lineNo++;
getline(sceneFile, currentLine);
trim(currentLine);
if (!currentLine.size())
continue;
if (currentLine[0] == '#')
continue;
vector<string> tokens = split(currentLine);
string identifier = tokens[0];
tokens.erase(tokens.begin());
if (identifier == "dim") { // Image dimensions.
int w, h;
if (!parseInt(tokens[0], w) || !parseInt(tokens[1], h)) {
printf("Warning: failed to parse image dimensions on line %d.\n", lineNo);
continue;
}
width = w;
height = h;
} else if (identifier == "aa") { // Anti-aliasing.
int a;
if (!parseInt(tokens[0], a)) {
printf("Warning: failed to parse anti-aliasing sample count on line %d.\n", lineNo);
continue;
}
antiAliasing = a;
} else if (identifier == "rd") { // Maximum reflection depth.
int d;
if (!parseInt(tokens[0], d)) {
printf("Warning: failed to parse max reflection depth on line %d.\n", lineNo);
continue;
}
reflectionDepth = d;
} else if (identifier == "cam") {
if (tokens.size() != 15) {
printf("Warning: camera on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse eye position for camera on line %d.\n", lineNo);
continue;
}
Vector3f eye(x, y, z);
if (!parseFloat(tokens[3], x) || !parseFloat(tokens[4], y) || !parseFloat(tokens[5], z)) {
printf("Warning: could not parse LL position for camera on line %d.\n", lineNo);
continue;
}
Vector3f ll(x, y, z);
if (!parseFloat(tokens[6], x) || !parseFloat(tokens[7], y) || !parseFloat(tokens[8], z)) {
printf("Warning: could not parse LR position for camera on line %d.\n", lineNo);
continue;
}
Vector3f lr(x, y, z);
if (!parseFloat(tokens[9], x) || !parseFloat(tokens[10], y) || !parseFloat(tokens[11], z)) {
printf("Warning: could not parse UL position for camera on line %d.\n", lineNo);
continue;
}
Vector3f ul(x, y, z);
if (!parseFloat(tokens[12], x) || !parseFloat(tokens[13], y) || !parseFloat(tokens[14], z)) {
printf("Warning: could not parse UR position for camera on line %d.\n", lineNo);
continue;
}
Vector3f ur(x, y, z);
camera = Camera(eye, ll, lr, ul, ur);
} else if (identifier == "tri") {
if (tokens.size() < 9) {
printf("Warning: triangle on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse vertex A for triangle on line %d.\n", lineNo);
continue;
}
Vector3f vertexA(x, y, z);
if (!parseFloat(tokens[3], x) || !parseFloat(tokens[4], y) || !parseFloat(tokens[5], z)) {
printf("Warning: could not parse vertex B for triangle on line %d.\n", lineNo);
continue;
}
Vector3f vertexB(x, y, z);
if (!parseFloat(tokens[6], x) || !parseFloat(tokens[7], y) || !parseFloat(tokens[8], z)) {
printf("Warning: could not parse vertex C for triangle on line %d.\n", lineNo);
continue;
}
Vector3f vertexC(x, y, z);
Triangle* triangle = new Triangle(vertexA, vertexB, vertexC);
triangle->setTransform(currentTransform);
triangle->setMaterial(currentMaterial);
objects.push_back(triangle);
} else if (identifier == "sph") {
if (tokens.size() < 4) {
printf("Warning: sphere on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z, r;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z) || !parseFloat(tokens[3], r)) {
printf("Warning: could not parse sphere on line %d.\n", lineNo);
continue;
}
Sphere* sphere = new Sphere(Vector3f(x, y, z), r);
sphere->setTransform(currentTransform);
sphere->setMaterial(currentMaterial);
objects.push_back(sphere);
} else if (identifier == "ltp") {
if (tokens.size() < 6) {
printf("Warning: point light on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z, r, g, b;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z) ||
!parseFloat(tokens[3], r) || !parseFloat(tokens[4], g) || !parseFloat(tokens[5], b)) {
printf("Warning: could not parse point light on line %d.\n", lineNo);
continue;
}
int falloff = 0;
if (tokens.size() >= 7) {
if (!parseInt(tokens[6], falloff)) {
printf("Warning: could not parse falloff for point light on line %d.\n", lineNo);
continue;
}
}
lights.push_back(new PointLight(Vector3f(x, y, z), Color(r, g, b), falloff));
} else if (identifier == "ltd") {
if (tokens.size() < 6) {
printf("Warning: directional light on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z, r, g, b;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z) ||
!parseFloat(tokens[3], r) || !parseFloat(tokens[4], g) || !parseFloat(tokens[5], b)) {
printf("Warning: could not parse directional light on line %d.\n", lineNo);
continue;
}
lights.push_back(new DirectionalLight(Vector3f(x, y, z), Color(r, g, b)));
} else if (identifier == "lta") {
if (tokens.size() < 3) {
printf("Warning: ambient light on line %d is missing parameters.\n", lineNo);
continue;
}
float r, g, b;
if (!parseFloat(tokens[0], r) || !parseFloat(tokens[1], g) || !parseFloat(tokens[2], b)) {
printf("Warning: could not parse ambient light on line %d.\n", lineNo);
continue;
}
lights.push_back(new AmbientLight(Color(r, g, b)));
} else if (identifier == "mat") {
if (tokens.size() < 13) {
printf("Warning: material on line %d is missing parameters.\n", lineNo);
continue;
}
float r, g, b;
if (!parseFloat(tokens[0], r) || !parseFloat(tokens[1], g) || !parseFloat(tokens[2], b)) {
printf("Warning: could not parse ambient color for material on line %d.\n", lineNo);
continue;
}
Color ambient(r, g, b);
if (!parseFloat(tokens[3], r) || !parseFloat(tokens[4], g) || !parseFloat(tokens[5], b)) {
printf("Warning: could not parse diffuse color for material on line %d.\n", lineNo);
continue;
}
Color diffuse(r, g, b);
if (!parseFloat(tokens[6], r) || !parseFloat(tokens[7], g) || !parseFloat(tokens[8], b)) {
printf("Warning: could not parse specular color for material on line %d.\n", lineNo);
continue;
}
Color specular(r, g, b);
float specularCoefficient;
if (!parseFloat(tokens[9], specularCoefficient)) {
printf("Warning: could not parse specular coefficient for material on line %d.\n", lineNo);
continue;
}
if (!parseFloat(tokens[10], r) || !parseFloat(tokens[11], g) || !parseFloat(tokens[12], b)) {
printf("Warning: could not parse reflective color for material on line %d.\n", lineNo);
continue;
}
Color reflective(r, g, b);
currentMaterial = Material(ambient, diffuse, specular, reflective, specularCoefficient);
} else if (identifier == "xft") {
if (tokens.size() < 3) {
printf("Warning: translation transformation on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse translation transformation on line %d.\n", lineNo);
continue;
}
if (isTransformSet)
currentTransform = currentTransform * Translation<float, 3>(Vector3f(x, y, z));
else {
currentTransform = Translation<float, 3>(Vector3f(x, y, z));
isTransformSet = true;
}
} else if (identifier == "xfs") {
if (tokens.size() < 3) {
printf("Warning: scale transformation on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse scale transformation on line %d.\n", lineNo);
continue;
}
if (isTransformSet)
currentTransform = currentTransform * Scaling(Vector3f(x, y, z));
else {
currentTransform = Scaling(Vector3f(x, y, z));
isTransformSet = true;
}
} else if (identifier == "xfr") {
if (tokens.size() < 3) {
printf("Warning: rotation transformation on line %d is missing parameters.\n", lineNo);
continue;
}
float x, y, z;
if (!parseFloat(tokens[0], x) || !parseFloat(tokens[1], y) || !parseFloat(tokens[2], z)) {
printf("Warning: could not parse rotation transformation on line %d.\n", lineNo);
continue;
}
if (isTransformSet)
currentTransform = currentTransform * rotationTransformFromAxisAngle(x, y, z);
else {
currentTransform = rotationTransformFromAxisAngle(x, y, z);
isTransformSet = true;
}
} else if (identifier == "xfz") {
resetTransformToIdentity(currentTransform);
isTransformSet = false;
} else if (identifier == "obj") {
if (tokens.size() < 1) {
printf("Warning: obj specified without file name on line %d.\n", lineNo);
continue;
}
if (!parseObjFile(tokens[0], objects, currentTransform, currentMaterial, tokens.size() >= 2 && tokens[2] == "-f"))
printf("Warning: error occurred while parsing \"%s.obj\" on line %d.\n", tokens[0].c_str() , lineNo);
} else if (identifier == "bg") {
if (tokens.size() < 3) {
printf("Warning: background on line %d is missing parameters.\n", lineNo);
continue;
}
if (tokens.size() == 3) {
float red, green, blue;
if (!parseFloat(tokens[0], red) || !parseFloat(tokens[1], green) || !parseFloat(tokens[2], blue)) {
printf("Warning: failed to parse background color on line %d.\n", lineNo);
continue;
}
environment = SkyBox(Color(red, green, blue));
} else {
float minX, maxX, minY, maxY, minZ, maxZ;
if (!parseFloat(tokens[1], minX) || !parseFloat(tokens[2], maxX) || !parseFloat(tokens[3], minY)
|| !parseFloat(tokens[4], maxY) || !parseFloat(tokens[5], minZ) || !parseFloat(tokens[6], maxZ)) {
printf("Warning: failed to parse min/max for axes on line %d.\n", lineNo);
continue;
}
int bgWidth, bgHeight;
if (!parseInt(tokens[7], bgWidth) || !parseInt(tokens[8], bgHeight)) {
printf("Warning: failed to parse image dimensions on line %d.\n", lineNo);
continue;
}
environment = SkyBox(tokens[0], minX, maxX, minY, maxY, minZ, maxZ, bgWidth, bgHeight);
}
}
}
sceneFile.close();
return true;
}
#endif /** __PARSING_H__ */