【发布时间】:2011-04-24 07:04:44
【问题描述】:
我在 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;
}
我得到 5 个单独的切片,分别来自 image0、image2(!!不是 image1)、image3、image3(再次!)和黑色(这将包含各种纹理的合并,在这种情况下并不重要,我的问题是首先获得正确的纹理)。我多次检查图像加载代码,我确实加载了 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 查找纹理的方式一定有什么非常错误的地方,但我不知道它是什么。
有人能发光吗?
【问题讨论】:
标签: opengl-es glsl textures shader