简体   繁体   中英

glActiveTexture Doesn't Work

The following:

glActiveTexture(GL_TEXTURE0 + 0);

unsigned i, w;
unsigned tex_test = Memento::_LoadImageIntoTexture("ground.png", i, w);

glBindTexture(GL_TEXTURE_2D, tex_test);
glUniform1ui(program.GetUniformLocation("tex"), 0);

Will run fine, and the image I expect is shown properly. However...

glActiveTexture(GL_TEXTURE0 + 1);

unsigned i, w;
unsigned tex_test = Memento::_LoadImageIntoTexture("ground.png", i, w);

glBindTexture(GL_TEXTURE_2D, tex_test);
glUniform1ui(program.GetUniformLocation("tex"), 1);

When I attempt to change the active texture to anything other than 0, nothing is displayed. This is driving me crazy because I just can't find any solution, and it makes no sense. Help?

From OpenGL specs for glActiveTexture :

A texture unit consists of the texture enable state, texture matrix stack, texture environment and currently bound texture. Modifying any of these states has an effect only on the active texture unit.

You probably forgot to call glEnable(GL_TEXTURE_2D) , and/or glTexEnv(...) if you use non default values.

My guess is that the texture loading function is messing with the active texture unit. Personally I think that a new "proxy" texture unit should be introduced, that can not be used as a smapling source, but can be used for loading image data. That would allow to perform texture loading without messing with the texture units' state.

Anyway, what happens if you change your code to this:

unsigned i, w;
unsigned tex_test = Memento::_LoadImageIntoTexture("ground.png", i, w);

unsigned int texture_unit = 1;
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, tex_test);
glUniform1ui(program.GetUniformLocation("tex"), texture_unit);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM