Fixed Function Texture Binding

From OpenGL.org
Jump to: navigation, search

This article explains how OpenGL deals with glBindTexture and glEnable and glDisable.

There are a few texture targets : GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP.
If you take into account an extension like GL_ARB_texture_rectangle, you also have GL_TEXTURE_RECTANGLE_ARB.

You can bind multiple textures to the same texture image unit :

 glActiveTexture(GL_TEXTURE0);
 glBindTexture(GL_TEXTURE_2D, textureIDX);
 glBindTexture(GL_TEXTURE_3D, textureIDY);

Now, both textures are bound to texture image unit 0. Which one will get sampled?
If you are using the fixed pipeline, then it depends on glEnable. If you call

 glEnable(GL_TEXTURE_2D);
 glEnable(GL_TEXTURE_3D);

then GL tells the GPU to bind the 3D texture because the 3D texture is considered a higher priority. There is no particular reason for this. That's simply how the spec is made. Of course, if you glDisable(GL_TEXTURE_3D), then GL instructs the GPU to bind the 2D texture.

Thus there is a kind of hierarchy. 1. Cubemap 2. 3D 3. 2D 4. 1D or something like that.
This is a feature that is best ignored since it serves no purpose. It would have been better to change glBindTexture into

 glBindTexture(uint texture);

and to change glEnable/glDisable to

 glEnable(GLenum target); where target is GL_TEXTURE

As for shaders, glEnable(GL_TEXTURE_2D), glEnable(GL_TEXTURE_3D) and so on have no effect yet you can still bind different texture types to the same texture image unit.
This is where the spec clearly states that you can only sample 1 of those textures.
That is because the OpenGL driver will check to see what you are trying to sample. Is it a uniform sampler2D?
Is it a uniform sampler3D?
The GL driver will tell the GPU which texture to bind.