intersections: add inside/outside to prepare_computations.
This commit is contained in:
parent
2316df896a
commit
9924330f98
@ -1,6 +1,10 @@
|
|||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
|
|
||||||
use crate::{rays::Ray, spheres::Sphere, tuples::Tuple};
|
use crate::{
|
||||||
|
rays::Ray,
|
||||||
|
spheres::Sphere,
|
||||||
|
tuples::{dot, Tuple},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Intersection<'i> {
|
pub struct Intersection<'i> {
|
||||||
@ -136,6 +140,7 @@ pub struct PrecomputedData<'i> {
|
|||||||
pub point: Tuple,
|
pub point: Tuple,
|
||||||
pub eyev: Tuple,
|
pub eyev: Tuple,
|
||||||
pub normalv: Tuple,
|
pub normalv: Tuple,
|
||||||
|
pub inside: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Precomputes data common to all intersections.
|
/// Precomputes data common to all intersections.
|
||||||
@ -149,6 +154,7 @@ pub struct PrecomputedData<'i> {
|
|||||||
/// tuples::Tuple,
|
/// tuples::Tuple,
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
|
/// // Precomputing the state of an intersection.
|
||||||
/// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 0., 1.));
|
/// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 0., 1.));
|
||||||
/// let shape = Sphere::default();
|
/// let shape = Sphere::default();
|
||||||
/// let i = Intersection::new(4., &shape);
|
/// let i = Intersection::new(4., &shape);
|
||||||
@ -158,14 +164,40 @@ pub struct PrecomputedData<'i> {
|
|||||||
/// assert_eq!(comps.point, Tuple::point(0., 0., -1.));
|
/// assert_eq!(comps.point, Tuple::point(0., 0., -1.));
|
||||||
/// assert_eq!(comps.eyev, Tuple::vector(0., 0., -1.));
|
/// assert_eq!(comps.eyev, Tuple::vector(0., 0., -1.));
|
||||||
/// assert_eq!(comps.normalv, Tuple::vector(0., 0., -1.));
|
/// assert_eq!(comps.normalv, Tuple::vector(0., 0., -1.));
|
||||||
|
///
|
||||||
|
/// // The hit, when an intersection occurs on the outside.
|
||||||
|
/// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 0., 1.));
|
||||||
|
/// let shape = Sphere::default();
|
||||||
|
/// let i = Intersection::new(4., &shape);
|
||||||
|
/// let comps = prepare_computations(&i, &r);
|
||||||
|
/// assert_eq!(comps.inside, false);
|
||||||
|
///
|
||||||
|
/// // The hit, when an intersection occurs on the inside.
|
||||||
|
/// let r = Ray::new(Tuple::point(0., 0., 0.), Tuple::vector(0., 0., 1.));
|
||||||
|
/// let shape = Sphere::default();
|
||||||
|
/// let i = Intersection::new(1., &shape);
|
||||||
|
/// let comps = prepare_computations(&i, &r);
|
||||||
|
/// assert_eq!(comps.point, Tuple::point(0., 0., 1.));
|
||||||
|
/// assert_eq!(comps.eyev, Tuple::vector(0., 0., -1.));
|
||||||
|
/// assert_eq!(comps.inside, true);
|
||||||
|
//// // Normal would have been (0, 0, 1), but is inverted when inside.
|
||||||
|
/// assert_eq!(comps.normalv, Tuple::vector(0., 0., -1.));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn prepare_computations<'i>(i: &'i Intersection, r: &Ray) -> PrecomputedData<'i> {
|
pub fn prepare_computations<'i>(i: &'i Intersection, r: &Ray) -> PrecomputedData<'i> {
|
||||||
let point = r.position(i.t);
|
let point = r.position(i.t);
|
||||||
|
let normalv = i.object.normal_at(point);
|
||||||
|
let eyev = -r.direction;
|
||||||
|
let (inside, normalv) = if dot(normalv, eyev) < 0. {
|
||||||
|
(true, -normalv)
|
||||||
|
} else {
|
||||||
|
(false, normalv)
|
||||||
|
};
|
||||||
PrecomputedData {
|
PrecomputedData {
|
||||||
t: i.t,
|
t: i.t,
|
||||||
object: i.object,
|
object: i.object,
|
||||||
point,
|
point,
|
||||||
eyev: -r.direction,
|
normalv,
|
||||||
normalv: i.object.normal_at(point),
|
inside,
|
||||||
|
eyev,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user