aboutsummaryrefslogtreecommitdiff
path: root/src/world/scene.rs
blob: 444e915399164d352165505d00226e2748f15593 (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
use crate::core::{Ray, Intersection};
use crate::material::Material;

use super::Object;

pub struct Scene {
    objs: Vec<Object>,
}

pub struct SceneIntersect<'a> {
    pub mat: &'a dyn Material,
    pub i: Intersection,
}

impl Scene {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_object(&mut self, obj: Object) {
        self.objs.push(obj);
    }

    pub fn add_objects(&mut self, objs: Vec<Object>) {
        for obj in objs {
            self.add_object(obj);
        }
    }

    pub fn intersect(&self, ray: &Ray) -> Option<SceneIntersect> {
        let mut min: Option<SceneIntersect> = None;

        for obj in self.objs.iter() {
            if let Some(i) = obj.shape.intersect(&ray) {
                match min {
                    Some(ref si) if si.i.t < i.t => (),
                    _ => min = Some(SceneIntersect {i, mat: obj.mat.as_ref() }),
                }
            }
        }

        min
    }
}

impl Default for Scene {
    fn default() -> Self {
        Self {
            objs: Vec::new(),
        }
    }
}