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!
Here are some links I read :
http://www.michimichbeck.de/wordpress/?p=229
http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/ (check out the second technique!)

The comment I’m using is a prerequisit because in the function I negate the z value so I do get a negative Z in view space.
Any thoughts ?

In the link given to you, getting rid of matrices is exactly what is done. The derivation of the formula does use matrices, but once the formula is calculated, no matrices are involved anymore.

Ah my bad I was making a mulitplication mistake in my shader !
It’s working now!
Thanks for your help anyway ^^