Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Depth Calculation for Bounding Boxes

I´m writing an opengl app which uses transparency/translucency algorithms based on depth ordering.

My first approach calculates bounding box center distance to camera for all the objects, but i realized that one bounding box can be closest to camera than other, but its center is farthest.

The orientation of the boxes can be different so axis aren't aligned.

enter image description here

How can i calculate correctly bounding box distance to camera?

like image 809
Roberto Peribáñez Iglesias Avatar asked Nov 02 '22 02:11

Roberto Peribáñez Iglesias


1 Answers

The best approximation can be done by sorting the objects using the farthest OBB point Z-component in camera space (the point with the highest/lowest Z-component in camera space depending on handedness) and hence comes the name Z-sorting. Also you can use a squared distance just fine.

Remember that when issuing a draw commands you need to render one of them first (you can't draw them partially unless you break each of them to separate objects.

In your case object1 should be drawn first, and notice that it has the farthest point along the camera axis.

The algorithm in a nutshell:

  • Transform each bounding box points into camera space.
  • Find the farthest point along the camera Z axis point for each box. This will become the depth key for each bounding box
  • Sort the boxes using their depth keys found.
  • Draw the objects back to front.

P.S. I am yet to think of a case where this might fail that's why I said approximation.

like image 124
concept3d Avatar answered Jan 04 '23 15:01

concept3d