-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
370 lines (315 loc) · 10.7 KB
/
Parser.cpp
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
#include "Parser.h"
#include <iostream>
#include <fstream>
#include "Primitive.h"
#include "Light.h"
Parser::Parser()
{
shininess = 0;
global_inverse_translation = translation = rotation = scale = inverse_translation = inverse_scale = inverse_rotation = Mat4(1.0);
attenuation = Vec3(1.0f, 0.0f, 0.0f);
// verts = nullptr;
// vertindex = 0;
}
// buildSceneFromFile()
// params: empty scene to be created, scene filename (using the CS184 scene description language)
// return: none
// notes: parses the scene description language into a fully fleshed scene (as long as it's fully described in the scene file!)
void Parser::buildSceneFromFile(Scene &scene, const std::string &filename)
{
std::string str, cmd; //strings for current line of file and current command
std::ifstream infile;
infile.open(filename);
if (infile.is_open())
{
getline(infile, str);
while(infile)
{
if ( (str.find_first_not_of(" \t\r\n") != std::string::npos) && (str[0] != '#')) //rule out comments & blank lines
{
std::stringstream ss(str); //create stringstream to read values one at a time, command first
ss >> cmd;
bool valid; //check validity of input before we do anything with it
if (cmd == "size") // set scene width and height
{
valid = readvals(ss, params, 2);
if (valid)
{
scene.m_width = static_cast<int>(params[0]);
scene.m_height = static_cast<int>(params[1]);
}
}
if (cmd == "maxdepth") //waste the values for maxdepth/output - we don't want to parse them
{
int temp;
ss >> temp;
}
if (cmd == "output")
{
ss >> str;
}
if (cmd == "camera") //camera creation
{
valid = readvals(ss, params, 10);
if (valid)
{
Camera cam(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], 0.5, 5.0, false, 4);
scene.m_camera = cam;
}
}
if (cmd == "dof")
{
valid = readvals(ss, params, 2);
if (valid)
{
scene.m_camera.dof = true;
scene.m_camera.aperture = params[0];
scene.m_camera.focallength = params[1];
}
}
if (cmd == "maxverts") //vertex specifications
{
valid = readvals(ss, params, 1);
if (valid)
{
// verts = new Point[static_cast<int>(params[0])]; //allocate space for all the vertices promised in the scene file - must come before verts are specified! Segfaults galore if not
}
}
if (cmd == "vertex")
{
valid = readvals(ss, params, 3);
if (valid)
{
Vec3 p(params[0], params[1], params[2]);
verts.push_back(p);
}
}
if (cmd == "attenuation") //lighting and material settings
{
valid = readvals(ss, params, 3);
if (valid)
{
Vec3 a(params[0], params[1], params[2]);
attenuation = a;
}
}
if (cmd == "ambient")
{
valid = readvals(ss, params, 3);
if (valid)
{
Vec3 a(params[0], params[1], params[2]);
ambient = a;
}
}
if (cmd == "diffuse")
{
valid = readvals(ss, params, 3);
if (valid)
{
Vec3 d(params[0], params[1], params[2]);
diffuse = d;
}
}
if (cmd == "specular")
{
valid = readvals(ss, params, 3);
if (valid)
{
Vec3 s(params[0], params[1], params[2]);
specular = s;
}
}
if (cmd == "emission")
{
valid = readvals(ss, params, 3);
if (valid)
{
Vec3 e(params[0], params[1], params[2]);
emission = e;
}
}
if (cmd == "shininess")
{
valid = readvals(ss, params, 1);
if (valid)
{
shininess = params[0];
}
}
if (cmd == "directional") //light specifications
{
valid = readvals(ss, params, 6);
if (valid)
{
Vec3 d(params[0], params[1], params[2]);
Vec3 c(params[3], params[4], params[5]);
auto l = Light(true, d, Vec3(1.0, 0.0, 0.0), c);
scene.addLight(l);
}
}
if (cmd == "point")
{
valid = readvals(ss, params, 6);
if (valid)
{
Vec3 o(params[0], params[1], params[2]);
Vec3 c(params[3], params[4], params[5]);
auto l = Light(false, o, attenuation, c);
scene.addLight(l);
}
}
if (cmd == "pushTransform") //transform stack management commands
{
if (transforms.empty()) transforms.push(Mat4(1.0));
else (transforms.push(transforms.top()));
}
if (cmd == "popTransform")
{
if (transforms.empty()) std::cerr << "Trying to pop empty stack!\n";
else
{
transforms.pop();
if (!transforms.empty())
{
translation = transforms.top();
inverse_translation = global_inverse_translation; //some awkwardness to maintain inverse translation and translation matrices properly, even when there are global transforms
//wouldn't be necessary if our mat4 could invert itself... T.T
}
else
{
translation = inverse_translation = Mat4(1.0);
}
rotation = inverse_rotation = scale = inverse_scale = Mat4(1.0);
}
}
if (cmd == "translate") //transformations to be applied to the top of the stack
{
valid = readvals(ss, params, 3);
if (valid)
{
Mat4 current_trans = Mat4::translation(params[0], params[1], params[2]); //Track inverse of the transforms we've applied, for creation of ellipses - since our mat4 class doesn't have inversion capability, we have to do it by hand
Mat4 current_inv_trans = Mat4::translation(-params[0], -params[1], -params[2]);
rightmult(current_trans, transforms);
translation *= current_trans; //combine the current translation with the overall translation
inverse_translation *= current_inv_trans;
if (transforms.size() == 1)
{
global_inverse_translation *= current_inv_trans; //if this is a global camera translation and not a specific object, take note!
}
}
}
if (cmd == "rotate")
{
valid = readvals(ss, params, 4);
if (valid)
{
Mat4 current_rot = Mat4::rotation(params[3], Vec3(params[0], params[1], params[2]));
Mat4 current_inv_rot = Mat4::rotation(-params[3], Vec3(params[0], params[1], params[2]));
rightmult(current_rot, transforms);
rotation *= current_rot;
inverse_rotation *= current_inv_rot;
}
}
if (cmd == "scale")
{
valid = readvals(ss, params, 3);
if (valid)
{
Mat4 current_scale = Mat4::scale(params[0], params[1], params[2]);
Mat4 current_inv_scale = Mat4::scale(1.0f / params[0], 1.0f / params[1], 1.0f / params[2]);
rightmult(current_scale, transforms);
scale *= current_scale;
inverse_scale *= current_inv_scale;
}
}
if (cmd == "sphere") //object definitions
{
valid = readvals(ss, params, 4);
if (valid)
{
// Mat4 current_scale = Mat4::scale(params[3], params[3], params[3]);
// Mat4 current_inv_scale = Mat4::scale(1.0f / params[3], 1.0f / params[3], 1.0f / params[3]);
// Mat4 current_trans = Mat4::translation(params[0], params[1], params[2]);
// Mat4 current_inv_trans = Mat4::translation(-params[0], -params[1], -params[2]);
// current_scale *= scale;
// current_inv_scale *= inverse_scale;
// current_trans *= translation;
auto m = Material(ambient, diffuse, specular, emission, shininess);
auto p = Vec3(params[0], params[1], params[2]);
if (!transforms.empty()) p = transforms.top() * p;
auto s = Sphere(m, p, params[3] * scale.m_data[0][0]);
scene.addSphere(s);
// current_inv_trans *= inverse_translation;
// std::unique_ptr<Primitive> e(new Ellipse(rotation, current_trans, current_scale, inverse_rotation, current_inv_trans, current_inv_scale, ambient, diffuse, specular, emission, shininess));
// scene.addObject(std::move(e)); //all spheres are currently represented as ellipses - could be more efficient with a little more parsing
}
}
if (cmd == "tri")
{
valid = readvals(ss, params, 3);
if (valid)
{
int i0 = static_cast<int>(params[0]);
int i1 = static_cast<int>(params[1]);
int i2 = static_cast<int>(params[2]);
Vec3 vert1 = verts[i0];
Vec3 vert2 = verts[i1];
Vec3 vert3 = verts[i2];
if (!transforms.empty()) //transform vertices by the current model transform
{
vert1 = transforms.top() * vert1;
vert2 = transforms.top() * vert2;
vert3 = transforms.top() * vert3;
} //otherwise don't transform, there's no transform to apply
auto m = Material(ambient, diffuse, specular, emission, shininess);
Vec3 vs[] = {vert1, vert2, vert3};
auto t = Triangle(m, vs);
// std::unique_ptr<Primitive> t(new Triangle(vert1, vert2, vert3, ambient, diffuse, specular, emission, shininess));
// scene.addObject(std::move(t));
scene.addTriangle(t);
}
}
if (cmd == "plane") {
valid = readvals(ss, params, 6);
if (valid) {
auto m = Material(ambient, diffuse, specular, emission, shininess);
auto normal = Vec3(params[0], params[1], params[2]);
auto point = Vec3(params[3], params[4], params[5]);
auto p = Plane(m, normal, point);
scene.addPlane(p);
}
}
}
getline(infile, str); //read the next command
}
}
else
{
std::cerr << "Unable to open file!\n";
}
}
// readvals()
// params: string to read from, float array to read into, number of values to read
// return: bool succesful read
// notes:
bool Parser::readvals(std::stringstream &s, float *dest, const int numvals)
{
for (int i = 0; i < numvals; i++) {
s >> params[i];
if (s.fail()) {
std::cout << "Failed reading value " << i << " will skip\n";
return false;
}
}
return true;
}
// rightmult()
// params: matrix to multiply, stack to right-multiply onto
// return: void
// notes: right-multiplies the top of the stack by m
void Parser::rightmult(const Mat4 &m, std::stack<Mat4> &t)
{
Mat4 &top = t.top();
top *= m;
}