aboutsummaryrefslogtreecommitdiff
path: root/src/hittable/mod.rs
blob: e888c71f17a48863eae5ab983560555648c89d82 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
pub mod instance;
mod bvh_node;
pub use bvh_node::BVHNode;
mod constant_medium;
pub use constant_medium::ConstantMedium;
mod hittable_box;
pub use hittable_box::HittableBox;
mod hittable_list;
pub use hittable_list::HittableList;
mod xy_rect;
pub use xy_rect::XYRect;
mod xz_rect;
pub use xz_rect::XZRect;
mod yz_rect;
pub use yz_rect::YZRect;
mod sphere;
pub use sphere::Sphere;
mod triangle;
pub use triangle::Triangle;
mod model;
pub use model::Model;
mod aabb;

use std::sync::Arc;

use crate::ray::Ray;
use crate::vec3::{Point3, Vec3};
use crate::material::Material;
use aabb::AABB;

#[derive(Clone)]
pub struct HitRecord {
    pub p: Point3,
    pub normal: Vec3,
    pub material: Option<Arc<dyn Material>>,
    pub t: f64,
    pub u: f64,
    pub v: f64,
    pub front_face: bool,
}

impl HitRecord {
    pub fn new() -> HitRecord {
        HitRecord {
            p: Point3 {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            normal: Vec3 {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            material: None,
            t: 0.0,
            u: 0.0,
            v: 0.0,
            front_face: false,
        }
    }

    pub 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.clone()
        } else {
            -outward_normal
        };
    }
}

pub trait Hittable: Send + Sync {
    fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>;
    fn bounding_box(&self, time_start: f64, time_end: f64) -> Option<AABB>;
}