-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
464 lines (384 loc) · 14.4 KB
/
Scene.cpp
File metadata and controls
464 lines (384 loc) · 14.4 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
#include "Scene.h"
#include "Camera.h"
#include "Light.h"
#include "Material.h"
#include "Shape.h"
#include "tinyxml2.h"
//not default
#include <typeinfo>
#include "Image.h"
#include <limits>
#include <math.h>
using namespace tinyxml2;
/*
* Must render the scene from each camera's viewpoint and create an image.
* You can use the methods of the Image class to save the image as a PPM file.
*/
void Scene::renderScene(void)
{
/***********************************************
* *
* TODO: Implement this function *
* *
***********************************************
*/
for (auto camera = cameras.begin(); camera != cameras.end(); camera++)
{
Image renderedImage((*camera)->imgPlane.nx, (*camera)->imgPlane.ny);
unsigned long int counter=0;
unsigned long int total=0;
for(int y = 0 ; y < (*camera)->imgPlane.ny; y++)
{
for(int x = 0 ; x < (*camera)->imgPlane.nx; x++)
{
Ray primaryRay = (*camera)->getPrimaryRay(y, x);
Color values{0,0,0};
// Color values{backgroundColor.r,backgroundColor.g,backgroundColor.};
float minT = std::numeric_limits<float>::infinity(); // INFINITY
float infinity = std::numeric_limits<float>::infinity(); // INFINITY
ReturnVal hitPoint;
for (auto object = objects.begin(); object != objects.end(); object++)
{
ReturnVal intersectionDetails = (*object)->intersect(primaryRay);
if(intersectionDetails.isIntersects && minT > intersectionDetails.t1){
// cout << "minT: " << minT << endl;
// hitPoint = intersectionDetails; // shallow copy, what happens to array variables?
hitPoint.id = intersectionDetails.id;
hitPoint.matIndex = intersectionDetails.matIndex;
hitPoint.t1 = intersectionDetails.t1;
hitPoint.t2 = intersectionDetails.t2;
hitPoint.type = intersectionDetails.type;
hitPoint.isIntersects = intersectionDetails.isIntersects;
hitPoint.intersectionPoint1[0] = intersectionDetails.intersectionPoint1[0];
hitPoint.intersectionPoint1[1] = intersectionDetails.intersectionPoint1[1];
hitPoint.intersectionPoint1[2] = intersectionDetails.intersectionPoint1[2];
hitPoint.intersectionPoint2[0] = intersectionDetails.intersectionPoint2[0];
hitPoint.intersectionPoint2[1] = intersectionDetails.intersectionPoint2[1];
hitPoint.intersectionPoint2[2] = intersectionDetails.intersectionPoint2[2];
hitPoint.normal[0] = intersectionDetails.normal[0];
hitPoint.normal[1] = intersectionDetails.normal[1];
hitPoint.normal[2] = intersectionDetails.normal[2];
minT = intersectionDetails.t1;
}
}
Vector3f diffuseLight = {50,50,50};
if(hitPoint.isIntersects && minT < infinity && minT >= (-1)*intTestEps){
for (auto i = lights.begin(); i != lights.end(); i++)
{
Vector3f point;
point.x = hitPoint.intersectionPoint1[0];
point.y = hitPoint.intersectionPoint1[1];
point.z = hitPoint.intersectionPoint1[2];
Vector3f intensityOverDistance = (*i)->computeLightContribution(point);
Vector3f wi = vectorSubtraction((*i)->position, point);
Vector3f diffuseRef = materials[hitPoint.matIndex-1]->diffuseRef;
float cosTheta = 0;
Vector3f normal;
normal.x = hitPoint.normal[0];
normal.y = hitPoint.normal[1];
normal.z = hitPoint.normal[2];
// normal = normal);
// cout << normal.x << ", " << normal.y << ", " << normal.z << "type: "<<hitPoint.type <<endl;
if(dotProduct(normalize(wi), normal)>0.0){
cosTheta = dotProduct(normalize(wi), normal);
}
diffuseRef = scalarMultiplication(cosTheta, diffuseRef);
Vector3f diffuseShading;
diffuseShading.r = diffuseRef.r * intensityOverDistance.r;
diffuseShading.g = diffuseRef.g * intensityOverDistance.g;
diffuseShading.b = diffuseRef.b * intensityOverDistance.b;
diffuseLight.r += diffuseShading.r;
diffuseLight.g += diffuseShading.g;
diffuseLight.b += diffuseShading.b;
// cout <<"light position "<<(*i)->position.x<<", "<<(*i)->position.y<<", "<<(*i)->position.x << endl;
}
Vector3f ambi;
ambi.x = materials[hitPoint.matIndex-1]->ambientRef.x * ambientLight.x;
ambi.y = materials[hitPoint.matIndex-1]->ambientRef.y * ambientLight.y;
ambi.z = materials[hitPoint.matIndex-1]->ambientRef.z * ambientLight.z;
values.red = diffuseLight.r + ambi.x;
values.grn = diffuseLight.g + ambi.y;
values.blu = diffuseLight.b + ambi.z;
counter++;
}
else{
values.red = backgroundColor.x;
values.grn = backgroundColor.y;
values.blu = backgroundColor.z;
}
total++;
renderedImage.setPixelValue(y,x, values);
}
}
renderedImage.saveImage((*camera)->imageName);
cout << endl<<"total colored pixels: " << counter << " total pixel: " <<total <<endl << endl;
}
}
////////////////// Private functions starts//////////////////////
Vector3f Scene::normal( Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.y * b.z - a.z*b.y;
result.y = a.z * b.x - a.x*b.z;
result.z = a.x * b.y - a.y*b.x;
return result;
}
Vector3f Scene::normalize( Vector3f v) const{
float len = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
if (len != 0.0)
{
v.x /= len;
v.y /= len;
v.z /= len;
}
return v;
}
float Scene::determinant(Vector3f a, Vector3f b, Vector3f c) const{
return a.x*b.y*c.z + b.x*c.y*a.z + c.x*a.y*b.z - a.z*b.y*c.x - b.z*c.y*a.x - c.z*a.y*b.x;
}
Vector3f Scene::scalarMultiplication(float t, Vector3f direction) const{
Vector3f result;
result.x = direction.x * t;
result.y = direction.y * t;
result.z = direction.z * t;
return result;
}
Vector3f Scene::vectorSubtraction(Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
return result;
}
Vector3f Scene::vectorAddition(Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
float Scene::dotProduct(Vector3f a, Vector3f b) const{
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}
////////////////// Private functions ends //////////////////////
// Parses XML file.
Scene::Scene(const char *xmlPath)
{
const char *str;
XMLDocument xmlDoc;
XMLError eResult;
XMLElement *pElement;
maxRecursionDepth = 1;
shadowRayEps = 0.001;
eResult = xmlDoc.LoadFile(xmlPath);
XMLNode *pRoot = xmlDoc.FirstChild();
pElement = pRoot->FirstChildElement("MaxRecursionDepth");
if(pElement != nullptr)
pElement->QueryIntText(&maxRecursionDepth);
pElement = pRoot->FirstChildElement("BackgroundColor");
str = pElement->GetText();
sscanf(str, "%f %f %f", &backgroundColor.r, &backgroundColor.g, &backgroundColor.b);
pElement = pRoot->FirstChildElement("ShadowRayEpsilon");
if(pElement != nullptr)
pElement->QueryFloatText(&shadowRayEps);
pElement = pRoot->FirstChildElement("IntersectionTestEpsilon");
if(pElement != nullptr)
eResult = pElement->QueryFloatText(&intTestEps);
// Parse cameras
pElement = pRoot->FirstChildElement("Cameras");
XMLElement *pCamera = pElement->FirstChildElement("Camera");
XMLElement *camElement;
while(pCamera != nullptr)
{
int id;
char imageName[64];
Vector3f pos, gaze, up;
ImagePlane imgPlane;
eResult = pCamera->QueryIntAttribute("id", &id);
camElement = pCamera->FirstChildElement("Position");
str = camElement->GetText();
sscanf(str, "%f %f %f", &pos.x, &pos.y, &pos.z);
camElement = pCamera->FirstChildElement("Gaze");
str = camElement->GetText();
sscanf(str, "%f %f %f", &gaze.x, &gaze.y, &gaze.z);
camElement = pCamera->FirstChildElement("Up");
str = camElement->GetText();
sscanf(str, "%f %f %f", &up.x, &up.y, &up.z);
camElement = pCamera->FirstChildElement("NearPlane");
str = camElement->GetText();
sscanf(str, "%f %f %f %f", &imgPlane.left, &imgPlane.right, &imgPlane.bottom, &imgPlane.top);
camElement = pCamera->FirstChildElement("NearDistance");
eResult = camElement->QueryFloatText(&imgPlane.distance);
camElement = pCamera->FirstChildElement("ImageResolution");
str = camElement->GetText();
sscanf(str, "%d %d", &imgPlane.nx, &imgPlane.ny);
camElement = pCamera->FirstChildElement("ImageName");
str = camElement->GetText();
strcpy(imageName, str);
cameras.push_back(new Camera(id, imageName, pos, gaze, up, imgPlane));
pCamera = pCamera->NextSiblingElement("Camera");
}
// Parse materals
pElement = pRoot->FirstChildElement("Materials");
XMLElement *pMaterial = pElement->FirstChildElement("Material");
XMLElement *materialElement;
while(pMaterial != nullptr)
{
materials.push_back(new Material());
int curr = materials.size() - 1;
eResult = pMaterial->QueryIntAttribute("id", &materials[curr]->id);
materialElement = pMaterial->FirstChildElement("AmbientReflectance");
str = materialElement->GetText();
sscanf(str, "%f %f %f", &materials[curr]->ambientRef.r, &materials[curr]->ambientRef.g, &materials[curr]->ambientRef.b);
materialElement = pMaterial->FirstChildElement("DiffuseReflectance");
str = materialElement->GetText();
sscanf(str, "%f %f %f", &materials[curr]->diffuseRef.r, &materials[curr]->diffuseRef.g, &materials[curr]->diffuseRef.b);
materialElement = pMaterial->FirstChildElement("SpecularReflectance");
str = materialElement->GetText();
sscanf(str, "%f %f %f", &materials[curr]->specularRef.r, &materials[curr]->specularRef.g, &materials[curr]->specularRef.b);
materialElement = pMaterial->FirstChildElement("MirrorReflectance");
if(materialElement != nullptr)
{
str = materialElement->GetText();
sscanf(str, "%f %f %f", &materials[curr]->mirrorRef.r, &materials[curr]->mirrorRef.g, &materials[curr]->mirrorRef.b);
}
else
{
materials[curr]->mirrorRef.r = 0.0;
materials[curr]->mirrorRef.g = 0.0;
materials[curr]->mirrorRef.b = 0.0;
}
materialElement = pMaterial->FirstChildElement("PhongExponent");
if(materialElement != nullptr)
materialElement->QueryIntText(&materials[curr]->phongExp);
pMaterial = pMaterial->NextSiblingElement("Material");
}
// Parse vertex data
pElement = pRoot->FirstChildElement("VertexData");
int cursor = 0;
Vector3f tmpPoint;
str = pElement->GetText();
while(str[cursor] == ' ' || str[cursor] == '\t' || str[cursor] == '\n')
cursor++;
while(str[cursor] != '\0')
{
for(int cnt = 0 ; cnt < 3 ; cnt++)
{
if(cnt == 0)
tmpPoint.x = atof(str + cursor);
else if(cnt == 1)
tmpPoint.y = atof(str + cursor);
else
tmpPoint.z = atof(str + cursor);
while(str[cursor] != ' ' && str[cursor] != '\t' && str[cursor] != '\n')
cursor++;
while(str[cursor] == ' ' || str[cursor] == '\t' || str[cursor] == '\n')
cursor++;
}
vertices.push_back(tmpPoint);
}
// Parse objects
pElement = pRoot->FirstChildElement("Objects");
// Parse spheres
XMLElement *pObject = pElement->FirstChildElement("Sphere");
XMLElement *objElement;
while(pObject != nullptr)
{
int id;
int matIndex;
int cIndex;
float R;
eResult = pObject->QueryIntAttribute("id", &id);
objElement = pObject->FirstChildElement("Material");
eResult = objElement->QueryIntText(&matIndex);
objElement = pObject->FirstChildElement("Center");
eResult = objElement->QueryIntText(&cIndex);
objElement = pObject->FirstChildElement("Radius");
eResult = objElement->QueryFloatText(&R);
objects.push_back(new Sphere(id, matIndex, cIndex, R, &vertices));
pObject = pObject->NextSiblingElement("Sphere");
}
// Parse triangles
pObject = pElement->FirstChildElement("Triangle");
while(pObject != nullptr)
{
int id;
int matIndex;
int p1Index;
int p2Index;
int p3Index;
eResult = pObject->QueryIntAttribute("id", &id);
objElement = pObject->FirstChildElement("Material");
eResult = objElement->QueryIntText(&matIndex);
objElement = pObject->FirstChildElement("Indices");
str = objElement->GetText();
sscanf(str, "%d %d %d", &p1Index, &p2Index, &p3Index);
objects.push_back(new Triangle(id, matIndex, p1Index, p2Index, p3Index, &vertices));
pObject = pObject->NextSiblingElement("Triangle");
}
// Parse meshes
pObject = pElement->FirstChildElement("Mesh");
while(pObject != nullptr)
{
int id;
int matIndex;
int p1Index;
int p2Index;
int p3Index;
int cursor = 0;
int vertexOffset = 0;
vector<Triangle> faces;
vector<int> *meshIndices = new vector<int>;
eResult = pObject->QueryIntAttribute("id", &id);
objElement = pObject->FirstChildElement("Material");
eResult = objElement->QueryIntText(&matIndex);
objElement = pObject->FirstChildElement("Faces");
objElement->QueryIntAttribute("vertexOffset", &vertexOffset);
str = objElement->GetText();
while(str[cursor] == ' ' || str[cursor] == '\t' || str[cursor] == '\n')
cursor++;
while(str[cursor] != '\0')
{
for(int cnt = 0 ; cnt < 3 ; cnt++)
{
if(cnt == 0)
p1Index = atoi(str + cursor) + vertexOffset;
else if(cnt == 1)
p2Index = atoi(str + cursor) + vertexOffset;
else
p3Index = atoi(str + cursor) + vertexOffset;
while(str[cursor] != ' ' && str[cursor] != '\t' && str[cursor] != '\n')
cursor++;
while(str[cursor] == ' ' || str[cursor] == '\t' || str[cursor] == '\n')
cursor++;
}
faces.push_back(*(new Triangle(-1, matIndex, p1Index, p2Index, p3Index, &vertices)));
meshIndices->push_back(p1Index);
meshIndices->push_back(p2Index);
meshIndices->push_back(p3Index);
}
objects.push_back(new Mesh(id, matIndex, faces, meshIndices, &vertices));
pObject = pObject->NextSiblingElement("Mesh");
}
// Parse lights
int id;
Vector3f position;
Vector3f intensity;
pElement = pRoot->FirstChildElement("Lights");
XMLElement *pLight = pElement->FirstChildElement("AmbientLight");
XMLElement *lightElement;
str = pLight->GetText();
sscanf(str, "%f %f %f", &ambientLight.r, &ambientLight.g, &ambientLight.b);
pLight = pElement->FirstChildElement("PointLight");
while(pLight != nullptr)
{
eResult = pLight->QueryIntAttribute("id", &id);
lightElement = pLight->FirstChildElement("Position");
str = lightElement->GetText();
sscanf(str, "%f %f %f", &position.x, &position.y, &position.z);
lightElement = pLight->FirstChildElement("Intensity");
str = lightElement->GetText();
sscanf(str, "%f %f %f", &intensity.r, &intensity.g, &intensity.b);
lights.push_back(new PointLight(position, intensity));
pLight = pLight->NextSiblingElement("PointLight");
}
}