From 78ddaff5855bf8446adef9e18eb0d7b7ddcee52a Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:45:56 +0000 Subject: init --- src/hittable/instance/translate.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/hittable/instance/translate.rs (limited to 'src/hittable/instance/translate.rs') 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, + pub offset: Vec3, +} + +impl Hittable for Translate { + fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option { + 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 { + 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, + }) + } +} -- cgit v1.2.3