Jump to content
  • Advertisement
Sign in to follow this  
kiaran

Screen Space UV Coordinates

This topic is 3173 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

How does one access a texel in a GLSL fragment shader that corresponds to the screen-space location of the pixel on a texture which is the same dimension as the screen? (sorry if that didn't make any sense) My scenario is this: 1. I render my scene to a FBO texture minus transparent objects. Making myself a 'background' texture. 2. Now I want to render my transparent objects using the FBO texture to do texture warping effects (heat haze, water etc). 3. I render my transparent objects but I need to use a 'dynamic' UV coordinate in my fragment shader to access the correct texel of the background texture. How do I get that UV coordinate? Will glFragCoord do what I want? Was hoping someone might give me a hint before I waste a day fiddling around. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
If I understand your question correctly, are you just asking how to translate the world space coordinate of your transparent object into a screen space coordinate (which you can then divide by width and height to get UV coordinate)?

gluProject sounds like something you might want to look into, you can give it a world-space XYZ coordinate and your screen settings and it will give you the pixel on the screen that that corresponds to.

If you need to compute this in the fragment shader and can't use gluProject, then you have to do the viewport * projection * modelview * coordinate matrix multiplication yourself to perform the transformation.

Share this post


Link to post
Share on other sites
Yes, but gl_FragCoord.xy is in pixels, so to get the correct texel you need either texelFetch or to divide the xy coord by the texture size. What GLSL version are you using?

Example:
outColor = texture(tex0, gl_FragCoord.xy/textureSize(tex0, 0))
outColor = texelFetch(tex0, ivec2(gl_FragCoord.xy), 0)

Share this post


Link to post
Share on other sites
Unless absolutely necessary, I would like to use strictly GLSL 1.2 so that customers with OpenGL 2.1 support or greater can be assured compatibility.

karwosts - I would like to keep the computation in the shader for simplicity. Thank you for your insights.

Erik Rufelt - Your usage of glFragCoord looks like exactly what I need. I didn't even know about texelFetch function. Looks like it was introduced in GLSL 1.3 and certainly simplifies things a bit since I don't need to pass the texture dimensions to the shader. But this is a small price to pay to keep backwards compatibility.

I'll post later today when I get a chance to try this out!

Share this post


Link to post
Share on other sites
Just thought I should write back to confirm that glFragCoord does exactly what I needed and that I got it all working.

Thanks for the help guys.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!