Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

c:/home/kevn/src/animaniac/ani/Dynamics/operators/ColorWithAgeOperator.h

Go to the documentation of this file.
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          // set some default colors to cycle through
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       // colors to interpolate between
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          // find the range that we'll examine... range = {0...(size-1)} so that we can always check i and i+1
00051          // NOTE: range will at least be 0, since size >= 1
00052          assert( mColorTransitions.size() > 0 && "to use this operator, you must define some colors" );
00053          float range = (float)mColorTransitions.size() - 1.0f; 
00054 
00055          // the indices of the two colors to interpolate between 
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          // find the value to interpolate this color with the next color (should be 0..1)
00062          float val = indexf - (float)index;
00063 
00064          gmtl::Vec4f color;
00065          gmtl::lerp( color, val, mColorTransitions[index], mColorTransitions[next_index] );
00066 
00067          //color.setHSV( rel, 0.8f, 1.0f );
00068          p->setColor( color );
00069       }
00070    }
00071 } // end namespace
00072 
00073 #endif

Generated on Wed Jun 12 01:54:01 2002 for Animaniac by doxygen1.2.15