OpenGL's incorrect alpha handling

Everything todo with programming goes HERE.
Post Reply
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

OpenGL's incorrect alpha handling

Post by Jonathan »

Any idea how to fix it, other than modifying a software renderer?
ˌɑrməˈɡɛˌtrɑn
User avatar
hang3r
Core Dumper
Posts: 188
Joined: Fri Sep 16, 2005 9:05 pm
Location: Australia

Post by hang3r »

More information??? How is it handling the alpha blending wrong??? At least give a short example, then maybe someone could help 8)
User avatar
Z-Man
God & Project Admin
Posts: 11587
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Post by Z-Man »

If it's the bit that you need to perform depth sorting and sometimes, on circular overlaps, even cut objects in pieces to get the correct result, there are techniques of depth slicing/peeling.
User avatar
hang3r
Core Dumper
Posts: 188
Joined: Fri Sep 16, 2005 9:05 pm
Location: Australia

Post by hang3r »

Ah order independent transparency. I always make sure I order things in the correct order and just disable depth testing / enable blending, then enable/disable when I don’t need transparency. But there are circumstances where you can’t enable blending, and I guess this is the case?
User avatar
Z-Man
God & Project Admin
Posts: 11587
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Post by Z-Man »

Simply disabling depth testing and enabling blending works only for additive blending; it is "commutative". Addtive blending is

Code: Select all

color -> color + addition
and if you do two of those, the order does not matter. Alpha blending is

Code: Select all

color -> color * ( 1 - alpha ) + addition * alpha
and there, two operations don't commute:

Code: Select all

o1: color -> color * ( 1 - alpha1 ) + addition1 * alpha1
o2: color -> color * ( 1 - alpha2 ) + addition2 * alpha2
o1 after o2 gives

Code: Select all

color -> ( color * ( 1 - alpha2 ) + addition2 * alpha2 ) * ( 1 - alpha1 ) + addition1 * alpha1 = color * ( 1 - alpha2 ) * ( 1 - alpha1 ) + addition2 * alpha2 * ( 1 - alpha1 ) + addition1 * alpha1
and o2 after o1 gives

Code: Select all

color -> ( color * ( 1 - alpha1 ) + addition1 * alpha1 ) * ( 1 - alpha2 ) + addition2 * alpha2 = color * ( 1 - alpha1 ) * ( 1 - alpha2 ) + addition1 * alpha1 * ( 1 - alpha2 ) + addition2 * alpha2
which differ in the contribution of the two added colors.

Uh, I just had an idea. Use two passes. In the first pass, do

Code: Select all

color -> color * ( 1 - alpha )
and in the second pass, do

Code: Select all

color -> color + alpha * addition
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 :)
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Post by Jonathan »

I didn't mean that, and I guess I can't blame you. Two examples of what I meant:
- Interpolation of texels.
- Alpha result of alpha blending.
ˌɑrməˈɡɛˌtrɑn
User avatar
Z-Man
God & Project Admin
Posts: 11587
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Post by Z-Man »

Jonathan wrote:- Interpolation of texels
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:- Alpha result of alpha blending.
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.

Hmm, maybe you should tell us exactly what you're trying to do and what the problem is.
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Post by Jonathan »

Yep, that should work. It was more of a vague complaint that there's no more convenient way (there must be a reason most programs using OpenGL and transparency have "broken" renderings), not really a question. Sometimes I feel like posting something like this, I don't know why. :o
ˌɑrməˈɡɛˌtrɑn
Post Reply