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