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
|
// This file is part of the 64k demo project.
// It implements BVH construction and traversal.
#include "3d/bvh.h"
#include <algorithm>
namespace {
struct ObjectInfo {
int index;
AABB aabb;
vec3 centroid;
};
AABB get_world_aabb(const Object3D& obj) {
BoundingVolume local = obj.get_local_bounds();
mat4 model = obj.get_model_matrix();
vec3 corners[8] = {
{local.min.x, local.min.y, local.min.z},
{local.max.x, local.min.y, local.min.z},
{local.min.x, local.max.y, local.min.z},
{local.max.x, local.max.y, local.min.z},
{local.min.x, local.min.y, local.max.z},
{local.max.x, local.min.y, local.max.z},
{local.min.x, local.max.y, local.max.z},
{local.max.x, local.max.y, local.max.z},
};
AABB world;
for (int i = 0; i < 8; ++i) {
vec4 p = model * vec4(corners[i].x, corners[i].y, corners[i].z, 1.0f);
world.expand(p.xyz());
}
return world;
}
int build_recursive(std::vector<BVHNode>& nodes,
std::vector<ObjectInfo>& obj_info, int start, int end) {
int node_idx = (int)nodes.size();
nodes.emplace_back();
AABB bounds;
for (int i = start; i < end; ++i) {
bounds.expand(obj_info[i].aabb);
}
int count = end - start;
if (count == 1) {
// Leaf node
nodes[node_idx].min_x = bounds.min.x;
nodes[node_idx].min_y = bounds.min.y;
nodes[node_idx].min_z = bounds.min.z;
nodes[node_idx].left_idx = -1;
nodes[node_idx].max_x = bounds.max.x;
nodes[node_idx].max_y = bounds.max.y;
nodes[node_idx].max_z = bounds.max.z;
nodes[node_idx].right_idx = obj_info[start].index;
} else {
// Internal node
// Find axis with largest variance (or just largest extent of centroids)
AABB centroid_bounds;
for (int i = start; i < end; ++i) {
centroid_bounds.expand(obj_info[i].centroid);
}
vec3 extent = centroid_bounds.max - centroid_bounds.min;
int axis = 0;
if (extent.y > extent.x)
axis = 1;
if (extent.z > (axis == 0 ? extent.x : extent.y))
axis = 2;
float split = (centroid_bounds.min[axis] + centroid_bounds.max[axis]) * 0.5f;
// Partition
int mid = start;
for (int i = start; i < end; ++i) {
if (obj_info[i].centroid[axis] < split) {
std::swap(obj_info[i], obj_info[mid]);
mid++;
}
}
// Fallback if partition failed
if (mid == start || mid == end) {
mid = start + count / 2;
}
int left = build_recursive(nodes, obj_info, start, mid);
int right = build_recursive(nodes, obj_info, mid, end);
nodes[node_idx].min_x = bounds.min.x;
nodes[node_idx].min_y = bounds.min.y;
nodes[node_idx].min_z = bounds.min.z;
nodes[node_idx].left_idx = left;
nodes[node_idx].max_x = bounds.max.x;
nodes[node_idx].max_y = bounds.max.y;
nodes[node_idx].max_z = bounds.max.z;
nodes[node_idx].right_idx = right;
}
return node_idx;
}
} // namespace
void BVHBuilder::build(BVH& out_bvh, const std::vector<Object3D>& objects) {
out_bvh.nodes.clear();
if (objects.empty())
return;
std::vector<ObjectInfo> obj_info;
for (int i = 0; i < (int)objects.size(); ++i) {
if (objects[i].type == ObjectType::SKYBOX)
continue;
AABB aabb = get_world_aabb(objects[i]);
obj_info.push_back({i, aabb, aabb.center()});
}
if (obj_info.empty())
return;
out_bvh.nodes.reserve(obj_info.size() * 2);
build_recursive(out_bvh.nodes, obj_info, 0, (int)obj_info.size());
}
void BVH::query(const AABB& box, std::vector<int>& out_indices) const {
if (nodes.empty())
return;
std::vector<int> stack;
stack.push_back(0);
while (!stack.empty()) {
int idx = stack.back();
stack.pop_back();
const BVHNode& node = nodes[idx];
AABB node_aabb({node.min_x, node.min_y, node.min_z},
{node.max_x, node.max_y, node.max_z});
if (node_aabb.intersects(box)) {
if (node.left_idx < 0) {
out_indices.push_back(node.right_idx);
} else {
stack.push_back(node.left_idx);
stack.push_back(node.right_idx);
}
}
}
}
|