raytracing.github.io icon indicating copy to clipboard operation
raytracing.github.io copied to clipboard

InOneWeekend: [material.h] Metal scatter function return value expression needs more explanation

Open iWrote opened this issue 4 years ago • 2 comments

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?

iWrote avatar Jun 30 '20 15:06 iWrote

Sounds like it could be a good suggestion. Will investigate.

hollasch avatar Jul 08 '20 04:07 hollasch

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.

trevordblack avatar Apr 23 '21 22:04 trevordblack

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

trevordblack avatar Jul 10 '23 20:07 trevordblack