aboutsummaryrefslogtreecommitdiffstats
path: root/src/hittable.rs
blob: 1c618722ff1eee832487cfddf591e0e44abed83d (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
use cgmath::{Vector3, dot};
use crate::material::Material;
use crate::ray::Ray;

pub struct Hit<'a> {
    pub t: f64,
    pub p: Vector3<f64>,
    pub normal: Vector3<f64>,
    pub front_face: bool,
    pub material: &'a (dyn Material)
}

impl<'a> Hit<'a> {
    pub fn new(ray: &Ray, t: f64, out_normal: Vector3<f64>, material: &'a(dyn Material)) -> Hit<'a> {
        let front_face = dot(ray.dir, out_normal) < 0.0;
        let normal = if front_face { out_normal } else { -out_normal };
        let p = ray.at(t);
        Hit { t, p, normal, front_face, material }
    }
}

pub trait Hittable {
    fn is_hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<Hit>;
}