<p>While following <a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/global-illumination-path-tracing/global-illumination-path-tracing-practical-implementation" rel="nofollow noreferrer">scratchapixel's</a> tutorial on indirect diffuse implementation in path tracing, the samples gets divided by the pdf, which in case of a uniform pdf results in <span class="math-container">$1/2\pi$</span> so we have that the sum of N samples gets multiplied by this factor:
<span class="math-container">$$k=\frac{1}{N * \frac{1}{2  \pi}} = \frac{2\pi}{N}$$</span></p>

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

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

<p><span class="math-container">$$hitColor = (directDiffuse + \frac{2\pi * indirectDiffuse}{N}) * \frac{albedo}{\pi}; $$</span></p>

<p>Other implementations instead just average the samples and the <span class="math-container">$2\pi$</span> term above is completely absent, like in Shirley's "<a href="https://www.realtimerendering.com/raytracing/Ray%20Tracing%20in%20a%20Weekend.pdf" rel="nofollow noreferrer">ray tracing in a weekend</a>" p.20</p>

<p>Which one is correct?</p>

<p>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)</p>

<p><a href="https://i.sstatic.net/W9BY4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/W9BY4.png" alt="enter image description here"></a></p>

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

<p><a href="https://i.sstatic.net/8AlLb.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8AlLb.png" alt="enter image description here"></a></p>

<p>But I believe this does not allow us to remove the <span class="math-container">$2\pi$</span> term in the former, because we are doing the same thing (sampling the BRDF hemisphere) albeit differently.</p>

<p>When evaluating direct lighting with point lights, the solid angle term is also <a href="https://learnopengl.com/PBR/Lighting" rel="nofollow noreferrer">completely absent</a>, which makes things even more strange:</p>

<p><a href="https://i.sstatic.net/5rkhs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5rkhs.png" alt="enter image description here"></a></p>

<p>Here the integral over the hemisphere is 0 except in the direction <span class="math-container">$\omega i$</span>, but (for example considering only diffuse) we only use:</p>

<p><span class="math-container">$$ L(P) = albedo*L(\omega i, P)*cos\theta$$</span></p>

<p>where <span class="math-container">$L(P)$</span> is the radiance from the point <span class="math-container">$P$</span>, and <span class="math-container">$L(\omega i, P)$</span> gets replaced with our light radiance, <span class="math-container">$\theta$</span> is the angle with from <span class="math-container">$\omega i$</span> to the surface normal. Where has the solid angle term (<span class="math-container">$ \omega i$</span>) gone?</p>

CC BY-SA 4.0
2020-04-16T19:46:46.373Z
`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
CC BY-SA 4.0
2020-04-17T04:37:23.580Z
@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\pi*indirectDiffuse}{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
CC BY-SA 4.0
2020-04-17T08:02:30.490Z
1) 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
CC BY-SA 4.0
2020-04-17T08:07:22.767Z
@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
CC BY-SA 4.0
2020-04-17T08:14:24.867Z
↑corrected RT weekend author's name
yggdrasil
CC BY-SA 4.0
2020-04-17T08:30:02.967Z
@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
CC BY-SA 4.0
2020-04-17T08:36:06.340Z
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
CC BY-SA 4.0
2020-04-17T08:58:38.353Z
@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 $2*NdotL$ (assuming $\pi$ cancels out with the albedo norm factor) in Shirley's implementetion (p.27) from `return attenuation*color(scattered, world, depth+1);` to `return 2*NdotL*attenuation*color(scattered, world, depth+1);` it should converge to the same exact image. Am I correct?
yggdrasil
CC BY-SA 4.0
2020-04-17T09:01:29.800Z
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
CC BY-SA 4.0
2020-04-17T09:10:06.273Z
In the linked pdf it seems the samples are still taken inside a ball indeed (p.22-23)
yggdrasil
CC BY-SA 4.0
2020-04-17T09:57:42.450Z
See the latest code on github.
lightxbulb
CC BY-SA 4.0
2020-04-21T09:05:03.500Z
@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
CC BY-SA 4.0
2020-04-21T10:06:34.093Z
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
CC BY-SA 4.0
2020-04-21T18:50:56.377Z
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
CC BY-SA 4.0
2020-04-22T07:32:01.803Z
@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
CC BY-SA 4.0
2020-04-22T07:39:31.723Z
For mirrors you use a Dirac delta to get only the light coming from a specific direction.
lightxbulb
<lighting><brdf>
Pdf division in BRDF sampling
yggdrasil
# Pdf division in BRDF sampling

(Asked by: username_0 on 2020-04-16)

While following [scratchapixel's](https://www.scratchapixel.com/lessons/3d-basic-rendering/global-illumination-path-tracing/global-illumination-path-tracing-practical-implementation) 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](https://www.realtimerendering.com/raytracing/Ray%20Tracing%20in%20a%20Weekend.pdf)" 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](https://i.sstatic.net/W9BY4.png)](https://i.sstatic.net/W9BY4.png)

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

[![enter image description here](https://i.sstatic.net/8AlLb.png)](https://i.sstatic.net/8AlLb.png)

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](https://learnopengl.com/PBR/Lighting), which makes things even more strange:

[![enter image description here](https://i.sstatic.net/5rkhs.png)](https://i.sstatic.net/5rkhs.png)

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?

**Tags:** lighting, brdf

> **Comments:**
> **username_1**: `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.  
> **username_0**: @username_1 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\pi*indirectDiffuse}{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.  
> **username_1**: 1) 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$.  
> **username_0**: @username_1 `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  
> **username_0**: ↑corrected RT weekend author's name  
> **username_0**: @username_1 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?  
> **username_1**: 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$.  
> **username_0**: @username_1 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 $2*NdotL$ (assuming $\pi$ cancels out with the albedo norm factor) in Shirley's implementetion (p.27) from `return attenuation*color(scattered, world, depth+1);` to `return 2*NdotL*attenuation*color(scattered, world, depth+1);` it should converge to the same exact image. Am I correct?  
> **username_1**: 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.  
> **username_0**: In the linked pdf it seems the samples are still taken inside a ball indeed (p.22-23)  
> **username_1**: See the latest code on github.  
> **username_0**: @username_1 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?  
> **username_1**: 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  
> **username_1**: 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.  
> **username_0**: @username_1 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)  
> **username_1**: For mirrors you use a Dirac delta to get only the light coming from a specific direction.  

---

## 0 answers

