aboutsummaryrefslogtreecommitdiff
path: root/src/world/shapes/box.rs
blob: e07d5fe591793756dc4e3618ffbfdf42ce409672 (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
use crate::core::{Ray, Bound3f, Vector3f};
use crate::world::shapes::{Plane, Rect};
use crate::world::{Instancable, Intersection, Hittable, DynHittable};
use crate::world::container::HittableList;

pub struct BoxShp {
    sides: HittableList,
}

impl BoxShp {
    pub fn new(size: Vector3f) -> Self {
        let mut cont = HittableList::new();
        cont.add(Rect::new_with_offset(size.x, size.y, size.z / 2.0, Plane::XY));
        cont.add(Rect::new_with_offset(size.x, size.y, -size.z / 2.0, Plane::XY));
        cont.add(Rect::new_with_offset(size.x, size.z, size.y / 2.0, Plane::XZ));
        cont.add(Rect::new_with_offset(size.x, size.z, -size.y / 2.0, Plane::XZ));
        cont.add(Rect::new_with_offset(size.y, size.z, size.x / 2.0, Plane::YZ));
        cont.add(Rect::new_with_offset(size.y, size.z, -size.x / 2.0, Plane::YZ));

        Self {sides: cont}
    }
}

impl Into<DynHittable> for BoxShp {
    fn into(self) -> DynHittable {
        DynHittable::new(Box::new(self))
    }
}

impl Hittable for BoxShp {
    fn intersect(&self, ray: &Ray) -> Option<Intersection> {
        self.sides.intersect(ray)
    }

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

impl Instancable for BoxShp {}