aboutsummaryrefslogtreecommitdiff
path: root/src/world/hittable.rs
blob: c5a353eedbfb8383d2739c8619f613b7825b2d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::core::{Vector3f, Bound3f, Ray};
use crate::Float;
use crate::material::Material;

/// Returns the context of a intersection
pub struct Intersection<'a> {
    /// Normal vector at intersection
    pub n: Vector3f,
    pub p: Vector3f,
    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
        }
    }
}

/// Defines a common trait for objects in the scene
pub trait Hittable: Sync + Send {
    /// Returns the intersection with ray
    fn intersect(&self, ray: &Ray) -> Option<Intersection>;

    /// Returns the axis alligned bounding box containing self
    fn bounding_box(&self) -> Bound3f;
}