Use the stencil buffer.
clear color/stencil (to 0)/depth
disable color writes
enable depth writes/test
Set alpha test to allow only transparent geometry.
Draw stuff.
Set Stencil Func(GL_LESS,8,255)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
You now have a one bit set (8) in the stencil buffer at the front layer transparent faces
Clear Depth
Set Stencil Func(GL_LESS,12,8)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
The second layer now has two bits set, 4+8=12 (note that due to the mask of 8, only the first bit from the first part takes part in the stencil comparison)
Clear Depth
Set Stencil Func(GL_LESS,14,4)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
Clear Depth
Set Stencil Func(GL_LESS,15,2)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
We are now four transparency layers into the screen (we have appropriate depth information for that) and can start recursively working our way back.
enable color writes
set depth function to GL_EQUAL
Set Stencil Func(GL_GREATER,14,255)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
disable color writes
We have now drawn the backmost transparent layer
set depth function to GL_LESS (or GL_LEQUAL)
Set Stencil Func(GL_EQUAL,14,255)
Set Stencil Op
(GL_KEEP,GL_KEEP,GL_KEEP)
Draw stuff
We now have the depth information for the second backmost layer in the depth buffer
enable color writes
set depth function to GL_EQUAL
Set Stencil Func(GL_GREATER,12,255)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
disable color writes
set depth function to GL_LESS (or GL_LEQUAL)
Set Stencil Func(GL_EQUAL,12,255)
Set Stencil Op
(GL_KEEP,GL_KEEP,GL_KEEP)
Draw stuff
enable color writes
set depth function to GL_EQUAL
Set Stencil Func(GL_GREATER,8,255)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
disable color writes
set depth function to GL_LESS (or GL_LEQUAL)
Set Stencil Func(GL_EQUAL,8,255)
Set Stencil Op
(GL_KEEP,GL_KEEP,GL_KEEP)
Draw stuff
enable color writes
set depth function to GL_EQUAL
Set Stencil Func(GL_GREATER,0,255)
Set Stencil Op(GL_KEEP,GL_KEEP,GL_REPLACE)
Draw stuff
disable color writes
set depth function to GL_LESS (or GL_LEQUAL)
Set Stencil Func(GL_EQUAL,0,255)
Set Stencil Op
(GL_KEEP,GL_KEEP,GL_KEEP)
Draw stuff
That’s it, you have now four layers of transparency, fully sorted. Of course you’ve done a lot of passes and I haven’t sorted out yet where the opaque stuff fits into the picture. But I think this should work in all possible geometry cases.
[This message has been edited by zeckensack (edited 03-14-2002).]
[This message has been edited by zeckensack (edited 03-15-2002).]