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 4 of 4

Thread: Computing screen coordinates oddities

  1. #1
    Junior Member Regular Contributor
    Join Date
    Dec 2009
    Posts
    105

    Computing screen coordinates oddities

    I would like to have access to the fragment's screen position from inside the fragment shader. I assumed I'd be able to take the clip space X/Y in the vertex shader and divide by the W to get a nice clean X/Y from -1 to 1. But this, err, doesn't seem to work. It's all distorted, clearly clinging to the shapes somewhat. But for some reason it works if I do the W divide in the fragment. I don't fancy wasting a lot of cycles (this is multiple additive passes) doing what could be interpolated if I can help it, any ideas why this works the way it does.

    (Fragment version)
    Code :
       vec4 v4TransformMe = m4Transform * vec4( ( v3VertexIn * fFalloff ) + v3Position, 1.0 );
     
       v3TexCoordOut = vec3( v4TransformMe.xy, v4TransformMe.w );
     
       gl_Position = v4TransformMe;
    Code :
    v3TexCoordOut.xy / v3TexCoordOut.z

    (Vertex interpolated version)
    Code :
       vec4 v4TransformMe = m4Transform * vec4( ( v3VertexIn * fFalloff ) + v3Position, 1.0 );
     
       v3TexCoordOut = v4TransformMe.xy / v4TransformMe.w;
     
       gl_Position = v4TransformMe;

    (the nasty scale and position in the vertex shader is being replaced with a properly built matrix as soon as this is sorted, I swear ;])

    I realise that depth is nonlinear, but I assumed that even so it's done on a vertex level and interpolated via fragment... is this not the case?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,568

    Re: Computing screen coordinates oddities

    Quote Originally Posted by NeXEkho
    I would like to have access to the fragment's screen position from inside the fragment shader. I assumed I'd be able to take the clip space X/Y in the vertex shader and divide by the W to get a nice clean X/Y from -1 to 1. But this, err, doesn't seem to work. It's all distorted, clearly clinging to the shapes somewhat. But for some reason it works if I do the W divide in the fragment.
    Check this out. They all say the same thing in different ways.

    * http://citeseerx.ist.psu.edu/viewdoc...1&type=pdf
    * http://en.wikipedia.org/wiki/Texture...ve_correctness
    * http://www.lysator.liu.se/~mikaelk/d...ectivetexture/
    * http://www.gamedev.net/reference/art...article331.asp

  3. #3
    Junior Member Regular Contributor
    Join Date
    Dec 2009
    Posts
    105

    Re: Computing screen coordinates oddities

    I see, thanks! I do recall that graphical artifact occurring on a number of Playstation games.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2009
    Posts
    612

    Re: Computing screen coordinates oddities

    gl_FragCoord is a built in GLSL read only variable to get the position of the fragment from within a fragment shader, been there since atleast GLSL 1.1, i.e. OpenGL 2.0. Also available in GLSL of OpenGL ES2.

Posting Permissions

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