-
Notifications
You must be signed in to change notification settings - Fork 0
/
bvh.hpp
66 lines (52 loc) · 1.42 KB
/
bvh.hpp
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
#pragma once
#include <vector>
#include <atomic>
#include <memory>
#include <ctime>
#include "vector.hpp"
#include "global.hpp"
#include "aabb.hpp"
#include "object.hpp"
#include "ray.hpp"
#include "intersection.hpp"
struct BVHBuildNode {
public:
AABB boundingBox;
Object *object;
float area; // only needed by path tracing
BVHBuildNode *left;
BVHBuildNode *right;
public:
BVHBuildNode() {
boundingBox = AABB(Vector3f(0, 0, 0), Vector3f(0, 0, 0));
object = nullptr;
area = 0;
left = nullptr;
right = nullptr;
}
};
/*
Bounding Volume Hierarchy implementation
CORE:
- BVH build (with Surface Area Heuristic)
- ray intersection with BVH
- sample point on BVH
NOTE:
- MeshTriangle is also accelerated by BVH
*/
class BVH {
public:
enum class SplitMethod { NAIVE, SAH };
private:
const SplitMethod splitMethod;
BVHBuildNode *root;
public:
BVH(std::vector<Object *> _objects, SplitMethod _splitMethod = SplitMethod::NAIVE);
~BVH() {}
Intersection intersect(const Ray &ray) const;
void sample(Intersection &position, float &pdf); // only needed by path tracing
private:
BVHBuildNode *recursiveBuild(std::vector<Object *> objects);
Intersection getIntersection(BVHBuildNode *node, const Ray &ray) const;
void getSample(BVHBuildNode *node, float p, Intersection &position, float &pdf); // only needed by path tracing
};