PDA

View Full Version : Disable Texture Unit in fix function pipeline



doublejay
01-24-2009, 11:45 PM
Hello

I am using OpenGL fix function pipeline. I have been looking for a way to deactivate the texture units correctly. I am not convince with the way I am doing it right now. I do something like this to disable Texture Unit 1 for instance.


glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_1D, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_3D, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, 0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_3D);
glDisable(GL_TEXTURE_CUBE_MAP_EXT);
glDisable(GL_TEXTURE_RECTANGLE_ARB);

Yet it seems that I can set Texture Unit 1 environment so it does not uses a texture:


glActiveTextureARB(GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
// RGB
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_CONSTANT_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
// ALPHA
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);


I see no reason why the texture unit should be disabled if it does not uses a texture. It seems to me there is something missing to make sure that a texture unit is really disabled and that no subsequent texture unit will be used for the fragment.
How do you disable a texture unit so the result produced by the preceding unit becomes the final result?

Thanks
Jay

Jan
01-25-2009, 02:10 AM
glActiveTextureARB (GL_TEXTURE1_ARB);
glDisable (GL_TEXTURE_1D);
glDisable (GL_TEXTURE_2D);
glDisable (GL_TEXTURE_3D);
glDisable (GL_TEXTURE_CUBE_MAP_EXT);
glDisable (GL_TEXTURE_RECTANGLE_ARB);

That's all you need to do.
Jan.

doublejay
01-25-2009, 02:06 PM
No contest! I know this works and I use it all the time. However, I was hoping an explanation or maybe a reference in the opengl spec or extension where it is made clear how to disable a texture unit. Unfortunately, I haven't found it myself.
If my texture unit environment does not use a texture, why is it that glDisable(GL_TEXTURE_1D/2D/3D/Cube/Rectangle) is enough to disable a texture unit that is correctly set?

Jay

arekkusu
01-25-2009, 05:45 PM
It's right there in the spec (2.1, section 3.8.16 Texture Application):

"Texturing is enabled and disabled individually for each texture unit. If texturing
is disabled for one of the units, then the fragment resulting from the previous unit
is passed unaltered to the following unit."

In other words, if there is not a valid (mipmap complete) texture bound and enabled on a unit, the unit is no-oped, regardless of the TexEnv/TexCombine state. That also means that if you only want to use non-texture inputs on a unit (like PRIMARY_COLOR, or CONSTANT) you still need to have a valid (dummy) texture bound.

doublejay
01-25-2009, 09:31 PM
Thank you for the precision. I was intrigued by the fact there wasn't a single function to disable a texture unit. I thought I was missing something. A single line command instead of many calls would have been nice though!

Thanks!
Jay

remdul
01-26-2009, 02:45 AM
I guess I'm spoiled by years of experience with GL, but it seems rather straightforward to me.

glDisable(GL_TEXTURE_2D);

That is all you need if you only use 2d textures. If you need multi-texturing you only have one additional call:

glActiveTexture(GL_TEXTURE0 + n);
glDisable(GL_TEXTURE_2D);

Of course you still need to 'select' your textures with glBindTexture(GL_TEXTURE_2D, texname); in both situations.
Though glDisable(GL_TEXTURE_2D) has now become obsolete if you use shaders.