diff options
author | Julian T <julian@jtle.dk> | 2021-02-06 17:27:42 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-02-06 17:27:42 +0100 |
commit | 0d5e6bd9363d5ed5c4f28174819fc0f5fd9aa586 (patch) | |
tree | f9ecafe7350ad616486e509ed55904295f505f83 /src/scene/shapes/sphere.rs | |
parent | 1e83ea211055eb234b89c69b5d03602e3fcb98fb (diff) |
Reorganized scene module, and fixed bug in sphere intersect
Diffstat (limited to 'src/scene/shapes/sphere.rs')
-rw-r--r-- | src/scene/shapes/sphere.rs | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/scene/shapes/sphere.rs b/src/scene/shapes/sphere.rs index da7f321..acca2b7 100644 --- a/src/scene/shapes/sphere.rs +++ b/src/scene/shapes/sphere.rs @@ -3,7 +3,7 @@ //! Spheres are relatively easy to calculate intersections between use crate::Float; use crate::core::{Ray, Vector3f}; -use super::Shape; +use crate::scene::{Hittable, Intersection}; pub struct Sphere { radius: Float, @@ -17,11 +17,17 @@ impl Sphere { center, } } + + fn norm_at(&self, point: &Vector3f) -> Vector3f { + let mut v = *point - self.center; + v /= self.radius; + v + } } -impl Shape for Sphere { +impl Hittable for Sphere { // Implementation from ray tracing in a weekend - fn intersect(&self, ray: &Ray) -> Option<Float> { + fn intersect(&self, ray: &Ray) -> Option<Intersection> { let oc = ray.origin - self.center; let a = ray.direction.len_squared(); let half_b = oc.dot(&ray.direction); @@ -29,18 +35,20 @@ impl Shape for Sphere { let disc = half_b*half_b - a*c; if disc < 0.0 { - None + return None } else { - Some( (-half_b - disc.sqrt()) / a) + let distance = (-half_b - disc.sqrt()) / a; + if distance < 0.0 { + return None + } + let w = ray.at(distance); + Some(Intersection { + n: self.norm_at(&w), + p: w, + }) } } - - fn norm_at(&self, point: &Vector3f) -> Vector3f { - let mut v = *point - self.center; - v /= self.radius; - v - } } #[cfg(test)] |