From 86ad7845219e8db06fe47b62794180e1b40f90a5 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 1 Aug 2021 22:55:07 +0200 Subject: Implement dielectric material from RTIAW --- src/world/shapes/sphere.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/world') diff --git a/src/world/shapes/sphere.rs b/src/world/shapes/sphere.rs index 9ecedd6..1df9c35 100644 --- a/src/world/shapes/sphere.rs +++ b/src/world/shapes/sphere.rs @@ -1,7 +1,7 @@ //! Implements sphere //! //! Spheres are relatively easy to calculate intersections between -use crate::Float; +use crate::{Float, NEAR_ZERO}; use crate::core::{Ray, Vector3f, Bound3f}; use crate::world::{Hittable, Intersection}; @@ -37,10 +37,17 @@ impl Hittable for Sphere { if disc < 0.0 { None } else { - let distance = (-half_b - disc.sqrt()) / a; - if distance < 0.0 { - return None + let disc_sqrt = disc.sqrt(); + + let mut distance = -half_b - disc_sqrt; + if distance <= NEAR_ZERO { + distance = -half_b + disc_sqrt; + } + if distance <= NEAR_ZERO { + return None; } + + distance /= a; let w = ray.at(distance); Some(Intersection::new( self.norm_at(&w), -- cgit v1.2.3