Jump to content
  • Advertisement
Sign in to follow this  
KarimIO

OpenGL Screen Space Ambient Occlusion Issues

This topic is 478 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

I'm having problems with Ambient Occlusion. I've tried to follow both John Chapman's improvements over the Crytek AO and LearnOpenGL's tutorial of it when I failed to properly obtain the correct result. http://i.imgur.com/FbNsq9X.png?1
 
As the camera turns or moves, the screen flashes consistently though it seems arbitrary. Can anyone tell me what I'm doing wrong?
 
    #version 140
    
    in vec2 UV;
    in vec4 vRay;
    out vec3 SSAOout;
    
    const int kernelSize = 64;
    
    uniform mat4 projection;
    uniform sampler2D gbuffer0;
    uniform sampler2D gbuffer1;
    uniform sampler2D gbuffer2;
    uniform sampler2D gbuffer3;
    uniform sampler2D noiseTex;
    uniform vec3 kernels[kernelSize];
    
    vec4 ViewPosFromDepth(float depth, vec2 TexCoord) {
        float z = depth * 2.0 - 1.0;
    
        vec4 clipSpacePosition = vec4(TexCoord * 2.0 - 1.0, z, 1.0);
        vec4 viewSpacePosition = invProjMat * clipSpacePosition;
        viewSpacePosition /= viewSpacePosition.w;
    
        return viewSpacePosition;
    }
    
    const vec2 noiseScale = vec2(1366.0/4.0, 768.0/4.0);
    
    const float near = 0.1;
    const float far = 100.0;
    
    float LinearizeDepth(float depth)
    {
        float z = depth * 2.0 - 1.0; // back to NDC 
        return (2.0 * near * far) / (far + near - z * (far - near));    
    }
    
    void main() {
        vec4 gbuffer0Val = texture(gbuffer0, UV);
        vec4 gbuffer1Val = texture(gbuffer1, UV);
        vec4 gbuffer2Val = texture(gbuffer2, UV);
        vec4 gbuffer3Val = texture(gbuffer3, UV);
    
        vec3 fragPos    = ViewPosFromDepth(gbuffer0Val.r, UV).rgb;
        float dist      = LinearizeDepth(gbuffer0Val.r);
        vec3 normal     = gbuffer1Val.rgb;
        normal          = decodeNormal(normal);
        vec3 randomVec  = vec3(2 * texture(noiseTex, UV * noiseScale).rg - 1, 0); 
    
        vec3 tangent   = normalize(randomVec - normal * dot(randomVec, normal));
        vec3 bitangent = cross(normal, tangent);
        mat3 TBN       = mat3(tangent, bitangent, normal);
    
        float bias = 0.025;
        float radius = 3.0;
        float occlusion = 0.0;
    
        for(int i = 0; i < kernelSize; i++) {
            vec3 sampleKernel = TBN * kernels;
            sampleKernel = fragPos + sampleKernel * radius;
    
            vec4 offset = vec4(sampleKernel, 1.0);
            offset      = projection * offset;    // from view to clip-space
            offset.xyz /= offset.w;               // perspective divide
            offset.xyz  = offset.xyz * 0.5 + 0.5; // transform to range 0.0 - 1.0
    
            float sampleDepth = texture(gbuffer0, 1-offset.xy).r;
            sampleDepth = LinearizeDepth(sampleDepth);
            float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth));
            occlusion += (sampleDepth >= sampleKernel.z + bias ? 1.0 : 0.0) * rangeCheck;
        }
    
        occlusion = 1.0 - (occlusion / kernelSize);
        SSAOout = vec3(occlusion);
    }

Share this post


Link to post
Share on other sites
Advertisement

Going to have to debug more than posting a shader. Output the linear world positions and read them back as floats. Do they look correct?

float dist      = LinearizeDepth(gbuffer0Val.r);

Are you even using this variable to test against the other samples?

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!