diff options
author | Julian T <julian@jtle.dk> | 2021-07-31 14:02:52 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-07-31 14:02:52 +0200 |
commit | cee9bcf4a2c8ffbfe6487f7886b2247eaba1c92c (patch) | |
tree | 7fd405d9a3f6351d6341706fba50a8616d6c3a84 /src/world | |
parent | 55044e56304c8484b8ff52f362ab1c66c9c5ca93 (diff) |
Better documentation and cleanup
Diffstat (limited to 'src/world')
-rw-r--r-- | src/world/hittable.rs | 22 | ||||
-rw-r--r-- | src/world/mod.rs | 7 | ||||
-rw-r--r-- | src/world/shapes/sphere.rs | 12 |
3 files changed, 28 insertions, 13 deletions
diff --git a/src/world/hittable.rs b/src/world/hittable.rs index c5a353e..e11a3bc 100644 --- a/src/world/hittable.rs +++ b/src/world/hittable.rs @@ -7,16 +7,26 @@ pub struct Intersection<'a> { /// Normal vector at intersection pub n: Vector3f, pub p: Vector3f, + pub front: bool, pub t: Float, pub m: Option<&'a dyn Material>, } -impl Intersection<'_> { - pub fn norm_against_ray(&self, r: &Ray) -> Vector3f { - if self.n.dot(&r.direction) < 0.0 { - self.n - } else { - -self.n +impl<'a> Intersection<'a> { + pub fn new(out_normal: Vector3f, point: Vector3f, ray: &Ray, t: Float) -> Self { + let front = ray.direction.dot(&out_normal) < 0.0; + Intersection { + n: { if front { out_normal } else { -out_normal } }, + front, + p: point, + m: None, + t, + } + } + + pub fn add_material_if_none(&mut self, mat: &'a dyn Material) { + if let None = self.m { + self.m = Some(mat); } } } diff --git a/src/world/mod.rs b/src/world/mod.rs index 53d8ad3..3a09522 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -27,7 +27,12 @@ impl Object { impl Hittable for Object { fn intersect(&self, ray: &Ray) -> Option<Intersection> { - self.shape.intersect(ray).map(|mut i| {i.m = Some(self.mat.as_ref()); i}) + if let Some(mut inter) = self.shape.intersect(ray) { + inter.add_material_if_none(self.mat.as_ref()); + Some(inter) + } else { + None + } } fn bounding_box(&self) -> Bound3f { diff --git a/src/world/shapes/sphere.rs b/src/world/shapes/sphere.rs index e3348de..9ecedd6 100644 --- a/src/world/shapes/sphere.rs +++ b/src/world/shapes/sphere.rs @@ -42,12 +42,12 @@ impl Hittable for Sphere { return None } let w = ray.at(distance); - Some(Intersection { - n: self.norm_at(&w), - p: w, - t: distance, - m: None, - }) + Some(Intersection::new( + self.norm_at(&w), + w, + ray, + distance, + )) } } |