Problems with Draw Sorting for Alpha Blending

Hi! I'm having a problem finding a way to sort my alpha blended objects for drawing. My language is C# and I'm using the April 2006 DirectX SDK. Currently, I'm calculating the distance from the camera and using an IComparer class object in an Arraylist. When I'm using exceptionally large objects (such as foreground cloud effects) I find that because their distance is measured from the origin objects that are intended to be drawn behind them will be drawn out of order and subsequently drawn over completely (giving the appearance of not being drawn at all).

Here's the code I'm using:
public int Compare(object x, object y)
{
jObject xc = ((jObjectNode)x).getObject();
jObject yc = ((jObjectNode)y).getObject();
int returnable = 0;

float cx, cy, cz = 0.0f;

if (cam != null)
{
cx = xc.Position.X - cam.Position.X;
cy = xc.Position.Y - cam.Position.Y;
cz = xc.Position.Z - cam.Position.Z;
xc.Distance = (float)Math.Sqrt(cx * cx + cy * cy + cz * cz);

cx = yc.Position.X - cam.Position.X;
cy = yc.Position.Y - cam.Position.Y;
cz = yc.Position.Z - cam.Position.Z;
yc.Distance = (float)Math.Sqrt(cx * cx + cy * cy + cz * cz);

returnable = (int)xc.Distance.CompareTo(yc.Distance);
}
return -returnable;
}
I figured this solution was too simple when I implemented it but I don't have or know of a better solution. This leads to my question : Is there a method of sorting objects by the position of their volumes on screen Here's two screenshots to help clarify my question:





The first screenshot has everything hunky dory. Things are rendered correctly. However, at certain angles I see that the front most image will obscure some images because the distance is being calculated from the object's origin. This is the problem.

What is the technique for sorting these based on what's on screen instead of from their arbitrary positions Thank you in advance for any help you can provide!


Answer this question

Problems with Draw Sorting for Alpha Blending

  • highgrade

    First, are these alpha blended objects essentially camera-oriented sprites   If so, you'll have a problem no matter which way you try to do it.  This is because the object essentially rotates as you rotate your camera.  If the sprite is small, that can be ok, but if it is large, the edges of the sprite can obscure other objects, depending on sort order.

    Your technique of using distance, instead of view-space Z will give you a stable sort result when you rotate your camera, but if you have Z-write on, that can give you an unstable visual result, since the edge of a sprite can move behind another sprite that compares as further away.

    I'm guessing your problem is actually caused by leaving Z-write on.  You will probably be happy enough with the results if you turn it off.  Of course this isn't a complete solution for when you integrate solid objects with Z-write on, since you can end up with the edges of large transparent sprites moving in front of, or behind objects depending on the camera angle.

    If you take a look at the diagram below, and imagine rotating the camera, what happens when the edges of the flat camera-oriented sprites.  This is actually what is being drawn, yet your comparison assumes the entire object is at the same distance from the camera.  (I won't attempt to draw a smooth curve that the comparison represents with ASCII art).

    --------------------------+---------------------------
    --+--

     

                                      \/  <--- camera

     

    Of course, you might not be using camera-oriented sprites... In which case I've just wasted a bunch of space ;).   If these are just large transparent objects in your scene, there's no way to sort them the way you seem to want, such that all parts of them are sorted correctly against all parts of other objects.  That's what the Z-buffer is for.  Of course, that's not an answer for transparent objects.  What you'll likely have to do, is split your large objects into smaller pieces that don't exhibit problems when you sort the individual pieces along with the other objects in your scene.



  • Juliano Horta

    Unfortunately, these are billboard quads with actual geometry. But thank you for the help! At the least, I'll be able to argue with my artists.

  • Problems with Draw Sorting for Alpha Blending