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!

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:

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.

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:


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.

[QUOTE=jameswh;1259356]
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?[/QUOTE]
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.