Sign in to follow this  
geo2004

GL_BLEND and the alpha channel

Recommended Posts

geo2004    154
Hello all. I need a way to blend away the black background of an image so that my scene shows through the black part of my sprite (2D). I was doing it by masking (learned from the NeHe page), but that is a nuisance having to create 2 pictures per sprite. I read that I need to use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); in order to blend away the black of a sprite, but it didn't work. It just shows the sprite normally. (As I was typing this i realized my background doesn't have an alpha channel, its just a jpg...is that the problem?) What do I need to set as my alpha channel in my picture (I'm using GIMP to do this) and which BlendFunc to do need? My sprites are png files. Is the alpha of the sprite just all black or white, or does it need to be a grey-scale? Jeff

Share this post


Link to post
Share on other sites
CrimsonSun    336
When you load your image, you could make the alpha of each pixel that is black to be 0 and the alpha of each pixel that isn't to 1. Then create an RGBA texture object and enable blending.

Black isn't a very good color for transparency, though. Pretty much everyone I see uses that awful neon-pinkish color.

Share this post


Link to post
Share on other sites
mrcheesewheel    325
Alpha blend blends based on the alpha value... so if your image doesn't have an alpha, then clearly the blend won't work (either you'll get nothing or the whole image depending on the blend func). You firstly need to work with an image format that supports an alpha channel (as you said... png would work)... then you need to load the image such that the alpha is loaded too (make sure your png loader actually does this). The other option is to load the image, then create a comparable image but with black pixels having an alpha 0 (or 1 depending on your blendfunc choice) and everything else 1 (or 0). The blend func then blends by linear combination of the two parameters scaled by the values... Just think about the numbers in the alpha of source and destination (depending on your choices) and the appropriate blendfunc parameters should be obvious.
Hope this helps,

Dan

Share this post


Link to post
Share on other sites
geo2004    154
Quote:
then you need to load the image such that the alpha is loaded too (make sure your png loader actually does this)

I'm using DevIL to load my images, specifically ilutGLLoadImage("Data/MyPic.png");
How can I be sure that this is loading in the Alpha channel?

Quote:
Black isn't a very good color for transparency, though. Pretty much everyone I see uses that awful neon-pinkish color.
Why is that? Do they use the pink because it's a less common color than black, therefor parts of the picture won't get accidently blended if they're not supposed to(if parts of your sprite are black, it would get blended)?

Thanks for he help
Jeff

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