Sign in to follow this  
filipz

SSAO problem in GLSL

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[i].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 this post


Link to post
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.

Your code does:

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 this post


Link to post
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
First Image

reconstructed z
Second Image



I dont know, where is the problem.
Vertex shader for ssao is:

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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this