View Full Version : Convert Eye Coords to Window Coords in GLSL?

03-28-2011, 05:10 PM
Hey guys, I don't know a lot about the projection and coordinate systems in OpenGL, but I'm trying to implement a light scattering (god rays) shader via mod that allows GLSL into a game. The mod can tell me a uniform float called sunVector which is the position of the sun (area from where the scattering will come from) in eye coordinates. What the shader I'm implementing needs are window coordinates.
So my question is, how can I convert them? I know I need to modify it by the projection matrix or something, but how?
I'm a real noob at this right now; this isn't my type of programming . . . thanks!

03-29-2011, 02:12 AM
It should be simple first multiply the value by the projection matrix to get it into clipspace.Do the perspective division. Then scale and translate the value by half screen screen to obtain the screenspace value. So its something like this,

//esPos is eye space position
//P is the projection matrix
csPos = P*esPos;
csPos.xyz/=csPos.w; //prespective division
winX = (1+csPos.x)* half_screen_width;
winY = (1+csPos.y)* half_screen_height;
winZ = csPos.z; //clipspace z is in 0-1 range with 0 at the near
//clip plane and 1 at the far clip plane.

03-29-2011, 09:52 PM
Ah, so which one of those would be floats and which matrices?

03-30-2011, 11:09 AM
only P is the projection matrix the rest are vec3.

03-30-2011, 03:01 PM
Hopefully this helps, i had the same question awhile ago and was working with transforming window coords to clip space and clip space to window coords. (Clip-space is where you are after your perspective and transformation vertex, or in my case quaternion, calculations are)

http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=293687#Post2936 87

The bottom two posts go over the math from window coords to clip space, and clip space to window coords. I verified it for accuracy with multiple tests so it should be good.

Hope this helps. Once you get clip space, just reversing your projection and vertex transformations are easy, and brings you right back to your original 3D world coords.