aboutsummaryrefslogtreecommitdiffstats
path: root/src/scene.rs
blob: 368c78403e3e3acf4de12511de32ab3bc44f43ec (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
use crate::hittable::{Hittable, Hit};
use crate::ray::Ray;

pub struct Scene {
    objects: Vec<Box<dyn Hittable>>
}

impl Scene {
    pub fn new() -> Scene {
        Scene { objects: Vec::new() }
    }

    pub fn add(&mut self, obj: Box<dyn Hittable>) {
        self.objects.push(obj)
    }

    pub fn clear(&mut self) {
        self.objects = Vec::new()
    }
}

impl Hittable for Scene {
    fn is_hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<Hit> {
        let mut closest_so_far = t_max;
        let mut hit = None;

        for obj in self.objects.iter() {
            if let Some(h) = obj.is_hit(ray, t_min, closest_so_far) {
                closest_so_far = h.t;
                hit = Some(h);
            }
        }
        hit
    }
}