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?

Upvotes: 5

Views: 1392

Answers (2)

legends2k
legends2k

Reputation: 32984

Calculating the distance from camera to centre for each OBB (oriented bounding box) involves the performance hungry sqrt due to the nature of the Euclidean distance function; also you need the closest box from the camera for which approaching the problem by mesauring the distance to centre makes little sense.

Alternative Methods

Z-sorting

If you know the vertices of the boxes in camera space, sorting them by Z would give you the closest one. The box containing that point would be the one closest to the camera. However, since OBBs are usually stored by their centre, orientation and extents, one wouldn't readily have the vertices in camera space; hence it usually requires transforming the vertices of box from box space to the camera space for all vertices of all boxes; this would be costly, perhaps if performed on the GPU, it might be OK.

Intersection Testing

This is the alternative I can think of, that is perhaps faster than sorting but less accurate, is to perform a fast ray-obb intersection testing on those boxes (with ray.origin = camera and ray.direction = obb.centre - camera). Each test would return you a t the distance along the vector where the hit on that box occurs; scaling the ray with t and taking only its Z component would essentially give you the projection of the vector on the Z-axis (this would be the Z coordinate of the point where the ray hits the box); the one with the least Z component will be the closest box from the camera along the Z direction.

This page has both the algorithm and the implementation of ray-obb test; it uses the Kay-Kajiya's slab method described in Real-Time Rendering.

Upvotes: 1

concept3d
concept3d

Reputation: 2278

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.

Upvotes: 1

Related Questions