Sign in to follow this  
aewarnick

Alpha blending properly

Recommended Posts

aewarnick    114
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?

Share this post


Link to post
Share on other sites
Brother Bob    10344
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?

Share this post


Link to post
Share on other sites
Brother Bob    10344
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.

Share this post


Link to post
Share on other sites
aewarnick    114
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);

Share this post


Link to post
Share on other sites
hiyo    122
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.

Share this post


Link to post
Share on other sites
aewarnick    114
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.

Share this post


Link to post
Share on other sites
_the_phantom_    11250
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this