SSAO shading moves weird with camera (calculating gbuffer wrong)
<p>I'm trying to implement this version of ssao with this tutorial:</p>

<p><a href="http://www.learnopengl.com/#!Advanced-Lighting/SSAO" rel="nofollow noreferrer">http://www.learnopengl.com/#!Advanced-Lighting/SSAO</a> </p>

<p>Here is what I end up with for my render textures.</p>

<p><a href="https://i.stack.imgur.com/FGNq3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FGNq3.png" alt="enter image description here"></a></p>

<p>When I move the camera the shadows seem to follow</p>

<p><a href="https://i.stack.imgur.com/EsnNm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EsnNm.png" alt="enter image description here"></a></p>

<p>Seems like I am missing some kind of matrix multiplication with the camera.</p>

<p>CODE</p>

<p><strong>gBuffer Vertex</strong></p>

<pre><code>#version 330 core
layout (location = 0) in vec3 vertexPosition;
layout (location = 1) in vec3 vertexNormal;

out vec3 position;
out vec3 normal;

uniform mat4 m;
uniform mat4 v;
uniform mat4 p;
uniform mat4 n;

void main()
{
    vec4 viewPos = v * m * vec4(vertexPosition, 1.0f);
    position = viewPos.xyz;
    gl_Position = p * viewPos;
    normal = vec3(n * vec4(vertexNormal, 0.0f));
}
</code></pre>

<p><strong>gBuffer Fragment</strong></p>

<pre><code>#version 330 core
layout (location = 0) out vec4 gPosition;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec4 gColor;

in vec3 position;
in vec3 normal;

const float NEAR = 0.1f;
const float FAR = 50.0f;
float LinearizeDepth(float depth)
{
    float z = depth * 2.0f - 1.0f;
    return (2.0 * NEAR * FAR) / (FAR + NEAR - z * (FAR - NEAR));
}

void main()
{   
    gPosition.xyz = position;
    gPosition.a = LinearizeDepth(gl_FragCoord.z);
    gNormal = normalize(normal);
    gColor.rgb = vec3(1.0f);
}
</code></pre>

<p><strong>SSAO Vertex</strong></p>

<pre><code>#version 330 core

layout (location = 0) in vec3 vertexPosition;
layout (location = 1) in vec2 texCoords;

out vec2 UV;

void main(){
    gl_Position =  vec4(vertexPosition, 1.0f);
    UV = texCoords;
}
</code></pre>

<p><strong>SSAO Fragment</strong></p>

<pre><code>#version 330 core

out float FragColor;
in vec2 UV;

uniform sampler2D gPositionDepth;
uniform sampler2D gNormal;
uniform sampler2D texNoise;
uniform vec3 samples[32];
uniform mat4 projection;

// parameters (you'd probably want to use them as uniforms to more easily tweak the effect)
int kernelSize = 32;
float radius = 1.0;

// tile noise texture over screen based on screen dimensions divided by noise size
const vec2 noiseScale = vec2(1024.0f/4.0f, 1024.0f/4.0f);

void main()
{
    // Get input for SSAO algorithm
    vec3 fragPos = texture(gPositionDepth, UV).xyz;
    vec3 normal = texture(gNormal, UV).rgb;
    vec3 randomVec = texture(texNoise, UV * noiseScale).xyz;
    // Create TBN change-of-basis matrix: from tangent-space to view-space
    vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
    vec3 bitangent = cross(normal, tangent);
    mat3 TBN = mat3(tangent, bitangent, normal);
    // Iterate over the sample kernel and calculate occlusion factor
    float occlusion = 0.0;
    for(int i = 0; i &lt; kernelSize; ++i)
    {
        // get sample position
        vec3 sample = TBN * samples[i]; // From tangent to view-space
        sample = fragPos + sample * radius;

        // project sample position (to sample texture) (to get position on screen/texture)
        vec4 offset = vec4(sample, 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

       // get sample depth
        float sampleDepth = -texture(gPositionDepth, offset.xy).w; // Get depth value of kernel sample

        // range check &amp; accumulate
        float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth ));
        occlusion += (sampleDepth &gt;= sample.z ? 1.0 : 0.0) * rangeCheck;
    }
    occlusion = 1.0 - (occlusion / kernelSize);
    FragColor = occlusion;
}
</code></pre>

<p>I've read around and saw someone had a similar issue and passed the view matrix into the ssao shader and multiplied the sampleDepth:</p>

<pre><code>float sampleDepth = (viewMatrix * -texture(gPositionDepth, offset.xy)).w;
</code></pre>

<p>But seems like it just makes things worse.</p>

<p>Heres another view from up top where you can see the shadows move with the camera</p>

<p><a href="https://i.stack.imgur.com/VEeRK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VEeRK.png" alt="enter image description here"></a></p>

<p>If I position my camera in certain ways things line up</p>

<p><a href="https://i.stack.imgur.com/dNyvM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dNyvM.png" alt="enter image description here"></a></p>