library: render: scene: clean-up ReflTrans handling

This commit is contained in:
Bruno BELANYI 2020-03-20 23:46:38 +01:00
parent b29e6d1613
commit fccf3caef1

View file

@ -171,34 +171,30 @@ impl Scene {
let object_color = object.texture.texel_color(texel); let object_color = object.texture.texel_color(texel);
let normal = object.shape.normal(&point); let normal = object.shape.normal(&point);
let reflected = reflected(incident_ray, normal); let reflected_ray = reflected(incident_ray, normal);
let lighting = self.illuminate(point, object_color, &properties, normal, reflected); let lighting = self.illuminate(point, object_color, &properties, normal, reflected_ray);
match properties.refl_trans { if properties.refl_trans.is_none() {
None => lighting, // Avoid calculating reflection when not needed
Some(ReflTransEnum::Transparency { coef, index }) => { return lighting;
}
let reflected = self.reflection(point, reflected_ray, reflection_limit, diffraction_index);
// We can unwrap safely thanks to the check for None before
match properties.refl_trans.unwrap() {
ReflTransEnum::Transparency { coef, index } => {
// Calculate the refracted ray, if it was refracted // Calculate the refracted ray, if it was refracted
refracted(incident_ray, normal, diffraction_index, index).map_or_else( refracted(incident_ray, normal, diffraction_index, index).map_or_else(
// Total reflection // Total reflection
|| self.reflection(point, reflected, reflection_limit, diffraction_index), || reflected.clone(),
// Refraction (refracted ray, amount of *reflection*) // Refraction (refracted ray, amount of *reflection*)
|(r, refl_t)| { |(r, refl_t)| {
let refr_light = self.refraction(point, coef, r, reflection_limit, index) let refracted = self.refraction(point, coef, r, reflection_limit, index);
* (1. - refl_t) let refr_light = refracted * (1. - refl_t) + reflected.clone() * refl_t;
+ self.reflection(
point,
reflected,
reflection_limit,
diffraction_index,
) * refl_t;
refr_light * coef + lighting * (1. - coef) refr_light * coef + lighting * (1. - coef)
}, },
) )
} }
Some(ReflTransEnum::Reflectivity { coef }) => { ReflTransEnum::Reflectivity { coef } => reflected * coef + lighting * (1. - coef),
self.reflection(point, reflected, reflection_limit, diffraction_index) * coef
+ lighting * (1. - coef)
}
} }
} }