I’m having trouble implementing multitexturing in OpenGL 3.1. I’m sure this is at least partly because I’ve never worked with samplers before (I learned how to use textures from this tutorial, which explained mipmapping well but skipped over sampler objects).
My current setup is this: I have two samplers, l_sampler1 and l_sampler2, global to the main program. Not the best, but I’m just trying to figure out how they work before I package them up in classes. In the initialization code, I have:
glGenSamplers(1, &l_sampler1); glGenSamplers(1, &l_sampler2); glBindSampler(unsigned int(0), l_sampler1); // Bind sampler 1 to texture unit 0? glBindSampler(unsigned int(1), l_sampler2); // Bind sampler 1 to texture unit 1? glSamplerParameteri(l_sampler1, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glSamplerParameteri(l_sampler1, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glSamplerParameteri(l_sampler2, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glSamplerParameteri(l_sampler2, GL_TEXTURE_MIN_FILTER, GL_NEAREST); ... // later on in the initialization: MirrorShader.updateUniform("renderedTexture", unsigned int(0)); // Upload texture unit 0 to the fragment shader? MirrorShader.updateUniform("mirrorTexture", unsigned int(1)); // Upload texture unit 1 to the fragment shader?
And in the render loop I have:
glActiveTexture(GL_TEXTURE0); // Generate, bind, and glTexImage texture1 glActiveTexture(GL_TEXTURE1); // Generate, bind, and glTexImage texture2 glActiveTexture(GL_TEXTURE0); // I dunno why, for luck maybe? :) // bind texture1, and pray that it somehow makes the whole thing work // render stuff
What happens is that only the last bound texture gets used, no matter which sampler I access in the fragment shader. I’m not sure if the same texture is being uploaded to both units or both samplers are linked to the first unit or what.
I think I’m getting the arguments right, though. Initially I thought it was glBindSampler(GL_TEXTURE0, samplerId) rather than glBindSampler(0, samplerId), but changing it didn’t solve the problem.
Where am I going wrong?
As a side note, that Shader.updateUniform function is an overloaded one I made that accepts, among other things, either a float or an unsigned int as an argument. When I tried the above code with MirrorShader.updateUniform(“renderedTexture”, GL_TEXTURE0) the compiler (MSVC2010) complained about an ambiguous call to the overloaded function, claiming it couldn’t determine whether I wanted to pass a float or a uint. Which is GL_TEXTURE0? I thought it was a uint.