Sorting Billboards

Started by
8 comments, last by fyhuang 19 years, 8 months ago
Hello, I'm rendering a lot of billboards (128x128x128) that lie on a fixed grid. I want to render them back to front, but the sorting algorithm is too slow. A Distance per billboard in the quicksort callback is just too much for the amount of data. Some time ago I've read that there is a better way to sort particles that lie on a fixed grid, anyone knows how to do that? Regards, Lyve
_____________________________________http://www.winmaze.de, a 3D shoot em up in OpenGL, nice graphics, multiplayer, chat rooms, a nice community, worth visiting! ;)http://www.spheretris.tk, an upcoming Tetrisphere clone for windows, a 3D tetris game on a sphere with powerful graphics for Geforce FX and similar graphics cards.
Advertisement
I'm using my simple implementation of bubble sort, certinly matching up to my needs:

void GraphicsSystem::Sort(std::list <Particle*> *List){std::list<Particle*>::iterator Iterator;std::list<Particle*>::iterator InnerIndex;	for (Iterator = List->begin() ; Iterator != List->end() ; ++Iterator)	{		for (InnerIndex = Iterator ; InnerIndex != List->end() ; ++InnerIndex)		{			if((**Iterator) > (**InnerIndex))			{			Swap(*Iterator, *InnerIndex);			break;			}		}	}}


Iterator is an iterator, *Iterator is a pointer to a particle, and **Iterator is the particle instance itself. The particle class has the > operator overloaded (checks if it's Z is greater than the input particle).
Uh oh, bubble sort is even slower than quicksort.

If you do this for a lot of particles every frame, it will be VERY slow
_____________________________________http://www.winmaze.de, a 3D shoot em up in OpenGL, nice graphics, multiplayer, chat rooms, a nice community, worth visiting! ;)http://www.spheretris.tk, an upcoming Tetrisphere clone for windows, a 3D tetris game on a sphere with powerful graphics for Geforce FX and similar graphics cards.
I've found the solution again:

"... uses a cheap hack to avoid sorting the block. Because the cells are in a fixed 3D grid, it is easy to find the most distant corner (or corners), and then use the triple-nested for loop, simply going along each axis in an order determined by the most distant corners."


I think this is a very nice trick ;)

_____________________________________http://www.winmaze.de, a 3D shoot em up in OpenGL, nice graphics, multiplayer, chat rooms, a nice community, worth visiting! ;)http://www.spheretris.tk, an upcoming Tetrisphere clone for windows, a 3D tetris game on a sphere with powerful graphics for Geforce FX and similar graphics cards.
Nice solution Lyve. I used simmilar to transverse octree in f2b order without sorting. If later you'll need to use sorting I sugest you use something that exploits temporal and spatial coherence.

Enokh: You might want to read up on other sorting algos like radix, quick,...
You should never let your fears become the boundaries of your dreams.
Quote:Original post by Lyve
I've found the solution again:

"... uses a cheap hack to avoid sorting the block. Because the cells are in a fixed 3D grid, it is easy to find the most distant corner (or corners), and then use the triple-nested for loop, simply going along each axis in an order determined by the most distant corners."


I think this is a very nice trick ;)


Which also means it can be precomputed as lists of pointers or indices since there are only 4 (rather 2D) or 8 (full 3D) corners. This might speed up things a bit if you have far less bilboards than grid cells. This also works if you don't have a grid.
"Coding math tricks in asm is more fun than Java"
Note that bubble sort (though slower in general) may be faster than quick sort in this case.

Bubble sort is fast when sorting sequences that are already partially sorted. Because there may be very few changes between every sort, that could be the case here... Perhaps worth a try?
You don't need distance per billboard - all you need is the squared distance per billboard - just leave out the sqrt() and it will run fast enough...
- fyhuang [ site ]
Of course I'm already using only the squared distance.

"and it will run fast enough..."

For what? Sorting 128x128x128 = 2097152 squared distances in realtime? Of course it's NOT fast enough.
_____________________________________http://www.winmaze.de, a 3D shoot em up in OpenGL, nice graphics, multiplayer, chat rooms, a nice community, worth visiting! ;)http://www.spheretris.tk, an upcoming Tetrisphere clone for windows, a 3D tetris game on a sphere with powerful graphics for Geforce FX and similar graphics cards.
Oops, didn't read that, sorry.
- fyhuang [ site ]

This topic is closed to new replies.

Advertisement