raytracing.github.io
raytracing.github.io copied to clipboard
InOneWeekend: [material.h] Metal scatter function return value expression needs more explanation
virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
) const {
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected);
attenuation = albedo;
return (dot(scattered.direction(), rec.normal) > 0);
}
Listing 46 [material.h] Metal scatter function
return (dot(scattered.direction(), rec.normal) > 0);
appears equivalent to return rec.front_face;
Is it not? Why not use the latter?
Sounds like it could be a good suggestion. Will investigate.
rec.front_face
is not equivalent to return (dot(scattered.direction(), rec.normal) > 0);
rec.front_face
is calculated based on the incoming ray:
(dot(r.direction(), rec.normal) > 0);
So, it's not quite right to use front_face.
Your confusion is warranted though, because reflect will always return true for that conditional.
The reason it's written the way that it is, is so that once fuzz is added, the conditional may not necessarily still return true.
With #1157 this should now be closed.
When the metal material's scatter
function is first introduced, it returns true
, only changing to return (dot(scattered.direction(), rec.normal) > 0);
after fuzz is introduced