Jump to content
  • Advertisement
Sign in to follow this  

SSAO problem in GLSL

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

If you intended to correct an error in the post then please contact us.

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

Link to post
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 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!