<p>I was looking to implement SSAO; demonstrated on learnopeng (<a href="https://learnopengl.com/Advanced-Lighting/SSAO" rel="nofollow noreferrer">https://learnopengl.com/Advanced-Lighting/SSAO</a>). While I have implemented the way they have done it i.e using a position texture for positions and normal texture... I was looking to re-implement it using the depth buffer for positions &amp; normals.</p>
<p>I have obtained my positions from the depth buffer and stored them in a vec3 as shown below:</p>
<pre><code>vec3 computeViewPosFromDepth(vec2 texcoords) {

   float depth = texture(depthMap, vec2(1.0 - uvCoords.x, uvCoords.y)).x;

   vec4 clipSpace = vec4(vec2(1.0 - uvCoords.x, uvCoords.y) * 2.0 - 1.0, depth, 1.0);
   vec4 viewSpace = inverse(camera.proj) * clipSpace;
   return viewSpace.xyz /= viewSpace.w;
}

// contains my positions
vec3 positions = computeViewPosFromDepth(vec2(1.0 - uvCoords.x, uvCoords.y));
</code></pre>
<p>but when it comes to sampling the depth value of my sample position I can no longer do the following:</p>
<pre><code>float sampleDepth = texture(gPosition, offset.xy).z; 
</code></pre>
<p>i.e not use texture()</p>
<p>I am wondering how else I could go about obtainig the depth value of my sample position, given that my positions are from the depth buffer</p>

CC BY-SA 4.0
2021-10-28T22:22:20.510Z
Why can you not use `texture` to get the depth value of your sample position?
Nathan Reed
CC BY-SA 4.0
2021-10-29T11:19:53.140Z
Did not seem to work out for me. I tried doing texture(depthMap, offset.xy).r in order to get the depth value of the sample position but I'm not getting any AO in the output by doing this so I assume this is the incorrect approach?
user17440
<glsl><z-buffer><ssao>
Obtaining sample position z value when using positions from depth buffer for SSAO
user17440
# Obtaining sample position z value when using positions from depth buffer for SSAO

(Asked by: username_0 on 2021-10-28)

I was looking to implement SSAO; demonstrated on learnopeng (<https://learnopengl.com/Advanced-Lighting/SSAO>). While I have implemented the way they have done it i.e using a position texture for positions and normal texture... I was looking to re-implement it using the depth buffer for positions & normals.

I have obtained my positions from the depth buffer and stored them in a vec3 as shown below:

```
vec3 computeViewPosFromDepth(vec2 texcoords) {

   float depth = texture(depthMap, vec2(1.0 - uvCoords.x, uvCoords.y)).x;

   vec4 clipSpace = vec4(vec2(1.0 - uvCoords.x, uvCoords.y) * 2.0 - 1.0, depth, 1.0);
   vec4 viewSpace = inverse(camera.proj) * clipSpace;
   return viewSpace.xyz /= viewSpace.w;
}

// contains my positions
vec3 positions = computeViewPosFromDepth(vec2(1.0 - uvCoords.x, uvCoords.y));

```

but when it comes to sampling the depth value of my sample position I can no longer do the following:

```
float sampleDepth = texture(gPosition, offset.xy).z; 

```

i.e not use texture()

I am wondering how else I could go about obtainig the depth value of my sample position, given that my positions are from the depth buffer

**Tags:** glsl, z-buffer, ssao

> **Comments:**
> **username_1**: Why can you not use `texture` to get the depth value of your sample position?  
> **username_0**: Did not seem to work out for me. I tried doing texture(depthMap, offset.xy).r in order to get the depth value of the sample position but I'm not getting any AO in the output by doing this so I assume this is the incorrect approach?  

---

## 0 answers

