From 72c6944ab946a1db4d1dafac3f94d393a27663c8 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 17 Jul 2021 15:58:57 -0700 Subject: [PATCH] world: implement World::shade_hit. --- rtchallenge/src/world.rs | 49 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/rtchallenge/src/world.rs b/rtchallenge/src/world.rs index 126fd06..4991a2b 100644 --- a/rtchallenge/src/world.rs +++ b/rtchallenge/src/world.rs @@ -1,7 +1,7 @@ use crate::{ - intersections::Intersections, + intersections::{Intersections, PrecomputedData}, lights::PointLight, - materials::Material, + materials::{lighting, Material}, matrices::Matrix4x4, rays::Ray, spheres::{intersect, Sphere}, @@ -55,6 +55,7 @@ impl World { } /// Intesects the ray with this world. + /// /// # Examples /// ``` /// use rtchallenge::{rays::Ray, tuples::Tuple, world::World}; @@ -81,4 +82,48 @@ impl World { }); Intersections::new(xs) } + + /// Compute shaded value for given precomputation. + /// + /// # Examples + /// ``` + /// use rtchallenge::{ + /// intersections::{prepare_computations, Intersection}, + /// lights::PointLight, + /// rays::Ray, + /// tuples::{Color, Tuple}, + /// world::World, + /// }; + /// + /// // Shading an intersection. + /// let w = World::test_world(); + /// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 0., 1.)); + /// let s = &w.objects[0]; + /// let i = Intersection::new(4., &s); + /// let comps = prepare_computations(&i, &r); + /// let c = w.shade_hit(&comps); + /// assert_eq!(c, Color::new(0.38066, 0.47583, 0.2855)); + /// + /// // Shading an intersection from the inside. + /// let mut w = World::test_world(); + /// w.light = Some(PointLight::new( + /// Tuple::point(0., 0.25, 0.), + /// Color::new(1., 1., 1.), + /// )); + /// let r = Ray::new(Tuple::point(0., 0., 0.), Tuple::vector(0., 0., 1.)); + /// let s = &w.objects[1]; + /// let i = Intersection::new(0.5, &s); + /// let comps = prepare_computations(&i, &r); + /// let c = w.shade_hit(&comps); + /// assert_eq!(c, Color::new(0.90498, 0.90498, 0.90498)); + /// ``` + pub fn shade_hit(&self, comps: &PrecomputedData) -> Color { + lighting( + &comps.object.material, + &self.light.as_ref().expect("World has no lights"), + comps.point, + comps.eyev, + comps.normalv, + ) + } }