PDA

View Full Version : Obtaining window coordinates in fragment shader



myk45
12-05-2012, 12:18 AM
Hello All,

i tried obtaining the window coordinates in my fragment shader. So, this is my approach:

In the vertex shader:


varying vec2 my_fragCoord; my_fragCoord = (gl_ModelViewProjectionMatrix * gl_Vertex).xy; // getting clip coordinates.
my_fragCoord /= w; // Do perspective divide


1) And, now in the fragment shader, i scale appropriately by the window size. Is this the right way to do it? Or is there a better way
to achieve the same?


Thanks!

Aleksandar
12-05-2012, 12:50 AM
No, it is not the right way. Just use the built-in variable gl_FragCoord in a fragment shader.

thokra
12-05-2012, 01:41 AM
To elaborate on Aleksandar's suggestion: What you did is not obtain the window coordinates but the normalized device coordinates which are in [-1, 1] for x,y and z. Taking into accunt the current viewport, window coordinates can be computed using the formulae found here (http://www.opengl.org/wiki/GLAPI/glViewport). Again, this is just for completeness. You can just as easily follow Aleksander's advice.