# smallpt: when to stop diffuse ray scattering

For those familiar with smallpt, the ray tracer in 99 lines: I am checking the code, and there is something that is not clear to me in the DIFFUSE ray scattering. After the rays are shot, they keep bouncing until the if-statement with the Russian Roulette stops them.

if (++depth>5) if (erand48(Xi)<p) f=f*(1/p); else return obj.e;


This means that, when rays are scattered and hit the light source, they don't actually stop there (which is what I thought at first: rays bounce until they hit the light source, or until they reach a specific amount of bounces). Am I missing something?

• p is gonna be zero for light sources. Light sources are defined by emission variable and color is zero for them. Hence the reflectance parameter calculated previous to this line (p) would be zero giving us obj.e Mar 29 '19 at 15:41
• Getting a bounce off a lightsource is normal - imagine that your light source actually reflects light (like in the real world). However as @gallickgunner mentioned in the specific scene that you have the light source does not reflect any light (this doesn't need to be the case however). Mar 29 '19 at 19:27
• @lightxbulb - wouldn't the contribution by the reflected light in that case be negligible unless the light's emission is very dim. I've always wondered that we could model real world light sources by an outer non-emissive shell and an inner emissive part. That way we can safely say that we stop when ever we hit a light souce or more specifically the core emissive part. Mar 30 '19 at 23:20
• @gallickgunner What does the lightsource being dim have to do with its reflectivity? On what basis do you say it's "negligible"? And why do you believe you can throw away "negligible" contributions? You can always model a light the way you said, but that's only if that model is accurate to your use case. Note that the proposed model also ruins NEE. Mar 31 '19 at 6:32
• I mean if the light source isn't dim, you aren't going to see the outer shell anyway. The emission part will be much stronger than an indirect reflection off the surface of the light source. It's gonna be "negligible" in the sense that it's not gonna add much to what's already there due to the emission part. Mar 31 '19 at 7:12

p is gonna be zero for light sources. Light sources are defined by emission variable and color is zero for them. Hence the reflectance parameter calculated previous to this line p would be zero giving us obj.e