# Sorting objects to draw (OpenGL)

This topic is 4515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Doing some OpenGL programming, and I read that a good way to draw objects is to sort them and drawing them from back to front. I am curious as to how such a procedure would be done. I am guessing there is some form of function that helps when doing this, but I have been unable to find a suitable one. Does anybody have any idea how to sort objects to be rendered so I can draw them from back to front?

##### Share on other sites
Firstly, you might want to check the age of that information. I guess its a bit old as current advice is sort and render in a front to back manner in order to take advantage of the depth buffer on the card.

There isn't a built in function todo this, you will have to perform your own sorting on the objects.

##### Share on other sites
Well, it really depends on what your scene looks like... if you have a good space paritioning system (tiles, BSP, Quadtree, Octree, KD-tree), there's usually a simple way to determine which partition gets drawn first, and you render recursively.

If you don't have, then you can keep a sorted list from one frame to another. Each frame, update the distance to the camera, and perform a small and easy sort which works well on almost-sorted input (such as insertion sort).

##### Share on other sites
The most simple procedure is to sort them by distance, and then draw them in that order.
Painter's algorithms work that way: you sort the objects in such an order, that the ones far away are drawn first, then the ones more to the front, so that these overdraw the further away ones.
There's however a problem: if the same object is partially behind and partially in front of another one (like two crossing triangles), there'll always be a part drawn wrong.

However, in many situations, you can use the ZBuffer instead, then you can draw them in any order and the ZBuffer will take care of the visibility. Look up about OpenGL zbuffer.

But for many game engines, some space partitioning system is better (in combination with a ZBuffer)

##### Share on other sites
Aha, hmm. I am not using any particular space partitioning stuff, and I don't plan to either. I read about using this front to back, or back to front, when using the occlusion culling extension, something I think will prove useful in my application.

##### Share on other sites
Quote:
 Original post by ToohrVykWell, it really depends on what your scene looks like... if you have a good space paritioning system (tiles, BSP, Quadtree, Octree, KD-tree), there's usually a simple way to determine which partition gets drawn first, and you render recursively. If you don't have, then you can keep a sorted list from one frame to another. Each frame, update the distance to the camera, and perform a small and easy sort which works well on almost-sorted input (such as insertion sort).

This is the suggestion I feel I am most familiar with, but having done a lot of theory on how to make it work, I think it will fail as making a simple distance check between the camera position and each object's position is not an accurate way to determine their depth as objects are of different sizes.

So, for example.. a small object in front of a larger object, which closest edge is closer than the small object (even though the smaller object's _position_ is closer (i.e. the center point)). This scenario will make it so that the smaller object is drawn before the large one, making it so that the smaller one covers parts of the larger one.

Unless... I have totally misconcepted how it all works.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011386
• ### Who's Online (See full list)

There are no registered users currently online

×