Search code examples
openglglsltexturestexture-mappingfragment-shader

Plain mapping of texture to object in GLSL fragment shader


I have a problem with mapping of texture in fragment shader. I have a texture that has same size as window (I use part of scene rendered in previous pass, but I use brick texture in the example below) and I need to map it as in 2D. Is it somehow possible ? I tried different ways, but nothing worked.

Could you please advice me how should the fragment shader looks like ? Please any advice will be helpfull. Thank you!

tepaot with plain mapping


Solution

  • I have a texture that has same size as window (…) and I need to map it as in 2D.

    So if I understand you correctly, then you want to use the previously generated texture as if it was some kind of "layer", where texture pixels map 1:1 to viewport pixels, and the texture has the very same size like the viewport?

    If so then this is very easy (as of GLSL version 1.30). There is the function texelFetch, which takes a pixel index as coordinate (unlike texture which takes a value in the range [0; 1]). The built in variable gl_FragCoord gives the window coordinates of the currently processed fragment. So by using the coordinates in gl_FragCoord for texelFetch you get the desired result.