Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ds/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Aabb {
}

impl Renderable for Aabb {
fn intersects(&self, ray: &ds::Ray) -> bool {
fn intersects(&self, ray: &ds::Ray) -> Option<f64> {
let axes = [ // each slab we check
(&self.x, ray.origin.x, ray.direction.x),
(&self.y, ray.origin.y, ray.direction.y),
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/object/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64> {
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;
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/object/renderable.rs
Original file line number Diff line number Diff line change
@@ -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<f64>;
}

pub trait ToGpu<T> {
Expand Down
9 changes: 6 additions & 3 deletions src/object/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,24 @@ impl Renderable for Sphere {
self.center
}

fn intersects(&self, ray: &ds::Ray) -> bool {
fn intersects(&self, ray: &ds::Ray) -> Option<f64> {
let oc = self.center - ray.origin;
let a = ray.direction.dot(&ray.direction);
let h = ray.direction.dot(&oc);
let c = oc.dot(&oc) - self.radius * self.radius;
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
}
}

Expand Down
Loading