Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: How to blend multiple overlayed transparent polygons as a "single block" ?

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    21

    How to blend multiple overlayed transparent polygons as a "single block" ?

    Hi All

    I need to draw multiple overlapping shapes with seperate draw array calls, all in the same colour (lets say white), which has a non 1.0 alpha value (for example a color of 1.0, 1.0, 1.0, 0.5). I need these to blend over the background "as one", i.e. i dont want to see the overlapping part at a different intensity, which is what would normally happen as the overlapping parts will be drawn twice and will blend together.

    Is there anyway to do this? (I can only think of first rendering to a transparent texture, and then rendering that, but that seems a rather complex and expensive solution for this...). Maybe theres a blending mode I should use?

    I would definitely be open to using a pixel shader if that is the "correct" way to do this, but until now ive never used shaders so if its the way to go I would appreciate some tips on the best way to do that.

    Thanks!

  2. #2
    Intern Contributor Brokenmind's Avatar
    Join Date
    Feb 2014
    Location
    Aachen / Germany
    Posts
    71
    There's one specific function you need to modify. Here is a similar problem with an answer that points in the right direction.
    Anyway, this function is the key:
    Code :
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    The overlapping is caused by this combination of values, where the alpha values are being summed up. If you could change this to a fixed value, as stated in the above thread, the overlapping might be gone.

  3. #3
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    glBlendEquation() might allow you to do what you're looking for. By default, the values calculated based on the blend function are added. But there are other options. You could try:
    Code :
    glBlendEquation(GL_MAX);
    This would take the maximum of the two terms. If I correctly understand what you're trying to do, this might give you the desired result.

    Edit: On second thought, I'm not sure it will work. Might still be worth playing with. The really safe approach to do what you want is to draw your translucent shapes into an FBO, with black background, and no blending enabled. Then take the resulting texture, and draw it screen sized, with blending enabled, using your constant alpha value.

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by jameswh View Post
    Is there anyway to do this? (I can only think of first rendering to a transparent texture, and then rendering that, but that seems a rather complex and expensive solution for this...). Maybe theres a blending mode I should use?
    Blending alone won't work, as it can't tell the difference between what is the "background" and what is the "overlapping shapes".

    You can use the stencil buffer to keep track of which parts have already been "painted over", and exclude those from subsequent operations. This assumes that you have a stencil buffer. If you don't, then you'll need to render the overlay into a texture (without blending) then blend the completed overlay onto the background.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •