feat: Starting bvh raytracing

This commit is contained in:
2026-11-12 22:47:17 +01:00
parent 27f660f6fb
commit e7fbeb7887
7 changed files with 501 additions and 147 deletions
+31 -4
View File
@@ -149,6 +149,7 @@ pub struct Bvh
{
/// List of the bvh's nodes
pub nodes: Vec<BvhNode>,
pub depth: usize,
}
pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
@@ -160,6 +161,7 @@ pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
{
return Bvh {
nodes,
depth: 1,
//prim_indices,
};
}
@@ -174,9 +176,14 @@ pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
0,
));
build_node(0, primitives, &mut nodes, leaf_max);
let mut max_depth = 0usize;
build_node(0, primitives, &mut nodes, leaf_max, 0, &mut max_depth);
println!("Max depth {}", max_depth);
Bvh { nodes }
Bvh {
nodes,
depth: max_depth,
}
}
pub fn build_node<T>(
@@ -184,6 +191,8 @@ pub fn build_node<T>(
primitives: &mut [BvhPrimitive<T>],
nodes: &mut Vec<BvhNode>,
leaf_max: usize,
depth: usize,
max_depth: &mut usize,
)
{
// We only extend a leaf if necessary
@@ -248,12 +257,30 @@ pub fn build_node<T>(
nodes[node_index] = BvhNode::internal(nodes[node_index].aabb, left_index as u32);
// Build children
build_node(left_index, primitives, nodes, leaf_max);
build_node(left_index + 1, primitives, nodes, leaf_max);
build_node(
left_index,
primitives,
nodes,
leaf_max,
depth + 1,
max_depth,
);
build_node(
left_index + 1,
primitives,
nodes,
leaf_max,
depth + 1,
max_depth,
);
}
None =>
{
// Sah says keep as leaf, do nothing
if depth > *max_depth
{
*max_depth = depth;
}
}
}
}