Feat: Rudimentary dielectric... but broken

Dielectric material matching Raytracing in a Weekend book chapter 10.2.
But it isn't right. The spheres turn into solid black holes.

As I'm making the commit, I've found the problem. I'm separating it out
so I can tag it and explore the code a bit more. See the step-by-step
changes, and such.
This commit is contained in:
2023-06-06 17:39:09 -05:00
parent ef5e3fa388
commit 4ea2208208
3 changed files with 43 additions and 20 deletions

View File

@@ -11,8 +11,9 @@ use rand::distributions::Uniform;
#[derive(Copy, Clone, Debug)]
pub enum Material{
Lambertian{ albedo: Vec3 },
Metal{ albedo:Vec3, fuzz: f32 },
Lambertian { albedo: Vec3 },
Metal { albedo:Vec3, fuzz: f32 },
Dielectric { index_refraction: f32 },
}
impl Material {
@@ -58,6 +59,19 @@ impl Material {
*attenuation = *albedo;
return Vec3::dot(scattered.dir, rec.normal) > 0.0;
},
Material::Dielectric { index_refraction } => {
*attenuation = Vec3::ones();
let refraction_ratio = if rec.front_face { 1.0 / index_refraction } else { *index_refraction };
let unit_direction = Vec3::as_unit(&ray_in.dir);
let refracted = Vec3::refract(unit_direction, rec.normal, refraction_ratio);
*scattered = Ray {
orig: rec.p,
dir: refracted
};
return true;
},
}
}
}