mmp / pbrt-v3

Source code for pbrt, the renderer described in the third edition of "Physically Based Rendering: From Theory To Implementation", by Matt Pharr, Wenzel Jakob, and Greg Humphreys.
http://pbrt.org
BSD 2-Clause "Simplified" License
4.91k stars 1.19k forks source link

tMax of shadow rays #178

Closed dominikh closed 6 years ago

dominikh commented 6 years ago

The Interaction::SpawnRayTo method sets the shadow ray's max distance to 1 - ShadowEpsilon, "so that they stop before the surface of light sources". Even after hours of looking at this, I fail to see how this works in the general case, where the shaded point and the light source may be an arbitrary distance >= 1 away from each other. All the uses of the returned ray assume that it is in world space and do not transform or otherwise modify tMax. Why shouldn't tMax be set to Distance(origin, p2) - ShadowEpsilon instead?

Clearly the current implementation is correct, else it would render most scenes incorrectly; yet why this works is not evident to me from the source code or the book.

https://github.com/mmp/pbrt-v3/blob/8dbd937584c48a4e82261b8aefbab9141dae9171/src/core/interaction.h#L71

mmp commented 6 years ago

Note that the ray direction isn't normalized; d is set to be p2 - origin. Thus, at t=0, the ray is at origin, and at t=1 it's at p2. (And thus, taking an epsilon off there makes it stop just before p2.

dominikh commented 6 years ago

I see! Thanks for the quick response.

I'll leave the issue open in case you want to clarify that piece of code in the next edition. Feel free to close the issue if not :-)

mmp commented 6 years ago

Noted for the 4th edition. :-) Thanks!