# SSAO problem in GLSL

This topic is 3085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am trying to implement SSAO. But I get darker geometry in further distance from camera. I think it is because z coordinates in far distances are small, so occlusion is small too. Nowhere I didn't found solution to this problem. Maybe it is possible to multiply total occlusion for pixel according to distance from camera. But this didnt work for me. I am using following pixel shader:

void main()
{

float  depth =  texture2D(positionTexture, gl_TexCoord[0].st).w;
vec3 randNormal = texture2D( noiseTexture, gl_TexCoord[0].st ).xyz;
vec3 eyePos = depth * viewDirection;

float occlusion = 0.0;

vec4 se;
vec4 ss;
vec4 sn;
float zd;

vec3 normal = texture2D(positionTexture, gl_TexCoord[0].st).xyz;

for( int i = 0; i < 16; i++ )
{
vec3 curSample = samples.xyz;
curSample = reflect(curSample, randNormal.xyz);

se = vec4( eyePos + ( radius * curSample ), 1.0 );
sn =  gl_ProjectionMatrix *se;
ss.xy = ( sn.xy * vec2( 0.5, -0.5 ) ) + vec2( 0.5, 0.5 );
float sampleDepth = texture2D(positionTexture, ss.xy).w *farPlane;

float zd =  max(se.z - sampleDepth, 0.0);
occlusion += 1.0 / (1.0 + zd * zd * 0.1);
};

gl_FragData[0] = vec4(occlusion/numSamples);

}


Depth in texture is stored as -position.z /farPlane So how can I fix it?

##### Share on other sites
I dont fully understand your shader from looking at it but maybe you have to adjust the radius of your sampling depending on the depth since it is in screenspace (i think)? just a random thought :)

##### Share on other sites
Maybe this could help,
You're using your eye position added with the offset for the comparison. I think the common thing is to use your depth for that pixel instead.

float zd = max(se.z - sampleDepth, 0.0);

And I think it should be

float zd = max(depth*farPlane - sampleDepth, 0.0);

HTH. Regards,
Juan Pablo

##### Share on other sites
I found that problem is probably in reconstructing 3d position from depth.
First is image displaying only (1-eyePos.z/farPlane) and second is
testDepth, which is value from projection eyePos to screenspace;

original z

reconstructed z

I dont know, where is the problem.
void main(){	vec4 Pos = gl_Vertex;	Pos.xy = sign(Pos.xy);	viewDirection = vec3(cornerFrustrum.x * Pos.x, cornerFrustrum.y * Pos.y, cornerFrustrum.z);	gl_TexCoord[0] = gl_MultiTexCoord0;	gl_Position = ftransform();}

[Edited by - filipz on September 10, 2009 4:59:56 PM]