use crate::{math::Ray, hit::{Hit, HitRecord}};
use std::rc::Rc;
#[derive(Clone)]
pub struct Scene<'a> {
objects: Vec<Rc<dyn Hit + 'a>>
}
impl<'a> Scene<'a> {
pub fn new() -> Self {
Self {
objects: Vec::new()
}
}
pub fn add_object(&mut self, object: impl Hit + 'a) {
self.objects.push(Rc::new(object));
}
pub fn object_count(&self) -> usize {
self.objects.len()
}
}
impl<'a> Hit for Scene<'a> {
fn hit(&self, ray: &Ray, t_min: f64, mut t_max: f64) -> Option<HitRecord> {
let mut hit_record = None;
for object in &self.objects {
if let Some(temp_hit_record) = object.hit(ray, t_min, t_max) {
if temp_hit_record.t() < t_max && temp_hit_record.t() > t_min {
t_max = temp_hit_record.t();
hit_record = Some(temp_hit_record);
}
}
}
hit_record
}
}