PDA

View Full Version : Converting gl_FragCoord to model space



Zeno
04-28-2009, 05:51 PM
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 :)

Zeno
04-29-2009, 12:40 PM
Oops, nevermind, it turns out I was sending the wrong values for the viewport vector. The code snippet above is correct.

knackered
04-29-2009, 12:48 PM
idiot.
:)

Zeno
04-29-2009, 02:37 PM
Although it's an honor to have Knackered reply to my post, I only accept criticism from people with lower member #'s than me ;)

I was sending my fbo width and height which was bigger than my viewport in this case. Why isn't all OpenGL state (like viewport) available to GLSL programs as built in uniforms? Seems weird that I have to send information to GPU that it already knows.

Hampel
04-30-2009, 02:22 AM
Keep in mind, that there still seems to be problems with gl_FragCoord on ATI systems when used in FBOs... :-(