Blending - order does matter ?

I think I’m going to answer my own question here, but I would appreciate some confirmation regarding the order of drawing in opengl when blending is involved.

I have created some 3D Shape classes for drawing spheres, cylinders, etc using the gluQuadric and glutSolid/Wire objects and I have added methods to allow me to use blending with alpha values to make the objects transparent.

Additionally I have 3DS models that I load in the scene and other textured spheres (earth, moon, etc).

When I drew my scene I initially drew each 3DS model and any 3D shapes that I attached to it and then I would move on down the list of 3DS models and corresponding shapes and then a funny thing happened…my 3D shapes that were semi-transparent seemed to only intersect with the model that was drawn before they were…ie, before the blended enabled shapes were drawn and not with any models after. The models that were drawn after seemed to always be on top (or drawn over) the 3D shapes even though in many cases the model may have been inside the 3D shape.

So, I recalled that this is possibly due to the order of drawing the models and blended shapes…that I should draw all the models first, then finally loop through the shapes that use blending in order to display them all correctly - ie so that the blended shapes don’t appear in the background.

See the attached picture for the problem I had…the large YELLOW sphere is supposed to intersect the model but in the top-left picture it is BEHIND it, but in the bottom-right picture is is correctly intersecting it.

Absolutely. Alpha-translucent polys generally do not write to the z-buffer, meaning that if you render something at a later time in the same screen-space, it will get rendered ON TOP of the alpha-translucent poly.

For any rendering that contains alpha-translucency you are required to perform 2 passes:

  1. render all opaque geometry (front-to-back for performance)
  2. render all translucent geometry (back-to-front for correctness when translucencies overlap)

So before rendering you basically have to divide all your geometry into 2 buckets (opaque and translucent), and sort the buckets. The iterate through them in the order mentioned above.

When you mention that you have to “sort the buckets” are you saying that to improve performance I have to sort each opaque geometry by depth and then draw it and also sort the translucent geometry by depth and then drawn it? or can I simply separate into opaque and translucent buckets and not actually sort within the buckets by depth?

I mean, if I don’t do the sorting then as long as I still separate into opaque and translucent then the drawing will be correct (it is so far for me) but it won’t be as efficient as I could have gotten it by internally sorting it?

Thank you for your answer.

Sorting the translucent geometry will improve correctness. The opaque geometry will look the same no mather what order they are rendered. Ordering the opaque geometry by depth or state can improve performance.

thank you