 # Image to View Space

Hi,
I need to get 3d view positions from a fragment. Each fragment has an associated linear z Depth. Here’s then what I use :
http://www.michimichbeck.de/img/Projektion.png

But it doesn’t work correctly, and I don’t know why… I know the rest of the shader is valid because if I save the xyz view space position for each fragment, I get the good results.
Here’s the shader code I use to compute projection and unprojection :

``````
// get view position of a point
// Z is in [zNear, zFar] (so > 0)
vec3 getViewPosition(vec2 fragment, float depth, float tanFovY, float aspect)
{
vec3 pos;
pos.x = (fragment.x*2.0-1.0)*tanFovY*aspect;
pos.y = (fragment.y*2.0-1.0)*tanFovY;
pos.z = -1.0;
pos = pos*depth;
return pos;
}

// get frag position from view (above's reciproqual)
// view is a gl view pos so z < 0
vec2 getFragPosition(vec3 view, float tanFovY, float aspect)
{
// extract positive linear depth
float z = -view.z;
vec2 frag;
frag.x = 0.5 + (view.x*0.5 / (z*tanFovY*aspect));
frag.y = 0.5 + (view.y*0.5 / (z*tanFovY));
// done !
return frag;
}

``````

Here’s a result comparison :
correct : http://yfrog.com/elcorrectop
incorrect : http://img375.imageshack.us/img375/6935/incorrect.png

This is an ssao output… Has anyone of you encoutered such an artifact ?
Regards,
B

This is getting to be quite an FAQ. I should add this to the wiki.

For the depth component, see this:

For X/Y it should be even simpler.

``````// get view position of a point
// Z is in [zNear, zFar] (so &gt; 0)
``````

This comment is not true. In OpenGL eye-space (what you term “view” space), the eye is at the origin looking down the “negative” Z axis.

While the near/far values you provide to glFrustum are positive, the actual eye-space clip planes are z=-near and z=-far.

Hi, thanks for the link! Unfortunatly I’d like to get rid of matrices and I know it’s possible!