add reflectance

This commit is contained in:
Fabien Freling 2021-05-27 22:12:15 +02:00
parent 4a478b08a0
commit 7d830d13d1

View file

@ -58,13 +58,21 @@ pub const Dielectric = struct {
const sin_theta = math.sqrt(1.0 - cos_theta * cos_theta);
const cant_refract = refraction_ratio * sin_theta > 1.0;
const direction = if (cant_refract) unit_direction.reflect(hit.normal)
const should_reflect = reflectance(cos_theta, refraction_ratio) > rng.float(f32);
const direction = if (cant_refract or should_reflect) unit_direction.reflect(hit.normal)
else unit_direction.refract(hit.normal, refraction_ratio);
return ScatteredRay{
.ray = Ray{ .origin = hit.p, .direction = direction},
.color = attenuation,
};
}
fn reflectance(cosine: f32, ref_idx: f32) f32 {
// Use Schlick's approximation for reflectance.
var r0 = (1 - ref_idx) / (1 + ref_idx);
r0 = r0*r0;
return r0 + (1 - r0) * math.pow(f32, 1 - cosine, 5);
}
};
pub const ScatteredRay = struct {