Alpha blending properly

Started by
8 comments, last by _the_phantom_ 19 years ago
I have looked all over the net trying to find out how to fade an image into the background and have tried all of these combinations and more: float a= .5; glColor4f(1.0f, 1.0f, 1.0f, a); //glColor4i(1, 1, 1, 1); glEnable(GL_BLEND); //glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE); None of them work properly. For this example my image has no alpha channel. Others will though. What am I doing wrong?
*C++*->
Advertisement
glEnable(GL_BLEND);static float a = 0.5f;glColor4f(1, 1, 1, a);RenderQuad();if(a > 0) a -= 0.01f;
The more applications I write, more I find out how less I know
I should not have said fade. It's the blending that does not work right.
*C++*->
So you don't have a color containing a variable alpha once the fragment reaches the blending stage. Are you sure you have set the combiners to actually forward the alpha from the primary color?
I have no idea what you're talking about. Please help.
*C++*->
You said the image you're drawing didn't have an alpha channel, and without alpha channel, alpha blending isn't going to do much. Since you're setting the alpha via the primary color, you need a way to get the alpha channel from the primary color into the rendered image, and you can do that with the texture combiners. Just setting the primary color alpha isn't enough if you're not using it.

For a simple case, setting the texture combiner function to GL_MODULATE may work. Otherwise, you may have to do a little more to get it working. Check out this for example.
I actually got it working correctly with the 24bpp image but not the 32bpp image. What enums do I have to use for an image with an alpha channel?

The image just dissapears with the setting I have for the 24bpp setup. In the image most of the alpha channels are set to 255 while the rest are 0 (for a mask).

float a= (float)alpha/255.0f;
glColor4f(1.0f, 1.0f, 1.0f, a);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
*C++*->
hi, i think i have same problem with u, I m really mad with this thing,if have time pls help me in the topic 'Masking Problem' thank you.
Take this for example. The old old nes game Mario Bros. When you were hit you flashed. There are 2 alpha settings there:
1. Sprite mask 0
2. Flash level 0-255

I have them both down individually. For the mask I use the alpha channel of the 32bpp image. For the flash we just solved that one for a 24bpp image. But combining them together efficiently is a different story. Because the only thing I can come up with is looping through all of the pixels in the image for every flash change setting the alpha values that are not 0 to the flash level for each frame and then drawing it to screen. Very ineficient.

There has to be a better way.
*C++*->
my suggestion is to go and read the section on Texture Functions in the red book (Chapter 9, pg 410 in the 4th Ed.) as this explains the functions behind what you need todo.

Once you understand that you might want to look at teh more advanced Texture Combiner Functions section further into the chapter.

This topic is closed to new replies.

Advertisement