bvh
FreeBodyEngine.graphics.raytrace.bvh
#
CPU-side BVH construction for GLRaytraceShader (graphics/gl33/compute.py).
This is a data-format contract, not a codegen concern: the layout produced here (2 texels/node AABB, one int4 meta/node, a fixed traversal-stack depth on the GLSL side) is fixed by GL33Generator._generate_raytrace_intrinsics(). A simple median-split builder is enough to prove the mechanism works - a surface-area-heuristic (SAH) builder would produce a better-quality BVH for the same primitive count, but is a quality optimization, not a correctness requirement, and is left as a follow-on.
build_bvh(triangles, max_leaf_size=4)
#
Builds a median-split BVH over triangles (shape (N, 3, 3), float32
vertex positions - N triangles, 3 vertices each, 3 floats per vertex).
Returns (aabb_array, meta_array, ordered_triangles):
- aabb_array: (num_nodes2, 4) float32 - 2 texels per node, node i's
AABB min at row 2*i, max at row 2*i+1 (the unused 4th component is
left 0). Node 0 is always the root.
- meta_array: (num_nodes, 4) int32 - (left_child, right_child,
first_prim, prim_count) per node. A leaf has prim_count > 0 and its
primitives are ordered_triangles[first_prim : first_prim+prim_count];
an interior node has prim_count == 0 and real left_child/right_child.
- ordered_triangles: (N, 3, 3) float32 - triangles reordered so each
leaf's primitives are contiguous. Upload this*, not the original
triangles, as the raytrace shader's triangles buffer field -
meta_array's first_prim indices refer to this order.