depth sorting without GL_DEPTH buffer

sometimes you cant use depth buffer, sometimes there are problems with images & alpha & depth, sometimes you need a sorted list with indicies, heres an example how to sort a paired list. grreetings ascorbin

  
bool MyDataSortPredicate(const pair<int, float>& lhs, const pair<int, float>& rhs)  
{  
    return lhs.second < rhs.second;  
}  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	  
	ofSetFrameRate(1);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	  
	// 20 random Points  
	vector <ofPoint> points;  
	points.resize(20);  
	for(int i=0; i<points.size(); i++) {  
		points[i].set(ofRandom(100, 300), ofRandom(100, 300), ofRandom(10, 30));  
	}  
	  
	list< pair<int, float> > depthList;  
	  
	// put indexed points and z-values into the list  
	for(int i=0; i<points.size(); i++) {  
		depthList.push_back( make_pair(i, points[i].z) );  
	}  
	  
	// sort the list  
	depthList.sort(MyDataSortPredicate);  
	  
	// iterate through list  
	std::list<pair<int, float> >::iterator it;  
    for(it = depthList.begin(); it != depthList.end(); it++) {  
          
        int id = it->first;  
		// draw point  
		ofSetColor(255, 100, 0);  
        ofCircle(points[id].x, points[id].y, points[id].z);  
		ofSetColor(0, 0, 0);  
		ofDrawBitmapString(ofToString(id), points[id].x, points[id].y);  
    }  
}  

1 Like

Super helpful function. Thanks Ascorbin!