# Convering Cg to GLSL + gl_FragCoord

## Recommended Posts

Hello I am in the process of converting some Cg code to GLSL, but I am having trouble getting it right. The Cg code i think is the problem looks like this
float2 texc = ((IN.Pos.xy / IN.Pos.w) + 1) / 2; // find the right place to lookup in the backside buffer


where IN.pos is define like
 OUT.Pos =  mul( ModelViewProj, IN.Position );


I have written it like
vec2 texc = ((gl_FragCoord.xy/gl_FragCoord.w)+1.0)/2.0;


I would like to know if that is correct ? Another question i have is what gl_FragCoord is ? From what i understand xy is the fragments location in screenspace and z is the depth. But if i draw a quad over the screen with
 gl_FragColor = vec4(FragCoord.x , FragCoord.y , 0 , 1);


i only get yellow which means that both x y = 1. Should it not be an gradient varying from black to yellow with red and green in the coorners ? Any answer is appreciated! / Andreas

##### Share on other sites
You window might be from 1024 x 700 pixels so any values that are above 1.0 gets clamped to 1.0 if you render to some integer format.
The only way to avoid the clamping is to render to some floating point target.

http://www.opengl.org/wiki/index.php/GL_EXT_framebuffer_object
http://www.opengl.org/wiki/index.php/Floating_point_and_mipmapping_and_filtering

##### Share on other sites
float2 ScreenSize=float2(1280,720);
gl_FragCoord.xy = (((IN.Pos.xy / IN.Pos.w) + 1) / 2) * ScreenSize + 0.5;

And also, (gl_FragCoord.xy - 0.5) is always integer. [0..1279][0..719]
You don't need to do that division by gl_FragCoord.w :)

##### Share on other sites
Quote:
 Original post by idinevfloat2 ScreenSize=float2(1280,720);gl_FragCoord.xy = (((IN.Pos.xy / IN.Pos.w) + 1) / 2) * ScreenSize + 0.5;And also, (gl_FragCoord.xy - 0.5) is always integer. [0..1279][0..719]You don't need to do that division by gl_FragCoord.w :)

You should not write to gl_FragCoord. It is read only.
Furthermore, no need to multiply by screen size because gl_FragCooord is already in window space.

All I'm saying is that since gl_FragCoord.xy contains high values, that they will get clamped.
As a result
gl_FragColor = vec4(FragCoord.x , FragCoord.y , 0.0 , 1.0);
will just show yellow.

PS : you can write that as
gl_FragColor = vec4(FragCoord.xy, 0.0 , 1.0);

##### Share on other sites

The problem was that I thought gl_FragCoord where in the interval [0..1].
So gl_FragCoord / screenSize gave the right result.

/ Andreas

##### Share on other sites
V-man, I was showing how gl_FragCoord is computed (what values to expect from it)...

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982280

• 10
• 9
• 13
• 24
• 11