Drawing a scene with transparent and opaque objects correctly...

In case I want to draw a scene having both transparent and opaque objects, should I do the following:

  1. Draw the opaque objects first, with Z-Buffer enabled.
  2. Specify alpha-blending function.
  3. Mask Z-Buffer (Z-Buffer is read, but not updated).
  4. Draw the transparent objects.

Am I right?


Yes :slight_smile:

Another solution is to use one Order Independent Transparency algo such as Dual Depth Peeling

  1. Draw the transparent objects.
    Nitpick: for correct blending this should be “draw transparent objects from back to front”.

[QUOTE=The Little Body;1241177]Yes :slight_smile:

Another solution is to use one Order Independent Transparency algo such as Dual Depth Peeling

I am using the dual depth peeling algorithms in my application to render transparent primitives. I have 2 lists in my application, one consists of transparent primitives, and the other one of opaque primitives. I have observed that if I render any opaque primitives BEFORE rendering transparent primitives using one of the algorithms in the dual depth peeling demo, the opaque primitives do not get rendered.
On the contrary, If I draw the opaque primitives AFTER rendering transparent primitives using the algorithm, the order of rendering gets disturbed, and the output, consequently, does not come out as expected.

Is there any restriction while using the algorithm for rendering transparent and opaque primitives together that I’m not aware of/that I’ve missed? Please help me as I’m stuck with this for almost a week.

You should check your status.
There are two main way to blend transparent object. OVER and UNDER (and other less used method like MAX, MIN, ADDITIVE blending that always create artifact)
Over operator is the most popular, you have to render object from the farthest to the nearest with color = source * srcAlpha + dest * (1-srcAlpha)

With the under operator you draw object from the nearest to the farthest. You have to use the following operation color = destAlpha * (srcAlpha * srcColor) + destColor and alpha = (1-srcAlpha) * destAlpha
Cause this you have to clear the alpha to 1.0 then draw your alpha object with UNDER operator and then render opaque object with the same formula (but the alpha will alway be one).

The problem with under operation is that you always have blending enable. Otherwise you have to render alpha object on another buffer and the compose the buffer at the end.