aboutsummaryrefslogtreecommitdiff
path: root/src/world/mod.rs
blob: dd96b9173c1007b08f080018b430532d7cd7faa8 (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
//! Manages world objects, and implements intersection
pub mod shapes;

mod scene;
pub mod container;
mod hittable;
mod instancing;

pub use scene::*;
pub use hittable::{Intersection, Hittable, DynHittable};
pub use shapes::Shape;
pub use instancing::{Instance, Instancable};

use std::sync::Arc;
use crate::material::Material;
use crate::core::{Bound3f, Ray};

pub struct Object {
    pub inner: DynHittable,
    pub mat: Arc<dyn Material>,
}

impl Object {
    pub fn new<T: Into<DynHittable>>(mat: Arc<dyn Material>, inner: T) -> Self {
        Object {
            mat,
            inner: inner.into(),
        }
    }
}

impl Hittable for Object {
    fn intersect(&self, ray: &Ray) -> Option<Intersection> {
        if let Some(mut inter) = self.inner.intersect(ray) {
            inter.add_material_if_none(self.mat.as_ref());
            Some(inter)
        } else {
            None
        }
    }

    fn bounding_box(&self) -> Bound3f {
        self.inner.bounding_box()
    }
}