aboutsummaryrefslogtreecommitdiff
path: root/src/hittable/instance/translate.rs
diff options
context:
space:
mode:
authorlamp2023-03-05 21:45:56 +0000
committerlamp2023-03-05 21:45:56 +0000
commit78ddaff5855bf8446adef9e18eb0d7b7ddcee52a (patch)
tree0d0e93cfa28751a2f96518eeb231cf715958e1fa /src/hittable/instance/translate.rs
init
Diffstat (limited to 'src/hittable/instance/translate.rs')
-rw-r--r--src/hittable/instance/translate.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/hittable/instance/translate.rs b/src/hittable/instance/translate.rs
new file mode 100644
index 0000000..a9c8162
--- /dev/null
+++ b/src/hittable/instance/translate.rs
@@ -0,0 +1,27 @@
+use std::sync::Arc;
+
+use crate::{hittable::{HitRecord, Hittable, AABB}, ray::Ray, vec3::Vec3};
+
+pub struct Translate {
+ pub hittable: Arc<dyn Hittable>,
+ pub offset: Vec3,
+}
+
+impl Hittable for Translate {
+ fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
+ let moved_ray = Ray { origin: &ray.origin - &self.offset, direction: ray.direction.clone(), time: ray.time };
+ let mut hit_record = self.hittable.hit(&moved_ray, t_min, t_max)?;
+ hit_record.p += self.offset.clone();
+ let normal = hit_record.normal.clone();
+ hit_record.set_face_normal(&moved_ray, &normal);
+ Some(hit_record)
+ }
+
+ fn bounding_box(&self, time_start: f64, time_end: f64) -> Option<AABB> {
+ let output_box = self.hittable.bounding_box(time_start, time_end)?;
+ Some(AABB {
+ minimum: &output_box.minimum + &self.offset,
+ maximum: &output_box.maximum + &self.offset,
+ })
+ }
+}