PDA

View Full Version : alpha blending



MrRolf
09-18-2002, 02:19 PM
Hello all.

I am trying to blend two textures. One is a full RGB texture, with all 1.0's for the Alpha, and the second texture is no RGB (set to 0.0's), and an alpha pattern. I want to blend them so the final image is the RGB of the first texture, and the aplpha of the second texture.
tex1=(r,g,b,1) tex2=(0,0,0,a) final=(r,g,b,a)
I'm using the multitexture hardware extensions.
Here's how I load the textures:

glBindTexture(GL_TEXTURE_2D, *tex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _CLAMP);
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8,cinfo.out put_width,cinfo.output_height,GL_RGB,GL_UNSIGNED_B YTE,ourImageArray);
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *tex);

//and load the alpha image
glBindTexture(GL_TEXTURE_2D, *g); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,TextureIma ge[0]->sizeX,TextureImage[0]->sizeY,GL_RGBA,GL_UNSIGNED_BYTE,beta); glActiveTextureARB(GL_TEXTURE2_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *g);


Here's the what I do to draw them:

glEnable(GL_BLEND);
glAlphaFunc(GL_GREATER, 0.01f);
glEnable(GL_ALPHA_TEST);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,*tex);

glActiveTextureARB(GL_TEXTURE2_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *g);
....
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,...);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB,...);
glVertex3f(...);

The code above draws the texture with full transparencey where alpha < 0.01f. But I need to blend the alpha of the second texture with the first texture, so I get a smooth blend. Right now there is no blending.
I've been reading about blending, and I believe that I need a glBendFunc(..) call in there. I do not know where to put it, or what parameters to call.

Any ideas would be appreciated.
Thanks.

Bob
09-18-2002, 10:04 PM
Firs of all, you don't start counting texture units with TEXTURE1, you begin with TEXTURE0. So for two texture units, you should use TEXTRURE0 and TEXTURE1.


tex1=(r,g,b,1) tex2=(0,0,0,a) final=(r,g,b,a)

What kind of texture is tex2? Is it a single channel texture, uploaded as alpha only? Or is it a four channel image, with RGB explicitly set to 0? If it's a pure alpha texture, RGB will have the value 1, not 0. This means to get the desired result, you can just modulate the two textures.

MrRolf
09-19-2002, 02:56 AM
tex2 2 is a four channel image with RGB set to 0. I'll try to make it a single channel and modulate the textures then.
Thanks for the advice.

MrRolf
09-19-2002, 03:47 AM
Ahh.. I figured it out. I needed to glDiable(GL_DEPTH_TEST) for the textures to blend correctly.