refactor: cleaner request path + raytracing code
This commit is contained in:
+17
-8
@@ -84,9 +84,11 @@ public struct Bvh : IStructure
|
||||
typedef BvhCursor Cursor;
|
||||
public BvhParameters parameters;
|
||||
|
||||
/// Kept for compatibility with the existing call sites. The restart
|
||||
/// traversal does not need per-thread storage, so this is unused.
|
||||
public uint32_t thread_index;
|
||||
/// Reject nodes/objects whose entry distance is beyond this value.
|
||||
/// Set to FLT_MAX for unbounded primary rays. Distances are in the
|
||||
/// object's local units, which equal world units for the rigid
|
||||
/// rotation/translation transforms used here.
|
||||
public float max_t;
|
||||
|
||||
public BvhCursor begin(Ray world_ray)
|
||||
{
|
||||
@@ -100,14 +102,15 @@ public struct Bvh : IStructure
|
||||
c.leaf_depth = 0;
|
||||
c.have_leaf = false;
|
||||
|
||||
// Early out when the ray misses the whole scene.
|
||||
// Early out when the ray misses the whole scene or the root is beyond
|
||||
// the requested range.
|
||||
let root = this.parameters.nodes[0];
|
||||
let root_t = intersect_aabb(
|
||||
world_ray.origin,
|
||||
c.inv_direction,
|
||||
root.aabb.min,
|
||||
root.aabb.max);
|
||||
c.done = !(root_t.y >= max(root_t.x, 0.));
|
||||
c.done = !(root_t.y >= max(root_t.x, 0.) && root_t.x <= this.max_t);
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -121,6 +124,8 @@ public struct Bvh : IStructure
|
||||
cell.local_ray.origin = float3(0.);
|
||||
cell.local_ray.direction = float3(0.);
|
||||
cell.t = 0.;
|
||||
cell.cube_to_world = mat3_identity();
|
||||
cell.world_origin = float3(0.);
|
||||
|
||||
if (c.done)
|
||||
{
|
||||
@@ -189,8 +194,8 @@ public struct Bvh : IStructure
|
||||
this.parameters.nodes[right].aabb.min,
|
||||
this.parameters.nodes[right].aabb.max);
|
||||
|
||||
let hit_left = t_left.y >= max(t_left.x, 0.);
|
||||
let hit_right = t_right.y >= max(t_right.x, 0.);
|
||||
let hit_left = t_left.y >= max(t_left.x, 0.) && t_left.x <= this.max_t;
|
||||
let hit_right = t_right.y >= max(t_right.x, 0.) && t_right.x <= this.max_t;
|
||||
|
||||
if (hit_left && hit_right)
|
||||
{
|
||||
@@ -258,6 +263,8 @@ public struct Bvh : IStructure
|
||||
cell.local_ray.origin = float3(0.);
|
||||
cell.local_ray.direction = float3(0.);
|
||||
cell.t = 0.;
|
||||
cell.cube_to_world = mat3_identity();
|
||||
cell.world_origin = float3(0.);
|
||||
|
||||
let object = this.parameters.objects[object_index];
|
||||
|
||||
@@ -272,7 +279,7 @@ public struct Bvh : IStructure
|
||||
float3(0.),
|
||||
float3(1.));
|
||||
|
||||
if (t.y < max(t.x, 0.))
|
||||
if (t.y < max(t.x, 0.) || t.x > this.max_t)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -285,6 +292,8 @@ public struct Bvh : IStructure
|
||||
cell.local_ray.origin = local_origin + local_direction * entry_t;
|
||||
cell.local_ray.direction = local_direction;
|
||||
cell.t = entry_t;
|
||||
cell.cube_to_world = float3x3(object.local_to_world);
|
||||
cell.world_origin = mul(object.local_to_world, float4(0., 0., 0., 1.)).xyz;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user