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 10 of 19

Thread: Faking gl_FragCoord?

Hybrid View

  1. #1
    Member Regular Contributor
    Join Date
    Mar 2007
    Location
    California
    Posts
    396

    Faking gl_FragCoord?

    Since gl_FragCoord does not work properly on AMD hardware I need to fake it. I am having a little trouble visualizing this.

    Let's say texcoord is the vec2 I want to write out for gl_FragCoord. The upper-left corner of the screen would be (0,0,z), and the bottom-right corner would be (1,1,z).

    Here is my light volume vertex shader:
    Code :
    varying vec3 texcoord;
     
    void main(void) {
    	gl_Position = ftransform();
    	gl_FrontColor = gl_Color;
    	texcoord=ftransform()
    }

    Now how do I turn the screen position into the gl_FragCoord value?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Apr 2001
    Posts
    180

    Re: Faking gl_FragCoord?

    I'd check out the "Shader Inputs" heading in section 3.11.2, and section 2.11 for the actual transformations, in the 2.1 spec. Seems fairly straight forward, but a bit tedious to repeat here.

  3. #3
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,270

    Re: Faking gl_FragCoord?

    Code :
    varying vec4 texcoord; // notice it's vec4, fix the vtxshader
    void main(){
         vec2 tmp_scr1=vec2(1280.0/2.0,720.0/2.0);
         vec2 tmp_scr2=vec2(1280.0/2.0+0.5,720.0/2.0+0.5);
         vec2 fake_FragCoord = (texcoord.xy/texcoord.w)*tmp_scr1+tmp_scr2;  
    }

    btw gah this board's "cannot resolve host, as you're behind a firewall. Your whole post is gone now haha". A dozen times already >_>

  4. #4
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,580

    Re: Faking gl_FragCoord?

    Quote Originally Posted by Ilian Dinev
    btw gah this board's "cannot resolve host, as you're behind a firewall. Your whole post is gone now haha". A dozen times already >_>
    I had this, but because I used http://opengl.org/... instead of the more correct http://www.opengl.org/...

  5. #5
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,270

    Re: Faking gl_FragCoord?

    Nah, I access via http://www.opengl.org/... , but forget to turn-off the Sunbelt/Kerios firewall before posting here. At least if some javascript or whatever didn't force-delete text on history.back()...

  6. #6
    Member Regular Contributor
    Join Date
    Mar 2007
    Location
    California
    Posts
    396

    Re: Faking gl_FragCoord?

    I tried this for the vertex shader, and it is very close to being correct...not sure why it isn't completely correct though. There are some lighting offsets and glitches. It's hard to explain. This is the same as your code, just a little simpler.

    It looks like the interpolation between the vertices is making the image "wavy". Here's a video:
    http://www.leadwerks.com/post/fragcoord.wmv

    Code :
    uniform vec2 buffersize;
     
    varying vec2 fake_FragCoord;
     
    void main(void) {
    	gl_FrontColor = gl_Color;
    	vec4 pos = ftransform();
    	fake_FragCoord = ((pos.xy/pos.w)*0.5+0.5)*buffersize;
    	gl_Position=pos;
    }

    Actually, I think this code is probably completely correct, but it just won't work as a varying without distorting the values.

Posting Permissions

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