-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBVH.cpp
executable file
·144 lines (123 loc) · 4.19 KB
/
BVH.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
#include "BVH.hpp"
#include <algorithm>
#include <cassert>
BVHAccel::BVHAccel(std::vector<Object *> p, int maxPrimsInNode,
SplitMethod splitMethod)
: maxPrimsInNode(std::min(255, maxPrimsInNode)), splitMethod(splitMethod),
primitives(std::move(p)) {
time_t start, stop;
time(&start);
if(primitives.empty())
return;
root = recursiveBuild(primitives);
time(&stop);
double diff = difftime(stop, start);
int hrs = (int) diff / 3600;
int mins = ((int) diff / 60) - (hrs * 60);
int secs = (int) diff - (hrs * 3600) - (mins * 60);
printf(
"\rBVH Generation complete: \nTime Taken: %i hrs, %i mins, %i secs\n\n",
hrs, mins, secs);
}
BVHBuildNode *BVHAccel::recursiveBuild(std::vector<Object *> objects) {
BVHBuildNode *node = new BVHBuildNode();
// Compute bounds of all primitives in BVH node
Bounds3 bounds;
for(int i = 0; i < objects.size(); ++i)
bounds = Union(bounds, objects[i]->getBounds());
if(objects.size() == 1) {
// Create leaf _BVHBuildNode_
node->bounds = objects[0]->getBounds();
node->object = objects[0];
node->left = nullptr;
node->right = nullptr;
node->area = objects[0]->getArea();
return node;
} else if(objects.size() == 2) {
node->left = recursiveBuild(std::vector{objects[0]});
node->right = recursiveBuild(std::vector{objects[1]});
node->bounds = Union(node->left->bounds, node->right->bounds);
node->area = node->left->area + node->right->area;
return node;
} else {
Bounds3 centroidBounds;
for(int i = 0; i < objects.size(); ++i)
centroidBounds =
Union(centroidBounds, objects[i]->getBounds().Centroid());
int dim = centroidBounds.maxExtent();
switch(dim) {
case 0:
std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) {
return f1->getBounds().Centroid().x <
f2->getBounds().Centroid().x;
});
break;
case 1:
std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) {
return f1->getBounds().Centroid().y <
f2->getBounds().Centroid().y;
});
break;
case 2:
std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) {
return f1->getBounds().Centroid().z <
f2->getBounds().Centroid().z;
});
break;
}
auto beginning = objects.begin();
auto middling = objects.begin() + (objects.size() / 2);
auto ending = objects.end();
auto leftshapes = std::vector<Object *>(beginning, middling);
auto rightshapes = std::vector<Object *>(middling, ending);
assert(objects.size() == (leftshapes.size() + rightshapes.size()));
node->left = recursiveBuild(leftshapes);
node->right = recursiveBuild(rightshapes);
node->bounds = Union(node->left->bounds, node->right->bounds);
node->area = node->left->area + node->right->area;
}
return node;
}
Intersection BVHAccel::Intersect(const Ray &ray) const {
Intersection isect;
if(!root)
return isect;
isect = BVHAccel::getIntersection(root, ray);
return isect;
}
Intersection BVHAccel::getIntersection(BVHBuildNode *node, const Ray &ray) const {
// Traverse the BVH to find intersection
Intersection isect;
if(!node || !node->bounds.IntersectP(ray, ray.direction_inv, {ray.direction.x < 0, ray.direction.y < 0, ray.direction.z < 0}))
return isect;
if(node->left == nullptr && node->right == nullptr) {
return node->object->getIntersection(ray);
}
Intersection leftIsect = getIntersection(node->left, ray);
Intersection rightIsect = getIntersection(node->right, ray);
if(leftIsect.happened && rightIsect.happened) {
return leftIsect.distance < rightIsect.distance ? leftIsect : rightIsect;
} else if(leftIsect.happened) {
return leftIsect;
} else if(rightIsect.happened) {
return rightIsect;
} else {
return {};
}
}
void BVHAccel::getSample(BVHBuildNode *node, float p, Intersection &pos, float &pdf) {
if(node->left == nullptr || node->right == nullptr) {
node->object->Sample(pos, pdf);
pdf *= node->area;
return;
}
if(p < node->left->area)
getSample(node->left, p, pos, pdf);
else
getSample(node->right, p - node->left->area, pos, pdf);
}
void BVHAccel::Sample(Intersection &pos, float &pdf) {
float p = std::sqrt(get_random_float()) * root->area;
getSample(root, p, pos, pdf);
pdf /= root->area;
}