Skip to main content
gamedev.net
🔒 Locked

Z-Sorting Conundrum

Started by TheGilb 3 replies 1.5k views
Original Post
TheGilb
TheGilb
Hi everyone. I was looking for a quick answer to a little problem I was having, any help would be greatly appreciated I have an STL vector of objects that represent transparent renderable objects (For arguments sake let''s say billboarded particles). I have to sort this list from back to front. Can someone provide me with instructions on how to do this? I remember something about there being a graceful way to do this using STL by writing a ''less'' function? Which can be used by an STL sorting algorithm?.. If someone could just point me in a generally good direction that would be great. Thanks in advance. P.S. Yes, I am new to STL algorithms
TheGilb
TheGilb
I would like to take this opportunity to thank the Anonymous Poster who offered his help and advise, though I found what I considered to be a more elegant solution he still pointed me in a reasonable direction. For documentation purposes, I am going to post my solution here.

class TransparentObject {
...

bool operator< (TransparentObject& b) {
return zpos < b.zpos;
}
}
std::sort(ObjList.begin(), ObjList.end());

Any half decent STL reference will inform you that the sort algorithm can either take a less function (In the poster''s reply as a class with operator() overloaded), or will use the < operator if a less function is not specified.
benjamin bunny
benjamin bunny
If you''re sorting thousands of objects, you might want to use a radix sort, rather than STL''s standard sort, which is probably either quicksort or mergesort (can anyone confirm this?). Radix sort completes in linear time, and can be persuaded to work with floating point numbers. See here.
sjelkjd
sjelkjd
stl uses quicksort, with a hueristic to change it to heapsort? (I think), if the data pose a quadratic case for quicksort.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.