Buuhh!! Help me please!
I just need to apply two texture onto a polygon: the both textures have to modulate the color of the lit polygon, but the second texture has an alpha channel in addition. The alpha channel decides what pixel should be discarded, so the first texture is only partially covered by the second. Quite common effect, isn’t it? But I cannot get it work!
I load the image using SDL_Image and (I suppose the alpha is correctly loaded) I create the texture:
[source]
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m->w, m->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, m->pixels);
[/source]
Then I use the following code to render the first:
[source]
glClientActiveTextureARB(GL_TEXTURE0_ARB);
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbouv);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
[/source]
and the second:
[source]
glClientActiveTextureARB(GL_TEXTURE1_ARB);
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture2);
glEnable(GL_BLEND);
glBlendFunc (GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbouv);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
[/source]
I tried every parameter I could think of, but the result is always the same: transparent pixels are black (instead than showing the first texture) or everythink is black.
Then I tried with combiners:
[source]
//Set up the blending of the 2 textures
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_ONE_MINUS_SRC_ALPHA);
[/source]
With the same results. I know that this code wont generate the effect I need, but I would expect at least that the alpha channel is recognized and used while texturing. I enabled GL_BLEND and disabled it, I tried all the blending functions, I moved the instructions all around the code, I spent 3 days searching over the internet. How to activate alpha blending among the texture stages?