From 8e3bc57bf03457c33088e62c9d30da0565730257 Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 6 Aug 2021 12:39:51 +0200 Subject: Add box shape and replicate RTTNW box --- src/world/container/list.rs | 8 ++++---- src/world/mod.rs | 7 ++++++- src/world/shapes/box.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/world/shapes/mod.rs | 26 ++------------------------ 4 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 src/world/shapes/box.rs (limited to 'src/world') diff --git a/src/world/container/list.rs b/src/world/container/list.rs index 22b6d88..f1a658d 100644 --- a/src/world/container/list.rs +++ b/src/world/container/list.rs @@ -1,9 +1,9 @@ -use crate::world::{Object, Hittable, Intersection}; +use crate::world::{Object, Hittable, DynHittable, Intersection}; use crate::core::{Bound3f, Ray}; pub struct HittableList { - elems: Vec, + elems: Vec, } impl HittableList { @@ -11,8 +11,8 @@ impl HittableList { Self::default() } - pub fn add(&mut self, h: Object) { - self.elems.push(h); + pub fn add>(&mut self, h: T) { + self.elems.push(h.into()); } } diff --git a/src/world/mod.rs b/src/world/mod.rs index dd96b91..0394ffe 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -8,7 +8,6 @@ mod instancing; pub use scene::*; pub use hittable::{Intersection, Hittable, DynHittable}; -pub use shapes::Shape; pub use instancing::{Instance, Instancable}; use std::sync::Arc; @@ -29,6 +28,12 @@ impl Object { } } +impl Into for Object { + fn into(self) -> DynHittable { + DynHittable::new(Box::new(self)) + } +} + impl Hittable for Object { fn intersect(&self, ray: &Ray) -> Option { if let Some(mut inter) = self.inner.intersect(ray) { diff --git a/src/world/shapes/box.rs b/src/world/shapes/box.rs new file mode 100644 index 0000000..e07d5fe --- /dev/null +++ b/src/world/shapes/box.rs @@ -0,0 +1,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 for BoxShp { + fn into(self) -> DynHittable { + DynHittable::new(Box::new(self)) + } +} + +impl Hittable for BoxShp { + fn intersect(&self, ray: &Ray) -> Option { + self.sides.intersect(ray) + } + + fn bounding_box(&self) -> Bound3f { + self.sides.bounding_box() + } +} + +impl Instancable for BoxShp {} diff --git a/src/world/shapes/mod.rs b/src/world/shapes/mod.rs index 6e4b2ce..d15840c 100644 --- a/src/world/shapes/mod.rs +++ b/src/world/shapes/mod.rs @@ -1,32 +1,10 @@ mod sphere; mod rectangle; +mod r#box; pub use sphere::Sphere; pub use rectangle::{Rect, Plane}; +pub use r#box::BoxShp; use crate::world::{Hittable, Intersection}; use crate::core::{Bound3f, Ray}; - -pub enum Shape { - Sphere(Sphere), -} - -impl Hittable for Shape { - fn intersect(&self, ray: &Ray) -> Option { - match self { - Self::Sphere(sph) => sph.intersect(ray) - } - } - - fn bounding_box(&self) -> Bound3f { - match self { - Self::Sphere(sph) => sph.bounding_box() - } - } -} - -impl From for Shape { - fn from(s: Sphere) -> Self { - Self::Sphere(s) - } -} -- cgit v1.2.3