diff options
Diffstat (limited to 'src/object.rs')
-rw-r--r-- | src/object.rs | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/object.rs b/src/object.rs index 0da9148..c1bce61 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,4 +1,5 @@ use crate::calculus::calculus::{Point3, Ray, Vec3}; +use crate::interval::Interval; #[derive(Clone)] pub struct HitRecord { @@ -27,7 +28,7 @@ impl HitRecord { } pub trait Hittable { - fn hit(&self, r: &Ray, ray_tmin: f32, ray_tmax: f32, rec: &mut HitRecord) -> bool; + fn hit(&self, r: &Ray, ray_t: Interval, rec: &mut HitRecord) -> bool; } pub struct Sphere { @@ -43,7 +44,7 @@ impl Sphere { } impl Hittable for Sphere { - fn hit(&self, r: &Ray, ray_tmin: f32, ray_tmax: f32, rec: &mut HitRecord) -> bool { + fn hit(&self, r: &Ray, ray_t: Interval, rec: &mut HitRecord) -> bool { let oc = self.center.sub(&r.origin); let a = r.direction.mag_sqr(); let h = r.direction.dot_prod(&oc); @@ -56,9 +57,9 @@ impl Hittable for Sphere { let sqrtd = discriminant.sqrt(); let mut root = (h - sqrtd) / a; - if root <= ray_tmin || ray_tmax <= root { + if root <= ray_t.min || ray_t.max <= root { root = (h + sqrtd) / a; - if root <= ray_tmin || ray_tmax <= root { + if root <= ray_t.min || ray_t.max <= root { return false; } } @@ -89,14 +90,15 @@ impl HittableList { } impl Hittable for HittableList { - fn hit(&self, r: &Ray, ray_tmin: f32, ray_tmax: f32, rec: &mut HitRecord) -> bool { + fn hit(&self, r: &Ray, ray_t: Interval, rec: &mut HitRecord) -> bool { let mut temp_rec = HitRecord::default(); let mut hit_anything = false; - let mut closest_so_far = ray_tmax; + let mut closest_so_far = ray_t.max; self.objects.iter().for_each( |object| { - if object.hit(r, ray_tmin, closest_so_far, &mut temp_rec) { + let interval = Interval::new(ray_t.min, closest_so_far); + if object.hit(r, interval, &mut temp_rec) { hit_anything = true; closest_so_far = temp_rec.t; *rec = temp_rec.clone(); |