# Understanding BRDF sampling

I'm trying to understand BRDF importance sampling following the exemples of the lambertian/diffuse model and the reciprocal Phong model.

I would like to be able to extend the principle to other models. If you know good ressources on the subject please feel free to give me references.

1) In the diffuse case, the pdf $$p_d(\theta,\phi)$$ of directions is : $$$$p_d(\theta,\phi)=\frac{f_r\cos{\theta}\sin{\theta}}{\int_0^{2\pi}\int_0^{\frac{\pi}{2}} f_r\cos{\theta}\sin{\theta}\,\mathrm{d}\theta\mathrm{d}\phi} =\frac{\cos{\theta}\sin{\theta}}{\pi}$$$$

In the reciprocal Phong model the pdf $$p_s(\theta_s,\phi_s)$$ is :

$$$$p_s(\theta_s,\phi_s)=\frac{k_s\cos^n{\theta_s}\sin{\theta_s}}{\int_0^{2\pi}\int_0^{\frac{\pi}{2}}k_s\cos^n{\theta_s}\sin{\theta_s}\,\mathrm{d}\theta_s\mathrm{d}\phi_s} =\frac{n+1}{2\pi}\cos^n{\theta_s}\sin{\theta_s}$$$$

But it seems that people use the densities : $$$$p_d(\theta,\phi)=\frac{\cos{\theta}}{\pi}$$$$ and $$$$p_s(\theta_s,\phi_s)=\frac{n+1}{2\pi}\cos^n{\theta_s}$$$$

What is the reason ? Could we use both versions ? Are they equivalent ?

2) Furthermore : $$$$\int_0^{2\pi}\int_0^{\frac{\pi}{2}} \frac{\cos{\theta}}{\pi}\,\mathrm{d}\theta\mathrm{d}\phi=2\pi\neq 1$$$$ and $$$$\int_0^{2\pi}\int_0^{\frac{\pi}{2}}\frac{n+1}{2\pi}\cos^n{\theta_s}\,\mathrm{d}\theta_s\mathrm{d}\phi_s = \quad?$$$$

Shouldn't the pdfs have an integral equal to 1?

3) In another post (Probability density function while using spherical coordinates), people explains that the sines functions are never taken into account because they are "artefacts" of the coordinate system.

Is that true ? Should we always reject sines functions ? And if so, why do we compute pdf with sines and reject them after ?

EDIT:

Thanks to the comment of @lightxbulb, it seems that the integrals of my second point should be evaluated with respect to solid angles: $$$$\int_{\Omega} \frac{\cos{\theta}}{\pi}\,\mathrm{d}\omega =\int_0^{2\pi}\int_0^{\frac{\pi}{2}} \frac{\cos{\theta}\sin{\theta}}{\pi}\,\mathrm{d}\theta\mathrm{d}\phi =1$$$$ and $$$$\int_{\Omega}\frac{n+1}{2\pi}\cos^n{\theta_s}\,\mathrm{d}\omega =\int_0^{2\pi}\int_0^{\frac{\pi}{2}}\frac{n+1}{2\pi}\cos^n{\theta_s}\sin{\theta_s}\,\mathrm{d}\theta_s\mathrm{d}\phi_s =1$$$$

which brings me to another point:

4) Here are different Monte-Carlo estimators for the reflected part of the rendering equation, with respect to different coordinate system:

Solid angles: $$$$L_r(\vec{x},\vec{\omega}_o)=\frac{1}{N}\sum_{i=1}^N \frac{L_i(\vec{x},\vec{\omega}_i(x,y,z)) f_r(\vec{x},\vec{\omega}_i(x,y,z),\vec{\omega}_o) \cos{\theta}}{p_1(\vec{\omega}_i(x,y,z))}$$$$ Spherical coordinates: $$$$L_r(\vec{x},\vec{\omega}_o)=\frac{1}{N}\sum_{i=1}^N \frac{L_i(\vec{x},\vec{\omega}_i(\theta,\phi)) f_r(\vec{x},\vec{\omega}_i(\theta,\phi),\vec{\omega}_o) \cos{\theta}\sin{\theta}}{p_2(\vec{\omega}_i(\theta,\phi))}$$$$ Unit square: $$$$L_r(\vec{x},\vec{\omega}_o)=\frac{\pi}{N}\sum_{i=1}^N \frac{L_i(\vec{x},\vec{\omega}_i(u,v)) f_r(\vec{x},\vec{\omega}_i(u,v),\vec{\omega}_o)}{p_3(\vec{\omega}_i(u,v))}$$$$

Sorry for the heavy notations but I would like to fully understand. So: $$$$p_1(\vec{\omega_i})=\frac{\cos{\theta}}{\pi}\quad ?$$$$

$$$$p_2(\vec{\omega_i})=\frac{\cos{\theta}\sin{\theta}}{\pi}\quad ?$$$$

$$$$p_3(\vec{\omega_i})=\quad ?$$$$

People seem to use: $$$$p_3(\vec{\omega_i})=\frac{\cos{\theta}}{\pi}$$$$

I'm doing something wrong but I don't know what

EDIT2:

The equation involving ($$\xi_1,\xi_2$$) on page 9 in the reference is not the reflected part of the rendering equation but the integral of the BRDF on the hemisphere. But we don't care here because 1) the luminance $$L_i$$ in the reflected part is not known (that's why we use importance sampling only on the BRDF) and 2) a Monte-Carlo estimator is something like: $$$$\int f(x)\, \mathrm{d}x\approx\frac{1}{N}\sum_{i=1}^N \frac{f(x_i)}{p(x_i)}$$$$ So that's just different integrands but the pdfs should be the same. Correct me if I'm wrong.

Here are the mappings:

From the spherical coordinates to solid angles: $$$$\begin{cases} x = \sin{\theta}\cos{\phi} \\ y = \sin{\theta}\sin{\phi} \\ z = \cos{\theta} \end{cases}$$$$

From the unit square to the spherical coordinates: $$$$\begin{cases} \theta = \arcsin{\sqrt{u}} \\ \phi = 2\pi v \end{cases}$$$$

I suppose that the expression "cosine weighted mapping" comes from the cosine with the normal ($$z=\cos{\theta}$$) ? If the cosine of the mapping modify the pdf why the sine of the Jacobian doesn't ? And if the mapping modify the pdf why ($$\theta = \arcsin{\sqrt{u}}$$) doesn't too ?

I have an implementation in front of me (ref : Lászlo Szirmay-Kalos, Monte Carlo Methods in Global Illumination, page 104 and 112). I think that what is done is to use the first estimator with the pdf $$p(\vec{\omega}_i)=\frac{\cos{\theta}}{\pi}$$. But the choice of the pdf is not explained and I'm very confused with the different mappings and the influence of each transformation on the mathematical formulation of the problem.

• 1) The versions without the sine are with respect to the solid angle measure, the sine pops out when you go to spherical coordinates from the Jacobian. Either way, the sine gets canceled with the sine from the rendering equation always. 2) You're missing the sine since you plugged in the pdfs with respect to the solid angle measure into an integral over spherical coordinates. 3) The sine comes from the parametrisation. It gets canceled in the estimator by the pdf. Apr 14, 2020 at 19:32
• Thanks for your answer @lightxbulb. If I understand correctly : whatever change of variable we do : from directions (solid angle) to spherical coordinates and from the hemisphere (spherical coordinates) to the unit square (to uniformly pick random numbers), because what we do is selecting a new direction (for reflection), the pdf stay in the "directions/solid angle" coordinate system ? Apr 14, 2020 at 20:01
• You always go to spherical coordinates at some point if you use the solid angle formulation. The issue you had comes from the fact that you used a pdf with respect to the solid angle measure as a pdf with respect to spherical coordinates. Note: $d\omega = \sin\theta \,d\theta d\phi$. The sine of the pdf always gets canceled with the sine in the rendering equation in the estimator. Apr 14, 2020 at 20:18
• What is this unit square estimator, why is there a $\pi$, and why is $\cos\theta$ missing from the numerator? Apr 15, 2020 at 18:20
• $\pi$ appeared and $\cos{\theta}\sin{\theta}$ disappeared because of the Jacobian of the mapping from the square to the spherical coordinates. See Lafortune : Using the Modified Phong Reflectance Model for Physically Based Rendering, page 9). Apr 15, 2020 at 18:31