Repository navigation
Expand file tree
/
Copy pathBVH.cpp
More file actions
154 lines (132 loc) · 4.87 KB
/
Copy pathBVH.cpp
File metadata and controls
154 lines (132 loc) · 4.87 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
#include <algorithm>
#include <cassert>
#include "BVH.hpp"
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);
}
BVHAccel::BVHAccel(std::vector<Object*> p) :primitives(std::move(p)), maxPrimsInNode(1), splitMethod(SplitMethod::NAIVE)
{
if (primitives.empty())
return;
root = recursiveBuild(primitives);
printf("Object BVH Generation complete: \n\n");
}
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<Object *>{objects[0]});
node->right = recursiveBuild(std::vector<Object *>{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
{
// TODO Traverse the BVH to find intersection
Intersection intersect;
Vector3f invdir(1./ray.direction.x,1./ray.direction.y,1./ray.direction.z);
std::array<int, 3> dirIsNeg;
dirIsNeg[0] = ray.direction.x<0;
dirIsNeg[1] = ray.direction.y<0;
dirIsNeg[2] = ray.direction.z<0;
if(!node || !node->bounds.IntersectP(ray,ray.direction_inv,dirIsNeg))
{
return intersect;
}
if(!node->right && !node->left)
return node->object->getIntersection(ray);
Intersection isect2= getIntersection(node->left,ray);
Intersection isect1= getIntersection(node->right,ray);
return isect1.distance < isect2.distance ? isect1:isect2;
}
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;
}