world: implement World::shade_hit.

This commit is contained in:
Bill Thiede 2021-07-17 15:58:57 -07:00
parent 9924330f98
commit 72c6944ab9

View File

@ -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,
)
}
}