Difference between revisions of "Fixed Function Texture Binding"

From OpenGL Wiki
Jump to: navigation, search
m (Bot: Updating section links to use redirects.)
 
Line 6: Line 6:
 
If you take into account an extension like GL_ARB_texture_rectangle, you also have GL_TEXTURE_RECTANGLE_ARB.<br>
 
If you take into account an extension like GL_ARB_texture_rectangle, you also have GL_TEXTURE_RECTANGLE_ARB.<br>
 
<br>
 
<br>
You can bind multiple textures to the same texture image unit :
+
You can bind multiple textures to the same texture image unit:
 +
<source lang="c">
 
   glActiveTexture(GL_TEXTURE0);
 
   glActiveTexture(GL_TEXTURE0);
 
   glBindTexture(GL_TEXTURE_2D, textureIDX);
 
   glBindTexture(GL_TEXTURE_2D, textureIDX);
 
   glBindTexture(GL_TEXTURE_3D, textureIDY);
 
   glBindTexture(GL_TEXTURE_3D, textureIDY);
 +
</source>
  
 
Now, both textures are bound to texture image unit 0. Which one will get sampled?<br>
 
Now, both textures are bound to texture image unit 0. Which one will get sampled?<br>
 
If you are using the fixed pipeline, then it depends on glEnable. If you call
 
If you are using the fixed pipeline, then it depends on glEnable. If you call
 +
<source lang="c">
 
   glEnable(GL_TEXTURE_2D);
 
   glEnable(GL_TEXTURE_2D);
 
   glEnable(GL_TEXTURE_3D);
 
   glEnable(GL_TEXTURE_3D);
 +
</source>
  
 
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.<br>
 
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.<br>
Line 20: Line 24:
 
<b>Thus there is a kind of hierarchy. 1. Cubemap 2. 3D 3. 2D 4. 1D or something like that.</b><br>
 
<b>Thus there is a kind of hierarchy. 1. Cubemap 2. 3D 3. 2D 4. 1D or something like that.</b><br>
 
This is a feature that is best ignored since it serves no purpose. It would have been better to change glBindTexture into
 
This is a feature that is best ignored since it serves no purpose. It would have been better to change glBindTexture into
 +
<source lang="c">
 
   glBindTexture(uint texture);
 
   glBindTexture(uint texture);
 +
</source>
  
 
and to change glEnable/glDisable to
 
and to change glEnable/glDisable to
   glEnable(GLenum target); where target is GL_TEXTURE
+
<source lang="c">
 +
   glEnable(GLenum target); // where target is GL_TEXTURE
 +
</source>
  
 
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.<br>
 
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.<br>

Latest revision as of 18:19, 2 January 2018

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.