Convering Cg to GLSL + gl_FragCoord

Started by
4 comments, last by idinev 15 years, 4 months ago
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
Advertisement
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
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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 :)
Quote:Original post by idinev
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 :)


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);
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Thanks for the answers !

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

/ Andreas
V-man, I was showing how gl_FragCoord is computed (what values to expect from it)...

This topic is closed to new replies.

Advertisement