From 78ddaff5855bf8446adef9e18eb0d7b7ddcee52a Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:45:56 +0000 Subject: init --- src/hittable/hittable_box.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/hittable/hittable_box.rs (limited to 'src/hittable/hittable_box.rs') diff --git a/src/hittable/hittable_box.rs b/src/hittable/hittable_box.rs new file mode 100644 index 0000000..7b95cc7 --- /dev/null +++ b/src/hittable/hittable_box.rs @@ -0,0 +1,40 @@ +use std::sync::Arc; + +use crate::{hittable::{HitRecord, Hittable, AABB, hittable_list::HittableList, xy_rect::XYRect, xz_rect::XZRect, yz_rect::YZRect}, material::Material, ray::Ray, vec3::Point3}; + +pub struct HittableBox { + min: Point3, + max: Point3, + sides: HittableList, +} + +impl HittableBox { + pub fn new(min: Point3, max: Point3, material: Arc) -> Self { + let mut sides = HittableList::new(); + + sides.add(Arc::new(XYRect { material: material.clone(), x0: min.x, x1: max.x, y0: min.y, y1: max.y, k: max.z })); + sides.add(Arc::new(XYRect { material: material.clone(), x0: min.x, x1: max.x, y0: min.y, y1: max.y, k: min.z })); + + sides.add(Arc::new(XZRect { material: material.clone(), x0: min.x, x1: max.x, z0: min.z, z1: max.z, k: max.y })); + sides.add(Arc::new(XZRect { material: material.clone(), x0: min.x, x1: max.x, z0: min.z, z1: max.z, k: min.y })); + + sides.add(Arc::new(YZRect { material: material.clone(), y0: min.y, y1: max.y, z0: min.z, z1: max.z, k: max.x })); + sides.add(Arc::new(YZRect { material: material.clone(), y0: min.y, y1: max.y, z0: min.z, z1: max.z, k: min.x })); + + Self { + min, + max, + sides, + } + } +} + +impl Hittable for HittableBox { + fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option { + self.sides.hit(ray, t_min, t_max) + } + + fn bounding_box(&self, _: f64, _: f64) -> Option { + Some(AABB { minimum: self.min.clone(), maximum: self.max.clone() }) + } +} -- cgit v1.2.3