Implement motion blur

This commit is contained in:
2022-07-03 20:29:06 +02:00
parent 558851b5f6
commit 3de572d4a9
5 changed files with 133 additions and 18 deletions

View File

@ -62,3 +62,68 @@ impl Hittable for Sphere {
})
}
}
pub struct MovableSphere {
pub center0: Point3,
pub center1: Point3,
pub radius: f64,
pub material: Material,
pub time0: f64,
pub time1: f64,
}
impl MovableSphere {
pub fn new(
center0: Point3,
center1: Point3,
radius: f64,
material: Material,
time0: f64,
time1: f64) -> Self {
MovableSphere { center0, center1, radius, material, time0, time1 }
}
pub fn center(&self, time: f64) -> Point3 {
self.center0 + ((time - self.time0) / (self.time1 - self.time0))*(self.center1 - self.center0)
}
}
impl Default for MovableSphere {
fn default() -> Self {
MovableSphere {
center0: Point3::default(),
center1: Point3::default(),
radius: 0.0,
material: Material::default(),
time0: 0.0,
time1: 0.0
}
}
}
impl Hittable for MovableSphere {
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
let oc = &ray.origin() - &self.center(ray.time());
let a = ray.direction().length_squared();
let half_b = oc.dot(&ray.direction());
let c = oc.length_squared() - &self.radius * &self.radius;
let discriminant = half_b * half_b - a * c;
if discriminant < 0.0 { return None; }
let sqrtd = discriminant.sqrt();
let mut root = (-half_b - sqrtd) / a;
if root < t_min || t_max < root {
root = (-half_b + sqrtd) / a;
if root < t_min || t_max < root { return None; }
}
let point = ray.at(root);
let normal = (point - self.center(ray.time())) / self.radius;
let dot = ray.direction().dot(&normal);
let front_face = dot < 0.0;
let normal = if front_face { normal } else { -normal };
Some(HitRecord {
point,
normal,
t: root,
front_face,
material: &self.material
})
}
}