4

I'm trying to implement deferred shading/lighting. In order to reduce the number/size of the buffers I use I wanted to use the depth texture to reconstruct world position later on.

I do this by multiplying the pixel's coordinates with the inverse of the projection matrix and the inverse of the camera matrix. This sort of works, but the position is a bit off. Here's the absolute difference with a sampled world position texture:

enter image description here

For reference, this is the code I use in the second pass fragment shader:

vec2 screenPosition_texture = vec2((gl_FragCoord.x)/WIDTH, (gl_FragCoord.y)/HEIGHT);
float pixelDepth = texture2D(depth, screenPosition_texture).x;

vec4 worldPosition = pMatInverse*vec4(VertexIn.position, pixelDepth, 1.0);
worldPosition = vec4(worldPosition.xyz/worldPosition.w, 1.0);
//worldPosition /= 1.85;
worldPosition = cMatInverse*worldPosition_byDepth;

If I uncomment worldPosition /= 1.85, the position is reconstructed a lot better (on my geometry/range of depth values). I just got this value by messing around after comparing my output with what it should be (stored in a third texture).

I'm using 0.1 near, 100.0 far and my geometries are up to about 15 away. I know there may be precision errors, but this seems a bit too big of an error too close to the camera. Did I miss anything here?

Guru
  • 21,652
  • 10
  • 63
  • 102
Dr Bearhands
  • 303
  • 2
  • 11
  • possible duplicate of [Calculate clipspace.w from clipspace.xyz and (inv) projection matrix](http://stackoverflow.com/questions/14523588/calculate-clipspace-w-from-clipspace-xyz-and-inv-projection-matrix) – Nicol Bolas Apr 27 '13 at 00:57
  • 1
    Thanks! Turns out I didn't transform my depth after fetching it from the buffer. Never knew about about the depth conversion from ndcspace. pixelDepth=pixelDepth*2.0-1.0; did the trick :) Edit: now how do I mark it as answered? – Dr Bearhands Apr 27 '13 at 13:04
  • You've to answer your own question! – Felix K. Jul 24 '14 at 15:41

1 Answers1

2

As mentioned in a comment:
I didn't convert the depth value from NDC space to clip space.
I should have added this line:

pixelDepth = pixelDepth * 2.0 - 1.0;
Kromster
  • 7,181
  • 7
  • 63
  • 111
Dr Bearhands
  • 303
  • 2
  • 11