From c4369f86c920888bfaa00e46d74e3f5a1872a9ab Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 31 Jan 2021 17:21:11 +0100 Subject: Add Scene type and Sphere intersect --- src/scene/scene.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/scene/scene.rs (limited to 'src/scene/scene.rs') diff --git a/src/scene/scene.rs b/src/scene/scene.rs new file mode 100644 index 0000000..a2f5b88 --- /dev/null +++ b/src/scene/scene.rs @@ -0,0 +1,38 @@ +use super::shapes::Shape; +use crate::Float; +use crate::core::Ray; + +pub struct Scene { + shps: Vec>, +} + +pub struct Intersection<'a> { + pub shp: &'a dyn Shape, + pub t: Float, +} + +impl Scene { + pub fn new() -> Self { + Self { + shps: Vec::new(), + } + } + + pub fn add_shape(&mut self, shp: Box) { + self.shps.push(shp); + } + + pub fn intersect(&self, ray: Ray) -> Option { + for shp in self.shps.iter() { + if let Some(t) = shp.intersect(&ray) { + return Some(Intersection { + shp: shp.as_ref(), + t, + }) + } + } + + None + } +} + -- cgit v1.2.3