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
:sick::sick: