PDA

View Full Version : multitexturing and alpha channel



04-13-2006, 07:40 AM
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:

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);


Then I use the following code to render the first:

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_MO DULATE);

and the second:

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_MO DULATE);

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:

//Set up the blending of the 2 textures
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULAT E);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOU S);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_CO LOR);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_TEXTURE );
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_ONE_MI NUS_SRC_ALPHA);

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?

Serge K
04-15-2006, 01:26 AM
Why do you call glEnable(GL_BLEND) in the set-up of each texture unit? Blending has nothing to do with textures, it only controls how the resulting fragment color is combined with frame buffer.

You have to explain better what is that you want to achieve.

Just a guess: you want one of these functions?

C = Cf * ( (1-At1)*Ct0 + At1*Ct1 )
C = Cf * ( Ct0*( (1-At1)*1 + At1*Ct1 ) )

The first one can be done with 2 combiner stages, if GL_ARB_texture_env_crossbar or GL_NV_texture_env_combine4 is available.
For the second function 3 combiner stages or some more flexible extension are required.