2022-05-11 18:38:30 +02:00
|
|
|
use super::HitRecord;
|
2022-05-04 18:21:48 +02:00
|
|
|
use super::Hittable;
|
|
|
|
use super::Ray;
|
2022-05-18 16:45:50 +02:00
|
|
|
use super::Arc;
|
2022-05-04 18:21:48 +02:00
|
|
|
|
|
|
|
pub struct HittableList {
|
2022-05-18 16:45:50 +02:00
|
|
|
objects: Vec<Arc<dyn Hittable>>,
|
2022-05-04 18:21:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HittableList {
|
2022-05-11 18:38:30 +02:00
|
|
|
pub fn new() -> Self {
|
2022-05-04 18:21:48 +02:00
|
|
|
HittableList {
|
2022-05-18 16:45:50 +02:00
|
|
|
objects: Vec::<Arc<dyn Hittable>>::new(),
|
2022-05-04 18:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.objects.clear();
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:45:50 +02:00
|
|
|
pub fn add(&mut self, obj: Arc<dyn Hittable>) {
|
2022-05-04 18:21:48 +02:00
|
|
|
self.objects.push(obj);
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:45:50 +02:00
|
|
|
pub fn clone(&self) -> Self {
|
|
|
|
HittableList {
|
|
|
|
objects : self.objects.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 18:21:48 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-05-11 18:38:30 +02:00
|
|
|
}
|