Reputation: 109
Is that possible to bind one texture to two ( or more ) different uniform samplers in openGL? When rendering with two different textures it goes like this:
Shader:
uniform sampler2D texture1;
uniform sampler2D texture2;
....
Client:
//Initial shader program setup.
glLinkProgram(program);
GLuint texture1Loc = glGetUniformLocation(program, "texture1");
GLuint texture2Loc = glGetUniformLocation(program, "texture2");
glUseProgram(program);
glUniform1i(texture1Loc, 0); //Texture unit 0 is for texture1 sampler.
glUniform1i(texture2Loc, 1); //Texture unit 1 is for texture2 sampler.
//When rendering an object with this program.
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture2);
//Render stuff
glDraw*();
But when I try to bind one texture object to two different texture units, it seems like that the unit that was binded first stays unbinded:
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1); // bind texture1
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1); // bind texture1 - again, but to another unit
Of course it is possible to set same unit for both samplers, but from time to time I also want to use my shader for different textures - not only to set thesame texture object to both samplers.
glUniform1i(texture1Loc, 0); //Texture unit 0 is for texture1 sampler.
glUniform1i(texture2Loc, 0); //Texture unit 0 is for texture2 sampler ( thesame unit ).
This solution actualy works pretty good, but it doesn't fit me needs as described. It is also possible to change texture unit for a sampler just before binding, but it doesn't seem to be a clean solution to me.
glUniform1i(texture1Loc, 0); //Texture unit 0 is for texture1 sampler.
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(texture2Loc, 1); //Texture unit 1 is for texture2 sampler
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture2);
....
glUniform1i(texture1Loc, 0); //Texture unit 0 is for texture1 sampler.
glUniform1i(texture2Loc, 0); //Texture unit 0 is for texture2 sampler.
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
Is there any solution for that? Maybe the very first approach is correct but I do something wrong? Is that possible to bind one texture to many units?
Upvotes: 1
Views: 2482
Reputation: 54572
Binding the same texture to two different texture units, and using both texture units in a shader, should be perfectly fine. There's either a different problem in your code, or a problem in the OpenGL implementation you are using.
The only somewhat related error condition I can find is the following, on page 82 of the OpenGL 3.3 spec, in the sub-section "Validation" under section "2.11 Vertex Shaders":
This error is generated by any command that transfers vertices to the GL if: [..] any two active samplers in the current program object are of different types, but refer to the same texture image unit, [..]
But that's not what you're doing, and I've never seen anything specified that would prevent you from binding the same texture to multiple texture units. If such a restriction existed, I would expect it to be in the same section as the one quoted above, and no such thing is specified there.
Upvotes: 3