diff options
Diffstat (limited to 'src/scene')
-rw-r--r-- | src/scene/scene.rs | 16 | ||||
-rw-r--r-- | src/scene/shapes/sphere.rs | 5 |
2 files changed, 16 insertions, 5 deletions
diff --git a/src/scene/scene.rs b/src/scene/scene.rs index 8575217..83a43c7 100644 --- a/src/scene/scene.rs +++ b/src/scene/scene.rs @@ -7,6 +7,11 @@ pub struct Scene { objs: Vec<Object>, } +pub struct SceneIntersect<'a> { + pub mat: &'a dyn Material, + pub i: Intersection, +} + impl Scene { pub fn new() -> Self { Self { @@ -24,14 +29,19 @@ impl Scene { } } - pub fn intersect(&self, ray: &Ray) -> Option<(&dyn Material, Intersection)> { + pub fn intersect(&self, ray: &Ray) -> Option<SceneIntersect> { + let mut min: Option<SceneIntersect> = None; + for obj in self.objs.iter() { if let Some(i) = obj.shape.intersect(&ray) { - return Some((obj.mat.as_ref(), i)) + match min { + Some(ref si) if si.i.t < i.t => (), + _ => min = Some(SceneIntersect {i, mat: obj.mat.as_ref() }), + } } } - None + min } } diff --git a/src/scene/shapes/sphere.rs b/src/scene/shapes/sphere.rs index 3f07b9e..eaa6625 100644 --- a/src/scene/shapes/sphere.rs +++ b/src/scene/shapes/sphere.rs @@ -44,6 +44,7 @@ impl Hittable for Sphere { Some(Intersection { n: self.norm_at(&w), p: w, + t: distance, }) } @@ -63,7 +64,7 @@ mod tests { direction: Vector3f::new_xyz(0.0, 1.0, 1.5).norm(), }; - let dist = sph.intersect(&ray); - assert!((dist.unwrap() - 3.28).abs() < 0.01); + let dist = sph.intersect(&ray).unwrap(); + assert!((dist.t - 3.28).abs() < 0.01); } } |