<p>How are you randomizing the samples you take? It looks to me like maybe the random values are the same across all pixels in the image, which would explain why you see 10 coherent bright spots when you have 10 samples.</p>
<p>The samples should be re-randomized for each pixel. This should produce a noisy result, with a single blurred-out bright spot instead of 10 (or however many) coherent bright spots.</p>

CC BY-SA 4.0
2020-11-12T19:35:27.647Z
That's a really good point. All the pixels compute the sample function for generating a couple of float values (even though the normal is used as reference for computing the sampling direction). However, I don't know how to generate random values that are different for each pixel in a shader.
user1754322
CC BY-SA 4.0
2020-11-13T00:24:27.573Z
@user1754322 The basic idea is to include the pixel coordinates (gl_FragCoord.xy) as seed values in the hash function used to generate the random values. Also common to include a frame index so that it gets re-randomized each frame, this allows temporal accumulation of samples as well. There was a [nice paper on GPU hash functions](http://jcgt.org/published/0009/03/02/) recently. For something quick and dirty I have [an old blog post](http://www.reedbeta.com/blog/quick-and-easy-gpu-random-numbers-in-d3d11/) as well (you'll have to translate from HLSL to GLSL).
Nathan Reed
CC BY-SA 4.0
2020-11-11T23:10:13.870Z
2020-11-11T23:10:13.870Z
Nathan Reed
<p>I have made a program that samples uniformly an environment map and computes the lighting using the GGX BRDF.</p>
<p>I have made a small example that reproduces the issue I'm having. You can check it out online: <a href="https://shorturl.at/tvzGY" rel="nofollow noreferrer">shorturl.at/tvzGY</a></p>
<p>The algorithm works as follows:</p>
<ol>
<li>We compute <code>V</code>, the unit vector that points from the surface to the camera. <code>FPosition</code> is the position of the fragment in camera space so we can use that to compute <code>V</code>.</li>
<li>We will take a few sample directions <code>L</code> in uniform random directions. These samples are taken inside a hemisphere in the direction of the normal <code>N</code>.</li>
<li>In the real program, we would use <code>L</code> to sample the environment map but, for simplicity, in the provided example, I'm just assuming a complitely white environment (the issue happens anyways).</li>
<li>For each of these samples we compute the rendering equation with the GGX BRDF, and we accumulate that into a variable.</li>
<li>And finally we divide by the number of samples.</li>
</ol>
<p>I'm using the formulas for GGX I found in this site: <a href="http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html" rel="nofollow noreferrer">http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html</a></p>
<p>The problem I'm having is that it doesn't look right. If you open the link you will see this.</p>
<p><a href="https://i.sstatic.net/czf1m.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/czf1m.png" alt="enter image description here" /></a></p>
<p>That's with roughness = 0.1, metallic = 1.0, and numSamples = 10.</p>
<p>In the top right corner, switch to the sphere. And you will see this.</p>
<p><a href="https://i.sstatic.net/72sZr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/72sZr.png" alt="enter image description here" /></a></p>
<p>You see those bright spots, the number of them correlates with the number of samples. That could make sense, maybe. But when I increment the number of samples to 100, we get this.</p>
<p><a href="https://i.sstatic.net/3a96c.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3a96c.png" alt="enter image description here" /></a></p>
<p>There are more spots but they are also smaller and blurrier. I think I must be doing something wrong because it doesn't look like a metal. Or is it just because of the uniform sampling? Even with 1000 samples per pixel it doesn't look right to me.</p>

<opengl><glsl><lighting><ggx><ibl>
Problem sampling uniformly envmap with GGX
user1754322
# Problem sampling uniformly envmap with GGX

(Asked by: username_0 on 2020-11-11)

I have made a program that samples uniformly an environment map and computes the lighting using the GGX BRDF.

I have made a small example that reproduces the issue I'm having. You can check it out online: [shorturl.at/tvzGY](https://shorturl.at/tvzGY)

The algorithm works as follows:

1. We compute `V`, the unit vector that points from the surface to the camera. `FPosition` is the position of the fragment in camera space so we can use that to compute `V`.
2. We will take a few sample directions `L` in uniform random directions. These samples are taken inside a hemisphere in the direction of the normal `N`.
3. In the real program, we would use `L` to sample the environment map but, for simplicity, in the provided example, I'm just assuming a complitely white environment (the issue happens anyways).
4. For each of these samples we compute the rendering equation with the GGX BRDF, and we accumulate that into a variable.
5. And finally we divide by the number of samples.

I'm using the formulas for GGX I found in this site: <http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html>

The problem I'm having is that it doesn't look right. If you open the link you will see this.

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

That's with roughness = 0.1, metallic = 1.0, and numSamples = 10.

In the top right corner, switch to the sphere. And you will see this.

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

You see those bright spots, the number of them correlates with the number of samples. That could make sense, maybe. But when I increment the number of samples to 100, we get this.

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

There are more spots but they are also smaller and blurrier. I think I must be doing something wrong because it doesn't look like a metal. Or is it just because of the uniform sampling? Even with 1000 samples per pixel it doesn't look right to me.

**Tags:** opengl, glsl, lighting, ggx, ibl


---

## 1 answers

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

How are you randomizing the samples you take? It looks to me like maybe the random values are the same across all pixels in the image, which would explain why you see 10 coherent bright spots when you have 10 samples.

The samples should be re-randomized for each pixel. This should produce a noisy result, with a single blurred-out bright spot instead of 10 (or however many) coherent bright spots.

> **Comments:**
> **username_0**: That's a really good point. All the pixels compute the sample function for generating a couple of float values (even though the normal is used as reference for computing the sampling direction). However, I don't know how to generate random values that are different for each pixel in a shader.  
> **username_1**: @username_0 The basic idea is to include the pixel coordinates (gl_FragCoord.xy) as seed values in the hash function used to generate the random values. Also common to include a frame index so that it gets re-randomized each frame, this allows temporal accumulation of samples as well. There was a [nice paper on GPU hash functions](http://jcgt.org/published/0009/03/02/) recently. For something quick and dirty I have [an old blog post](http://www.reedbeta.com/blog/quick-and-easy-gpu-random-numbers-in-d3d11/) as well (you'll have to translate from HLSL to GLSL).  

---

