I'm attempting to load two textures and pass them on to two samplers in my shader, however both the samplers return the first texture I load. Furthermore if I don't load a texture into GL_TEXTURE0, both the samplers return black. I've reduced it to a single sampler/texture and still have the same issue:
GLuint texture1;
glGenTextures(2, &texture1);
glActiveTexture(GL_TEXTURE0); //if this is GL_TEXTURE开发者_JS百科1 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;
Any ideas on what I'm doing wrong? I'm reasonably sure my sampler indices are correct, as is my shader. LoadTextureData boils down to a glTexImage2D.
Edit: This is how I attempt to load and set the two textures
//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);
The result is both samplers return the 'normals' image.
LoadTextureData, assignment of sampler IDs, and my shader can be found at http://paste2.org/p/1514200
The long of the short of it: make sure you assign textures to samplers -after- the shader is loaded. I was tricked into thinking uniforms[] is assigned correctly because I was only checking it after all loading had completed.
GLuint texture1;
glGenTextures(2, &texture1);
You generate two textures, but alloc variable only for one.
Try this:
GLuint textures[2];
glGenTextures(2, textures);
This just happened to me and I spent 2 or 3 hours trying to figure it out. Your answer was right for you but this was what I was doing...
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureInfo[textureIndex].texture);
glUniform1i(glPrograms[currentProgram].glUniforms[U_textureSampler], 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureInfo[textureIndex].texture);
glUniform1i(glPrograms[currentProgram].glUniforms[U_secondTextureSampler], 0);
My entire 3 hours of debugging, testing things, cursing, trying to figure out why BOTH textures were the same thing... was because of the 0 instead of a 1 on the 2nd one.
It should have been:
glUniform1i(glPrograms[currentProgram].glUniforms[U_secondTextureSampler], 1);
It's not just the GL_TEXTURE0 and GL_TEXTURE1 that makes it the 2nd texture. It's that 1 there too.
Hope it saves someone else a couple of hours.
精彩评论