Page 1 of 2 1 2 >
Topic Options
Rate This Topic
#246614 - 10/01/08 02:06 PM Faking gl_FragCoord?
Leadwerks Offline
Regular Contributor
***

Registered: 03/11/07
Posts: 410
Loc: California
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?


Edited by Leadwerks (10/01/08 02:30 PM)

Top
#246615 - 10/01/08 03:35 PM Re: Faking gl_FragCoord? [Re: Leadwerks]
Lord crc Offline
Regular Contributor
*****

Registered: 04/02/01
Posts: 175
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.

Top
#246620 - 10/01/08 05:11 PM Re: Faking gl_FragCoord? [Re: Lord crc]
Ilian Dinev Offline
Frequent Contributor
****

Registered: 01/26/08
Posts: 822
Loc: Bulgaria
 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 >_>


Edited by Ilian Dinev (10/01/08 05:19 PM)

Top
#246623 - 10/01/08 05:23 PM Re: Faking gl_FragCoord? [Re: Ilian Dinev]
ZbuffeR Offline
OpenGL Guru
*****

Registered: 12/01/03
Posts: 3752
Loc: Grenoble - France
 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/...

Top
#246629 - 10/01/08 07:52 PM Re: Faking gl_FragCoord? [Re: ZbuffeR]
Ilian Dinev Offline
Frequent Contributor
****

Registered: 01/26/08
Posts: 822
Loc: Bulgaria
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()...

Top
#246643 - 10/02/08 04:02 AM Re: Faking gl_FragCoord? [Re: Ilian Dinev]
Leadwerks Offline
Regular Contributor
***

Registered: 03/11/07
Posts: 410
Loc: California
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.


Edited by Leadwerks (10/02/08 04:26 AM)

Top
#246646 - 10/02/08 05:35 AM Re: Faking gl_FragCoord? [Re: Leadwerks]
Pfirsich Offline
Newbie

Registered: 10/02/08
Posts: 5
Loc: Germany
I didn't experience the same issues, but when i got closer to the light-volumes they distorted pretty hard. In the Nvidia-XMas-Tree-Demo the following code was used (at least the same principle), which worked for me too (on my 8600 GT. I don't know if it works on AMD-Cards too).
 Code:
uniform vec2 buffersize;
varying vec2 fake_FragCoord;

void main( ) {
   gl_Position = ftransform( );
   gl_Position /= gl_Position.w;
   fake_FragCoord = (gl_Position.xy * 0.5 + 0.5) * buffersize;
}

In fact it's the same, but only if I don't do the perspective division in the vertex-shader the distortions appear. If I do it myself everything works fine (except when you a in the light-volumes (but I didn't test if these problems occur if I render only the backfaces). I don't know why that works and I would be happy if someone could explain it^^.

EDIT: Forgot the .w and some brackets...
EDIT: I watched the video again.. I did have the same issues.


Edited by Pfirsich (10/02/08 10:24 AM)

Top
#246659 - 10/02/08 11:44 AM Re: Faking gl_FragCoord? [Re: Pfirsich]
Leadwerks Offline
Regular Contributor
***

Registered: 03/11/07
Posts: 410
Loc: California
So the verdict is your code does not work, right?

You should never read from output variables like that, BTW. Once you write to gl_Position, just assume you can never read back that value. You will get some really unpredictable glitches if you read from write-only variables.


Edited by Leadwerks (10/02/08 11:45 AM)

Top
#246661 - 10/02/08 01:10 PM Re: Faking gl_FragCoord? [Re: Leadwerks]
Pfirsich Offline
Newbie

Registered: 10/02/08
Posts: 5
Loc: Germany
 Quote:

It can be written at any time during shader execution. It may also be read back
by a vertex shader after being written.

According to the GLSL 1.20.8-specification. And this code works (at least for me). As long as you do the perspective division in the vertex shader yourself, it works. Otherwise it doesn't work. I don't know why...

 Code:
void main( ) {
	gl_Position = ftransform( );
	gl_Position /= gl_Position.w;
	gl_TexCoord[0] = gl_Position * 0.5 + 0.5;
}

This is the code I use in my deferred-rendering-experiments for acessing the g-buffer-data.


Edited by Pfirsich (10/02/08 01:11 PM)

Top
#246662 - 10/02/08 01:11 PM Re: Faking gl_FragCoord? [Re: Leadwerks]
Ilian Dinev Offline
Frequent Contributor
****

Registered: 01/26/08
Posts: 822
Loc: Bulgaria
uhm, varyings get interpolated in a perspective-correct-way !
So, you must do the perspective division in the frag shader ;). Or target only GF8+ and specify linear interpolation of the varying vector.

Top
Page 1 of 2 1 2 >


Moderator:  Tom Nuydens, Zengar 
Who's Online
6 registered (dukey, Werty, obirsoy, skynet, AGL_Music, mrmoo), 44 Guests and 85 Spiders online.
Key: Admin, Global Mod, Mod
Newest Members
Nonozor, Maire Nicolas, minakshee, Koter, pixelwrangler
24934 Registered Users
Top Posters (30 Days)
Alfonse Reinheart 152
ZbuffeR 92
Dark Photon 73
marshats 47
Brolingstanz 44
Ilian Dinev 41
Iulian B 38
Stephen A 37
Kip Warner 28
devdept 26
skynet 24
Pierre 23
igorgiv 23
DarkShadow44 23
Yann LE PETITCORPS 22
scratt 21
Abdallah DIB 21
Aleksandar 20
Pierre Boudier 19
mikeynovemberoscar 19
Forum Stats
24934 Members
12 Forums
52392 Topics
271545 Posts

Max Online: 482 @ 08/11/08 06:19 PM