Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Problems with SSAO

  1. #1
    Newbie Newbie
    Join Date
    Dec 2016
    Posts
    5

    Problems with SSAO

    Hello

    I am having some weird issues with my SSAO-shader, as you can see on the following pictures, showing an asteroid in a space-environment:
    Click image for larger version. 

Name:	xt7nf8vj.jpg 
Views:	53 
Size:	86.8 KB 
ID:	2375Click image for larger version. 

Name:	9qfavafm.jpg 
Views:	51 
Size:	41.9 KB 
ID:	2376Click image for larger version. 

Name:	mtm6ehl2.jpg 
Views:	44 
Size:	32.2 KB 
ID:	2377
    2nd picture is SSAO only. 3rd picture shows the depthbuffer. It seems like it highlights every edge of the asteroid, which looks totally wrong. On meshes with sharper edges, like the cockpit, it doesn't show this behavior. I have no idea where this comes from, since I can't see any problems with the depthbuffer.

    This is my SSAO-code:
    Code :
    float readDepth(vec2 uv)
    {
      float n = 1.0; // camera z near
      float f = 100.0; // camera z far
      float z = texture2D(uDepthTexture, uv).x;
      return (2.0 * n) / (f + n - z * (f - n));	
    }
     
    float compareDepths( in float depth1, in float depth2 ) {
    	float aoCap = 1.0;
    	float aoMultiplier=10000.0;
    	float depthTolerance=0.000;
    	float aorange = 10.0;// units in space the AO effect extends to (this gets divided by the camera far range
    	float diff = sqrt( clamp(1.0-(depth1-depth2) / (aorange/(camerarange.y-camerarange.x)),0.0,1.0) );
    	float ao = min(aoCap,max(0.0,depth1-depth2-depthTolerance) * aoMultiplier) * diff;
    	return ao;
    }
     
    vec3 addssao() 
    {
    	vec2 texCoord = vUv.xy;
    	vec4 fragcolor=vec4(0.0);
     
    	float depth = readDepth( texCoord );
    	float d;
     
    	float pw = 1.0 / (float)uResolution.x;
    	float ph = 1.0 / (float)uResolution.y;
     
    	float aoCap = 1.0;
     
    	float ao = 0.0;
     
    	float aoMultiplier=10000.0;
     
    	float depthTolerance = 0.001;
     
    	float aoscale=1.0;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	pw*=2.0;
    	ph*=2.0;
    	aoMultiplier/=2.0;
    	aoscale*=1.2;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	pw*=2.0;
    	ph*=2.0;
    	aoMultiplier/=2.0;
    	aoscale*=1.2;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	pw*=2.0;
    	ph*=2.0;
    	aoMultiplier/=2.0;
    	aoscale*=1.2;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    	ao+=compareDepths(depth,d)/aoscale;
     
    	ao/=16.0;
     
    	fragcolor = vec4(1.0-ao);
    	fragcolor.a=1.0;
     
    	return fragcolor;
    }

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    466
    Quote Originally Posted by icebear View Post
    On meshes with sharper edges, like the cockpit, it doesn't show this behavior. I have no idea where this comes from, since I can't see any problems with the depthbuffer.
    if you cant determine if and how your rendered image changes with your SSAO, one solution is to calculate the "image difference" between the "normal" color and the SSAO-corrected color, like this:
    Code :
    layout (location = 0) out vec4 FragmentOutput;
     
    void main() 
    {
        vec4 color = ShadeYourFragmentSomehow();
        vec4 color_ssao = ComputeSSAOColor(color);
     
        vec4 whatinterestsme = abs(color - color_ssao);
     
        FragmentOutput = whatinterestsme;
    }

    then you can see a relatively black screen, only the difference will be visible

  3. #3
    Newbie Newbie
    Join Date
    Dec 2016
    Posts
    5
    I think i got it. In "compareDepth", I changed the "aoMultiplier"-value to 100. That means the SSAO isn't as strong as it could be, but at least those weird effects are gone and it still looks good enough for me. Thanks anway

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •