From: Kevin M. <ke...@vr...> - 2002-04-24 21:30:04
|
Lou was asking me for some code to sort polygons by. Here it is. It is pretty simple, letting STL do the work for you. There is a little bit of Math, but it is pretty simple. The code depends on the Math library in isugamedev/lib/Math, but you could easilly write your own. The hardest thing to do that I'm not showing, is how to get the rotation component from your camera matrix. It really isn't that hard to do though, just extract the upper left 3x3. #include <vector> // an array of particles std::vector<Particle*> particles; // set up the sorting function (see code below for the functor)... ComparePointProjectionsAlongDirection particle_sorter; particle_sorter.sortDir = camera_rot * Vec3<float>( 0.0f, 0.0f, 1.0f ); particle_sorter.sortDir.normalize(); // sort the sprites (which could be transparent) based on projection // information std::sort( particles.begin(), particles.end(), particle_sorter ); // here is the sort functor that you plug into std::sort // Allows for the comparison of projected point distances // onto a given vector // NOTE: For use with std::sort or std::stable_sort struct ComparePointProjectionsAlongDirection { public: ComparePointProjectionsAlongDirection() : sortDir() {} bool operator()(const Particle* x, const Particle* y) { float xVal = sortDir.dot( x->position() ); float yVal = sortDir.dot( y->position() ); // true if x precedes y in back-to-front sortDir rendering order. return (xVal < yVal); } Vec3<float> sortDir; // 3D Direction to sort by }; // and here is (sort of) what Particle looks like, note the NOTE: class Particle { public: // NOTE: you'd probably rewrite this function to average the 4 // points of your Quad or triangle... Vec3<float>& position() { return mPosition; } private: Vec3<float> mPosition; }; -- @--@---@---@----@-----@------@------@-----@----@---@---@--@ Kevin Meinert __ _ __ http://www.vrac.iastate.edu/~kevn \ || \| \ / ` Virtual Reality Applications Center \ ||.-'|--\\ Howe Hall, Iowa State University, Ames Iowa \|| \| \`__, ----------------------------------------------------------- |