00001 #ifndef COLOR_WITH_AGE_OPERATOR
00002 #define COLOR_WITH_AGE_OPERATOR
00003
00004 #include <vector>
00005 #include <gmtl/Vec.h>
00006 #include <gmtl/VecOps.h>
00007
00008 namespace ani
00009 {
00011 template<class __EntityType>
00012 class ColorWithAgeOperator : public ani::Operator<__EntityType>
00013 {
00014 public:
00015 typedef boost::shared_ptr<__EntityType> EntityTypePtr;
00016
00017 public:
00018 ColorWithAgeOperator() : ani::Operator<__EntityType>()
00019 {
00020
00021 gmtl::Vec4f a;
00022 a.set( 1.0f, 0.0f, 0.0f, 1.0f );
00023 mColorTransitions.push_back( a );
00024
00025 a.set( 0.0f, 1.0f, 0.0f, 0.7f );
00026 mColorTransitions.push_back( a );
00027
00028 a.set( 0.0f, 0.0f, 1.0f, 0.3f );
00029 mColorTransitions.push_back( a );
00030 }
00031 virtual ~ColorWithAgeOperator() {}
00032
00033
00034 void setColors( std::vector<gmtl::Vec4f>& colorTransitions ) { mColorTransitions = colorTransitions; }
00035 virtual void exec( DynamicSystem<__EntityType>& ps, float timeDelta );
00036
00037 std::vector<gmtl::Vec4f> mColorTransitions;
00038 };
00039
00040 template <class __EntityType>
00041 void ColorWithAgeOperator<__EntityType>::exec( DynamicSystem<__EntityType>& ps, float timeDelta )
00042 {
00043 std::vector<EntityTypePtr>::iterator it;
00044 for (it = ps.entities().begin(); it != ps.entities().end();
00045 ++it)
00046 {
00047 EntityTypePtr p = *it;
00048 float age__zero_to_one = p->age() / p->ageOfDeath();
00049
00050
00051
00052 assert( mColorTransitions.size() > 0 && "to use this operator, you must define some colors" );
00053 float range = (float)mColorTransitions.size() - 1.0f;
00054
00055
00056 float indexf = age__zero_to_one * range;
00057 int index = (int)indexf;
00058 int next_index = (index+1 != (int)mColorTransitions.size()) ? index+1 : (mColorTransitions.size() - 1);
00059 assert( index >= 0 && next_index < (int)mColorTransitions.size() && "out of bounds" );
00060
00061
00062 float val = indexf - (float)index;
00063
00064 gmtl::Vec4f color;
00065 gmtl::lerp( color, val, mColorTransitions[index], mColorTransitions[next_index] );
00066
00067
00068 p->setColor( color );
00069 }
00070 }
00071 }
00072
00073 #endif