<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Game Rendering &#187; SSAO</title>
	<atom:link href="http://www.gamerendering.com/category/lighting/ssao-lighting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gamerendering.com</link>
	<description></description>
	<lastBuildDate>Thu, 21 Jan 2010 01:32:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SSAO</title>
		<link>http://www.gamerendering.com/2009/01/14/ssao/</link>
		<comments>http://www.gamerendering.com/2009/01/14/ssao/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 19:55:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SSAO]]></category>
		<category><![CDATA[Screen Space]]></category>
		<category><![CDATA[Screen Space Ambient Occlusion]]></category>
		<category><![CDATA[Special Effects]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=569</guid>
		<description><![CDATA[This is my SSAO (Screen Space Ambient Occlusion) implementation and it&#8217;s both fast and gives good result. It&#8217;s inspired by the Crysis SSAO algorithm but also the Starcraft II implementation and a little from Nvidia&#8217;s implementation.
To use the SSAO shader, render a fullscreen quad with the SSAO shader applied to it. This shader will trace 10-16 [...]]]></description>
			<content:encoded><![CDATA[<p>This is my SSAO (Screen Space Ambient Occlusion) implementation and it&#8217;s both fast and gives good result. It&#8217;s inspired by the Crysis SSAO algorithm but also the Starcraft II implementation and a little from Nvidia&#8217;s implementation.</p>
<p class="western" lang="en-GB">To use the SSAO shader, render a fullscreen quad with the SSAO shader applied to it. This shader will trace 10-16 rays for every fragment (pixel when there are no multi-sampling) on the screen. The rays are shot in a random direction in a hemisphere around the normal of the current fragment. By doing texture lookups, the depth and the normals are compared between the current fragment and the traced ones. The comparison is a simple step-formula and from the result a SSAO term can be evaluated. This SSAO term is saved to the red channel to the texture. This result must be blurred before combined with the original scene render. The blur can for example be a bilateral blur. Both the SSAO term, and the blurring can be done in a much lower resolution (half or fourth the size) than the screen to save some clock cycles. Then after blurring, one can upsample it to full screen size and get a some blur for free.</p>
<p class="western" lang="en-GB">The combination with the original screen can be as simple as just multiplying this AO term with the already rendered screen.</p>
<p><a href="http://www.gamerendering.com/wp-content/uploads/ssaocomp.jpg"><img class="size-medium wp-image-570" title="SSAO enabled on the left, SSAO disabled on the right" src="http://www.gamerendering.com/wp-content/uploads/ssaocomp-400x148.jpg" alt="SSAO enabled on the left, SSAO disabled on the right" width="400" height="148" /></a></p>
<div class="mceTemp">The shader is written to save the AO in the red channel so render it to a single-channel texture to save memory.</div>
<div class="mceTemp">
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/myred.jpg"><img class="size-medium wp-image-583" title="The SSAO term is saved only in the red channel" src="http://www.gamerendering.com/wp-content/uploads/myred-400x301.jpg" alt="The SSAO term is saved only in the red channel" width="400" height="301" /></a></div>
<div class="mceTemp">The shader requires that the normalMap sampler holds a renderable texture with the normals of the scene in screen space in the RGB channels, and the linear depth (scaled to 0..1) in the A-channel. NOTE: These maps are not the same maps as when doing deferred lighting. For example, the normals should be pure face normals rendered to the screen (no normalmapping).</div>
<div class="mceTemp">The rnm sampler should hold a texture with random normals in the RGB-channel, and can for example be the picture below.</div>
<div class="mceTemp">
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/noise.png"><img class="size-medium wp-image-585" title="Random normals" src="http://www.gamerendering.com/wp-content/uploads/noise.png" alt="Random normals" width="64" height="64" /></a></div>
</div>
</div>
<p>These are the properties that worked well for me (but it&#8217;s different from setup to setup what gives best result):</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5695"><td class="code" id="p569code5"><pre class="c" style="font-family:monospace;">uniform <span style="color: #993333;">float</span> totStrength <span style="color: #339933;">=</span> <span style="color:#800080;">1.38</span><span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> strength <span style="color: #339933;">=</span> <span style="color:#800080;">0.07</span><span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> offset <span style="color: #339933;">=</span> <span style="color:#800080;">18.0</span><span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> falloff <span style="color: #339933;">=</span> <span style="color:#800080;">0.000002</span><span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> rad <span style="color: #339933;">=</span> <span style="color:#800080;">0.006</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here&#8217;s the SSAO GLSL fragment shader:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5696"><td class="code" id="p569code6"><pre class="c" style="font-family:monospace;">uniform sampler2D rnm<span style="color: #339933;">;</span>
uniform sampler2D normalMap<span style="color: #339933;">;</span>
varying vec2 uv<span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> totStrength<span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> strength<span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> offset<span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> falloff<span style="color: #339933;">;</span>
uniform <span style="color: #993333;">float</span> rad<span style="color: #339933;">;</span>
<span style="color: #339933;">#define SAMPLES 16 // 10 is good</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> invSamples <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">/</span><span style="color:#800080;">16.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// these are the random vectors inside a unit sphere</span>
vec3 pSphere<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> vec3<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.53812504</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.18565957</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.43192</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.13790712</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.24864247</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.44301823</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.33715037</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.56794053</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.005789503</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.6999805</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.04511441</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.0019965635</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.06896307</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.15983082</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.85477847</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.056099437</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.006954967</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.1843352</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.014653638</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.14027752</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0762037</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.010019933</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.1924225</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.034443386</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.35775623</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.5301969</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.43581226</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.3169221</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.106360726</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.015860917</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.010350345</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.58698344</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0046293875</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.08972908</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.49408212</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.3287904</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.7119986</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.0154690035</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.09183723</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.053382345</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.059675813</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.5411899</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.035267662</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.063188605</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.54602677</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.47761092</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.2847911</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.0271716</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//const vec3 pSphere[8] = vec3[](vec3(0.24710192, 0.6445882, 0.033550154),vec3(0.00991752, -0.21947019, 0.7196721),vec3(0.25109035, -0.1787317, -0.011580509),vec3(-0.08781511, 0.44514698, 0.56647956),vec3(-0.011737816, -0.0643377, 0.16030222),vec3(0.035941467, 0.04990871, -0.46533614),vec3(-0.058801126, 0.7347013, -0.25399926),vec3(-0.24799341, -0.022052078, -0.13399573));</span>
<span style="color: #666666; font-style: italic;">//const vec3 pSphere[12] = vec3[](vec3(-0.13657719, 0.30651027, 0.16118456),vec3(-0.14714938, 0.33245975, -0.113095455),vec3(0.030659059, 0.27887347, -0.7332209),vec3(0.009913514, -0.89884496, 0.07381549),vec3(0.040318526, 0.40091, 0.6847858),vec3(0.22311053, -0.3039437, -0.19340435),vec3(0.36235332, 0.21894878, -0.05407306),vec3(-0.15198798, -0.38409665, -0.46785462),vec3(-0.013492276, -0.5345803, 0.11307949),vec3(-0.4972847, 0.037064247, -0.4381323),vec3(-0.024175806, -0.008928787, 0.17719103),vec3(0.694014, -0.122672155, 0.33098832));</span>
<span style="color: #666666; font-style: italic;">//const vec3 pSphere[10] = vec3[](vec3(-0.010735935, 0.01647018, 0.0062425877),vec3(-0.06533369, 0.3647007, -0.13746321),vec3(-0.6539235, -0.016726388, -0.53000957),vec3(0.40958285, 0.0052428036, -0.5591124),vec3(-0.1465366, 0.09899267, 0.15571679),vec3(-0.44122112, -0.5458797, 0.04912532),vec3(0.03755566, -0.10961345, -0.33040273),vec3(0.019100213, 0.29652783, 0.066237666),vec3(0.8765323, 0.011236004, 0.28265962),vec3(0.29264435, -0.40794238, 0.15964167));</span>
   <span style="color: #666666; font-style: italic;">// grab a normal for reflecting the sample rays later on</span>
   vec3 fres <span style="color: #339933;">=</span> normalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>texture2D<span style="color: #009900;">&#40;</span>rnm<span style="color: #339933;">,</span>uv<span style="color: #339933;">*</span>offset<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">xyz</span><span style="color: #339933;">*</span><span style="color:#800080;">2.0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   vec4 currentPixelSample <span style="color: #339933;">=</span> texture2D<span style="color: #009900;">&#40;</span>normalMap<span style="color: #339933;">,</span>uv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #993333;">float</span> currentPixelDepth <span style="color: #339933;">=</span> currentPixelSample.<span style="color: #202020;">a</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// current fragment coords in screen space</span>
   vec3 ep <span style="color: #339933;">=</span> vec3<span style="color: #009900;">&#40;</span>uv.<span style="color: #202020;">xy</span><span style="color: #339933;">,</span>currentPixelDepth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// get the normal of current fragment</span>
   vec3 norm <span style="color: #339933;">=</span> currentPixelSample.<span style="color: #202020;">xyz</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #993333;">float</span> bl <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// adjust for the depth ( not shure if this is good..)</span>
   <span style="color: #993333;">float</span> radD <span style="color: #339933;">=</span> rad<span style="color: #339933;">/</span>currentPixelDepth<span style="color: #339933;">;</span>
&nbsp;
   vec3 ray<span style="color: #339933;">,</span> se<span style="color: #339933;">,</span> occNorm<span style="color: #339933;">;</span>
   <span style="color: #993333;">float</span> occluderDepth<span style="color: #339933;">,</span> depthDifference<span style="color: #339933;">,</span> normDiff<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>SAMPLES<span style="color: #339933;">;++</span>i<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// get a vector (randomized inside of a sphere with radius 1.0) from a texture and reflect it</span>
      ray <span style="color: #339933;">=</span> radD<span style="color: #339933;">*</span>reflect<span style="color: #009900;">&#40;</span>pSphere<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>fres<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// if the ray is outside the hemisphere then change direction</span>
      se <span style="color: #339933;">=</span> ep <span style="color: #339933;">+</span> sign<span style="color: #009900;">&#40;</span>dot<span style="color: #009900;">&#40;</span>ray<span style="color: #339933;">,</span>norm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>ray<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// get the depth of the occluder fragment</span>
      vec4 occluderFragment <span style="color: #339933;">=</span> texture2D<span style="color: #009900;">&#40;</span>normalMap<span style="color: #339933;">,</span>se.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// get the normal of the occluder fragment</span>
      occNorm <span style="color: #339933;">=</span> occluderFragment.<span style="color: #202020;">xyz</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// if depthDifference is negative = occluder is behind current fragment</span>
      depthDifference <span style="color: #339933;">=</span> currentPixelDepth<span style="color: #339933;">-</span>occluderFragment.<span style="color: #202020;">a</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// calculate the difference between the normals as a weight</span>
&nbsp;
      normDiff <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #339933;">-</span>dot<span style="color: #009900;">&#40;</span>occNorm<span style="color: #339933;">,</span>norm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// the falloff equation, starts at falloff and is kind of 1/x^2 falling</span>
      bl <span style="color: #339933;">+=</span> step<span style="color: #009900;">&#40;</span>falloff<span style="color: #339933;">,</span>depthDifference<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>normDiff<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #339933;">-</span>smoothstep<span style="color: #009900;">&#40;</span>falloff<span style="color: #339933;">,</span>strength<span style="color: #339933;">,</span>depthDifference<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// output the result</span>
   <span style="color: #993333;">float</span> ao <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">-</span>totStrength<span style="color: #339933;">*</span>bl<span style="color: #339933;">*</span>invSamples<span style="color: #339933;">;</span>
   gl_FragColor.<span style="color: #202020;">r</span> <span style="color: #339933;">=</span> ao<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is an optimized version of the same shader, but a little harder to read and understand</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5697"><td class="code" id="p569code7"><pre class="c" style="font-family:monospace;">uniform sampler2D rnm<span style="color: #339933;">;</span>
uniform sampler2D normalMap<span style="color: #339933;">;</span>
varying vec2 uv<span style="color: #339933;">;</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> totStrength <span style="color: #339933;">=</span> <span style="color:#800080;">1.38</span><span style="color: #339933;">;</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> strength <span style="color: #339933;">=</span> <span style="color:#800080;">0.07</span><span style="color: #339933;">;</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> offset <span style="color: #339933;">=</span> <span style="color:#800080;">18.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> falloff <span style="color: #339933;">=</span> <span style="color:#800080;">0.000002</span><span style="color: #339933;">;</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> rad <span style="color: #339933;">=</span> <span style="color:#800080;">0.006</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#define SAMPLES 10 // 10 is good</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> invSamples <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color:#800080;">1.38</span><span style="color: #339933;">/</span><span style="color:#800080;">10.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// these are the random vectors inside a unit sphere</span>
vec3 pSphere<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> vec3<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.010735935</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.01647018</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0062425877</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.06533369</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.3647007</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.13746321</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.6539235</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.016726388</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.53000957</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.40958285</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0052428036</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.5591124</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.1465366</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.09899267</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.15571679</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.44122112</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.5458797</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.04912532</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.03755566</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.10961345</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.33040273</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.019100213</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.29652783</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.066237666</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.8765323</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.011236004</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.28265962</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.29264435</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.40794238</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.15964167</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// grab a normal for reflecting the sample rays later on</span>
   vec3 fres <span style="color: #339933;">=</span> normalize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>texture2D<span style="color: #009900;">&#40;</span>rnm<span style="color: #339933;">,</span>uv<span style="color: #339933;">*</span>offset<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">xyz</span><span style="color: #339933;">*</span><span style="color:#800080;">2.0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> vec3<span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   vec4 currentPixelSample <span style="color: #339933;">=</span> texture2D<span style="color: #009900;">&#40;</span>normalMap<span style="color: #339933;">,</span>uv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #993333;">float</span> currentPixelDepth <span style="color: #339933;">=</span> currentPixelSample.<span style="color: #202020;">a</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// current fragment coords in screen space</span>
   vec3 ep <span style="color: #339933;">=</span> vec3<span style="color: #009900;">&#40;</span>uv.<span style="color: #202020;">xy</span><span style="color: #339933;">,</span>currentPixelDepth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// get the normal of current fragment</span>
   vec3 norm <span style="color: #339933;">=</span> currentPixelSample.<span style="color: #202020;">xyz</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #993333;">float</span> bl <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// adjust for the depth ( not shure if this is good..)</span>
   <span style="color: #993333;">float</span> radD <span style="color: #339933;">=</span> rad<span style="color: #339933;">/</span>currentPixelDepth<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//vec3 ray, se, occNorm;</span>
   <span style="color: #993333;">float</span> occluderDepth<span style="color: #339933;">,</span> depthDifference<span style="color: #339933;">;</span>
   vec4 occluderFragment<span style="color: #339933;">;</span>
   vec3 ray<span style="color: #339933;">;</span>
   <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>SAMPLES<span style="color: #339933;">;++</span>i<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// get a vector (randomized inside of a sphere with radius 1.0) from a texture and reflect it</span>
      ray <span style="color: #339933;">=</span> radD<span style="color: #339933;">*</span>reflect<span style="color: #009900;">&#40;</span>pSphere<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>fres<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// get the depth of the occluder fragment</span>
      occluderFragment <span style="color: #339933;">=</span> texture2D<span style="color: #009900;">&#40;</span>normalMap<span style="color: #339933;">,</span>ep.<span style="color: #202020;">xy</span> <span style="color: #339933;">+</span> sign<span style="color: #009900;">&#40;</span>dot<span style="color: #009900;">&#40;</span>ray<span style="color: #339933;">,</span>norm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>ray.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// if depthDifference is negative = occluder is behind current fragment</span>
      depthDifference <span style="color: #339933;">=</span> currentPixelDepth<span style="color: #339933;">-</span>occluderFragment.<span style="color: #202020;">a</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// calculate the difference between the normals as a weight</span>
 <span style="color: #666666; font-style: italic;">// the falloff equation, starts at falloff and is kind of 1/x^2 falling</span>
      bl <span style="color: #339933;">+=</span> step<span style="color: #009900;">&#40;</span>falloff<span style="color: #339933;">,</span>depthDifference<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #339933;">-</span>dot<span style="color: #009900;">&#40;</span>occluderFragment.<span style="color: #202020;">xyz</span><span style="color: #339933;">,</span>norm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #339933;">-</span>smoothstep<span style="color: #009900;">&#40;</span>falloff<span style="color: #339933;">,</span>strength<span style="color: #339933;">,</span>depthDifference<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// output the result</span>
   gl_FragColor.<span style="color: #202020;">r</span> <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">+</span>bl<span style="color: #339933;">*</span>invSamples<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To use the SSAO effect. Render a fullscreen quad over the screen with the following vertex shader and the previous SSAO fragment shader.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5698"><td class="code" id="p569code8"><pre class="c" style="font-family:monospace;">varying vec2  uv<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
gl_Position <span style="color: #339933;">=</span> sign<span style="color: #009900;">&#40;</span> gl_Position <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Texture coordinate for screen aligned (in correct range):</span>
uv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>vec2<span style="color: #009900;">&#40;</span> gl_Position.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span> gl_Position.<span style="color: #202020;">y</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> vec2<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here&#8217;s the source for a school project we did in six weeks (halftime work). It&#8217;s a simple FPS game, but does demonstrate the SSAO. Use the &#8220;O&#8221;-button to toggle the different SSAO modes. This source is unfortunately hard to build since it uses a lot of third party libraries (Ogre3D, boost, fmod, ode &#8230;)</p>
<p><a title="Source" href="http://sourceforge.net/svn/?group_id=244295">http://sourceforge.net/svn/?group_id=244295</a></p>
<p>And here&#8217;s the compiled version. Just unzip it and run the exe.</p>
<p><a title="Compiled Engine" href="http://www.gamerendering.com/uploads/release.zip">Download zip</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO%26amp%3Bbodytext%3DThis%2520is%2520my%2520SSAO%2520%2528Screen%2520Space%2520Ambient%2520Occlusion%2529%25C2%25A0implementation%2520and%2520it%2527s%2520both%2520fast%2520and%2520gives%2520good%2520result.%2520It%2527s%2520inspired%2520by%2520the%2520Crysis%2520SSAO%2520algorithm%2520but%2520also%2520the%2520Starcraft%2520II%2520implementation%2520and%2520a%2520little%2520from%2520Nvidia%2527s%2520implementation.%250D%250ATo%2520use%2520the%2520SSAO';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO%26amp%3Bnotes%3DThis%2520is%2520my%2520SSAO%2520%2528Screen%2520Space%2520Ambient%2520Occlusion%2529%25C2%25A0implementation%2520and%2520it%2527s%2520both%2520fast%2520and%2520gives%2520good%2520result.%2520It%2527s%2520inspired%2520by%2520the%2520Crysis%2520SSAO%2520algorithm%2520but%2520also%2520the%2520Starcraft%2520II%2520implementation%2520and%2520a%2520little%2520from%2520Nvidia%2527s%2520implementation.%250D%250ATo%2520use%2520the%2520SSAO';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Bt%3DSSAO';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO%26amp%3Bannotation%3DThis%2520is%2520my%2520SSAO%2520%2528Screen%2520Space%2520Ambient%2520Occlusion%2529%25C2%25A0implementation%2520and%2520it%2527s%2520both%2520fast%2520and%2520gives%2520good%2520result.%2520It%2527s%2520inspired%2520by%2520the%2520Crysis%2520SSAO%2520algorithm%2520but%2520also%2520the%2520Starcraft%2520II%2520implementation%2520and%2520a%2520little%2520from%2520Nvidia%2527s%2520implementation.%250D%250ATo%2520use%2520the%2520SSAO';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThis%2520is%2520my%2520SSAO%2520%2528Screen%2520Space%2520Ambient%2520Occlusion%2529%25C2%25A0implementation%2520and%2520it%2527s%2520both%2520fast%2520and%2520gives%2520good%2520result.%2520It%2527s%2520inspired%2520by%2520the%2520Crysis%2520SSAO%2520algorithm%2520but%2520also%2520the%2520Starcraft%2520II%2520implementation%2520and%2520a%2520little%2520from%2520Nvidia%2527s%2520implementation.%250D%250ATo%2520use%2520the%2520SSAO';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Bt%3DSSAO';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DSSAO%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DSSAO%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Btitle%3DSSAO';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2009%252F01%252F14%252Fssao%252F%26amp%3Bt%3DSSAO%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThis%2520is%2520my%2520SSAO%2520%2528Screen%2520Space%2520Ambient%2520Occlusion%2529%25C2%25A0implementation%2520and%2520it%2527s%2520both%2520fast%2520and%2520gives%2520good%2520result.%2520It%2527s%2520inspired%2520by%2520the%2520Crysis%2520SSAO%2520algorithm%2520but%2520also%2520the%2520Starcraft%2520II%2520implementation%2520and%2520a%2520little%2520from%2520Nvidia%2527s%2520implementation.%250D%250ATo%2520use%2520the%2520SSAO';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2009/01/14/ssao/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Screen Space Ambient Occlusion</title>
		<link>http://www.gamerendering.com/2008/11/04/screen-spaced-ambient-occlusion/</link>
		<comments>http://www.gamerendering.com/2008/11/04/screen-spaced-ambient-occlusion/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 21:24:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Image Enhancements]]></category>
		<category><![CDATA[SSAO]]></category>
		<category><![CDATA[AO]]></category>
		<category><![CDATA[Blur]]></category>
		<category><![CDATA[Gaussian Blur]]></category>
		<category><![CDATA[Screen Space]]></category>
		<category><![CDATA[Screen Spaced Ambient Occlusion]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=460</guid>
		<description><![CDATA[SSAO is the new technique that most new games just must include because of the hype around it since the computer game Crysis. It&#8217;s a technique for creating a rude approximation of ambient occlusion by using the a depth of the rendered scene. This works by comparing the current fragments depth with some random sample depths around it [...]]]></description>
			<content:encoded><![CDATA[<div class="mceTemp">SSAO is the new technique that most new games just must include because of the hype around it since the computer game Crysis. It&#8217;s a technique for creating a rude approximation of ambient occlusion by using the a depth of the rendered scene. This works by comparing the current fragments depth with some random sample depths around it to see if the current depth is occluded or not. The current fragment is occluded if the sample is closer to the eye than the current fragment. Although it sounds very bad to do so, in practice it does work beyond all expectations.</div>
<p>How to take the samples is a big concern as it will impact what will occlude and how much. The currently best implementations takes random samples in a hemisphere in the direction of the normal. This limits the amount of self occlusion. Another problem is that if you only take the depth into consideration then a flat surface might occlude itself because of the perspective. By also comparing the normal when calculating the AO, this problem will go away.</p>
<p>One of the hard parts of implementing SSAO is to choose the correct smoothing technique. Because of the big cost of taking occlusion samples you want to take as few samples as possible but this will give much noise in the SSAO so the result needs smoothing. Just doing a simple gaussian blur will not be good as the blur will make the SSAO bleed. Instead a blur that considers the depth and/or normals is needed. One of those is the bilateral filter which often is used in combination with SSAO.</p>
<p>The steps of a simple SSAO implementation:</p>
<ol>
<li>Render the scene. Save the linear depth in a texture. Save the normals in eye space in a texture.</li>
<li>Render a full screen quad with the SSAO shader. Save the result to a texture.</li>
<li>Blur the result in X</li>
<li>Blur the result in Y</li>
<li>Blend the blurred SSAO texture with the scene, or use it directly when rendering the scene.</li>
</ol>
<p>An optimization is to render the SSAO in a lower resolution than the screen and upsample it when blurring. Another optimization is to store both the normals and the depth in a single texture.</p>
<div id="attachment_465" class="wp-caption alignnone" style="width: 410px"><a href="http://www.gamerendering.com/wp-content/uploads/nvidia.jpg"><img class="size-medium wp-image-465" title="SSAO in the NVIDIA SDK " src="http://www.gamerendering.com/wp-content/uploads/nvidia-400x398.jpg" alt="SSAO in the NVIDIA SDK " width="400" height="398" /></a><p class="wp-caption-text">SSAO in the NVIDIA SDK </p></div>
<p>Probably one of the best implementations of SSAO is this one by NVIDIA (although it&#8217; rather slow). The SDK 10 has a paper about the technique and also source code!<br />
<a href="http://developer.download.nvidia.com/SDK/10.5/direct3d/samples.html">http://developer.download.nvidia.com/SDK/10.5/direct3d/samples.html</a></p>
<p>And here&#8217;s three papers/presentations from NVIDIA describing their SSAO in detail:<br />
<a href="http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_Ambient_Occlusion.pdf">http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_Ambient_Occlusion.pdf</a><br />
<a href="http://developer.download.nvidia.com/presentations/2008/SIGGRAPH/HBAO_SIG08b.pdf">http://developer.download.nvidia.com/presentations/2008/SIGGRAPH/HBAO_SIG08b.pdf</a><br />
<a href="http://developer.download.nvidia.com/SDK/10.5/direct3d/Source/ScreenSpaceAO/doc/ScreenSpaceAO.pdf">http://developer.download.nvidia.com/SDK/10.5/direct3d/Source/ScreenSpaceAO/doc/ScreenSpace AO.pdf</a></p>
<div id="attachment_475" class="wp-caption alignnone" style="width: 394px"><a href="http://www.gamerendering.com/wp-content/uploads/starcraft2a.jpg"><img class="size-medium wp-image-475" title="SSAO in Starcraft II" src="http://www.gamerendering.com/wp-content/uploads/starcraft2a.jpg" alt="SSAO in Starcraft II" width="384" height="287" /></a><p class="wp-caption-text">SSAO in Starcraft II</p></div>
<p>Some information of how Starcraft II will use SSAO is included in this paper ( see chapter 5.5 ):<br />
<a href="http://ati.amd.com/developer/SIGGRAPH08/Chapter05-Filion-StarCraftII.pdf">http://ati.amd.com/developer/SIGGRAPH08/Chapter05-Filion-StarCraftII.pdf</a></p>
<div id="attachment_462" class="wp-caption alignnone" style="width: 410px"><a href="http://www.gamerendering.com/wp-content/uploads/twoworlds.jpg"><img class="size-medium wp-image-462" title="SSAO in Two Worlds" src="http://www.gamerendering.com/wp-content/uploads/twoworlds-400x250.jpg" alt="SSAO in Two Worlds" width="400" height="250" /></a><p class="wp-caption-text">SSAO in Two Worlds</p></div>
<p>A link to a description of the SSAO implementation in the game Two Worlds:<br />
<a href="http://www.drobot.org/pub/GCDC_SSAO_RP_29_08.pdf">http://www.drobot.org/pub/GCDC_SSAO_RP_29_08.pdf</a></p>
<div id="attachment_464" class="wp-caption alignnone" style="width: 276px"><a href="http://www.gamerendering.com/wp-content/uploads/crysis.jpg"><img class="size-medium wp-image-464" title="SSAO in Crysis" src="http://www.gamerendering.com/wp-content/uploads/crysis.jpg" alt="SSAO in Crysis" width="266" height="166" /></a><p class="wp-caption-text">SSAO in Crysis</p></div>
<p>The paper and game that started it all (look in the chapter 8.5.4.3):<br />
<a href="http://delivery.acm.org/10.1145/1290000/1281671/p97-mittring.pdf?key1=1281671&amp;key2=9942678811&amp;coll=ACM&amp;dl=ACM&amp;CFID=15151515&amp;CFTOKEN=6184618">http://delivery.acm.org/10.1145/1290000/1281671/p97-mittring.pdf?key1=1281671&amp;key2=9942678811&amp;coll=ACM&amp;dl=ACM&amp;CFID=15151515&amp;CFTOKEN=6184618</a></p>
<div id="attachment_461" class="wp-caption alignnone" style="width: 410px"><a href="http://www.gamerendering.com/wp-content/uploads/start.jpg"><img class="size-medium wp-image-461" title="Hardware Accelerated Ambient Occlusion" src="http://www.gamerendering.com/wp-content/uploads/start-400x341.jpg" alt="Hardware Accelerated Ambient Occlusion" width="400" height="341" /></a><a href="http://www.gamerendering.com/wp-content/uploads/start.jpg"></a><p class="wp-caption-text">Hardware Accelerated Ambient Occlusion</p></div>
<p>One of the papers that probably inspired the Crysis team:<br />
<a href="http://perumaal.googlepages.com/">http://perumaal.googlepages.com/</a></p>
<p><a href="http://delivery.acm.org/10.1145/1290000/1281671/p97-mittring.pdf?key1=1281671&amp;key2=9942678811&amp;coll=ACM&amp;dl=ACM&amp;CFID=15151515&amp;CFTOKEN=6184618"></a></p>
<div id="attachment_466" class="wp-caption alignnone" style="width: 410px"><a href="http://www.gamerendering.com/wp-content/uploads/kindernoiser.jpg"><img class="size-medium wp-image-466" title="Kindernoiser SSAO" src="http://www.gamerendering.com/wp-content/uploads/kindernoiser-400x247.jpg" alt="Kindernoiser SSAO" width="400" height="247" /></a><p class="wp-caption-text">Kindernoiser SSAO</p></div>
<p>A simple but smart SSAO implementation, here with well commented shader source code:<br />
<a href="http://rgba.scenesp.org/iq/computer/articles/ssao/ssao.htm">http://rgba.scenesp.org/iq/computer/articles/ssao/ssao.htm</a></p>
<p>A gamedev.net thread with lots of discussion about SSAO<br />
<a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=463075">http://www.gamedev.net/community/forums/topic.asp?topic_id=463075</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion%26amp%3Bbodytext%3DSSAO%2520is%2520the%2520new%2520technique%2520that%2520most%2520new%2520games%2520just%2520must%2520include%2520because%2520of%2520the%2520hype%2520around%2520it%2520since%2520the%2520computer%2520game%2520Crysis.%2520It%2527s%2520a%2520technique%2520for%25C2%25A0creating%2520a%25C2%25A0rude%2520approximation%2520of%2520ambient%2520occlusion%2520by%2520using%2520the%2520a%2520depth%2520of%2520the%2520rendered%2520scene.%2520This%2520w';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion%26amp%3Bnotes%3DSSAO%2520is%2520the%2520new%2520technique%2520that%2520most%2520new%2520games%2520just%2520must%2520include%2520because%2520of%2520the%2520hype%2520around%2520it%2520since%2520the%2520computer%2520game%2520Crysis.%2520It%2527s%2520a%2520technique%2520for%25C2%25A0creating%2520a%25C2%25A0rude%2520approximation%2520of%2520ambient%2520occlusion%2520by%2520using%2520the%2520a%2520depth%2520of%2520the%2520rendered%2520scene.%2520This%2520w';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Bt%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion%26amp%3Bannotation%3DSSAO%2520is%2520the%2520new%2520technique%2520that%2520most%2520new%2520games%2520just%2520must%2520include%2520because%2520of%2520the%2520hype%2520around%2520it%2520since%2520the%2520computer%2520game%2520Crysis.%2520It%2527s%2520a%2520technique%2520for%25C2%25A0creating%2520a%25C2%25A0rude%2520approximation%2520of%2520ambient%2520occlusion%2520by%2520using%2520the%2520a%2520depth%2520of%2520the%2520rendered%2520scene.%2520This%2520w';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DSSAO%2520is%2520the%2520new%2520technique%2520that%2520most%2520new%2520games%2520just%2520must%2520include%2520because%2520of%2520the%2520hype%2520around%2520it%2520since%2520the%2520computer%2520game%2520Crysis.%2520It%2527s%2520a%2520technique%2520for%25C2%25A0creating%2520a%25C2%25A0rude%2520approximation%2520of%2520ambient%2520occlusion%2520by%2520using%2520the%2520a%2520depth%2520of%2520the%2520rendered%2520scene.%2520This%2520w';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Bt%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DScreen%2520Space%2520Ambient%2520Occlusion%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DScreen%2520Space%2520Ambient%2520Occlusion%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Btitle%3DScreen%2520Space%2520Ambient%2520Occlusion';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F04%252Fscreen-spaced-ambient-occlusion%252F%26amp%3Bt%3DScreen%2520Space%2520Ambient%2520Occlusion%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DSSAO%2520is%2520the%2520new%2520technique%2520that%2520most%2520new%2520games%2520just%2520must%2520include%2520because%2520of%2520the%2520hype%2520around%2520it%2520since%2520the%2520computer%2520game%2520Crysis.%2520It%2527s%2520a%2520technique%2520for%25C2%25A0creating%2520a%25C2%25A0rude%2520approximation%2520of%2520ambient%2520occlusion%2520by%2520using%2520the%2520a%2520depth%2520of%2520the%2520rendered%2520scene.%2520This%2520w';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/11/04/screen-spaced-ambient-occlusion/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
