# 🎯 Sampling the hemisphere

## Introduction

Generating samples within a hemisphere around a point is needed when doing Monte-Carlo path tracing. These samples can be chosen in a smart way, to be proportional to the BRDF. The following sections explain how to generate these samples, and how to properly weight them using the pdf, for several BRDF models.

The result of importance sampling can be a significant reduction in noise. In the following image, the sphere on the left uses cosine-weighted sampling while the sphere on the right uses BRDF importance-sampling.

## Spherical and cartesian coordinates

When generating samples in the hemisphere, this is usually done using spherical coordinates by calculating a $\theta$ and $\phi$ angle. These can then be converted to cartesian coordinates using the following formula.

\left\{\begin{matrix} \begin{aligned} x&=\cos(\phi) \cdot \sin(\theta)\\ y&=\sin(\phi) \cdot \sin(\theta)\\ z&=\cos(\theta) \end{aligned} \end{matrix}\right.

In these snippets you'll see that the sine/cosine is often calculated directly instead of calculating the angle $\theta$ and then taking the sine/cosine.

## Technique

The following steps are performed.

1. Calculate the normalized PDF
2. Split up the PDF (one for $\theta$, one for $\phi$)
3. Calculate the CDF by integrating the PDF
4. Set the CDF equal to some random number between 0 and 1 to obtain a $\theta$ and $\phi$ angle which define the sample

A common action that is performed throughout the following equations is going from an integral over the hemisphere using solid angles, to an integral over the hemisphere using spherical coordinates. This transformation is done as follows.

$\int _{\Omega }f(\theta)dw = \int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}f(\theta, \phi) \sin\theta d \theta d \phi$

## Uniform hemisphere

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned}\end{matrix}\right.

$pdf(w_{i}) = \frac{1}{2\pi}$

Derivation

For uniform sampling, the PDF should be proportional to 1. We PDF is normalized by dividing by the PDF, integrated over the whole hemisphere.

\begin{aligned} pdf(\omega) &= \frac{1}{\int _{\Omega }1d\omega} = \frac{1}{\int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}1\sin\theta d \theta d \phi} = \frac{1}{2\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{\sin\theta}{2\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for $\theta$ by integrating over the whole domain of the $\phi$ angle which is the same as multiplying by $2\pi$ since the pdf is isotropic. The pdf for $\phi$ may be obtained by using conditional probabilities and the rule of Bayes.

\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = \sin\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{\sin\theta}{2\pi}}{\sin\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}\sin\theta d\theta = 1-\cos\theta \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number $\varepsilon, \epsilon \left [ 0, 1 \right ]$, we can find an equation for the angle $\theta$ and $\phi$ that we can use for our sample.

\begin{alignat*}{2} cdf(\theta) &= 1-\cos\theta {}& =\varepsilon_{0} \rightarrow \theta &= \cos^{-1}(1-\varepsilon_{0}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since $\varepsilon$ is a number between 0 and 1, these equations can be written a bit cleaner as follows.

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

## Cosine-weighted hemisphere

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\sqrt{\varepsilon_{0}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

$pdf(w_{i}) = \frac{\cos\theta}{\pi}$

Derivation

For cosine-weighted sampling, the PDF should be proportional to a cosine. The PDF is normalized by dividing by the result of integrating the PDF over the whole hemisphere.

\begin{aligned} pdf(\omega) &= \frac{\cos\theta}{\int _{\Omega }\cos\theta d\omega} = \frac{\cos\theta}{\int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}\sin\theta\cos\theta d \theta d \phi} = \frac{\cos\theta}{\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{\sin\theta\cos\theta}{\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for $\theta$ by integrating over the whole domain of the $\phi$ angle which is the same as multiplying by $2\pi$ since the pdf is isotropic. The pdf for $\phi$ may be obtained by using conditional probabilities and the rule of Bayes.

\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = 2\sin\theta\cos\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{\sin\theta\cos\theta}{\pi}}{2\sin\theta\cos\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}2\sin\theta\cos\theta d\theta = \sin^2\theta \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number $\varepsilon, \epsilon \left [ 0, 1 \right ]$, we can find an equation for the angle $\theta$ and $\phi$ that we can use for our sample.

\begin{alignat*}{2} cdf(\theta) &= \sin^2\theta {}& =\varepsilon_{0} \rightarrow \theta &= \sin^{-1}(\sqrt{\varepsilon_{0}}) = \cos^{-1}(\sqrt{1-\varepsilon_{0}}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since $\varepsilon$ is a number between 0 and 1, these equations can be written a bit cleaner as follows.

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\sqrt{\varepsilon_{0}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

## Power cosine-weighted hemisphere

This is a generalization of the cosine-weighted sampling scheme in the previous section. Instead of sampling proportional to $\cos\theta$, here we sample proportional to $\cos^{\alpha}\theta$.

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+1}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

$pdf(w_{i}) = \frac{(\alpha+1)\cos^{\alpha}\theta}{2\pi}$

Derivation

For power cosine-weighted sampling, the PDF should be proportional to a cosine with a power exponent. The PDF is normalized by dividing by the result of integrating the PDF over the whole hemisphere

\begin{aligned} pdf(\omega) &= \frac{\cos^{\alpha}\theta}{\int _{\Omega }\cos^{\alpha}\theta d\omega} = \frac{\cos^{\alpha}\theta}{\int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}\sin\theta\cos^{\alpha}\theta d \theta d \phi} = \frac{(\alpha+1)\cos^{\alpha}\theta}{2\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{(\alpha+1)\cos^{\alpha}\theta\sin\theta}{2\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for $\theta$ by integrating over the whole domain of the $\phi$ angle which is the same as multiplying by $2\pi$ since the pdf is isotropic. The pdf for $\phi$ may be obtained by using conditional probabilities and the rule of Bayes.

\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = (\alpha+1)\cos^{\alpha}\theta\sin\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{(\alpha+1)\cos^{\alpha}\theta\sin\theta}{2\pi}}{(\alpha+1)\cos^{\alpha}\theta\sin\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}(\alpha+1)\cos^{\alpha}\theta\sin\theta d\theta = 1-\cos^{(\alpha+1)}\theta \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number $\varepsilon, \epsilon \left [ 0, 1 \right ]$, we can find an equation for the angle $\theta$ and $\phi$ that we can use for our sample.

\begin{alignat*}{2} cdf(\theta) &= 1-\cos^{(\alpha+1)}\theta {}& =\varepsilon_{0} \rightarrow \theta &= \cos^{-1}((1-\varepsilon_{0})^{\frac{1}{\alpha+1}}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since $\varepsilon$ is a number between 0 and 1, these equations can be written a bit cleaner as follows.

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+1}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

## Microfacet-BRDF hemisphere

For microfacet BRDFs, the sampling can be made proportional to the used normal distribution function $D(h)$. The steps are similar for each normal distribution function. The PDFs may be calculated by noting that NDFs need to satisfy the following equation where $\theta$ is the angle between the normal vector NN and the halfway vector $H$.

$\int _{\Omega }D(h)\cos\theta dh = 1$

### Blinn-Phong

$D(h) = \frac{(\alpha+2)\cos\theta^{\alpha}}{2\pi}$

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+2}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

$pdf(w_{i}) = \frac{(\alpha+2)\cos\theta^{(\alpha+1)}}{2\pi}$

Derivation

\begin{aligned} pdf(\omega) &= D(h)\cos\theta = \frac{(\alpha+2)\cos\theta^{\alpha}\cos\theta}{2\pi} = \frac{(\alpha+2)\cos\theta^{(\alpha+1)}}{2\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta}{2\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for $\theta$ by integrating over the whole domain of the $\phi$ angle which is the same as multiplying by $2\pi$ since the pdf is isotropic. The pdf for $\phi$ may be obtained by using conditional probabilities and the rule of Bayes.

\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = (\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta}{2\pi}}{(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta d\theta = 1-\cos\theta^{(\alpha+2)} \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number $\varepsilon, \epsilon \left [ 0, 1 \right ]$, we can find an equation for the angle $\theta$ and $\phi$ that we can use for our sample.

\begin{alignat*}{2} cdf(\theta) &= 1-\cos\theta^{(\alpha+2)}\theta {}& =\varepsilon_{0} \rightarrow \theta &= \cos^{-1}((1-\varepsilon_{0})^{\frac{1}{\alpha+2}}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since $\varepsilon$ is a number between 0 and 1, these equations can be written a bit cleaner as follows.

\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+2}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

### Beckmann

$D(h) = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}}{\alpha^{2}\cos^4\theta}$

\left\{\begin{matrix} \begin{aligned} \theta &= \tan^{-1}\sqrt{-\alpha^2\ln(1-\frac{\varepsilon_{0}}{\pi})} \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

$pdf(w_{i}) = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}}{\alpha^{2}\cos^3\theta}$

Derivation

\begin{aligned} pdf(\omega) &= D(h)\cos\theta = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\cos\theta}{\alpha^{2}\cos^4\theta} = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}}{\alpha^{2}\cos^3\theta} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta} \end{aligned}

Next, the PDF is split up. We obtain the pdf for $\theta$ by integrating over the whole domain of the $\phi$ angle which is the same as multiplying by $2\pi$ since the pdf is isotropic. The pdf for $\phi$ may be obtained by using conditional probabilities and the rule of Bayes.

\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = \frac{2\pi e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta} \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta}}{\frac{2\pi e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta}} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}\frac{2\pi e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta} d\theta = \pi(1-e^{\frac{-\tan\theta^{2}}{\alpha^{2}}})\\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number $\varepsilon, \epsilon \left [ 0, 1 \right ]$, we can find an equation for the angle $\theta$ and $\phi$ that we can use for our sample.

\begin{alignat*}{2} cdf(\theta) &= \pi(1-e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}) {}& =\varepsilon_{0} \rightarrow \theta &= \tan^{-1}\sqrt{-\alpha^2\ln(1-\frac{\varepsilon_{0}}{\pi})} \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

This gives us the following samples.

\left\{\begin{matrix} \begin{aligned} \theta &= \tan^{-1}\sqrt{-\alpha^2\ln(1-\frac{\varepsilon_{0}}{\pi})} \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.