I need to be able to convert gl_FragCoord.xy plus a depth value from a previous pass back into model space (or eye space would be fine too). I want to compare the coordinates to my incoming gl_Vertex.
The depth value is stored in a GL_DEPTH_COMPONENT rectangular texture. I’m doing the math like gluUnproject but it isn’t working.
Here’s the relevant snippet from the fragment program, can you tell what’s wrong? view is a uniform variable that contains the viewport values like this (0,0,width,height).
vec4 v = vec4(2.0*(gl_FragCoord.x-view.x)/view.z-1.0,
2.0*(gl_FragCoord.y-view.y)/view.w-1.0,
2.0*texture2DRect(DepthTex,gl_FragCoord.xy).z-1.0,
1.0 );
v = gl_ModelViewProjectionMatrixInverse * v;
v /= v.w;
I have also tried replacing the texture lookup with gl_FragCoord.z. That should give me my original interopolated vertex position back but it doesn’t. Any help is appreciated