0

While following scratchapixel's tutorial on indirect diffuse implementation in path tracing, the samples gets divided by the pdf, which in case of a uniform pdf results in $1/2\pi$ so we have that the sum of N samples gets multiplied by this factor: $$k=\frac{1}{N * \frac{1}{2 \pi}} = \frac{2\pi}{N}$$

The problem with this is that unless the albedo color is very dark, the final color quickly increases:

(From "Step 7: Add the Contribution of the Indirect Diffuse..." in the link above)

$$hitColor = (directDiffuse + \frac{2\pi * indirectDiffuse}{N}) * \frac{albedo}{\pi}; $$

Other implementations instead just average the samples and the $2\pi$ term above is completely absent, like in Shirley's "ray tracing in a weekend" p.20

Which one is correct?

There's a difference in Shirley's and scratchapixel implementation, in the fact that Shirley's is a progressive path tracer (evaluates one path at a time)

enter image description here

While scratchapixel's is a non-progressive or branched path tracer, evaluating multiple paths at once:

enter image description here

But I believe this does not allow us to remove the $2\pi$ term in the former, because we are doing the same thing (sampling the BRDF hemisphere) albeit differently.

When evaluating direct lighting with point lights, the solid angle term is also completely absent, which makes things even more strange:

enter image description here

Here the integral over the hemisphere is 0 except in the direction $\omega i$, but (for example considering only diffuse) we only use:

$$ L(P) = albedo*L(\omega i, P)*cos\theta$$

where $L(P)$ is the radiance from the point $P$, and $L(\omega i, P)$ gets replaced with our light radiance, $\theta$ is the angle with from $\omega i$ to the surface normal. Where has the solid angle term ($ \omega i$) gone?

yggdrasil
  • 145
  • 4
  • The problem with this is that unless the albedo color is very dark, the final color quickly increases - how is it a problem, and how does it "quickly increase"? Other implementations instead... - link those. – lightxbulb Apr 16 '20 at 19:46
  • @lightxbulb I've updated the question with more details. Regarding the quickly increasing color, let's assume for simplicity you have a fully white material (1, 1, 1) and we take only one indirect sample ($N=1$) which happens to be fully red (1, 0, 0), we end up with: $hitcolor = \frac{2\piindirectDiffuse}{N}\frac{albedo}{\pi} = \frac{2 * (1, 0, 0)}{1}*(1, 1, 1) = (2, 0, 0)$ which is double of the indirect diffuse sample we took. It looked strange to me. – yggdrasil Apr 17 '20 at 04:37
  • it's Shirley not Sherley. 2) The $2pi$ is missing in RT in a weekend, because it's using cosine weighted sampling and the constant $\pi$ factor is assumed to cancel with an implicit factor in the albedo. 3) The $2\pi$ factor will be absent in an estimator sampling point lights because those can be sampled only through next event estimation, in which case the pdf is not the one for bsdf sampling. 4) Point lights are represented though Dirac deltas which gets rid of the integrand and $d\omega_i$.
  • – lightxbulb Apr 17 '20 at 08:02
  • @lightxbulb The 2pi is missing in RT in a weekend, because it's using cosine weighted sampling and the constant π factor is assumed to cancel with an implicit factor in the albedo. - If the π term cancels out it should leave a $2$ factor, which isn't present? - Point lights are represented though Dirac deltas which gets rid of the integrand and dωi - Thanks for the explaination, this confirms what I was suspecting – yggdrasil Apr 17 '20 at 08:07
  • ↑corrected RT weekend author's name – yggdrasil Apr 17 '20 at 08:14
  • @lightxbulb Looking at this question: https://computergraphics.stackexchange.com/questions/4664/does-cosine-weighted-hemisphere-sampling-still-require-ndotl-when-calculating-co I understood there's no 2 factor in the case of cosine weighted sampling. However the hitcolor being doubled in the case of uniform sampling in my comment above still leaves me perplexed. Is it correct? – yggdrasil Apr 17 '20 at 08:30
  • There's no reason that the $2$ should bother you. It is a normalization factor to account for the fact that your domain has size $2\pi$ (the area of the upper unit hemisphere). You always have to divide by the pdf to get an unbiased estimator: $E[\frac{f}{pdf}] = \int f$. – lightxbulb Apr 17 '20 at 08:36
  • @lightxbulb I see, thanks for the clarification! So if I try to replace the cosine weighted sampling in RT weekend with an uniform one, and add a $2NdotL$ (assuming $\pi$ cancels out with the albedo norm factor) in Shirley's implementetion (p.27) from `return attenuationcolor(scattered, world, depth+1);toreturn 2NdotLattenuation*color(scattered, world, depth+1);` it should converge to the same exact image. Am I correct? – yggdrasil Apr 17 '20 at 08:58
  • Yes, it should converge to the same image provided that you change the sampling to take uniform directions and not cosine weighted. In the process of converging it will have a higher variance however because uniform is worse than cosine sampling. Note that originally in RT in a weekend there was a mistake where the samples were taken inside a ball rather than on its surface resulting in a $\cos^3$ pdf, but I believe they have corrected that since. – lightxbulb Apr 17 '20 at 09:01
  • In the linked pdf it seems the samples are still taken inside a ball indeed (p.22-23) – yggdrasil Apr 17 '20 at 09:10
  • See the latest code on github. – lightxbulb Apr 17 '20 at 09:57
  • @lightxbulb I'm sorry to bring back the discussion, but while reading RT weekend I had another doubt about the above question. In the case of reflected rays off a rough surface, Shirley uses sampling a resized and offset sphere (p.28). Is the pdf still $\frac{cos\theta}{\pi}$ in that case? – yggdrasil Apr 21 '20 at 09:05
  • No, it isn't. But I haven't derived the pdf for it, neither has he I believe. It's probably feasible to derive, but it would take some work, see for reference: https://github.com/vchizhov/Derivations/blob/master/Probability%20density%20functions%20of%20the%20projected%20offset%20disk%2C%20circle%2C%20ball%2C%20and%20sphere.pdf – lightxbulb Apr 21 '20 at 10:06
  • Wait, by rough surface you mean not the fuzzy mirror scattering but the diffuse? If that is the case, then I misunderstood you. For a rough surface (offset ball along the normal) it is indeed $\frac{\cos\theta}{\pi}$ (provided you take points on the ball, and not in the ball - you can find more details in the pdf I linked). If you were talking about the fuzzy mirror reflection then my previous comment about the pdf not being clear holds. – lightxbulb Apr 21 '20 at 18:50
  • @lightxbulb Yes, actually I meant the former (fuzzy mirror scattering, not diffuse). If I understood correctly, In the case of a non fuzzy perfect mirror, we have to yet again use a Dirac delta (in which case as with point lights, radiance along the ray equals total irradiance in the BRDF) – yggdrasil Apr 22 '20 at 07:32
  • For mirrors you use a Dirac delta to get only the light coming from a specific direction. – lightxbulb Apr 22 '20 at 07:39