简体   繁体   English

GLSL片段着色器中纹理到对象的纯映射

[英]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. 我有一个与窗口大小相同的纹理(我使用前一个渲染中渲染的场景的一部分,但我在下面的示例中使用了砖纹理),我需要将其映射为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

I have a texture that has same size as window (…) and I need to map it as in 2D. 我有一个与窗口大小相同的纹理(...),我需要将其映射为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? 因此,如果我对您的理解正确,那么您想使用先前生成的纹理,就好像它是某种“图层”一样,其中纹理像素将1:1映射到视口像素,并且纹理具有与视口相同的大小?

If so then this is very easy (as of GLSL version 1.30). 如果是这样,那么这非常容易(从GLSL版本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]). 有一个texelFetch函数,它将一个像素索引作为坐标(与采用[0; 1]范围内的值的texture不同)。 The built in variable gl_FragCoord gives the window coordinates of the currently processed fragment. 内置变量gl_FragCoord给出当前处理的片段的窗口坐标。 So by using the coordinates in gl_FragCoord for texelFetch you get the desired result. 因此,通过将gl_FragCoord的坐标用于texelFetch您可以获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM