ft: 9: diffuse materials

This commit is contained in:
2026-04-15 15:18:33 +02:00
parent 5d18a57799
commit e8f84b590b
6 changed files with 332 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ pub struct Camera {
image_width: u32,
image_height: u32,
anti_alias_rate: u32,
max_depth: u32,
center: Vec3,
pixel00_loc: Vec3,
pixel_delta_u: Vec3,
@@ -45,6 +46,7 @@ impl Camera {
image_width: image_width,
image_height: image_height,
anti_alias_rate: anti_alias_rate,
max_depth: 50,
center: camera_center,
pixel00_loc: pixel00_loc,
pixel_delta_u: pixel_delta_u,
@@ -70,7 +72,7 @@ impl Camera {
+ (y * self.pixel_delta_v / (self.anti_alias_rate + 1) as f32);
let ray_dir = pixel_loc - self.center;
let r = Ray::new(self.center, ray_dir);
pixel_colour += self.ray_colour(&hittables, &r);
pixel_colour += self.ray_colour(&hittables, &r, self.max_depth);
}
}
@@ -84,11 +86,15 @@ impl Camera {
imgbuf.save("output.png").unwrap();
}
fn ray_colour<T: Hittable>(&self, hittables: &Vec<T>, r: &Ray) -> Colour {
fn ray_colour<T: Hittable>(&self, hittables: &Vec<T>, r: &Ray, depth: u32) -> Colour {
if depth <= 0 {
return Colour::nil();
}
let closest = Hit::hit_list(hittables, r);
if let Some(hit) = closest {
let n = hit.n();
return 0.5 * Colour::new(n.x() + 1., n.y() + 1., n.z() + 1.);
let dir = *hit.n() + Vec3::random_unit_hemisphere(hit.n());
return 0.5 * self.ray_colour(hittables, &Ray::new(*hit.p(), dir), depth - 1);
}
// background