Original Post
Conider this:
You want to render a semitransparent object.
You enable alpha blending and make the standard
calls to Drawprimitive. All set ?
Of course not !
Problem 1: The object is not transparent onto itself
You want to see the backfaces shining through.
Ohh ! So you disable both z-buffering and culling. Now all
faces of the object are semitransparent and you are
very happy (?)
Not as happy as you would like !
Problem 2: With z-buffering and culling disabled, you get to
see all the objects faces, but they are rarely rendered in the
correct order.
How could this matter? My object are semitransparent, you say.
Thruth is, it matters less the more transparent it is. But if
you wanted full transparency in the first place, the easiest
course of action would be not to render anything at all.
So back to the drawing board ...
Easy solution: You render your object in two stages. First pass
is with culling reversed (that would normally be clockwise culling as opposed to counterclockwise culling, depending on
your preferred vertex orientation). Second pass would then be
with normal (counterclockwise) culling. Wow, it''s beautiful (?)
Not quite ..
Your brilliant method works well for simple shapes. In fact,
there is a handy rule stating when this would be adequate and
when it is not. Here it is, presented to you in two ways.
The first is to please the mathematician. The second is
for mortals like you and me to understand:
definition 1: A simple shape makes a convex hull in 3D
definition 2: If you can cover your object (try it out on the
kitchen bench) with plastic without getting any air pockets, then your shape is simple.
Some thinking quickly convinces you that all the standard
shapes like cube, cylinder, sphere etc. are all simple.
The teapot, for instance, is not. Concerning transparent
rendering, it would not be very wrong to concider it simple
though.
Solution: You would of course want to break down all your
complex objects into simple (or at least simpler) shapes.
We are now extremely happy, as we can make our objects
semitransparent at will (?) Ahem. We are really not halfway
there yet.
Problem 3: All our thinking so far has been about an objects
transparency onto itself (eg. its backfaces). The real problem
arises when ascertaining semitransparency onto other objects
in the scene. If our semitransparent hero is drawn first
for instance, then the object covered by it wouldn''t shine
through. It would be violently masked out by the z-buffer!
So all is hopeless after all? You need your z-buffer like
Tarzan needed Jane.
Well, one way would be to make sure your engine draws the
transparent objects in the front of the scene last. This
however isn''t allways trivial as it must imply your own
logic determining z-ordering (e.g what objects are closest
to the camera). No hurry. We will deal with such matters
as the problem arises.
The worst possible case would be when objects intersect.
Imagine for instance your beloved ghost teapot (beautifully
semitransparent on its quest for opaqueness) halfway through
a semitransparent wall! Now half your teapot should be
invisible (or partly invisible) indeed, but sad to say,
it is of course not. Why ? Because you render semitransparent objects with z-buffering disabled to ensure its transparency onto itself, remember ?
This scenario however, catches your interest, as its solution
would be a superset of every problem, meaning it would
inherently solve all problems. So you go ahead:
When the objects intersect, you need to render them in
multiple passes. The methods will not be trivial, but after
contemplating with your spare armadillo, your heart is again
strong and it all hits you like it did Marlon Brando (right
in the middle of your forhead):
1. You make a copy of your z-buffer. This can be an
expensive act in terms of performance, but you do not
fear this, as you know you can later refine this method,
say for instance, to encompass only calculated parts of it.
2. You render the wall with full transparency (invisible!)
Why would you do such a thing? It would have no effect at all?
Wrong! It would affect the z-buffer, and that is exactly what
you want in your first pass. Confused? Read ahead.
3. Now you render the teapot with z-buffering reversed.
You wagely remember a topic on this forum where a fellow
wondered what could possibly be the practical use of
reversed z-buffering. Well, here it is for you! The only part
of the teapot now rendered will be the one covered (partly)
by the (for the moment) invisible wall. Of course you use
your technique of rendering this half teapot in two passes
as earlier, to also ensure its transparency onto itself.
4. You now want your z-buffer back! Clean out this invisible wall. I shall make no more use of it! This you accomplish
elegantly by restoring it to the backup you made in step 1.
5. You now render the wall with its intended transparency.
This time you have set your z-buffering back to normal (not
reversed). Done? No. We still need that other part of the pot.
6. So finally you repeat step 3, rendering the teapot again.
This time the part of it that is not covered by the wall
will be rendered, as this time your z-buffering method is
restored to normal.
Now you are making scenes with perfect semitransparency
and are truly ready to impress the girl of your dreams
(or man, if you happen yourself to be a girl or to have
alternate preferances). If this turns out to be inadequate,
you could use a Shakespeare sonnett instead, eg.
"Oh know, sweet love, I allways write of you.
For you and love shall allways be my argument."
Voila!
Alternately, you could do this much simpler by simply
making all your objects semitransparent and render them
all in a big goo, disabling all culling and z-buffering.
But you try this, and are not at all satisfied, as it
looks horrible.
Now a fullfledged semitransparency expert, you still have
to concider many z-ordering logics of your own as your
scene grows complex, but having solved the superset problem,
you are ready to cope ...
** the ideas and methods here thrown may not be original
(in fact they cannot be), and the author would appreciate
to learn of any methods, be they refined, improved or (as John Clease would state it) something completly different **