title: OpenGL GLSL SSAO Implementation
body: <p>I try to implement Screen Space Ambient Occlusion (SSAO) based on the R5 Demo found here: <a href="http://blog.nextrevision.com/?p=76" rel="nofollow noreferrer">http://blog.nextrevision.com/?p=76</a></p>
  
  <p>In Fact I try to adapt their SSAO - Linear shader to fit into my own little engine.</p>
  
  <p><strong>1) I calculate View Space surface normals and Linear depth values.</strong>
  I Store them in a RGBA texture using the following shader:</p>
  
  <p>Vertex:</p>
  
  <pre><code>varNormalVS = normalize(vec3(vmtInvTranspMatrix * vertexNormal));
  depth = (modelViewMatrix * vertexPosition).z;
  depth = (-depth-nearPlane)/(farPlane-nearPlane);
  gl_Position = pvmtMatrix * vertexPosition;
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>gl_FragColor = vec4(varNormalVS.x,varNormalVS.y,varNormalVS.z,depth)
  </code></pre>
  
  <p>For my linear depth calculation I referred to: <a href="http://www.gamerendering.com/2008/09/28/linear-depth-texture/" rel="nofollow noreferrer">http://www.gamerendering.com/2008/09/28/linear-depth-texture/</a></p>
  
  <p>Is it correct?
  Texture seem to be correct, but maybe it is not?</p>
  
  <p><img src="https://i.stack.imgur.com/K2QKx.jpg" alt="enter image description here"></p>
  
  <p><strong>2) The actual SSAO Implementation:</strong>
  As mentioned above the original can be found here: <a href="http://blog.nextrevision.com/?p=76" rel="nofollow noreferrer">http://blog.nextrevision.com/?p=76</a></p>
  
  <p><strong>or faster:</strong> on pastebin <a href="http://pastebin.com/KaGEYexK" rel="nofollow noreferrer">http://pastebin.com/KaGEYexK</a></p>
  
  <p>In contrast to the original I only use 2 input textures since one of my textures stores both, normals as RGB and Linear Depht als Alpha.</p>
  
  <p>My second Texture, the random normal texture, looks like this:
  <a href="http://www.gamerendering.com/wp-content/uploads/noise.png" rel="nofollow noreferrer">http://www.gamerendering.com/wp-content/uploads/noise.png</a></p>
  
  <p>I use almost exactly the same implementation but my results are wrong.</p>
  
  <p>Before going into detail I want to clear some questions first:</p>
  
  <p><em>1) ssao shader uses projectionMatrix and it's inverse matrix.</em></p>
  
  <p>Since it is a post processing effect rendered onto a screen aligned quad via orthographic projection, the projectionMatrix is the orthographic matrix. Correct or Wrong?</p>
  
  <p><em>2) Having a combined normal and Depth texture instead of two seperate ones.</em></p>
  
  <p>In my opinion this is the biggest difference between the R5 implementation and my implementation attempt. I think this should not be a big problem, however, due to different depth textures this is most likley to cause problems.</p>
  
  <p>Please note that R5_clipRange looks like this</p>
  
  <pre><code>vec4 R5_clipRange = vec4(nearPlane, farPlane, nearPlane * farPlane, farPlane - nearPlane);
  </code></pre>
  
  <p>Original:</p>
  
  <pre><code>float GetDistance (in vec2 texCoord)
  {
  //return texture2D(R5_texture0, texCoord).r * R5_clipRange.w;
  const vec4 bitSh = vec4(1.0 / 16777216.0, 1.0 / 65535.0, 1.0 / 256.0, 1.0);
  return dot(texture2D(R5_texture0, texCoord), bitSh) * R5_clipRange.w;
  }
  </code></pre>
  
  <p>I have to admit I do not understand the code snippet. My depth his stored in the alpha of my texture and I thought it should be enought to just do this</p>
  
  <pre><code>return texture2D(texSampler0, texCoord).a  * R5_clipRange.w;
  </code></pre>
  
  <p>Correct or Wrong?</p>