<p>For explicit light sampling: yep, you just evaluate the BRDF for that incoming direction and the output direction back toward the camera. There's probability involved in the case of an area light: you have to randomly choose a point on the light source, and include a factor to convert from probability density over light source area to a density over solid angle at the receiver point. It looks like you've got that already (that's the $\cos \theta' / (p(\mathbf{x}')\|\mathbf{x}-\mathbf{x}'\|^2)$ in the formula you mentioned).</p>

<p>For sampling the reflection ray, you <em>could</em> just pick a random ray from the hemisphere and weight it by the BRDF, the same as you'd do for diffuse. But that won't be very efficient; it'll take a huge number of samples to converge to a low-noise result.</p>

<p>What you really want to do is <em>importance-sample</em> the BRDF. This means choosing a random ray not uniformly over the hemisphere, but using a probability distribution that matches the shape of the BRDF (as much as possible). Then you'll automatically get rays that are clustered around the sharp reflection peak in the BRDF when the roughness is low, and spread out more over the hemisphere when the roughness is high.</p>

<p>So how do you do it? The paper <a href="http://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.html" rel="noreferrer">Microfacet Models for Refraction through Rough Surfaces</a> by Walter et al (which popularized the GGX distribution in graphics) handily gives importance-sampling formulas for several common BRDFs in section 5.2. Their approach is to importance-sample the normal distribution of the BRDF, then leave the Fresnel and geometry factors to be factored into the path weight.</p>

<p>For example, their GGX importance-sampling formula is (equations 35–36):
$$\begin{aligned}\theta_m &amp;= \arctan \left( \frac{\alpha_g\sqrt{\xi_1}}{\sqrt{1-\xi_1}} \right) \\ \phi_m &amp;= 2\pi\xi_2\end{aligned}$$
where $\alpha_g$ is the GGX roughness parameter, and $\xi_1, \xi_2$ are a pair of uniform random numbers in [0, 1]. The outputs $\theta_m, \phi_m$ are the polar coordinates (relative to the surface normal) of the sampled microfacet normal.</p>

<p>So to use this, you'd choose random inputs $\xi_1, \xi_2$, then evaluate the above equations, and use the resulting normal to compute the reflection ray, as well as the Fresnel and geometry factors from the BRDF.</p>

CC BY-SA 3.0
2017-01-08T21:23:39.773Z
Thank you so, so much! Now everything makes a lot more sense to me. That paper is a wonderful resource too as my next step will be implementing glass after I have mirror done.
Henry Bergin
CC BY-SA 3.0
2017-01-08T20:07:08.267Z
2017-01-08T20:07:08.267Z
Nathan Reed
<p>Recently I asked a question regarding how to mix a glossy and diffuse shader in my path tracer: <a href="https://computergraphics.stackexchange.com/questions/4483/mix-shader-looks-wrong-on-my-path-tracer">Mix shader looks wrong on my path tracer</a>. 
I thought it was incorrect, but a comparison between mine and Blender's seems correct. However, now I am lost in how I would approach making the surface of a mirror look rough as this example shows:</p>

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

<p>I have done some research, and discovered that there are different types of reflectance models such as Beckmann and GGX. I've found some explanations of how to find the BRDF such as this site: <a href="http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html" rel="nofollow noreferrer">Specular BRDF</a>, but I can't find any explanation on how to do the reflection ray with explicit lighting. As shown in the pseudo code of my path tracer (below), for every object I shoot a ray towards a light and use the surface transport rendering equation to incorporate the BRDF. I am assuming this is where the GGX/Beckmann BRDF would be plugged in. (I'm guessing it's not quite that simple though and some probability must be involved). What really gets me though is that reflection ray. For diffuse it's easy because I just send off a random ray anywhere in the hemisphere of the surface normal. However, for specular, there's a more sharp bump in the BRDF. How would I translate that into a reflected ray? If I just jittered the ideal reflection ray a little, that wouldn't relate to how the microfacets are modeled in the GGX/Beckmann reflection.</p>

<p>Explicit lighting equation from Peter Shirley's Realistic Ray Tracing:</p>

<p>$$\large L_S(\mathbf x,\mathbf k_o)\approx\frac{\rho(\mathbf k_i,\mathbf k_o)L_e(\mathbf x',\mathbf x-\mathbf x')v(\mathbf x,\mathbf x')\cos\theta_i\cos\theta'}{p(\mathbf x')\left\lVert \mathbf x-\mathbf x'\right\rVert^2}$$</p>

<p>Where $p(\mathbf x)$ is the density function of the light triangle $1/\text{Area}$</p>

<p>$\mathbf x'$ is a random point on the light triangle</p>

<p>$\mathbf x$  is the hit point on the object</p>

<p>The cosines are the angles between the light's normal and the light ray, and the object's normal and the light ray</p>

<p>$\rho$ is the BRDF ($1/\pi$ for ideal diffuse)</p>

<p>And $v$ is either $1$ or $0$ depending on if it's in shadow</p>

<p>Pseudo code:</p>

<pre><code>rayColor(ray r, depth, int E=1)
{
    if(r doesn't hit triangle)
        return 0
    if(r hit is a light)
    {
       if(E)
         return light_emission
       else
         return 0
    }

    vector x = r.origin + r.direction*t // x is point where r hit tri
    vector n = normal where ray hit triangle
    n.normalize()
    vector nl = n.dot(r.d) &lt; 0 ? n : n*(-1) // properly orient normal

    if(++depth &gt; 5) return 0 // max bounces

    float triangle_area   = area of emitting triangle
    vector x_light_random = random point on emitting triangle
    vector light_normal   = normal of emitting triangle

    vector d = x_light_random = x_converted;


    if(light_normal.dot(d) &gt; 0) light_normal *= -1; // make it emit
                                                    // both sides

    object_normal.normalize();
    light_normal.normalize();

    BRDF = 1/PI // perfect diffuse
    light_emitted = 1 // emission of 1
    vector light_out = 0
    if(ray starting at x towards d hits light (i.e. not in shadow))
    {
         light_out = BRDF*light_emitted*(object_normal.dot(d))*
                            (-1*light_normal.dot(d)*triangle_area)/
                            (d.length*d.length*d.length*d.length)
    }
    vector direct_light = color_of_object_triangle*light_out;



  //----SPECULAR-----
  vector d2 = r.dir-n*2*n.dot(r.dir); // ideal reflection
  vector light_color = 1 // white since dialectics don't change spec
  vector specular = light_color*rayColor(createRay(x, d2),depth)

  float P = 0.5; // 50/50 chance of mirror/diffuse
  if(erand(Xi) &lt; P)
      return direct_light/P
  else
      return spec/(1-P)
</code></pre>

<raytracing><pathtracing><material><render>
Mimicking Blender's "roughness" in my path tracer
Henry Bergin
# Mimicking Blender's "roughness" in my path tracer

(Asked by: username_0 on 2017-01-06)

Recently I asked a question regarding how to mix a glossy and diffuse shader in my path tracer: [Mix shader looks wrong on my path tracer](https://computergraphics.stackexchange.com/questions/4483/mix-shader-looks-wrong-on-my-path-tracer).
I thought it was incorrect, but a comparison between mine and Blender's seems correct. However, now I am lost in how I would approach making the surface of a mirror look rough as this example shows:

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

I have done some research, and discovered that there are different types of reflectance models such as Beckmann and GGX. I've found some explanations of how to find the BRDF such as this site: [Specular BRDF](http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html), but I can't find any explanation on how to do the reflection ray with explicit lighting. As shown in the pseudo code of my path tracer (below), for every object I shoot a ray towards a light and use the surface transport rendering equation to incorporate the BRDF. I am assuming this is where the GGX/Beckmann BRDF would be plugged in. (I'm guessing it's not quite that simple though and some probability must be involved). What really gets me though is that reflection ray. For diffuse it's easy because I just send off a random ray anywhere in the hemisphere of the surface normal. However, for specular, there's a more sharp bump in the BRDF. How would I translate that into a reflected ray? If I just jittered the ideal reflection ray a little, that wouldn't relate to how the microfacets are modeled in the GGX/Beckmann reflection.

Explicit lighting equation from Peter Shirley's Realistic Ray Tracing:

$$\large L\_S(\mathbf x,\mathbf k\_o)\approx\frac{\rho(\mathbf k\_i,\mathbf k\_o)L\_e(\mathbf x',\mathbf x-\mathbf x')v(\mathbf x,\mathbf x')\cos\theta\_i\cos\theta'}{p(\mathbf x')\left\lVert \mathbf x-\mathbf x'\right\rVert^2}$$

Where $p(\mathbf x)$ is the density function of the light triangle $1/\text{Area}$

$\mathbf x'$ is a random point on the light triangle

$\mathbf x$ is the hit point on the object

The cosines are the angles between the light's normal and the light ray, and the object's normal and the light ray

$\rho$ is the BRDF ($1/\pi$ for ideal diffuse)

And $v$ is either $1$ or $0$ depending on if it's in shadow

Pseudo code:

```
rayColor(ray r, depth, int E=1)
{
    if(r doesn't hit triangle)
        return 0
    if(r hit is a light)
    {
       if(E)
         return light_emission
       else
         return 0
    }

    vector x = r.origin + r.direction*t // x is point where r hit tri
    vector n = normal where ray hit triangle
    n.normalize()
    vector nl = n.dot(r.d) < 0 ? n : n*(-1) // properly orient normal

    if(++depth > 5) return 0 // max bounces

    float triangle_area   = area of emitting triangle
    vector x_light_random = random point on emitting triangle
    vector light_normal   = normal of emitting triangle

    vector d = x_light_random = x_converted;

    if(light_normal.dot(d) > 0) light_normal *= -1; // make it emit
                                                    // both sides

    object_normal.normalize();
    light_normal.normalize();

    BRDF = 1/PI // perfect diffuse
    light_emitted = 1 // emission of 1
    vector light_out = 0
    if(ray starting at x towards d hits light (i.e. not in shadow))
    {
         light_out = BRDF*light_emitted*(object_normal.dot(d))*
                            (-1*light_normal.dot(d)*triangle_area)/
                            (d.length*d.length*d.length*d.length)
    }
    vector direct_light = color_of_object_triangle*light_out;

  //----SPECULAR-----
  vector d2 = r.dir-n*2*n.dot(r.dir); // ideal reflection
  vector light_color = 1 // white since dialectics don't change spec
  vector specular = light_color*rayColor(createRay(x, d2),depth)

  float P = 0.5; // 50/50 chance of mirror/diffuse
  if(erand(Xi) < P)
      return direct_light/P
  else
      return spec/(1-P)

```

**Tags:** raytracing, pathtracing, material, render


---

## 1 answers

### Answer by username_1 on 2017-01-08. Score: 6 | Accepted answer

For explicit light sampling: yep, you just evaluate the BRDF for that incoming direction and the output direction back toward the camera. There's probability involved in the case of an area light: you have to randomly choose a point on the light source, and include a factor to convert from probability density over light source area to a density over solid angle at the receiver point. It looks like you've got that already (that's the $\cos \theta' / (p(\mathbf{x}')\|\mathbf{x}-\mathbf{x}'\|^2)$ in the formula you mentioned).

For sampling the reflection ray, you *could* just pick a random ray from the hemisphere and weight it by the BRDF, the same as you'd do for diffuse. But that won't be very efficient; it'll take a huge number of samples to converge to a low-noise result.

What you really want to do is *importance-sample* the BRDF. This means choosing a random ray not uniformly over the hemisphere, but using a probability distribution that matches the shape of the BRDF (as much as possible). Then you'll automatically get rays that are clustered around the sharp reflection peak in the BRDF when the roughness is low, and spread out more over the hemisphere when the roughness is high.

So how do you do it? The paper [Microfacet Models for Refraction through Rough Surfaces](http://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.html) by Walter et al (which popularized the GGX distribution in graphics) handily gives importance-sampling formulas for several common BRDFs in section 5.2. Their approach is to importance-sample the normal distribution of the BRDF, then leave the Fresnel and geometry factors to be factored into the path weight.

For example, their GGX importance-sampling formula is (equations 35–36):
$$\begin{aligned}\theta\_m &= \arctan \left( \frac{\alpha\_g\sqrt{\xi\_1}}{\sqrt{1-\xi\_1}} \right) \\ \phi\_m &= 2\pi\xi\_2\end{aligned}$$
where $\alpha\_g$ is the GGX roughness parameter, and $\xi\_1, \xi\_2$ are a pair of uniform random numbers in [0, 1]. The outputs $\theta\_m, \phi\_m$ are the polar coordinates (relative to the surface normal) of the sampled microfacet normal.

So to use this, you'd choose random inputs $\xi\_1, \xi\_2$, then evaluate the above equations, and use the resulting normal to compute the reflection ray, as well as the Fresnel and geometry factors from the BRDF.

> **Comments:**
> **username_0**: Thank you so, so much! Now everything makes a lot more sense to me. That paper is a wonderful resource too as my next step will be implementing glass after I have mirror done.  

---

