Help with blending black against existing pixels

Discussion in 'Public Game Developers Forum' started by Peter Hirschberg, Mar 31, 2010.

  1. Peter Hirschberg

    Peter Hirschberg Active Member

    Dec 23, 2008
    40
    0
    0
    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
     
  2. Stroffolino

    Stroffolino Well-Known Member
    Patreon Silver

    Apr 28, 2009
    1,100
    8
    38
    Software Engineer
    Pennsylvania
    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.
     
  3. Venan

    Venan Well-Known Member

    Jan 13, 2010
    174
    0
    0
    #3 Venan, Mar 31, 2010
    Last edited: Mar 31, 2010
    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.
     
  4. InsertWittyName

    InsertWittyName Well-Known Member

    Nov 26, 2008
    202
    1
    0
    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.
     

Share This Page