summaryrefslogtreecommitdiff
path: root/racer-tracer/src/geometry.rs
blob: aa1c18c99358345438eb060b259a6ffcd0ec7fe3 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub mod sphere;

use crate::ray::Ray;
use crate::vec3::Vec3;

pub struct HitRecord {
    pub point: Vec3,
    pub normal: Vec3,
    pub t: f64,
    pub front_face: bool,
    pub color: Vec3,
}

impl HitRecord {
    fn new(point: Vec3, t: f64, color: Vec3) -> Self {
        Self {
            point,
            normal: Vec3::default(),
            t,
            front_face: true,
            color,
        }
    }

    fn set_face_normal(&mut self, ray: &Ray, outward_normal: Vec3) {
        self.front_face = ray.direction().dot(&outward_normal) < 0.0;
        self.normal = if self.front_face {
            outward_normal
        } else {
            -outward_normal
        };
    }
}

impl Default for HitRecord {
    fn default() -> Self {
        HitRecord {
            point: Vec3::default(),
            normal: Vec3::default(),
            t: 0.0,
            front_face: true,
            color: Vec3::default(),
        }
    }
}

pub trait Hittable {
    //pub trait Hittable {
    fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>;

    // Manual ugly clone since forcing on Clone would make it a super trait.
    // Clone requires Sized and super traits cannot be Sized.
    fn clone_box(&self) -> Box<dyn Hittable>;
}