View Full Version : Screen space ambient occlusion

04-27-2012, 11:02 PM
I am reading someone's code about SSAO, but there are some places I cannot understand.

Their code for SSAO is extremely simple:

uniform vec4 fk3f[32];
uniform vec4 fres;
uniform sampler2D tex0; // the depth map
uniform sampler2D tex1; // a small random normal map

void main(void)
vec4 zbu = texture2D( tex0, gl_Color.xy ); //gl_Color the 2d pixel coordinates

vec3 ep = zbu.x*gl_TexCoord[0].xyz/gl_TexCoord[0].z;//gl_TexCoord[0] contains the eye view vector

vec4 pl = texture2D( tex1, gl_Color.xy*fres.xy );
pl = pl*2.0 - vec4(1.0);

float bl = 0.0;
for( int i=0; i<32; i++ )
vec3 se = ep + rad*reflect(fk3f[i].xyz,pl.xyz);

vec2 ss = (se.xy/se.z)*vec2(.75,1.0);
vec2 sn = ss*.5 + vec2(.5);
vec4 sz = texture2D(tex0,sn);

float zd = 50.0*max( se.z-sz.x, 0.0 );
bl += 1.0/(1.0+zd*zd);
gl_FragColor = vec4(bl/32.0);

The author says the following code is for transforming points from eye space to clip space:

vec2 ss = (se.xy/se.z)*vec2(.75,1.0);

What's the mathematic behind this? The coefficient 0.75 looks strange to me.

In addition, the author uses a per pixel random plane to do a reflection on the sampling point around the shading point. But how does the random plane work? the sampling points are already random, is it really necessary to reflect them to be more random?

The original article containing the code can be found at

04-28-2012, 12:28 AM
I guess the 0.75 on x is to adapt a power of two texture to a 16/10 screen ratio. Mmh no scratch that, it would be 0.625 instead, so I have no clue.

The random plane is needed, as the sampling points are absolutely not random but aligned with the screen pixels.

04-28-2012, 01:00 AM
I guess the 0.75 on x is to adapt a power of two texture to a 16/10 screen ratio..
But maybe a 4/3 ratio...

04-28-2012, 01:22 AM
Do you know what does the uniform fres do in this line:

vec4 pl = texture2D( tex1, gl_Color.xy*fres.xy );