-
Notifications
You must be signed in to change notification settings - Fork 0
/
BVHNode.cpp
63 lines (46 loc) · 1.33 KB
/
BVHNode.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
#include "precomp.h"
BVHNode::BVHNode()
{
this->isLeaf = false;
this->boundingBox = new BoundingBox();
}
bool BVHNode::intersects(Ray* ray)
{
float tmin, tmax, txmin, txmax, tymin, tymax, tzmin, tzmax;
txmin = (this->boundingBox->min.x - ray->origin.x) * ray->invertedDirection.x;
txmax = (this->boundingBox->max.x - ray->origin.x) * ray->invertedDirection.x;
tymin = (this->boundingBox->min.y - ray->origin.y) * ray->invertedDirection.y;
tymax = (this->boundingBox->max.y - ray->origin.y) * ray->invertedDirection.y;
tzmin = (this->boundingBox->min.z - ray->origin.z) * ray->invertedDirection.z;
tzmax = (this->boundingBox->max.z - ray->origin.z) * ray->invertedDirection.z;
tmin = min(txmin, txmax);
tmax = max(txmin, txmax);
tmin = max(tmin, min(tymin, tymax));
tmax = min(tmax, max(tymin, tymax));
tmin = max(tmin, min(tzmin, tzmax));
tmax = min(tmax, max(tzmin, tzmax));
// early out
if (tmin > ray->t)
return false;
return tmax >= tmin && tmax >= 0;
}
void BVHNode::translate(vec3 vector)
{
this->boundingBox->translate(vector);
if (this->isLeaf) return;
this->left->translate(vector);
this->right->translate(vector);
}
void BVHNode::destroy()
{
if (this->isLeaf)
{
delete this->boundingBox;
delete this;
return;
}
this->left->destroy();
this->right->destroy();
delete this->boundingBox;
delete this;
}