10.6.Fuzzy Reflection

This commit is contained in:
dengqn 2025-08-12 17:37:25 +08:00
parent 37b793287d
commit b28a6cc7f5
3 changed files with 7 additions and 10 deletions

View File

@ -51,9 +51,9 @@ fn camera_render() {
let plane_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.899, 0.899, 0.999)}));
// let plane2_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.784,0.784,0.784)}));
let center_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.5, 0.5, 0.5)}));
let left_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.799, 0.599, 0.799)}));
let left_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.799, 0.599, 0.799), fuzz: 0.0005}));
let left_behind_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.799, 0.599, 0.599)}));
let right_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.8, 0.6, 0.2)}));
let right_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.8, 0.6, 0.2), fuzz: 0.3}));
world.put(Box::new(Sphere::new(Point::new(0.0, 0.0, -1.0), 0.5, center_m)));
world.put(Box::new(Sphere::new(Point::new(-1.0, 0.0, -1.0), 0.5, left_m)));

View File

@ -46,26 +46,23 @@ impl Material for Lambertian {
#[derive(Debug)]
pub struct Metal {
pub albedo: Color,
pub fuzz: f32
}
impl Clone for Metal {
fn clone(&self) -> Self {
Metal {
albedo: self.albedo,
fuzz: self.fuzz
}
}
}
impl Material for Metal {
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
/*
vec3 reflected = reflect(r_in.direction(), rec.normal);
scattered = ray(rec.p, reflected);
attenuation = albedo;
return true;
*/
let reflected = reflect(r_in.direction, hit_record.normal);
*scattered = Ray::new(hit_record.p, reflected);
let fuzz_relected = (reflected / reflected.length_squared()) + (Vec3::random_unit() * self.fuzz);
*scattered = Ray::new(hit_record.p, fuzz_relected);
*attenuation = self.albedo.clone();
true
}

View File

@ -50,7 +50,7 @@ impl Vec3 {
pub fn random_unit() -> Self {
loop {
let v = Vec3::random();
if f32::MIN < v.length_squared() && v.length_squared() <= 1.0 {
if 1e-160 < v.length_squared() && v.length_squared() <= 1.0 {
return v / v.length_squared();
}
}