sorting and hierarchy

Here’s a pretty basic question but may not have a good
solution, so I wanna find out what’s the generally
accepted solution.

Scenario:

Imagine 2 cubes, A and B. A is transform parent of B.

In a certain frame, B is spatially in front of A. That
is, B’s z-coord is less than A’s.

Question:

To avoid overdraw, we sort front to back, thus drawing
B first, then A.

But if we want to do all transforms in OpenGL,
without duplicating the calcs in software, how do we
maintain B’s transform matrix, since you have to first
specify A’s transform first?

In other words, the rendering order is B then A, but
the transform order is A then B. What’s the generally
accepted solution here? Thanks!

.rex

  1. calculate modelview matrices of all visible objects
  2. sort by z; I’m fairly certain a mat4*vec4(0,0,0,1) is cheap.
  3. to draw an object, load its modelview matrix into GL first.

You could use modelviewprojection matrices, too.
Generally accepted solutions used to depend on GL’s two matrix stacks. GL3 solutions would need to implement their matrix math and management anyway.

Ilian, thanks for the quick reply.

When you do 1) calc modelview matrices, do you
use OpenGL to do it and then read back the matrices
or duplicate it in software? I’m trying to figure
out if it is feasible to let OpenGL handle all
the transforms.

.rex

Duplicate it in software.

But yes, it’s feasible to let OpenGL do the calculations via its glTranslate/glRotate then use glGetFloatv. It’s simply many times slower and messier to do so.

Thanks!