OpenGL's incorrect alpha handling
- Jonathan
- A Brave Victim
- Posts: 3391
- Joined: Thu Feb 03, 2005 12:50 am
- Location: Not really lurking anymore
OpenGL's incorrect alpha handling
Any idea how to fix it, other than modifying a software renderer?
ˌɑrməˈɡɛˌtrɑn
Simply disabling depth testing and enabling blending works only for additive blending; it is "commutative". Addtive blending is
and if you do two of those, the order does not matter. Alpha blending is
and there, two operations don't commute:
o1 after o2 gives
and o2 after o1 gives
which differ in the contribution of the two added colors.
Uh, I just had an idea. Use two passes. In the first pass, do
and in the second pass, do
Do all first passes before all second passes. The order of the renderings during the two passes does not matter.
Of course, then the end result will not look exactly like several layers of stacked colored glass, but that was not in Jonathan's specification
Code: Select all
color -> color + addition
Code: Select all
color -> color * ( 1 - alpha ) + addition * alpha
Code: Select all
o1: color -> color * ( 1 - alpha1 ) + addition1 * alpha1
o2: color -> color * ( 1 - alpha2 ) + addition2 * alpha2
Code: Select all
color -> ( color * ( 1 - alpha2 ) + addition2 * alpha2 ) * ( 1 - alpha1 ) + addition1 * alpha1 = color * ( 1 - alpha2 ) * ( 1 - alpha1 ) + addition2 * alpha2 * ( 1 - alpha1 ) + addition1 * alpha1
Code: Select all
color -> ( color * ( 1 - alpha1 ) + addition1 * alpha1 ) * ( 1 - alpha2 ) + addition2 * alpha2 = color * ( 1 - alpha1 ) * ( 1 - alpha2 ) + addition1 * alpha1 * ( 1 - alpha2 ) + addition2 * alpha2
Uh, I just had an idea. Use two passes. In the first pass, do
Code: Select all
color -> color * ( 1 - alpha )
Code: Select all
color -> color + alpha * addition
Of course, then the end result will not look exactly like several layers of stacked colored glass, but that was not in Jonathan's specification
You mean that if one texel with alpha 0, color black and one with color white and alpha 1 are blended 50/50 and rendered on a black background, the result is only 25 percent white? You can pre-blend and use a different blending function.Jonathan wrote:- Interpolation of texels
Hmm, shouldn't it be alpha -> 1-(1-alpha_1)(1-alpha_2), the alpha value you'd get if you put two transparent glasses over each other? If I'm not mistaken, that's exactly what you get with the modified blending.Jonathan wrote:- Alpha result of alpha blending.
Hmm, maybe you should tell us exactly what you're trying to do and what the problem is.