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

pub struct Scene {
    objects: Vec<Box<dyn Hittable + std::marker::Sync>>,
}

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

    pub fn add(&mut self, obj: Box<dyn Hittable + std::marker::Sync>) {
        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
    }
}