我试图加载两个纹理并将它们传递到我的着色器中的两个采样器,但是两个采样器都返回我加载的第一个纹理。此外,如果我不将纹理加载到 GL_TEXTURE0 中,两个采样器都会返回黑色。我已将其缩减为单个采样器/纹理,但仍然存在相同的问题:
GLuint texture1;
glGenTextures(2, &texture1);
glActiveTexture(GL_TEXTURE0); //if this is GL_TEXTURE1 or any other, I get black
glBindTexture(GL_TEXTURE_2D, texture1);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[self LoadTextureData: [[NSBundle mainBundle] pathForResource:@"normals" ofType:@"png"]];
glUniform1i(uniforms[SAMPLER_1], 1); //whether this is 0 or 1 or anything else doesn't seem to make any difference for both samplers.
return TRUE;
关于我做错了什么的任何想法? 我有理由相信我的采样器索引是正确的,我的着色器也是如此。 LoadTextureData 归结为 glTexImage2D。
编辑: 这就是我尝试加载和设置两个纹理的方式
//texture 1
GLuint texture1;
glGenTextures(1, &texture1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[self LoadTextureData: [[NSBundle mainBundle] pathForResource:@"normals" ofType:@"png"]];
//texture 2
GLuint texture2;
glGenTextures(1, &texture2);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[self LoadTextureData: [[NSBundle mainBundle] pathForResource:@"bricks" ofType:@"jpg"]];
glUniform1i(uniforms[SAMPLER_1], 0);
glUniform1i(uniforms[SAMPLER_2], 1);
结果是两个采样器都返回“法线”图像。
LoadTextureData、采样器 ID 的分配和我的着色器可以在 http://paste2.org/p/1514200 找到
最佳答案
总而言之:确保在加载着色器后将纹理分配给采样器。我被骗了,以为 uniforms[] 被正确分配了,因为我只是在所有加载完成后才检查它。
关于ios - 无法在 opengl es 2.0 中加载多个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6650148/