Jump to content
  • Advertisement
Sign in to follow this  
Gobbla

Convering Cg to GLSL + gl_FragCoord

This topic is 3478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!