繁体   English   中英

使用不同texCoord(iPad)的GLSL Shader多纹理查找

[英]GLSL Shader multi-texture lookup with different texCoord (iPad)

我在GLSL纹理地狱中:我在片段着色器的统一sampler2D变量中加载了4个大小相同的不同纹理,并尝试使用不同的纹理坐标来访问它们:

uniform sampler2D image0, image1, image2, image3;
varying highp vec2 texCoord;

void main()
{   
highp vec2 tc = vec2(texCoord.x, mod(1.0-texCoord.y, 0.2) + 0.2);

lowp vec4 color0 = texture2D(image0, tc);
lowp vec4 color1 = texture2D(image1, tc);
lowp vec4 color2 = texture2D(image2, tc);
lowp vec4 color3 = texture2D(image3, tc);

if (texCoord.y < 0.2) { gl_FragColor = color0; }
else if (texCoord.y < 0.4) { gl_FragColor = color1; }
else if (texCoord.y < 0.6) { gl_FragColor = color2; }
else if (texCoord.y < 0.8) { gl_FragColor = color3; }
else { gl_FragColor = vec4(0.0); }
}

texCoord当然来自顶点着色器:

uniform lowp float ratio;
attribute highp vec4 vertex;
varying highp vec2 texCoord;

void main()
{
gl_Position = vertex;
texCoord.x = ((vertex.x * 0.5) + 0.5) * ratio;
texCoord.y = (vertex.y * 0.5) + 0.5;
}

我从image0,image2(!!不是image1),image3,image3(再次!)和黑色中得到5个单独的切片(这将合并各种纹理,在这种情况下不重要,我的问题是正确的首先贴图)。 我多次检查图像加载代码,确实加载了4张不同的图像:

- (void)linkTexture:(GLenum)tex image:(Image *)image varName:(const char *)varName
{
GLint texLocation;

texLocation = glGetUniformLocation(program, varName);
glUniform1i(texLocation, tex-GL_TEXTURE0);
glActiveTexture(tex);
glBindTexture(GL_TEXTURE_2D, image->texID);
}

再往下走:

    loadTexture("new_york_0.jpg", &image0, &renderer);
    [self linkTexture:GL_TEXTURE0 image:&image0 varName:"image0"];

    loadTexture("new_york_1.jpg", &image1, &renderer);
    [self linkTexture:GL_TEXTURE1 image:&image1 varName:"image1"];

    loadTexture("new_york_2.jpg", &image2, &renderer);
    [self linkTexture:GL_TEXTURE2 image:&image2 varName:"image2"];

    loadTexture("new_york_3.jpg", &image3, &renderer);
    [self linkTexture:GL_TEXTURE3 image:&image3 varName:"image3"];

我期望GPU查找纹理的方式一定存在某些错误,但是我不知道它是什么。

有人可以发光吗?

好吧,在第一种情况下,您当然会从相同的纹理中得到5个切片。

想想你在做什么。

取0.6的ay坐标。

(1 - 0.6)       = 0.4
mod( 0.4, 0.2 ) = 0.0
0.0 + 0.2       = 0.2

基本上,您将y坐标强制在0.2到0.4的范围内,它将始终为image1。 我不确定为什么您会看到image2 ...

至于第二种情况……这严重暗示您正在接收的纹理坐标是错误的。 那么..您想向我们展示顶点着色器以及此片段着色器吗?

不过,我不得不承认,我不明白为什么您不只是将这5个切片放到一个纹理中,而只是将其渲染...

您可能要检查一下:

http://www.vis.uni-stuttgart.de/glsldevil/index.html#downloads

glUniform1i修改当前程序的统一值。 确保在调用linkTexture之前先调用glUseProgram(myProgram)。

暂无
暂无

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

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