From a39c136f5fcb0d30d1d015d6e1fb9ce15e9c87f9 Mon Sep 17 00:00:00 2001 From: prushton2 Date: Sat, 11 Apr 2026 20:47:31 -0400 Subject: [PATCH] intersection returns distance along ray --- src/ds/aabb.rs | 8 ++++---- src/object/quad.rs | 10 +++++----- src/object/renderable.rs | 2 +- src/object/sphere.rs | 9 ++++++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/ds/aabb.rs b/src/ds/aabb.rs index e3500ce..610623b 100644 --- a/src/ds/aabb.rs +++ b/src/ds/aabb.rs @@ -47,7 +47,7 @@ impl Aabb { } impl Renderable for Aabb { - fn intersects(&self, ray: &ds::Ray) -> bool { + fn intersects(&self, ray: &ds::Ray) -> Option { let axes = [ // each slab we check (&self.x, ray.origin.x, ray.direction.x), (&self.y, ray.origin.y, ray.direction.y), @@ -61,7 +61,7 @@ impl Renderable for Aabb { if dir.abs() < f64::EPSILON { // Ray is parallel to this slab — check if origin is inside it if origin < interval.min() || origin > interval.max() { - return false; + return None; } continue; } @@ -76,10 +76,10 @@ impl Renderable for Aabb { t_exit = t_exit.min(t_far); if t_enter > t_exit { - return false; + return None; } } - return true + return Some(t_enter); } fn as_any(&self) -> &dyn std::any::Any { diff --git a/src/object/quad.rs b/src/object/quad.rs index 11824d1..90868c8 100644 --- a/src/object/quad.rs +++ b/src/object/quad.rs @@ -55,16 +55,16 @@ impl Renderable for Quad { self.q + self.u/2.0 + self.v/2.0 } - fn intersects(&self, ray: &ds::Ray) -> bool { + fn intersects(&self, ray: &ds::Ray) -> Option { let denominator = self.normal.dot(&ray.direction); if denominator.abs() < 0.00000001 { - return false; + return None; } let t = (self.d - self.normal.dot(&ray.origin)) / denominator; - if t < 0.0 { return false; } + if t < 0.0 { return None; } let intersection = ray.at(t); let planar_hit = intersection - self.q; @@ -76,10 +76,10 @@ impl Renderable for Quad { let beta = self.v.dot(&planar_hit) / v_len_sq; if alpha < 0.0 || alpha > 1.0 || beta < 0.0 || beta > 1.0 { - return false; + return None; } - return true + return Some(t) } } diff --git a/src/object/renderable.rs b/src/object/renderable.rs index 888a1f5..c5941ab 100644 --- a/src/object/renderable.rs +++ b/src/object/renderable.rs @@ -1,7 +1,7 @@ pub trait Renderable: Send + Sync { fn as_any(&self) -> &dyn std::any::Any; fn center(&self) -> crate::ds::Vector3; - fn intersects(&self, ray: &crate::ds::Ray) -> bool; + fn intersects(&self, ray: &crate::ds::Ray) -> Option; } pub trait ToGpu { diff --git a/src/object/sphere.rs b/src/object/sphere.rs index 897bac8..d3a75e4 100644 --- a/src/object/sphere.rs +++ b/src/object/sphere.rs @@ -43,7 +43,7 @@ impl Renderable for Sphere { self.center } - fn intersects(&self, ray: &ds::Ray) -> bool { + fn intersects(&self, ray: &ds::Ray) -> Option { let oc = self.center - ray.origin; let a = ray.direction.dot(&ray.direction); let h = ray.direction.dot(&oc); @@ -51,13 +51,16 @@ impl Renderable for Sphere { let discriminant = h*h - a*c; if discriminant < 0.0 { - return false; + return None; } let t1 = (h - discriminant.sqrt()) / a; let t2 = (h + discriminant.sqrt()) / a; - return t1 > 1e-8 || t2 > 1e-8 + if t1 > 1e-8 { return Some(t1) } + if t2 > 1e-8 { return Some(t2) } + + return None } }