Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Obtaining window coordinates in fragment shader

  1. #1
    Intern Contributor
    Join Date
    Jan 2012
    Posts
    81

    Obtaining window coordinates in fragment shader

    Hello All,

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

    In the vertex shader:

    Code :
    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!

  2. #2
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,162
    No, it is not the right way. Just use the built-in variable gl_FragCoord in a fragment shader.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,129
    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. Again, this is just for completeness. You can just as easily follow Aleksander's advice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •