-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhittablelist.rs
52 lines (46 loc) · 1.33 KB
/
hittablelist.rs
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
use cgmath::Vector3;
use crate::{ray::Ray, material::Material};
pub struct HitRecord <'a> {
pub p: Vector3<f64>,
pub normal: Vector3<f64>,
pub t: f64,
pub front_face: bool,
pub material: &'a dyn Material
}
impl <'a> HitRecord <'a> {
pub fn set_face_normal(&mut self, r: &Ray, outward_normal: Vector3<f64>) {
self.front_face = cgmath::dot(r.direction, outward_normal) < 0.0;
self.normal = match self.front_face {
true => outward_normal,
false => -outward_normal,
}
}
}
pub trait Hittable {
fn hit(&self, r: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>;
}
#[derive(Default)]
pub struct HittableList {
objects: Vec<Box<dyn Hittable + Send + Sync>>,
}
impl HittableList {
pub fn add(&mut self, obj: impl Hittable + Send + Sync + 'static) {
self.objects.push(Box::new(obj));
}
}
impl Hittable for HittableList {
fn hit(&self, r: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
let mut closest_so_far = t_max;
let mut rec: Option<HitRecord> = None;
for obj in &self.objects {
match obj.hit(r, t_min, closest_so_far) {
Some(r) => {
closest_so_far = r.t;
rec = Some(r);
}
None => (),
}
}
rec
}
}