A src/hittable_list.rs => src/hittable_list.rs +38 -0
@@ 0,0 1,38 @@
+use crate::hittable::*;
+use crate::ray::*;
+use crate::vec3::*;
+use std::sync::Arc;
+
+#[derive(Clone)]
+pub struct HittableList {
+ objects: Vec<Arc<dyn Hittable>>,
+}
+
+impl HittableList {
+ fn empty() -> Self {
+ HittableList{ objects: vec![] }
+ }
+
+ fn clear(&mut self) {
+ self.objects.clear();
+ }
+
+ fn add(&mut self, object: &Arc<dyn Hittable>) {
+ self.objects.push(object.clone());
+ }
+}
+
+impl Hittable for HittableList {
+ fn hit(&self, r: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
+ let mut temp_rec = None;
+ let mut closest_so_far = t_max;
+
+ for object in &self.objects {
+ if let Some(hr) = object.hit(r, t_min, closest_so_far) {
+ closest_so_far = hr.t;
+ temp_rec = Some(hr);
+ }
+ }
+ temp_rec
+ }
+}
M src/main.rs => src/main.rs +3 -0
@@ 7,6 7,9 @@ use ray::*;
mod hittable;
use hittable::*;
+mod hittable_list;
+use hittable_list::*;
+
mod sphere;
use sphere::*;