MCPcopy Create free account
hub / github.com/Bloom-Engine/engine / intersect_bvh

Function intersect_bvh

tools/bloom-reference/src/main.rs:883–964  ·  view source on GitHub ↗
(ray: &Ray, scene: &Scene, bvh: &Bvh)

Source from the content-addressed store, hash-verified

881}
882
883fn intersect_bvh(ray: &Ray, scene: &Scene, bvh: &Bvh) -> Option<Hit> {
884 let mut closest: Option<Hit> = None;
885 let mut max_t = f32::INFINITY;
886
887 // Fixed-size stack of node indices to visit. Depth 64 supports
888 // BVHs up to 2^64 triangles — way past any scene we'll see. Using
889 // a stack rather than recursion both avoids the call overhead and
890 // makes the traversal order (near-child-first) explicit.
891 let mut stack = [0u32; 64];
892 let mut sp: usize = 1;
893 stack[0] = 0;
894
895 while sp > 0 {
896 sp -= 1;
897 let node = &bvh.nodes[stack[sp] as usize];
898 if intersect_aabb(ray, node.bounds_min, node.bounds_max, max_t).is_none() {
899 continue;
900 }
901 if node.tri_count > 0 {
902 // Leaf: test each triangle.
903 for i in 0..node.tri_count as usize {
904 let tri_idx = bvh.triangle_indices[node.first_triangle as usize + i] as usize;
905 let tri = &scene.triangles[tri_idx];
906 if let Some((t, bary)) = intersect_triangle(ray, tri, max_t) {
907 max_t = t;
908 closest = Some(Hit {
909 t,
910 barycentric: bary,
911 triangle_index: tri_idx as u32,
912 });
913 }
914 }
915 } else {
916 // Internal: visit both children, near first. Pushing the
917 // far child first means the near is popped first.
918 let left = node.first_triangle;
919 let right = left + 1;
920 let ln = &bvh.nodes[left as usize];
921 let rn = &bvh.nodes[right as usize];
922 let lt = intersect_aabb(ray, ln.bounds_min, ln.bounds_max, max_t);
923 let rt = intersect_aabb(ray, rn.bounds_min, rn.bounds_max, max_t);
924 match (lt, rt) {
925 (Some(ld), Some(rd)) => {
926 if ld < rd {
927 if sp < 63 {
928 stack[sp] = right;
929 sp += 1;
930 }
931 if sp < 63 {
932 stack[sp] = left;
933 sp += 1;
934 }
935 } else {
936 if sp < 63 {
937 stack[sp] = left;
938 sp += 1;
939 }
940 if sp < 63 {

Callers 2

visibleFunction · 0.85
trace_pathFunction · 0.85

Calls 2

intersect_aabbFunction · 0.85
intersect_triangleFunction · 0.85

Tested by

no test coverage detected