# Blending with equal alpha

Hello!

I’m playing with the alpha blending example in the Red Book (v1.2, third edition, Example 6.1). In short, it places 2 colored triangles on the screen that overlap, and blends the colors together.

The example uses blending function:

`````` glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
``````

which I believe I understand. The original example has the triangles each drawn with 0.75 alpha, making the source triangle (second one drawn) have a higher translucency (since, from the book, source blending is 0.75, and destination blending is 1.0 – 0.75 = 0.25).

So far so good. Now, I thought if I changed the alpha to 0.50 in both triangles, the blended area would be the same color no matter what triangle was drawn first. However, I was wrong! The topmost triangle still has a more dominant affect on the blending. Why is this?

If the left triangle is the source and has color (1.0, 1.0, 0.0, 0.5), and the right triangle is the destination and has color (0.0, 1.0, 1.0, 0.5), then, if I’m understanding correctly, the final blended color with the aforementioned blend function is:

R = (1.0 * 0.5) + (0.0 * (1.0-0.5)) = 0.5
G = (1.0 * 0.5) + (1.0 * (1.0-0.5)) = 1.0
B = (0.0 * 0.5) + (1.0 * (1.0-0.5)) = 0.5
A = (0.5 * 0.5) + (0.5 * (1.0-0.5)) = 0.5

So, RGBA = (0.5, 1.0, 0.5, 0.5)

If you reverse the source and destination, it turns out the same:

R = (0.0 * 0.5) + (1.0 * (1.0-0.5)) = 0.5
G = (1.0 * 0.5) + (1.0 * (1.0-0.5)) = 1.0
B = (1.0 * 0.5) + (0.0 * (1.0-0.5)) = 0.5
A = (0.5 * 0.5) + (0.5 * (1.0-0.5)) = 0.5

So, RGBA = (0.5, 1.0, 0.5, 0.5)

If the blended colors come up the same in either case, why the color difference when changing the order of drawing?

Cheers,
-C

The alpha buffer affects the display color when the triangle is first rendered. Even with blending off, I believe it still gets “adjusted” by the alpha buffer. For instance:

clear color = (0.0,0.0,0.0,0.0)
triangle 1 = (1.0 ,1.0 ,0.0 ,0.5)
triangle 2 = (0.0 ,1.0 ,1.0 ,0.5)

triangle 1 drawn first = (0.5 ,0.5 ,0.0 ,0.5)
triangle 2 blended = (0.25,0.75,0.5 ,0.5)

triangle 2 drawn first = (0.0 ,0.5 ,0.5 ,0.5)
triangle 1 blended = (0.5 ,0.75,0.25,0.5)

These are clearly two seperate colors, and may account for the problem.

No, that’s not it. The blending operation isn’t commutative. This is an age old problem, and the solution, known as “order independent transparency” or OIT is something like the holy grail of graphics.

You’re getting this:
1)framebuffer:=
0.5first triangle + 0.5black

# 2)framebuffer:= 0.5second triangle + 0.5framebuffer

0.5*second triangle + 0.25*first triangle

Order matters, obviously.

Ah, okay. I think I see now. I wasn’t accounting for the fact that the first triangle also needs to blend against the framebuffer (which in this case is all black). Thanks for the replies!

Cheers,
-C