library: render: scene: use iterators for cast_ray

This commit is contained in:
Antoine Martin 2020-03-20 00:47:25 +01:00
parent 40c2ae1bc9
commit db3ba1b312

View file

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use super::{light_aggregate::LightAggregate, object::Object}; use super::{light_aggregate::LightAggregate, object::Object};
use crate::{ use crate::{
core::{Camera, LightProperties, LinearColor, ReflTransEnum}, core::{Camera, LightProperties, LinearColor, ReflTransEnum},
@ -111,20 +113,13 @@ impl Scene {
} }
fn cast_ray(&self, ray: Ray) -> Option<(f32, &Object)> { fn cast_ray(&self, ray: Ray) -> Option<(f32, &Object)> {
// NOTE(Bruno): should be written using iterators self.bvh
let mut shot_obj: Option<&Object> = None; .traverse(&ray, &self.objects)
let mut t = std::f32::INFINITY; .iter()
// NOTE: we don't care about all objects... Only the closest one .filter_map(|obj| obj.shape.intersect(&ray).map(|distance| (distance, *obj)))
for object in self.bvh.traverse(&ray, &self.objects).iter() { .min_by(|(dist_a, _), (dist_b, _)| {
match object.shape.intersect(&ray) { dist_a.partial_cmp(dist_b).unwrap_or(Ordering::Equal)
Some(dist) if dist < t => { })
t = dist;
shot_obj = Some(&object);
}
_ => {}
}
}
shot_obj.map(|obj| (t, obj))
} }
fn color_at( fn color_at(