★ TouchArcade needs your help. Click here to support us on Patreon.

Help with blending black against existing pixels

03-31-2010, 01:36 PM
#1
Joined: Dec 2008
Posts: 40
Help with blending black against existing pixels

Hello,

I have a situation where I have a bunch of blended primitives on the screen (not using a depth buffer), and then I want to blacken out part of the screen with a PNG image that has black and some transparent alpha areas. The areas of the image that are transparent I want to be a blend no-op, but the black areas I want to make the screen black, even though there are already pixels there. For the life of me I can't figure out the correct combination of blend arguments for glBlendFunc! It seems like blending can only make the image lighter, never darker.

This is obviously for Vector Tanks if that helps. I'm currently drawing lines with:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

To give them an additive effect ("Screen Mode" in Photoshop parlance).

Anybody know the magic incantation for me to be able to blit a black image on top?

I know I'm missing something obvious. I'm trying to remember if I tried turning off blending completely - I think I tried that and it still didn't work.

I would *think* it would be something like this, but it doesn't work.
glBlendFunc(GL_SRC_ALPHA, GL_ZERO);

Help!?

Thank you
-Peter
03-31-2010, 03:34 PM
#2
Joined: Apr 2009
Location: Pennsylvania
Posts: 1,006
If I understand what you're trying to do, try using modulation mode and glcolor(0,0,0). This will cause all colors in your source image to be multipled by 0 (making them black), while still using its alpha for blending purposes.
03-31-2010, 05:22 PM
#3
Joined: Jan 2010
Posts: 174
That's actually regular alpha blending you are using for your lines. If you want it to be additive, you should us src_alpha, GL_ONE.

If you use that same blend func that you are using for your lines with your image, (src_alpha, one_minus_src_alpha) that should work. Assuming your alpha is opaque (white) where you want the black to draw and black where you want the background to draw.

Last edited by Venan; 03-31-2010 at 05:26 PM.
04-01-2010, 02:15 PM
#4
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) should be fine for your lines (it's not 'additive' as Venan mentioned, but I don't think you actually want that...)

Use glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) for your PNG textures.

Xcode 'optimizes' the PNG files on copy by pre-multiplying the alpha.

In case it's not 100% clear, you'll change the blend mode before you draw each ie.

Code:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
drawLines();
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
drawTextures();
Hope that makes sense.

Didev Studios || Twitter || YouTube || Facebook
Releases
GeoSnake || Bugz || Review Scraper (Open Source)