Add translation and rotation

This commit is contained in:
2022-07-08 15:43:12 +02:00
parent f2a8db6430
commit f5f46a7a0b
4 changed files with 119 additions and 18 deletions

View File

@ -19,6 +19,30 @@ pub struct HitRecord<'material> {
pub material: &'material Arc<Material>
}
impl<'material> HitRecord<'material> {
pub fn normalized(&mut self, ray: &Ray) {
let dot = ray.direction().dot(&self.normal);
let front_face = dot < 0.0;
let normal = if front_face { self.normal } else { -self.normal };
self.normal = normal;
self.front_face = front_face;
}
/*pub fn normalized(& self) -> Self {
let dot = ray.direction().dot(self.normal);
let front_face = dot < 0.0;
let normal = if front_face { self.normal } else { -self.normal };
HitRecord {
point: self.point,
normal,
t: self.t,
u: self.u,
v: self.v,
front_face,
material: self.material
}
}*/
}
pub trait Hittable {
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>;
fn bounding_box(&self, time0: f64, time1: f64) -> Option<Aabb>;
@ -74,19 +98,18 @@ impl Hittable for Sphere {
}
let point = ray.at(root);
let normal = (point - self.center) / self.radius;
let dot = ray.direction().dot(&normal);
let front_face = dot < 0.0;
let (u, v) = Sphere::uv(&normal);
let normal = if front_face { normal } else { -normal };
Some(HitRecord {
let mut rec = HitRecord {
point,
normal,
t: root,
u,
v,
front_face,
front_face: false, // Will be set during normalied()
material: &self.material
})
};
rec.normalized(ray);
Some(rec)
}
fn bounding_box(&self, _time0: f64, _time1: f64) -> Option<Aabb> {
let s = Vec3::new(self.radius, self.radius, self.radius);