diff options
Diffstat (limited to 'src/scene')
-rw-r--r-- | src/scene/mod.rs | 29 | ||||
-rw-r--r-- | src/scene/scene.rs | 28 | ||||
-rw-r--r-- | src/scene/shapes/sphere.rs | 3 |
3 files changed, 26 insertions, 34 deletions
diff --git a/src/scene/mod.rs b/src/scene/mod.rs index f8c1e09..cd07236 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -6,27 +6,20 @@ pub mod shapes; mod scene; pub use scene::*; -use crate::core::{Ray, Vector3f}; -use crate::Float; +use std::rc::Rc; +use crate::core::Hittable; +use crate::material::Material; -/// Returns the context of a intersection -pub struct Intersection { - /// Normal vector at intersection - pub n: Vector3f, - pub p: Vector3f, +pub struct Object { + pub shape: Box<dyn Hittable>, + pub mat: Rc<dyn Material>, } -impl Intersection { - pub fn norm_against_ray(&self, r: &Ray) -> Vector3f { - if self.n.dot(&r.direction) < 0.0 { - self.n - } else { - -self.n +impl Object { + pub fn new(mat: Rc<dyn Material>, shape: Box<dyn Hittable>) -> Self { + Object { + mat, + shape, } } } - -/// Defines a common trait for objects in the scene -pub trait Hittable { - fn intersect(&self, ray: &Ray) -> Option<Intersection>; -} diff --git a/src/scene/scene.rs b/src/scene/scene.rs index 367003e..0ffbe97 100644 --- a/src/scene/scene.rs +++ b/src/scene/scene.rs @@ -1,33 +1,33 @@ -use super::{Intersection, Hittable}; -use crate::core::Ray; +use crate::core::{Ray, Intersection, Hittable}; +use crate::material::Material; -type Shape = Box<dyn Hittable>; +use super::Object; pub struct Scene { - shps: Vec<Shape>, + objs: Vec<Object>, } impl Scene { pub fn new() -> Self { Self { - shps: Vec::new(), + objs: Vec::new(), } } - pub fn add_shape(&mut self, shp: Shape) { - self.shps.push(shp); + pub fn add_object(&mut self, obj: Object) { + self.objs.push(obj); } - pub fn add_shapes(&mut self, shps: Vec<Shape>) { - for shp in shps { - self.add_shape(shp); + pub fn add_objects(&mut self, objs: Vec<Object>) { + for obj in objs { + self.add_object(obj); } } - pub fn intersect(&self, ray: &Ray) -> Option<Intersection> { - for shp in self.shps.iter() { - if let Some(i) = shp.intersect(&ray) { - return Some(i) + pub fn intersect(&self, ray: &Ray) -> Option<(&dyn Material, Intersection)> { + for obj in self.objs.iter() { + if let Some(i) = obj.shape.intersect(&ray) { + return Some((obj.mat.as_ref(), i)) } } diff --git a/src/scene/shapes/sphere.rs b/src/scene/shapes/sphere.rs index acca2b7..3f07b9e 100644 --- a/src/scene/shapes/sphere.rs +++ b/src/scene/shapes/sphere.rs @@ -2,8 +2,7 @@ //! //! Spheres are relatively easy to calculate intersections between use crate::Float; -use crate::core::{Ray, Vector3f}; -use crate::scene::{Hittable, Intersection}; +use crate::core::{Ray, Vector3f, Hittable, Intersection}; pub struct Sphere { radius: Float, |