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

Thread: gl_FragCoord.y always 0

  1. #1
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    120

    gl_FragCoord.y always 0

    Hello,

    I wrote a shader that needs to obtain texture coordinates from screen coordinates, but gl_FragCoord's y coordinate is always 0.
    x, z, and w are fine. As a result, I get lines going up and down the screen.

    What could be the cause of this?

    Thank for any help you can offer.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    120
    Restarted my computer and now it works. What is going on here?

    EDIT: Never mind, it came back on the second run of the program.

  3. #3
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,262
    How do you check the value of y, what gpu and drivers are you using, can you reproduce it on another platform?
    (it looks like a driver bug)

  4. #4
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    120
    I checked the value by discarding fragments with a y of 0. The problem went away when I did that. I found that it was not actually always 0, but 0 for fragments where it shouldn't be. It is almost as if it were drawing multiple passes, one normal, one stretched (with all y frag coords 0). However, it is just one quad, and even if I were rendering multiple passes, the frag coords shouldn't change. So discarding the fragments with a y of 0 got rid of the 1 pixel tall stretched image covering up the regular image.

    This does not work:

    Code :
    vec4 diffuseFinal = vec4(texture2D(effectCopy, gl_FragCoord.xy) / vec2(800, 600)).rgb, 1.0) * diffuseColor;

    But this does:

    Code :
    if(gl_FragCoord.y == 0)
            discard;
     
     
    vec4 diffuseFinal = vec4(texture2D(effectCopy, gl_FragCoord.xy) / vec2(800, 600)).rgb, 1.0) * diffuseColor;

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Apr 2009
    Posts
    533
    you know that gl_FragCoord.xy is the _pixel_ coordinate right? Also, what fractional value it gives depends on GL state (see: http://www.opengl.org/sdk/docs/mangl..._FragCoord.xml ). I think you do know that frag is a window coordinate, as you have a /vec2(800, 600) in the code, but the location is borked, I think the code should read:

    Code :
     
    vec4 diffuseFinal = vec4( texture2D(effectCopy, gl_FragCoord.xy/vec2(800, 600)).rgb, 1.0) * diffuse

    as a side note you'd really be better off doing the following for readability:


    Code :
    vec2 tex_coord;
    vec3 color;
     
     
    tex_coord=gl_FragCoord.xy/vec2(800.0, 600.0);
    color = texture2D( effectCopy, tex_coord).rgb;
     
    diffuseFinal = vec4(color.rgb, 1.0);

  6. #6
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    120
    Opps, copied it incorrectly. The way you show it is how I also did it in the shader. There was an offset applied to the texture coordinates originally (but I did test it without the offset as well, so that is not the problem), which I removed for clarity.

    The fractional value is OK, when it gives it to me at all.

Posting Permissions

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