2

I have two questions about OpenGL blending.

1) I know I have to draw opaque objects first and then draw from back to front the non-opaque ones. So I put them in a list depending in the distance to the center (0,0,0). But do transformations (rotate and translate) affect the "center" from where I measure the distance?

2) And second, if the items I draw are triangles, how do I measure the distance? To its incenter? To its orthocenter?

user1118321
  • 25,567
  • 4
  • 55
  • 86
Ediolot
  • 501
  • 2
  • 6
  • 19
  • Sorting always depends on the point of view. And sure, transformations do affect your sorting. The most common method for sorting are [BSP-Trees](http://computer-graphics.se/TSBK07-files/pdf/PDF09/6b.pdf). – BDL Nov 08 '14 at 15:19
  • You might want to read on how the depth buffer and blending works. – Colonel Thirty Two Nov 08 '14 at 16:01

1 Answers1

7

You certainly need to take the transformations into account for sorting. Applying all the transformations, and then sorting by the resulting depth (z-coordinate), is the most direct approach.

A mostly more efficient way of achieving the same thing is to apply the inverse transformations to your view direction once for each object (or set of objects that use the same transformations), and then calculate the depth of each vertex/triangle as the dot product of the vertex with this inverse-transformed view vector. This will require only one dot product per triangle, instead of applying full transformations to them. And the number of triangles is often orders magnitude larger than the number of objects.

As to which point to use: There's really no solution that will work for all cases. The center of the triangle should e as good as anything. This whole approach is an approximation that will work sufficiently well in many cases, but will not be entirely correct in some scenarios.

To illustrate the fundamental challenges with order-dependent transparency, let's look at a few examples. In the following figure, the view direction is from left to right, and we look at two triangles A and B edge on:

             \
              \
               B
                \
               \ \
                \ \
view ----->      \
                  A
                   \
                    \

Visually, it's clear that B is behind A, and needs to be drawn first. Yet:

  • The closest point of B is closer to the view point than the closest point of A.
  • The farthest point of B is closer to the view point than the farthest point of A.
  • The center point of B is closer to the view point than the center point of A.

You can't sort these triangles correctly by comparing one single depth value from each of them. To handle this properly, you have to take the geometry into account, and use more complex criteria to order them properly.

Then there are cases where there is no valid order:

               \    /
                \  /
view ----->      \/
                 /\
                B  A
               /    \

Here, there is no sorting order for A and B that would be valid. Parts of B are behind A, and parts of A are behind B. This will be the case whenever you have intersecting triangles. The only way to correctly resolve this is to split triangles.

There are also configuration without any intersecting triangles where there is no valid order. This is an example with 4 triangles, looking from the top this time:

             ___________
   |\        \          |
 __|_\________\         |___
|              \        |__/
|         ______\       |
|________/       \      |
   |     \        \_____|__
   |      \_______/        |
 __|       \               |
/__|        \______________|
   |         \        \ |
   |__________\        \|

These difficulties are an important reason why order independent transparency rendering methods are so attractive, beyond just avoiding the overhead of sorting.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Very good answer, just one remark: "You can't sort these triangles properly based on a simple numerical comparison" is only half-true. The general approach to find an ordering between triangles is to check whether all vertices of one triangle lie on the same side of the plane spanned by the other triangle. For non-intersecting triangles this is always the case. The normal of the plane (and the side on which the triangles lies compared to the plane) tell you which triangle will be in front. – BDL Nov 08 '14 at 17:22
  • How are the number of triangles orders of magnitude smaller than the number of objects in a scene? It should be the other way around since an object is composed of at least one triangle. – Andon M. Coleman Nov 08 '14 at 18:19
  • @AndonM.Coleman Argh. It's what I mean, not what I say... Thanks for pointing that out. – Reto Koradi Nov 08 '14 at 18:27
  • @BDL I reworded that. I meant to express that you can't just take one depth value from each triangle, and use a single comparison on them. I think the condition you describe is sufficient, but not necessary. You can have triangles that do **not** intersect, without all points of one triangle being on the same side of the plane spanned by the other triangle. – Reto Koradi Nov 08 '14 at 18:30
  • Thinking about this some more, there are also scenarios without any intersections where there is no valid ordering. I might try to add another fancy ascii diagram for that later today. – Reto Koradi Nov 08 '14 at 18:42