-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cpp
More file actions
460 lines (377 loc) · 14.5 KB
/
world.cpp
File metadata and controls
460 lines (377 loc) · 14.5 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
#include "world.h"
void World::test()
{
printf("\nWorld is active and up! Big win!");
}
void World::boxAllObjects()
{
//to do-- remove
float rootMin [3] ={-FLT_MAX, -FLT_MAX, -FLT_MAX};
float rootMax [3] ={FLT_MAX, FLT_MAX, FLT_MAX};
rootBox = new BoundingBox(rootMin, rootMax);
std::vector<BoundingBox*> boxes;
//construct all boxes
for(Object* obj : objects){
// for each point, find min/max to encapsulate
float min[3] = {0,0,0};
float max[3] = {0,0,0};
if(Triangle* tri = dynamic_cast<Triangle*>(obj)){
std::copy(std::begin(tri->point0), std::end(tri->point0), std::begin(min));
std::copy(std::begin(tri->point0), std::end(tri->point0), std::begin(max));
for(int i = 0; i < 3; i++){
if(tri->point1[i] < min[i]){
min[i] = tri->point1[i];
}
if(tri->point2[i] < min[i])
{
min[i] = tri->point2[i];
}
if(tri->point1[i] > max[i]){
max[i] = tri->point1[i];
}
if(tri->point2[i] > max[i])
{
max[i] = tri->point2[i];
}
}
}
else if(Sphere* sp = dynamic_cast<Sphere*>(obj)){
std::copy(std::begin(sp->center), std::end(sp->center), std::begin(min));
std::copy(std::begin(sp->center), std::end(sp->center), std::begin(max));
for(int i = 0; i < 3; i++)
{
min[i] = sp->center[i] - sp->radius;
max[i] = sp->center[i] + sp->radius;
}
}
else{
continue;
}
BoundingBox* box = new BoundingBox(min, max);
box->childObject = obj;
boxes.push_back(box);
}
//while boxes in boxes
rootBox = recursiveBuildBVH(boxes, 0);
}
/**
*
BoundingBox* World::recursiveBuildBVH(std::vector<BoundingBox*> boxes, int axis) {
if (boxes.empty()) return nullptr;
if (boxes.size() == 1) return boxes[0];
sort(boxes.begin(), boxes.end(), [axis](BoundingBox* a, BoundingBox* b) {
return a->boundMin[axis] < b->boundMin[axis];
});
size_t mid = boxes.size() / 2;
float split = boxes[mid]->boundMin[axis];
std::vector<BoundingBox*> left, right;
//for anything that straddles the split, we will add it to the left and update the left's max to reflect this
//this will lead to some overlapping BUT will create a nicer split
float left_max = 0;
for (BoundingBox* box : boxes) {
if (box->boundMax[axis] <= split) {
left.push_back(box);
if(left_max < box->boundMin[axis]){
left_max = box->boundMax[axis];
}
} else if (box->boundMin[axis] >= split) {
right.push_back(box);
} else {
left.push_back(box);
if(left_max < box->boundMin[axis]){
left_max = box->boundMin[axis];
}
}
}
BoundingBox* leftNode = recursiveBuildBVH(left, (axis + 1) % 3);
BoundingBox* rightNode = recursiveBuildBVH(right, (axis + 1) % 3);
float newMin[3], newMax[3];
if(leftNode != nullptr && rightNode != nullptr){
for (int i = 0; i < 3; ++i) {
newMin[i] = leftNode->boundMin[axis];
newMax[i] = std::max(left_max, rightNode->boundMax[i]);
}
}
else if(leftNode != nullptr)
{
for (int i = 0; i < 3; ++i) {
newMin[i] = leftNode->boundMin[i];
newMax[i] = leftNode->boundMax[i];
}
}
else if (rightNode != nullptr){
for(int i = 0; i < 3; i++){
newMin[i] = rightNode->boundMin[i];
newMax[i] = rightNode->boundMax[i];
}
}
BoundingBox* parent = new BoundingBox(newMin, newMax);
if (leftNode) parent->addBox(leftNode);
if (rightNode) parent->addBox(rightNode);
return parent;
}
*/
BoundingBox* World::recursiveBuildBVH(std::vector<BoundingBox*> boxes, int axis) {
std::vector<BoundingBox*> children;
//if more than 2 children, sort those children into boxes
if(boxes.size() > 2){
int pivot = boxes.size()/2; //where we split the boxes
//sort the boxes by the axis
sort(boxes.begin(), boxes.end(), [axis](BoundingBox* a, BoundingBox* b) {
return a->center[axis] < b->center[axis];
});
std::vector<BoundingBox*> minBoxes;
std::vector<BoundingBox*> maxBoxes;
//get all bottom chunks and put into list
for(int i = 0; i < boxes.size(); i++){
if(i < pivot){
minBoxes.push_back(boxes[i]);
}
else{
maxBoxes.push_back(boxes[i]);
}
}
//if min and max groups are bigger than 1, recurse
//else, they are just children of current box
if(minBoxes.size() > 1){
BoundingBox* minChildBox = recursiveBuildBVH(minBoxes, axis += 1);
children.push_back(minChildBox);
}
else if (minBoxes.size() == 1){
children.push_back(minBoxes[0]);
}
if(maxBoxes.size() > 1){
BoundingBox* maxChildBox = recursiveBuildBVH(maxBoxes, axis+=1);
children.push_back(maxChildBox);
}
else if(maxBoxes.size() == 1){
children.push_back(maxBoxes[0]);
}
}
else
{
for(BoundingBox* b : boxes)
{
children.push_back(b);
}
}
//find min/max from children
//find the min/max of everything
float min [3] = {};
float max [3] = {};
std::copy(std::begin(children[0]->boundMin), std::end(children[0]->boundMin), min);
std::copy(std::begin(children[0]->boundMax), std::end(children[0]->boundMax), max);
for(BoundingBox* b : children)
{
for(int j = 0; j < 3; j++){
if(b->boundMin[j] < min[j]){
min[j] = b->boundMin[j];
}
if(b->boundMax[j] > max[j]){
max[j] = b->boundMax[j];
}
}
}
//create this bounding box!
BoundingBox* thisBox = new BoundingBox(min, max);
for(BoundingBox* b : children)
{
thisBox->addBox(b);
}
//return this bounding box
return thisBox;
}
World::World()
{
ambientLight = new Color();
*ambientLight = {80, 80, 80};;
}
int World::addObject(Object* obj)
{
objects.push_back(obj);
return 1;
}
int World::addLight(Light* light)
{
lights.push_back(light);
}
//parameters: intersection info and pointer to return luminance
int World::applyPhong(IntersectionInfo* info, Color* L)
{
//ambient light first
L->add(ambientLight);
//add light for each light
for(int i = 0; i < lights.size(); i++)
{
Light* light = lights[i];
// can we see light from point
//calculate ray from point to light
//advance origin point by 0.01 along normal ray so does not hit itself
float alteredOrigin [3] = {};
std::copy(std::begin(info->intersectionLocation), std::end(info->intersectionLocation), std::begin(alteredOrigin));
for(int i = 0; i < 3; i++)
{
float x = 0;
x = info->normal[i];
alteredOrigin[i] += x*0.001;
}
Ray ray;
std::copy(std::begin(alteredOrigin), std::end(alteredOrigin), std::begin(ray.origin));
float dir [3] = {light->center[X_AXIS] - ray.origin[X_AXIS], light->center[Y_AXIS] - ray.origin[Y_AXIS], light->center[Z_AXIS] - ray.origin[Z_AXIS]};
//normalize the direction
float magnitude = sqrt(pow(dir[X_AXIS], 2) + pow(dir[Y_AXIS], 2) + pow(dir[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
dir[i] = dir[i]/magnitude;
}
std::copy(std::begin(dir), std::end(dir), std::begin(ray.direction));
//check if light is blocked
int lightBlocked = 0;
for(Object* obj : objects)
{
IntersectionInfo* lightInfo = new struct IntersectionInfo;
float dist = obj->intersect(lightInfo, ray);
if(dist != 1 && dist > 0 && lightInfo->mat.kT != 1.0){
const char* type = typeid(*obj).name();
lightBlocked = 1;
break;
}
delete lightInfo;
}
if(lightBlocked)
{
continue;
}
//now we know that we can actually see the light!!
//get dir of s (incoming light direction)
float intersection [3] = {0,0,0};
std::copy(std::begin(info->intersectionLocation), std::end(info->intersectionLocation), std::begin(intersection));
float incomingLightDir [3] = {light->center[X_AXIS] - intersection[X_AXIS], light->center[Y_AXIS]-intersection[Y_AXIS] , light->center[Z_AXIS]-intersection[Z_AXIS]};
//normalize the direction
float incomingMag = sqrt(pow(incomingLightDir[X_AXIS], 2) + pow(incomingLightDir[Y_AXIS], 2) + pow(incomingLightDir[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
incomingLightDir[i] = incomingLightDir[i]/incomingMag;
}
float length = sqrt(pow(incomingLightDir[X_AXIS], 2) + pow(incomingLightDir[Y_AXIS], 2) + pow(incomingLightDir[Z_AXIS], 2));
if(length < 0.999 || length > 1.001 )
{
printf("INCORRECT NORMALIZATION");
}
float normal_incoming_cross = dotProduct(incomingLightDir, info->normal);
if(normal_incoming_cross < 0)
{
normal_incoming_cross = 0;
}
normal_incoming_cross = normal_incoming_cross * info->mat.kD;
Color* color = new Color();
*color = light->mat.color;
color->red = color->red*normal_incoming_cross;
color->green = color->green*normal_incoming_cross;
color->blue = color->blue*normal_incoming_cross;
L->add(color);
delete color;
//calculate refelction vector
float reflectionVector [3] = {};
float normalLength = sqrt(pow(info->normal[X_AXIS], 2) + pow(info->normal[Y_AXIS], 2) + pow(info->normal[Z_AXIS], 2));
float temp = 2*dotProduct(incomingLightDir, info->normal)/pow(normalLength, 2);
std::copy(std::begin(info->normal), std::end(info->normal), reflectionVector);
for(int i = 0; i < 3; i++)
{
reflectionVector[i] = reflectionVector[i] * temp;
reflectionVector[i] = incomingLightDir[i] - reflectionVector[i];
}
//normalize reflection vector
float reflectionMag = sqrt(pow(reflectionVector[X_AXIS], 2) + pow(reflectionVector[Y_AXIS], 2) + pow(reflectionVector[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
reflectionVector[i] = reflectionVector[i]/reflectionMag;
}
//reverse of that
float viewingDir [3] = {};
std::copy(std::begin(camera->viewpoint), std::end(camera->viewpoint), viewingDir);
for(int i = 0; i < 3; i++){
viewingDir[i] -= info->intersectionLocation[i];
}
//normalize viewing direction
float viewingMag = sqrt(pow(viewingDir[X_AXIS], 2) + pow(viewingDir[Y_AXIS], 2) + pow(viewingDir[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
viewingDir[i] = viewingDir[i]/viewingMag;
}
// to kE as exponent
float specularScalar = pow(dotProduct(reflectionVector, viewingDir), info->mat.kE);
specularScalar = specularScalar * info->mat.kS;
Color* specColor = new Color();
*specColor = light->mat.color;
specColor->scale(specularScalar);
//add diffuse
L->add(specColor);
delete specColor;
if(L->red < 0 || L->blue < 0 || L->green < 0)
{
printf("\nnegative color error");
}
}
//tone reproduction
}
void World::getReflectionVector(IntersectionInfo* info, Ray incomingRay, Ray* reflectionRay)
{
//calculate refelction vector
float normalLength = sqrt(pow(info->normal[X_AXIS], 2) + pow(info->normal[Y_AXIS], 2) + pow(info->normal[Z_AXIS], 2));
// float temp = 2*dotProduct(reflectionRay.direction, info->normal)/pow(normalLength, 2);
float temp = dotProduct(incomingRay.direction, info->normal);
temp = temp / pow(normalLength, 2);
for(int i = 0; i < 3; i++)
{
reflectionRay->direction[i] = info->normal[i] * temp;
reflectionRay->direction[i] = reflectionRay->direction[i]*2;
reflectionRay->direction[i] = incomingRay.direction[i] - reflectionRay->direction[i];
}
std::copy(std::begin(info->intersectionLocation), std::end(info->intersectionLocation), reflectionRay->origin);
//normalize vector
float magnitude = sqrt(pow(reflectionRay->direction[X_AXIS], 2) + pow(reflectionRay->direction[Y_AXIS], 2) + pow(reflectionRay->direction[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
reflectionRay->direction[i] = reflectionRay->direction[i]/magnitude;
}
}
void World::getTransmissionVector(IntersectionInfo *info, Ray incomingRay, Ray *transmissionRay)
{
//set new transmission origin slightly offset into sphere
std::copy(std::begin(info->intersectionLocation), std::end(info->intersectionLocation), std::begin(transmissionRay->origin));
//get indices of refraction in place coming from and entering into (ni and nt)
float nI = incomingRay.originRefraction;
float nT = info->mat.iR;
float nRatio = nI/nT;
//calculate refelction vector
for(int i = 0; i < 3; i++)
{
transmissionRay->direction[i] = incomingRay.direction[i]*nRatio;
}
//get negative incoming
float negIncoming [3];
for(int i = 0; i < 3; i++)
{
negIncoming[i] = incomingRay.direction[i]*-1;
}
float cosTheta = dotProduct(negIncoming, info->normal);
float sinTheta = pow(nRatio, 2)*(1-pow(cosTheta,2));
//check for total internal reflection
if(sinTheta > nRatio)
{
getReflectionVector(info, incomingRay, transmissionRay);
return;
}
float temp = nRatio*cosTheta - sqrt(1 - pow(sinTheta,2));
for(int i = 0; i < 3; i++){
transmissionRay->direction[i] += info->normal[i]*temp;
}
//normalize vector
float magnitude = sqrt(pow(transmissionRay->direction[X_AXIS], 2) + pow(transmissionRay->direction[Y_AXIS], 2) + pow(transmissionRay->direction[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
transmissionRay->direction[i] = transmissionRay->direction[i]/magnitude;
}
transmissionRay->originRefraction = nT;
}