<p>Leaving the actual derivation of mathematics aside, I'll try to give a general description of how can we combine the NDF with importance sampling in spherical coordinates.</p>
<h2>What is NDF ?</h2>
<p>The problem is the following. Intiutively, we need the NDF for &quot;zooming in&quot; from the realm of macrosurfaces to that of microfacets.</p>
<p>Mathematically it is needed for the following equivalence:</p>
<p><span class="math-container">$$dA_h = NDF(h) dw_{h} A$$</span>.</p>
<ul>
<li><p><span class="math-container">$A$</span>: flat macrosurface patch</p>
</li>
<li><p><span class="math-container">$dw_{h}$</span>: is the area of a portion of a unit spherical cap defined by the orientation <span class="math-container">$h$</span></p>
</li>
<li><p><span class="math-container">$dA_{h}$</span>: is the total area that is being approximated by the orientation <span class="math-container">$h$</span></p>
</li>
</ul>
<p>see <a href="http://www.reedbeta.com/blog/hows-the-ndf-really-defined/" rel="nofollow noreferrer">here</a> for more.</p>
<p>These terms are understood better when we considere the following equivalence:
<span class="math-container">$$d\Omega = 4\pi (\frac{dS}{A})(\hat{r} \cdot \hat{n})$$</span></p>
<p>You can read this equivalence as the following:
One can approximate a solid angle subtended by a flat surface whose area equals <span class="math-container">$dS$</span>, orientation <span class="math-container">$\hat{n}$</span>, and distance from the viewer <span class="math-container">$r$</span> where <span class="math-container">$\hat{r} = \frac{\vec{r}}{r}$</span> and <span class="math-container">$A = 4\pi r^2$</span>. The <span class="math-container">$\vec{r}$</span> is a position vector of the surface area <span class="math-container">$dS$</span> with respect to the center of the sphere. You can read more about it in <a href="https://en.wikipedia.org/wiki/Solid_angle" rel="nofollow noreferrer">wikipedia</a></p>
<p>The point here is that whenever we consider approximating solid angles with projected surfaces, we need that direction and distance term.
Whether we consider solid angles over microfacets or macrosurfaces don't make a difference. From this perspective, you can think of <span class="math-container">$NDF$</span> as &quot;regulation&quot; term for two different granularities involved in approximating the same structure.</p>
<p>Since we are modeling the same structure from two different granularities, we can't &quot;touch&quot; properties that coincides with physical/geometric properties of the structure, but we can &quot;touch&quot; the regulation term, hence different ways of computing ndf are provided by different researchers.</p>
<p>Whatever it maybe though, it has to satisfy the following equivalence for unit sphere:
<span class="math-container">$$1=\frac{1}{A}\int_{S} (n \cdot h) dA_{h} = \int_{\Omega} NDF(h) (n \cdot h) dw_{h}$$</span></p>
<p>Where S is the domain of the area of the macrosurface patch. This reads simply as the following: The area we are calculating as a spherical cap using solid angle bits, and the area we are calculating as a macrosurface patch using macro surface patch bits has to be the same.</p>
<hr />
<h2>What is Importance Sampling ?</h2>
<p>Here is the second bit of information necessary to understand the procedure.
Let X be a random variable, ie a function whose signature is <span class="math-container">$X: M \to R $</span>, where <span class="math-container">$M$</span> is the sample space and <span class="math-container">$R$</span> is the set of real numbers. Expected value <span class="math-container">$E[X]$</span> is given under the probability distribution <span class="math-container">$P$</span> as the following <span class="math-container">$E[X;P] = \frac{1}{n} \sum_{i=1}^n x_i$</span> where n is the total number of samples drawn from the distribution and <span class="math-container">$x_i$</span> is sample generated from <span class="math-container">$X$</span> using the distribution P.</p>
<p>The precision of the estimate calculated this way depends on the variance of the random variable <span class="math-container">$X$</span> under the probability distribution <span class="math-container">$P$</span>. If we have infinite number of samples, then its okay to use low precision estimates, but we never do. In fact, it would be great if we have a small number of samples, with a more precise way of computing the estimate.</p>
<p>Now think of the following, we have some random variable X, that outputs values according to some distribution, and we want to understand how X works under that distribution. Wouldn't it be better to select the input values for X so that the graph made by the output values look more like the actual graph of the X under distribution P. This is what we are trying to do with importance sampling. The key here is to understand that though we may not know how X maps values in its domain to its codomain, we may know how the probability distribution for choosing samples works. Hence if we choose a function that is similar to random variable's actual probability distribution, we can significantly reduce variance.</p>
<hr />
<h2>How they interact ?</h2>
<p>Now coming to your question. We have seen that in order to use importance sampling we need to use function that resembles to the actual probability distribution used for sampling the spherical cap area. So if our function is <span class="math-container">$f=NDF(h) (n \cdot h)$</span>, we need use a function that is similar to the actual probability distribution of this function to sample values from it in order to get an estimate with higher precision.</p>
<p>Since we are dealing with points on unit sphere, we are dealing with <a href="https://en.wikipedia.org/wiki/Spherical_coordinate_system#Integration_and_differentiation_in_spherical_coordinates" rel="nofollow noreferrer">spherical coordinates</a>. The vectors associated with these points are also in spherical coordinates. Now spherical coordinates are defined using two angles and a distance to the center of the sphere. In unit sphere the distance is 1. This leaves us with
the two angles when considering vectors.</p>
<p>The probability distribution associated with <span class="math-container">$f$</span> then has two parameters, <span class="math-container">$\theta$</span> and <span class="math-container">$\phi$</span> due to using spherical coordinates.</p>
<p>Now the <a href="https://en.wikipedia.org/wiki/Probability_density_function" rel="nofollow noreferrer">probability density function</a> (pdf) of <span class="math-container">$f$</span> is given in <a href="https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html" rel="nofollow noreferrer">here</a>. From that we calculate its <a href="https://en.wikipedia.org/wiki/Cumulative_distribution_function" rel="nofollow noreferrer">cumulative distribution function</a> (cdf) which is also given in <a href="https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html" rel="nofollow noreferrer">here</a>.</p>
<p>We need the cdf because of the following property: If <span class="math-container">$T$</span> is a continuous random variable with cumulative distribution function <span class="math-container">$cdf(T)$</span>, then the random variable <span class="math-container">$Y = cdf(T)$</span> has a uniform distribution on [0, 1]. Meaning that the inverse of <span class="math-container">$cdf(T)$</span> whose input would come from a uniform distribution on [0, 1] would output a value according to the probability distribution of <span class="math-container">$T$</span>. More on that on <a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling" rel="nofollow noreferrer">wikipedia</a> as suggested by <a href="https://computergraphics.stackexchange.com/a/10661/13456">peter</a>.</p>
<p>Coming back to our point. We are looking for function closer to the actual probability distribution of <span class="math-container">$f$</span>. Why ? Because it is the actual probability distribution that &quot;causes&quot; <span class="math-container">$f$</span> to output <span class="math-container">$x_i$</span> (sample generated in accordance with the probability distribution of random variable as stated in importance sampling section), since the probability distribution of <span class="math-container">$f$</span> takes two parameters, the angles of spherical coordinate, we need to adapt pdf and cdf to deal with two parameters.</p>
<p>It turns out, we can sample one of the angles uniformly, hence
<code>phi = X_i.x * 2 * PI</code> given that <code>X_i.x</code> is a random number between [0,1].</p>
<p>Then we only need to deal with pdf and cdf of a single parameter.
If we can find out manually the inverse of cdf of that parameter, we can map a random value between [0, 1] to a value according to correct distribution of that parameter. The piece of code that deal with <code>X_i.y</code> represents that manually found inverse of cdf.</p>
<p>You can read about the involved mathematics from:</p>
<ul>
<li><p><a href="https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html" rel="nofollow noreferrer">Here</a></p>
</li>
<li><p><a href="https://agraphicsguynotes.com/posts/sample_microfacet_brdf/" rel="nofollow noreferrer">Here</a>. This gives the full derivation</p>
</li>
<li><p>and <a href="http://www.reedbeta.com/blog/hows-the-ndf-really-defined/" rel="nofollow noreferrer">Here</a></p>
</li>
</ul>

CC BY-SA 4.0
2021-02-11T16:32:54.070Z
2021-02-12T15:21:17.303Z
2021-02-12T15:21:17.303Z
Kaan E.
<p>Generally to importance sample a funtion, you calculate it using the inverse cdf method.</p>
<p><a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Inverse_transform_sampling</a></p>
<p>From when I remember, you basically take the integral/cdf of your function and normalize it. Then you know that your random number will be proportional to the the distribution, so you inverse it so that you are calculaiting x from f(x), where f(x) will be your random number.</p>
<p>Here it looks like they are inversing to generate n.h, which is the CosTheta. SinTheta is then just being calucated by the identity sqrt(1 - costheta^2).</p>

CC BY-SA 4.0
2021-02-11T14:10:06.627Z
Could you please Illustrate how they applied inverse transform sampling for this specific case?. They make it sound so complicated on Wikipedia.
Sync it
CC BY-SA 4.0
2021-02-11T14:12:27.920Z
Thank you for explaining the sinTheta part. Learned some more basic trigonometry after looking that up
Sync it
CC BY-SA 4.0
2021-02-11T12:07:12.280Z
2021-02-11T12:07:12.280Z
Peter
<p>I have read this <a href="https://learnopengl.com/PBR/Theory" rel="nofollow noreferrer">article</a>
On PBR  and in it we have NDF function defined as follows</p>
<p><a href="https://i.sstatic.net/fG7mB.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fG7mB.jpg" alt="enter image description here" /></a></p>
<p>Then I have read this <a href="http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html" rel="nofollow noreferrer">article</a> on random vector generation</p>
<p>Here are what I feel are the important bits.</p>
<p><a href="https://i.sstatic.net/zCxSa.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zCxSa.jpg" alt="enter image description here" /></a></p>
<p>Now jump to <a href="https://learnopengl.com/PBR/IBL/Specular-IBL" rel="nofollow noreferrer">Importance sampling</a> here is the function</p>
<p><a href="https://i.sstatic.net/5EY56.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5EY56.jpg" alt="enter image description here" /></a></p>
<p>Xi is the random vector returned from Hammersley function.</p>
<p>Notice how I have left the theory on top of the function. He clearly says &quot;We combine the NDF with the Hammersley equation in spherical space&quot;</p>
<p>This is my question. How did he do that? I have read the epic games real shading pdf and they don't provide any explanation whatsoever</p>
<p><a href="https://i.sstatic.net/xoOUW.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xoOUW.jpg" alt="enter image description here" /></a></p>
<p>All I need to understand is how did they derive the cosTheta and sinTheta terms.</p>
<p>Any explanation even a one liner as an comment would be immensely appreciated.</p>
<p>Thank you.</p>

<pbr><importance-sampling>
PBR : Understanding importance sampling
Sync it
# PBR : Understanding importance sampling

(Asked by: username_0 on 2021-02-11)

I have read this [article](https://learnopengl.com/PBR/Theory)
On PBR and in it we have NDF function defined as follows

[![enter image description here](https://i.sstatic.net/fG7mB.jpg)](https://i.sstatic.net/fG7mB.jpg)

Then I have read this [article](http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html) on random vector generation

Here are what I feel are the important bits.

[![enter image description here](https://i.sstatic.net/zCxSa.jpg)](https://i.sstatic.net/zCxSa.jpg)

Now jump to [Importance sampling](https://learnopengl.com/PBR/IBL/Specular-IBL) here is the function

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

Xi is the random vector returned from Hammersley function.

Notice how I have left the theory on top of the function. He clearly says "We combine the NDF with the Hammersley equation in spherical space"

This is my question. How did he do that? I have read the epic games real shading pdf and they don't provide any explanation whatsoever

[![enter image description here](https://i.sstatic.net/xoOUW.jpg)](https://i.sstatic.net/xoOUW.jpg)

All I need to understand is how did they derive the cosTheta and sinTheta terms.

Any explanation even a one liner as an comment would be immensely appreciated.

Thank you.

**Tags:** pbr, importance-sampling


---

## 2 answers

### Answer by username_1 on 2021-02-11. Score: 2 | Accepted answer

Leaving the actual derivation of mathematics aside, I'll try to give a general description of how can we combine the NDF with importance sampling in spherical coordinates.

## What is NDF ?

The problem is the following. Intiutively, we need the NDF for "zooming in" from the realm of macrosurfaces to that of microfacets.

Mathematically it is needed for the following equivalence:

$$dA\_h = NDF(h) dw\_{h} A$$.

* $A$: flat macrosurface patch
* $dw\_{h}$: is the area of a portion of a unit spherical cap defined by the orientation $h$
* $dA\_{h}$: is the total area that is being approximated by the orientation $h$

see [here](http://www.reedbeta.com/blog/hows-the-ndf-really-defined/) for more.

These terms are understood better when we considere the following equivalence:
$$d\Omega = 4\pi (\frac{dS}{A})(\hat{r} \cdot \hat{n})$$

You can read this equivalence as the following:
One can approximate a solid angle subtended by a flat surface whose area equals $dS$, orientation $\hat{n}$, and distance from the viewer $r$ where $\hat{r} = \frac{\vec{r}}{r}$ and $A = 4\pi r^2$. The $\vec{r}$ is a position vector of the surface area $dS$ with respect to the center of the sphere. You can read more about it in [wikipedia](https://en.wikipedia.org/wiki/Solid_angle)

The point here is that whenever we consider approximating solid angles with projected surfaces, we need that direction and distance term.
Whether we consider solid angles over microfacets or macrosurfaces don't make a difference. From this perspective, you can think of $NDF$ as "regulation" term for two different granularities involved in approximating the same structure.

Since we are modeling the same structure from two different granularities, we can't "touch" properties that coincides with physical/geometric properties of the structure, but we can "touch" the regulation term, hence different ways of computing ndf are provided by different researchers.

Whatever it maybe though, it has to satisfy the following equivalence for unit sphere:
$$1=\frac{1}{A}\int\_{S} (n \cdot h) dA\_{h} = \int\_{\Omega} NDF(h) (n \cdot h) dw\_{h}$$

Where S is the domain of the area of the macrosurface patch. This reads simply as the following: The area we are calculating as a spherical cap using solid angle bits, and the area we are calculating as a macrosurface patch using macro surface patch bits has to be the same.

---

## What is Importance Sampling ?

Here is the second bit of information necessary to understand the procedure.
Let X be a random variable, ie a function whose signature is $X: M \to R $, where $M$ is the sample space and $R$ is the set of real numbers. Expected value $E[X]$ is given under the probability distribution $P$ as the following $E[X;P] = \frac{1}{n} \sum\_{i=1}^n x\_i$ where n is the total number of samples drawn from the distribution and $x\_i$ is sample generated from $X$ using the distribution P.

The precision of the estimate calculated this way depends on the variance of the random variable $X$ under the probability distribution $P$. If we have infinite number of samples, then its okay to use low precision estimates, but we never do. In fact, it would be great if we have a small number of samples, with a more precise way of computing the estimate.

Now think of the following, we have some random variable X, that outputs values according to some distribution, and we want to understand how X works under that distribution. Wouldn't it be better to select the input values for X so that the graph made by the output values look more like the actual graph of the X under distribution P. This is what we are trying to do with importance sampling. The key here is to understand that though we may not know how X maps values in its domain to its codomain, we may know how the probability distribution for choosing samples works. Hence if we choose a function that is similar to random variable's actual probability distribution, we can significantly reduce variance.

---

## How they interact ?

Now coming to your question. We have seen that in order to use importance sampling we need to use function that resembles to the actual probability distribution used for sampling the spherical cap area. So if our function is $f=NDF(h) (n \cdot h)$, we need use a function that is similar to the actual probability distribution of this function to sample values from it in order to get an estimate with higher precision.

Since we are dealing with points on unit sphere, we are dealing with [spherical coordinates](https://en.wikipedia.org/wiki/Spherical_coordinate_system#Integration_and_differentiation_in_spherical_coordinates). The vectors associated with these points are also in spherical coordinates. Now spherical coordinates are defined using two angles and a distance to the center of the sphere. In unit sphere the distance is 1. This leaves us with
the two angles when considering vectors.

The probability distribution associated with $f$ then has two parameters, $\theta$ and $\phi$ due to using spherical coordinates.

Now the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) (pdf) of $f$ is given in [here](https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html). From that we calculate its [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) (cdf) which is also given in [here](https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html).

We need the cdf because of the following property: If $T$ is a continuous random variable with cumulative distribution function $cdf(T)$, then the random variable $Y = cdf(T)$ has a uniform distribution on [0, 1]. Meaning that the inverse of $cdf(T)$ whose input would come from a uniform distribution on [0, 1] would output a value according to the probability distribution of $T$. More on that on [wikipedia](https://en.wikipedia.org/wiki/Inverse_transform_sampling) as suggested by [username_2](https://computergraphics.stackexchange.com/a/10661/13456).

Coming back to our point. We are looking for function closer to the actual probability distribution of $f$. Why ? Because it is the actual probability distribution that "causes" $f$ to output $x\_i$ (sample generated in accordance with the probability distribution of random variable as stated in importance sampling section), since the probability distribution of $f$ takes two parameters, the angles of spherical coordinate, we need to adapt pdf and cdf to deal with two parameters.

It turns out, we can sample one of the angles uniformly, hence
`phi = X_i.x * 2 * PI` given that `X_i.x` is a random number between [0,1].

Then we only need to deal with pdf and cdf of a single parameter.
If we can find out manually the inverse of cdf of that parameter, we can map a random value between [0, 1] to a value according to correct distribution of that parameter. The piece of code that deal with `X_i.y` represents that manually found inverse of cdf.

You can read about the involved mathematics from:

* [Here](https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html)
* [Here](https://agraphicsguynotes.com/posts/sample_microfacet_brdf/). This gives the full derivation
* and [Here](http://www.reedbeta.com/blog/hows-the-ndf-really-defined/)


---

### Answer by username_2 on 2021-02-11. Score: 0

Generally to importance sample a funtion, you calculate it using the inverse cdf method.

<https://en.wikipedia.org/wiki/Inverse_transform_sampling>

From when I remember, you basically take the integral/cdf of your function and normalize it. Then you know that your random number will be proportional to the the distribution, so you inverse it so that you are calculaiting x from f(x), where f(x) will be your random number.

Here it looks like they are inversing to generate n.h, which is the CosTheta. SinTheta is then just being calucated by the identity sqrt(1 - costheta^2).

> **Comments:**
> **username_0**: Could you please Illustrate how they applied inverse transform sampling for this specific case?. They make it sound so complicated on Wikipedia.  
> **username_0**: Thank you for explaining the sinTheta part. Learned some more basic trigonometry after looking that up  

---

