LearnOpenGL

Translation in progress of learnopengl.com.
git clone https://git.mtkn.jp/LearnOpenGL
Log | Files | Refs

SSAO.html (34764B)


      1     <h1 id="content-title">SSAO</h1>
      2 <h1 id="content-url" style='display:none;'>Advanced-Lighting/SSAO</h1>
      3 <p>
      4   We've briefly touched the topic in the basic lighting chapter: ambient lighting. Ambient lighting is a fixed light constant we add to the overall lighting of a scene to simulate the <def>scattering</def> of light. In reality, light scatters in all kinds of directions with varying intensities so the indirectly lit parts of a scene should also have varying intensities. One type of indirect lighting approximation is called <def>ambient occlusion</def> that tries to approximate indirect lighting by darkening creases, holes, and surfaces that are close to each other. These areas are largely occluded by surrounding geometry and thus light rays have fewer places to escape to, hence the areas appear darker. Take a look at the corners and creases of your room to see that the light there seems just a little darker. 
      5 </p>
      6 
      7 <p>
      8   Below is an example image of a scene with and without ambient occlusion. Notice how especially between the creases, the (ambient) light is more occluded:
      9 </p>
     10 
     11 <img src="/img/advanced-lighting/ssao_example.png" alt="Example image of SSAO with and without"/>
     12 
     13 <p>
     14   While not an incredibly obvious effect, the image with ambient occlusion enabled does feel a lot more realistic due to these small occlusion-like details, giving the entire scene a greater feel of depth.
     15 </p>
     16   
     17 <p>
     18   Ambient occlusion techniques are expensive as they have to take surrounding geometry into account. One could shoot a large number of rays for each point in space to determine its amount of occlusion, but that quickly becomes computationally infeasible for real-time solutions. In 2007, Crytek published a technique called <def>screen-space ambient occlusion</def> (SSAO) for use in their title <em>Crysis</em>. The technique uses a scene's depth buffer in screen-space to determine the amount of occlusion instead of real geometrical data. This approach is incredibly fast compared to real ambient occlusion and gives plausible results, making it the de-facto standard for approximating real-time ambient occlusion.  
     19 </p>
     20 
     21 <p>
     22   The basics behind screen-space ambient occlusion are simple: for each fragment on a screen-filled quad we calculate an <def>occlusion factor</def> based on the fragment's surrounding depth values. The occlusion factor is then used to reduce or nullify the fragment's ambient lighting component. The occlusion factor is obtained by taking multiple depth samples in a sphere sample kernel surrounding the fragment position and compare each of the samples with the current fragment's depth value. The number of samples that have a higher depth value than the fragment's depth represents the occlusion factor.
     23 </p>
     24 
     25 <img src="/img/advanced-lighting/ssao_crysis_circle.png" class="clean" alt="Image of circle based SSAO technique as done by Crysis"/>
     26   
     27 <p>
     28   Each of the gray depth samples that are inside geometry contribute to the total occlusion factor; the more samples we find inside geometry, the less ambient lighting the fragment should eventually receive.
     29 </p>
     30   
     31 <p>
     32   It is clear the quality and precision of the effect directly relates to the number of surrounding samples we take. If the sample count is too low, the precision drastically reduces and we get an artifact called <def>banding</def>; if it is too high, we lose performance. We can reduce the amount of samples we have to test by introducing some randomness into the sample kernel. By randomly rotating the sample kernel each fragment we can get high quality results with a much smaller amount of samples. This does come at a price as the randomness introduces a noticeable <def>noise pattern</def> that we'll have to  fix by blurring the results. Below is an image (courtesy of <a href="http://john-chapman-graphics.blogspot.com/" target="_blank">John Chapman</a>) showcasing the banding effect and the effect randomness has on the results:
     33 </p>
     34   
     35 <img src="/img/advanced-lighting/ssao_banding_noise.jpg" alt="The SSAO image quality with multiple samples and a blur added"/>
     36   
     37 <p>
     38   As you can see, even though we get noticeable banding on the SSAO results due to a low sample count, by introducing some randomness the banding effects are completely gone.
     39 </p>
     40   
     41 <p>
     42   The SSAO method developed by Crytek had a certain visual style. Because the sample kernel used was a sphere, it caused flat walls to look gray as half of the kernel samples end up being in the surrounding geometry. Below is an image of Crysis's screen-space ambient occlusion that clearly portrays this gray feel:
     43 </p>
     44   
     45 <img src="/img/advanced-lighting/ssao_crysis.jpg" alt="Screen space ambient occlusion in the Crysis game by Crytek showing a gray feel due to them using a sphere kernel instead of a normal oriented hemisphere sample kernel in OpenGL"/>
     46   
     47 <p>
     48   For that reason we won't be using a sphere sample kernel, but rather a hemisphere sample kernel oriented along a surface's normal vector. 
     49 </p>
     50 
     51   <img src="/img/advanced-lighting/ssao_hemisphere.png" class="clean" alt="Image of normal oriented hemisphere sample kernel for SSAO in OpenGL"/>
     52   
     53   <p>
     54     By sampling around this <def>normal-oriented hemisphere</def> we do not consider the fragment's underlying geometry to be a contribution to the occlusion factor. This removes the gray-feel of ambient occlusion and generally produces more realistic results.
     55     This chapter's technique is based on this normal-oriented hemisphere method and a slightly modified version of John Chapman's brilliant <a href="http://john-chapman-graphics.blogspot.nl/2013/01/ssao-tutorial.html" target="_blank">SSAO tutorial</a>.
     56   </p>
     57   
     58 <h2>Sample buffers</h2>
     59 <p>
     60    SSAO requires geometrical info as we need some way to determine the occlusion factor of a fragment. For each fragment, we're going to need the following data:
     61 </p>
     62     
     63 <ul>
     64   <li>A per-fragment <strong>position</strong> vector.</li>  
     65   <li>A per-fragment <strong>normal</strong> vector.</li>  
     66   <li>A per-fragment <strong>albedo</strong> color.</li>  
     67   <li>A <strong>sample kernel</strong>.</li>
     68   <li>A per-fragment <strong>random rotation</strong> vector used to rotate the sample kernel.</li>
     69 </ul>
     70 
     71 <p>
     72   Using a per-fragment view-space position we can orient a sample hemisphere kernel around the fragment's view-space surface normal and use this kernel to sample the position buffer texture at varying offsets. For each per-fragment kernel sample we compare its depth with its depth in the position buffer to determine the amount of occlusion. The resulting occlusion factor is then used to limit the final ambient lighting component. By also including a per-fragment rotation vector we can significantly reduce the number of samples we'll need to take as we'll soon see. 
     73 </p>
     74     
     75     <img src="/img/advanced-lighting/ssao_overview.png" class="clean" alt="An overview of the SSAO screen-space OpenGL technique"/>
     76       
     77 <p>
     78   As SSAO is a screen-space technique we calculate its effect on each fragment on a screen-filled 2D quad. This does mean we have no geometrical information of the scene. What we could do, is render the geometrical per-fragment data into screen-space textures that we then later send to the SSAO shader so we have access to the per-fragment geometrical data. If you've followed along with the previous chapter you'll realize this looks quite like a deferred renderer's G-buffer setup. For that reason SSAO is perfectly suited in combination with deferred rendering as we already have the position and normal vectors in the G-buffer.
     79 </p>
     80       
     81 <note>
     82   In this chapter we're going to implement SSAO on top of a slightly simplified version of the deferred renderer from the <a href="https://learnopengl.com/Advanced-Lighting/Deferred-Shading" target="_blank">deferred shading</a> chapter. If you're not sure what deferred shading is, be sure to first read up on that.
     83  </note>
     84       
     85 <p>
     86   As we should have per-fragment position and normal data available from the scene objects, the fragment shader of the geometry stage is fairly simple: 
     87 </p>
     88       
     89 <pre><code>
     90 #version 330 core
     91 layout (location = 0) out vec4 gPosition;
     92 layout (location = 1) out vec3 gNormal;
     93 layout (location = 2) out vec4 gAlbedoSpec;
     94 
     95 in vec2 TexCoords;
     96 in vec3 FragPos;
     97 in vec3 Normal;
     98 
     99 void main()
    100 {    
    101     // store the fragment position vector in the first gbuffer texture
    102     gPosition = FragPos;
    103     // also store the per-fragment normals into the gbuffer
    104     gNormal = normalize(Normal);
    105     // and the diffuse per-fragment color, ignore specular
    106     gAlbedoSpec.rgb = vec3(0.95);
    107 }  
    108 </code></pre>
    109       
    110 <p>
    111   Since SSAO is a screen-space technique where occlusion is calculated from the visible view, it makes sense to implement the algorithm in view-space. Therefore, <var>FragPos</var> and <var>Normal</var> as supplied by the geometry stage's vertex shader are transformed to view space (multiplied by the view matrix as well). 
    112 </p>
    113       
    114 <note>
    115   It is possible to reconstruct the position vectors from depth values alone, using some clever tricks as Matt Pettineo described in his <a href="https://mynameismjp.wordpress.com/2010/09/05/position-from-depth-3/" target="_blank">blog</a>. This requires a few extra calculations in the shaders, but saves us from having to store position data in the G-buffer (which costs a lot of memory). For the sake of a more simple example, we'll leave these optimizations out of the chapter.
    116 </note>
    117       
    118 <p>
    119   The <var>gPosition</var> color buffer texture is configured as follows:
    120 </p>
    121       
    122 <pre><code>
    123 <function id='50'>glGenTextures</function>(1, &gPosition);
    124 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, gPosition);
    125 <function id='52'>glTexImage2D</function>(GL_TEXTURE_2D, 0, GL_RGBA16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
    126 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    127 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    128 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    129 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);  
    130 </code></pre>
    131       
    132 <p>
    133   This gives us a position texture that we can use to obtain depth values for each of the kernel samples. Note that we store the positions in a floating point data format; this way position values aren't clamped to [<code>0.0</code>,<code>1.0</code>] and we need the higher precision. Also note the texture wrapping method of <var>GL_CLAMP_TO_EDGE</var>. This ensures we don't accidentally oversample position/depth values in screen-space outside the texture's default coordinate region.
    134 </p>
    135       
    136 <p>
    137   Next, we need the actual hemisphere sample kernel and some method to randomly rotate it.
    138 </p>
    139       
    140 <h2>Normal-oriented hemisphere</h2>
    141 <p>
    142   We need to generate a number of samples oriented along the normal of a surface. As we briefly discussed at the start of this chapter, we want to generate samples that form a hemisphere. As it is difficult nor plausible to generate a sample kernel for each surface normal direction, we're going to generate a sample kernel in <a href="https://learnopengl.com/Advanced-Lighting/Normal-Mapping" target="_blank">tangent space</a>, with the normal vector pointing in the positive z direction.
    143 </p>
    144       
    145       <img src="/img/advanced-lighting/ssao_hemisphere.png" class="clean" alt="Image of normal oriented hemisphere sample kernel for use in SSAO in OpenGL"/>
    146         
    147 <p>
    148   Assuming we have a unit hemisphere, we can obtain a sample kernel with a maximum of <code>64</code> sample values as follows: 
    149 </p>
    150         
    151 <pre><code>
    152 std::uniform_real_distribution&lt;float&gt; randomFloats(0.0, 1.0); // random floats between [0.0, 1.0]
    153 std::default_random_engine generator;
    154 std::vector&lt;glm::vec3&gt; ssaoKernel;
    155 for (unsigned int i = 0; i &lt; 64; ++i)
    156 {
    157     glm::vec3 sample(
    158         randomFloats(generator) * 2.0 - 1.0, 
    159         randomFloats(generator) * 2.0 - 1.0, 
    160         randomFloats(generator)
    161     );
    162     sample  = glm::normalize(sample);
    163     sample *= randomFloats(generator);
    164     ssaoKernel.push_back(sample);  
    165 }
    166 </code></pre>
    167         
    168 <p>
    169   We vary the <code>x</code> and <code>y</code> direction in tangent space between <code>-1.0</code> and <code>1.0</code>, and vary the z direction of the samples between <code>0.0</code> and <code>1.0</code> (if we varied the z direction between <code>-1.0</code> and <code>1.0</code> as well we'd have a sphere sample kernel). As the sample kernel will be oriented along the surface normal, the resulting sample vectors will all end up in the hemisphere.
    170 </p>
    171         
    172 <p>
    173   Currently, all samples are randomly distributed in the sample kernel, but we'd rather place a larger weight on occlusions close to the actual fragment. We want to distribute more  kernel samples closer to the origin. We can do this with an accelerating interpolation function:
    174 </p>
    175         
    176 <pre><code>
    177    float scale = (float)i / 64.0; 
    178    scale   = lerp(0.1f, 1.0f, scale * scale);
    179    sample *= scale;
    180    ssaoKernel.push_back(sample);  
    181 }
    182 </code></pre>
    183         
    184 <p>
    185   Where <fun>lerp</fun> is defined as:
    186 </p>
    187         
    188 <pre><code>
    189 float lerp(float a, float b, float f)
    190 {
    191     return a + f * (b - a);
    192 }  
    193 </code></pre>
    194         
    195 <p>
    196   This gives us a kernel distribution that places most samples closer to its origin.
    197 </p>
    198         
    199 <img src="/img/advanced-lighting/ssao_kernel_weight.png" class="clean" alt="SSAO Sample kernels (normal oriented hemisphere) with samples more closer aligned to the fragment's center position in OpenGL"/>
    200   
    201           
    202 <p>
    203   Each of the kernel samples will be used to offset the view-space fragment position to sample surrounding geometry. We do need quite a lot of samples in view-space in order to get realistic results, which may be too heavy on performance. However, if we can introduce some semi-random rotation/noise on a per-fragment basis, we can significantly reduce the number of samples required.
    204 </p>
    205   
    206 <h2>Random kernel rotations</h2>
    207 <p>
    208   By introducing some randomness onto the sample kernels we largely reduce the number of samples necessary to get good results. We could create a random rotation vector for each fragment of a scene, but that quickly eats up memory. It makes more sense to create a small texture of random rotation vectors that we tile over the screen.
    209 </p>
    210   
    211 <p>
    212   We create a 4x4 array of random rotation vectors oriented around the tangent-space surface normal:
    213 </p>
    214   
    215 <pre><code>
    216 std::vector&lt;glm::vec3&gt; ssaoNoise;
    217 for (unsigned int i = 0; i &lt; 16; i++)
    218 {
    219     glm::vec3 noise(
    220         randomFloats(generator) * 2.0 - 1.0, 
    221         randomFloats(generator) * 2.0 - 1.0, 
    222         0.0f); 
    223     ssaoNoise.push_back(noise);
    224 }  
    225 </code></pre>
    226       
    227 <p>
    228   As the sample kernel is oriented along the positive z direction in tangent space, we leave the <code>z</code> component at <code>0.0</code> so we rotate around the <code>z</code> axis.
    229 </p>
    230   
    231 <p>
    232   We then create a 4x4 texture that holds the random rotation vectors; make sure to set its wrapping method to <var>GL_REPEAT</var> so it properly tiles over the screen.
    233 </p>
    234   
    235 <pre><code>
    236 unsigned int noiseTexture; 
    237 <function id='50'>glGenTextures</function>(1, &noiseTexture);
    238 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, noiseTexture);
    239 <function id='52'>glTexImage2D</function>(GL_TEXTURE_2D, 0, GL_RGBA16F, 4, 4, 0, GL_RGB, GL_FLOAT, &ssaoNoise[0]);
    240 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    241 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    242 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    243 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);  
    244 </code></pre>
    245   
    246 <p>
    247   We now have all the relevant input data we need to implement SSAO.
    248 </p>
    249   
    250 <h2>The SSAO shader</h2>
    251 <p>
    252    The SSAO shader runs on a 2D screen-filled quad that calculates the occlusion value for each of its fragments. As we need to store the result of the SSAO stage (for use in the final lighting shader), we create yet another framebuffer object:
    253   </p>
    254   
    255 <pre><code>
    256 unsigned int ssaoFBO;
    257 <function id='76'>glGenFramebuffers</function>(1, &ssaoFBO);  
    258 <function id='77'>glBindFramebuffer</function>(GL_FRAMEBUFFER, ssaoFBO);
    259   
    260 unsigned int ssaoColorBuffer;
    261 <function id='50'>glGenTextures</function>(1, &ssaoColorBuffer);
    262 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, ssaoColorBuffer);
    263 <function id='52'>glTexImage2D</function>(GL_TEXTURE_2D, 0, GL_RED, SCR_WIDTH, SCR_HEIGHT, 0, GL_RED, GL_FLOAT, NULL);
    264 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    265 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    266   
    267 <function id='81'>glFramebufferTexture2D</function>(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ssaoColorBuffer, 0);  
    268 </code></pre>
    269   
    270 <p>
    271   As the ambient occlusion result is a single grayscale value we'll only need a texture's red component, so we set the color buffer's internal format to <var>GL_RED</var>. 
    272 </p>
    273   
    274 <p>
    275   The complete process for rendering SSAO then looks a bit like this:
    276 </p>
    277   
    278 <pre><code>
    279 // geometry pass: render stuff into G-buffer
    280 <function id='77'>glBindFramebuffer</function>(GL_FRAMEBUFFER, gBuffer);
    281     [...]
    282 <function id='77'>glBindFramebuffer</function>(GL_FRAMEBUFFER, 0);  
    283   
    284 // use G-buffer to render SSAO texture
    285 <function id='77'>glBindFramebuffer</function>(GL_FRAMEBUFFER, ssaoFBO);
    286     <function id='10'>glClear</function>(GL_COLOR_BUFFER_BIT);    
    287     <function id='49'>glActiveTexture</function>(GL_TEXTURE0);
    288     <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, gPosition);
    289     <function id='49'>glActiveTexture</function>(GL_TEXTURE1);
    290     <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, gNormal);
    291     <function id='49'>glActiveTexture</function>(GL_TEXTURE2);
    292     <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, noiseTexture);
    293     shaderSSAO.use();
    294     SendKernelSamplesToShader();
    295     shaderSSAO.setMat4("projection", projection);
    296     RenderQuad();
    297 <function id='77'>glBindFramebuffer</function>(GL_FRAMEBUFFER, 0);
    298   
    299 // lighting pass: render scene lighting
    300 <function id='10'>glClear</function>(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    301 shaderLightingPass.use();
    302 [...]
    303 <function id='49'>glActiveTexture</function>(GL_TEXTURE3);
    304 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, ssaoColorBuffer);
    305 [...]
    306 RenderQuad();  
    307 </code></pre>
    308   
    309 <p>
    310   The <var>shaderSSAO</var> shader takes as input the relevant G-buffer textures, the noise texture, and the normal-oriented hemisphere kernel samples:
    311 </p>
    312   
    313 <pre><code>
    314 #version 330 core
    315 out float FragColor;
    316   
    317 in vec2 TexCoords;
    318 
    319 uniform sampler2D gPosition;
    320 uniform sampler2D gNormal;
    321 uniform sampler2D texNoise;
    322 
    323 uniform vec3 samples[64];
    324 uniform mat4 projection;
    325 
    326 // tile noise texture over screen, based on screen dimensions divided by noise size
    327 const vec2 noiseScale = vec2(800.0/4.0, 600.0/4.0); // screen = 800x600
    328 
    329 void main()
    330 {
    331     [...]
    332 }
    333 </code></pre>
    334   
    335 <p>
    336   Interesting to note here is the <var>noiseScale</var> variable. We want to tile the noise texture all over the screen, but as the <var>TexCoords</var> vary between <code>0.0</code> and <code>1.0</code>, the <var>texNoise</var> texture won't tile at all. So we'll calculate the required amount to scale <var>TexCoords</var> by dividing the screen's dimensions by the noise texture size.
    337 </p>
    338   
    339 <pre><code>
    340 vec3 fragPos   = texture(gPosition, TexCoords).xyz;
    341 vec3 normal    = texture(gNormal, TexCoords).rgb;
    342 vec3 randomVec = texture(texNoise, TexCoords * noiseScale).xyz;  
    343 </code></pre>
    344   
    345 <p>
    346   As we set the tiling parameters of <var>texNoise</var> to <var>GL_REPEAT</var>, the random values will be repeated all over the screen. Together with the <var>fragPos</var> and <var>normal</var> vector, we then have enough data to create a TBN matrix that transforms any vector from tangent-space to view-space:
    347 </p>
    348   
    349 <pre><code>
    350 vec3 tangent   = normalize(randomVec - normal * dot(randomVec, normal));
    351 vec3 bitangent = cross(normal, tangent);
    352 mat3 TBN       = mat3(tangent, bitangent, normal);  
    353 </code></pre>
    354   
    355 <p>
    356   Using a process called the <def>Gramm-Schmidt process</def> we create an orthogonal basis, each time slightly tilted based on the value of <var>randomVec</var>. Note that because we use a random vector for constructing the tangent vector, there is no need to have the TBN matrix exactly aligned to the geometry's surface, thus no need for per-vertex tangent (and bitangent) vectors.
    357 </p>
    358   
    359 <p>
    360   Next we iterate over each of the kernel samples, transform the samples from tangent to view-space, add them to the current fragment position, and compare the fragment position's depth with the sample depth stored in the view-space position buffer. Let's discuss this in a step-by-step fashion:
    361 </p>
    362   
    363 <pre><code>
    364 float occlusion = 0.0;
    365 for(int i = 0; i &lt; kernelSize; ++i)
    366 {
    367     // get sample position
    368     vec3 samplePos = TBN * samples[i]; // from tangent to view-space
    369     samplePos = fragPos + samplePos * radius; 
    370     
    371     [...]
    372 }  
    373 </code></pre>
    374   
    375 <p>
    376   Here <var>kernelSize</var> and <var>radius</var> are variables that we can use to tweak the effect; in this case a value of <var>64</var> and <var>0.5</var> respectively.
    377   For each iteration we first transform the respective sample to view-space. We then add the view-space kernel offset sample to the view-space fragment position. Then we multiply the offset sample by <var>radius</var> to increase (or decrease) the effective sample radius of SSAO.
    378 </p>
    379   
    380 <p>
    381   Next we want to transform <var>sample</var> to screen-space so we can sample the position/depth value of <var>sample</var> as if we were rendering its position directly to the screen. As the vector is currently in view-space, we'll transform it to clip-space first using the <var>projection</var> matrix uniform:
    382 </p>
    383   
    384 <pre><code>
    385 vec4 offset = vec4(samplePos, 1.0);
    386 offset      = projection * offset;    // from view to clip-space
    387 offset.xyz /= offset.w;               // perspective divide
    388 offset.xyz  = offset.xyz * 0.5 + 0.5; // transform to range 0.0 - 1.0  
    389 </code></pre>
    390   
    391 <p>
    392   After the variable is transformed to clip-space, we perform the perspective divide step by dividing its <code>xyz</code> components with its <code>w</code> component. The resulting normalized device coordinates are then transformed to the [<code>0.0</code>, <code>1.0</code>] range so we can use them to sample the position texture:
    393 </p>
    394   
    395 <pre><code>
    396 float sampleDepth = texture(gPosition, offset.xy).z; 
    397 </code></pre>
    398 
    399 <p>
    400   We use the <var>offset</var> vector's <code>x</code> and <code>y</code> component to sample the position texture to retrieve the depth (or <code>z</code> value) of the sample position as seen from the viewer's perspective (the first non-occluded visible fragment). We then check if the sample's current depth value is larger than the stored depth value and if so, we add to the final contribution factor:
    401 </p>
    402   
    403 <pre class="cpp"><code>
    404 occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0);  
    405 </code></pre>
    406   
    407 <p>
    408   Note that we add a small <code>bias</code> here to the original fragment's depth value (set to <code>0.025</code> in this example). A bias isn't always necessary, but it helps visually tweak the SSAO effect and solves acne effects that may occur based on the scene's complexity.  
    409 </p>
    410   
    411 <p>
    412   We're not completely finished yet as there is still a small issue we have to take into account. Whenever a fragment is tested for ambient occlusion that is aligned close to the edge of a surface, it will also consider depth values of surfaces far behind the test surface; these values will (incorrectly) contribute to the occlusion factor. We can solve this by introducing a range check as the following image (courtesy of <a href="http://john-chapman-graphics.blogspot.com/" target="_blank">John Chapman</a>) illustrates:
    413 </p>
    414   
    415   <img src="/img/advanced-lighting/ssao_range_check.png" alt="Image with and without range check of SSAO surface in OpenGL"/>
    416     
    417 <p>
    418   We introduce a range check that makes sure a fragment contributes to the occlusion factor if its depth values is within the sample's radius. We change the last line to:
    419 </p>
    420     
    421 <pre><code>
    422 float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth));
    423 occlusion       += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;         
    424 </code></pre>
    425   
    426 <p>
    427   Here we used GLSL's <fun>smoothstep</fun> function that smoothly interpolates its third parameter between the first and second parameter's range, returning <code>0.0</code> if less than or equal to its first parameter and <code>1.0</code> if equal or higher to its second parameter. If the depth difference ends up between <var>radius</var>, its value gets smoothly interpolated between <code>0.0</code> and <code>1.0</code> by the following curve:
    428 </p>
    429     
    430     <img src="/img/advanced-lighting/ssao_smoothstep.png" class="clean" alt="Image of smoothstep function in OpenGL used for rangecheck in SSAO in OpenGL"/>
    431       
    432 <p>
    433   If we were to use a hard cut-off range check that would abruptly remove occlusion contributions if the depth values are outside <var>radius</var>, we'd see obvious (unattractive) borders at where the range check is applied. 
    434 </p>
    435       
    436 <p>
    437   As a final step we normalize the occlusion contribution by the size of the kernel and output the results. Note that we subtract the occlusion factor from <code>1.0</code> so we can directly use the occlusion factor to scale the ambient lighting component.
    438 </p>
    439       
    440 <pre class="cpp"><code>
    441 }
    442 occlusion = 1.0 - (occlusion / kernelSize);
    443 FragColor = occlusion;  
    444 </code></pre>
    445       
    446 <p>
    447   If we'd imagine a scene where our favorite backpack model is taking a little nap, the ambient occlusion shader produces the following texture:
    448 </p>
    449       
    450 <img src="/img/advanced-lighting/ssao_without_blur.png" class="clean" alt="Image of SSAO shader result in OpenGL"/>
    451         
    452 <p>
    453   As we can see, ambient occlusion gives a great sense of depth. With just the ambient occlusion texture we can already clearly see the model is indeed laying on the floor, instead of hovering slightly above it. 
    454 </p>
    455         
    456 <p>
    457   It still doesn't look perfect, as the repeating pattern of the noise texture is clearly visible. To create a smooth ambient occlusion result we need to blur the ambient occlusion texture.
    458 </p>
    459         
    460 <h2>Ambient occlusion blur</h2>
    461 <p>
    462   Between the SSAO pass and the lighting pass, we first want to blur the SSAO texture. So let's create yet another framebuffer object for storing the blur result:
    463 </p>
    464         
    465 <pre><code>
    466 unsigned int ssaoBlurFBO, ssaoColorBufferBlur;
    467 <function id='76'>glGenFramebuffers</function>(1, &ssaoBlurFBO);
    468 <function id='77'>glBindFramebuffer</function>(GL_FRAMEBUFFER, ssaoBlurFBO);
    469 <function id='50'>glGenTextures</function>(1, &ssaoColorBufferBlur);
    470 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, ssaoColorBufferBlur);
    471 <function id='52'>glTexImage2D</function>(GL_TEXTURE_2D, 0, GL_RED, SCR_WIDTH, SCR_HEIGHT, 0, GL_RED, GL_FLOAT, NULL);
    472 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    473 <function id='15'>glTexParameter</function>i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    474 <function id='81'>glFramebufferTexture2D</function>(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ssaoColorBufferBlur, 0);
    475 </code></pre>
    476         
    477 <p>
    478   Because the tiled random vector texture gives us a consistent randomness, we can use this property to our advantage to create a simple blur shader:
    479 </p>
    480         
    481 <pre><code>
    482 #version 330 core
    483 out float FragColor;
    484   
    485 in vec2 TexCoords;
    486   
    487 uniform sampler2D ssaoInput;
    488 
    489 void main() {
    490     vec2 texelSize = 1.0 / vec2(textureSize(ssaoInput, 0));
    491     float result = 0.0;
    492     for (int x = -2; x &lt; 2; ++x) 
    493     {
    494         for (int y = -2; y &lt; 2; ++y) 
    495         {
    496             vec2 offset = vec2(float(x), float(y)) * texelSize;
    497             result += texture(ssaoInput, TexCoords + offset).r;
    498         }
    499     }
    500     FragColor = result / (4.0 * 4.0);
    501 }  
    502 </code></pre>
    503         
    504 <p>
    505   Here we traverse the surrounding SSAO texels between <code>-2.0</code> and <code>2.0</code>, sampling the SSAO texture an amount identical to the noise texture's dimensions. We offset each texture coordinate by the exact size of a single texel using <fun>textureSize</fun> that returns a <code>vec2</code> of the given texture's dimensions. We average the obtained results to get a simple, but effective blur:
    506 </p>
    507         
    508         <img src="/img/advanced-lighting/ssao.png" class="clean" alt="Image of SSAO texture with blur applied in OpenGL"/>
    509           
    510 <p>
    511   And there we go, a texture with per-fragment ambient occlusion data; ready for use in the lighting pass.
    512 </p>
    513           
    514 <h2>Applying ambient occlusion</h2>
    515 <p>
    516   Applying the occlusion factors to the lighting equation is incredibly easy: all we have to do is multiply the per-fragment ambient occlusion factor to the lighting's ambient component and we're done. If we take the Blinn-Phong deferred lighting shader of the previous chapter and adjust it a bit, we get the following fragment shader: 
    517 </p>                  
    518           
    519 <pre><code>
    520 #version 330 core
    521 out vec4 FragColor;
    522   
    523 in vec2 TexCoords;
    524 
    525 uniform sampler2D gPosition;
    526 uniform sampler2D gNormal;
    527 uniform sampler2D gAlbedo;
    528 uniform sampler2D ssao;
    529 
    530 struct Light {
    531     vec3 Position;
    532     vec3 Color;
    533     
    534     float Linear;
    535     float Quadratic;
    536     float Radius;
    537 };
    538 uniform Light light;
    539 
    540 void main()
    541 {             
    542     // retrieve data from gbuffer
    543     vec3 FragPos = texture(gPosition, TexCoords).rgb;
    544     vec3 Normal = texture(gNormal, TexCoords).rgb;
    545     vec3 Diffuse = texture(gAlbedo, TexCoords).rgb;
    546     float AmbientOcclusion = texture(ssao, TexCoords).r;
    547     
    548     // blinn-phong (in view-space)
    549     vec3 ambient = vec3(0.3 * Diffuse * AmbientOcclusion); // here we add occlusion factor
    550     vec3 lighting  = ambient; 
    551     vec3 viewDir  = normalize(-FragPos); // viewpos is (0.0.0) in view-space
    552     // diffuse
    553     vec3 lightDir = normalize(light.Position - FragPos);
    554     vec3 diffuse = max(dot(Normal, lightDir), 0.0) * Diffuse * light.Color;
    555     // specular
    556     vec3 halfwayDir = normalize(lightDir + viewDir);  
    557     float spec = pow(max(dot(Normal, halfwayDir), 0.0), 8.0);
    558     vec3 specular = light.Color * spec;
    559     // attenuation
    560     float dist = length(light.Position - FragPos);
    561     float attenuation = 1.0 / (1.0 + light.Linear * dist + light.Quadratic * dist * dist);
    562     diffuse  *= attenuation;
    563     specular *= attenuation;
    564     lighting += diffuse + specular;
    565 
    566     FragColor = vec4(lighting, 1.0);
    567 }
    568 </code></pre>
    569           
    570 <p>
    571   The only thing (aside from the change to view-space) we really changed is the multiplication of the scene's ambient component by <var>AmbientOcclusion</var>. With a single blue-ish point light in the scene we'd get the following result:
    572 </p>
    573           
    574 <img src="/img/advanced-lighting/ssao_final.png" class="clean" alt="Image of SSAO applied in OpenGL"/>
    575             
    576 <p>
    577   You can find the full source code of the demo scene <a href="/code_viewer_gh.php?code=src/5.advanced_lighting/9.ssao/ssao.cpp" target="_blank">here</a>.
    578 </p>
    579               
    580 <!--<ul>
    581   <li><strong>geometry</strong>: <a href="/code_viewer.php?code=advanced-lighting/ssao_geometry&type=vertex" target="_blank">vertex</a>, <a href="/code_viewer.php?code=advanced-lighting/ssao_geometry&type=fragment" target="_blank">fragment</a>.</li>
    582  <li><strong>SSAO</strong>: <a href="/code_viewer.php?code=advanced-lighting/ssao&type=vertex" target="_blank">vertex</a>, <a href="/code_viewer.php?code=advanced-lighting/ssao&type=fragment" target="_blank">fragment</a>.</li>
    583   <li><strong>blur</strong>: <a href="/code_viewer.php?code=advanced-lighting/ssao&type=vertex" target="_blank">vertex</a>, <a href="/code_viewer.php?code=advanced-lighting/ssao_blur&type=fragment" target="_blank">fragment</a>.</li>
    584   <li><strong>lighting</strong>: <a href="/code_viewer.php?code=advanced-lighting/ssao&type=vertex" target="_blank">vertex</a>, <a href="/code_viewer.php?code=advanced-lighting/ssao_lighting&type=fragment" target="_blank">fragment</a>.</li>
    585 </ul>
    586 -->
    587   
    588 <p>
    589   Screen-space ambient occlusion is a highly customizable effect that relies heavily on tweaking its parameters based on the type of scene. There is no perfect combination of parameters for every type of scene. Some scenes only work with a small radius, while other scenes require a larger radius and a larger sample count for them to look realistic. The current demo uses <code>64</code> samples, which is a bit much; play around with a smaller kernel size and try to get good results.
    590 </p>
    591             
    592 <p>
    593   Some parameters you can tweak (by using uniforms for example): kernel size, radius, bias, and/or the size of the noise kernel. You can also raise the final occlusion value to a user-defined power to increase its strength:
    594 </p>
    595             
    596 <pre><code>
    597 occlusion = 1.0 - (occlusion / kernelSize);       
    598 FragColor = pow(occlusion, power);
    599 </code></pre>
    600             
    601 <p>
    602   Play around with different scenes and different parameters to appreciate the customizability of SSAO.</p>
    603             
    604 <p>
    605   Even though SSAO is a subtle effect that isn't too clearly noticeable, it adds a great deal of realism to properly lit scenes and is definitely a technique you'd want to have in your toolkit.
    606 </p>
    607             
    608 <h2>Additional resources</h2>
    609 <ul>
    610     <li><a href="http://john-chapman-graphics.blogspot.nl/2013/01/ssao-tutorial.html" target="_blank">SSAO Tutorial</a>: excellent SSAO tutorial by John Chapman; a large portion of this chapter's code and techniques are based of his article.</li> 
    611   <li><a href="https://mtnphil.wordpress.com/2013/06/26/know-your-ssao-artifacts/" target="_blank">Know your SSAO artifacts</a>: great article about improving SSAO specific artifacts.</li>
    612   <li><a href="http://ogldev.atspace.co.uk/www/tutorial46/tutorial46.html" target="_blank">SSAO With Depth Reconstruction</a>: extension tutorial on top of SSAO from OGLDev about reconstructing position vectors from depth alone, saving us from storing the expensive position vectors in the G-buffer.</li>
    613  </ul>         
    614            
    615 
    616     </div>
    617     
    618     <div id="hover">
    619         HI
    620     </div>
    621    <!-- 728x90/320x50 sticky footer -->
    622 <div id="waldo-tag-6196"></div>
    623 
    624    <div id="disqus_thread"></div>
    625 
    626     
    627 
    628 
    629 </div> <!-- container div -->
    630 
    631 
    632 </div> <!-- super container div -->
    633 </body>
    634 </html>