aboutsummaryrefslogtreecommitdiff
path: root/src/core/hittable.rs
blob: e495d5b6b2d3099d6b15be2e8b22f2c1db9cf3cc (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
use crate::core::{Vector3f, Ray};
use crate::Float;

/// Returns the context of a intersection
pub struct Intersection {
    /// Normal vector at intersection
    pub n: Vector3f,
    pub p: Vector3f,
    pub t: Float,
}

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 {
    fn intersect(&self, ray: &Ray) -> Option<Intersection>;
}