Renderer/src/hittable_list.rs

46 lines
1 KiB
Rust

use super::HitRecord;
use super::Hittable;
use super::Ray;
use super::Arc;
pub struct HittableList {
objects: Vec<Arc<dyn Hittable>>,
}
impl HittableList {
pub fn new() -> Self {
HittableList {
objects: Vec::<Arc<dyn Hittable>>::new(),
}
}
pub fn clear(&mut self) {
self.objects.clear();
}
pub fn add(&mut self, obj: Arc<dyn Hittable>) {
self.objects.push(obj);
}
pub fn clone(&self) -> Self {
HittableList {
objects : self.objects.clone()
}
}
pub fn hit(&self, r: &Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
let mut hit_anything = false;
let mut closest_so_far = t_max;
for obj in &self.objects {
let mut temp_rec = HitRecord::empty();
if obj.hit(&r, t_min, closest_so_far, &mut temp_rec) {
hit_anything = true;
closest_so_far = temp_rec.t;
*rec = temp_rec;
}
}
return hit_anything;
}
}