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

OgreColourInterpolatorAffector.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE 
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under 
00011 the terms of the GNU Lesser General Public License as published by the Free Software 
00012 Foundation; either version 2 of the License, or (at your option) any later 
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT 
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with 
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreColourInterpolatorAffector.h"
00026 #include "OgreParticleSystem.h"
00027 #include "OgreStringConverter.h"
00028 #include "OgreParticle.h"
00029 
00030 
00031 namespace Ogre {
00032     
00033     // init statics
00034     ColourInterpolatorAffector::CmdColourAdjust     ColourInterpolatorAffector::msColourCmd[MAX_STAGES];
00035     ColourInterpolatorAffector::CmdTimeAdjust       ColourInterpolatorAffector::msTimeCmd[MAX_STAGES];
00036 
00037     //-----------------------------------------------------------------------
00038     ColourInterpolatorAffector::ColourInterpolatorAffector()
00039     {
00040         for (int i=0;i<MAX_STAGES;i++)
00041         {
00042             // set default colour to transparent grey, transparent since we might not want to display the particle here
00043             // grey because when a colour component is 0.5f the maximum difference to another colour component is 0.5f
00044             mColourAdj[i]   = ColourValue(0.5f, 0.5f, 0.5f, 0.0f);
00045             mTimeAdj[i]     = 1.0f;
00046         }
00047 
00048         mType = "ColourInterpolator";
00049 
00050         // Init parameters
00051         if (createParamDictionary("ColourInterpolatorAffector"))
00052         {
00053             ParamDictionary* dict = getParamDictionary();
00054 
00055             for (int i=0;i<MAX_STAGES;i++)
00056             {
00057                 msColourCmd[i].mIndex   = i;
00058                 msTimeCmd[i].mIndex     = i;
00059 
00060                 String  colour_title    = String("colour") << i;
00061                 String  time_title      = String("time") << i;
00062                 String  colour_descr    = String("Stage ") << i << String(" colour.");
00063                 String  time_descr      = String("Stage ") << i << String(" time.");
00064 
00065                 dict->addParameter(ParameterDef(colour_title, colour_descr, PT_COLOURVALUE), &msColourCmd[i]);
00066                 dict->addParameter(ParameterDef(time_title,   time_descr,   PT_REAL),        &msTimeCmd[i]);
00067             }
00068         }
00069     }
00070     //-----------------------------------------------------------------------
00071     void ColourInterpolatorAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
00072     {
00073         Particle*           p;
00074         ParticleIterator    pi              = pSystem->_getIterator();
00075 
00076 
00077         while (!pi.end())
00078         {
00079             p = pi.getNext();
00080             const Real      life_time       = p->mTotalTimeToLive;
00081             Real            particle_time   = 1.0f - (p->mTimeToLive / life_time); 
00082 
00083             if (particle_time <= mTimeAdj[0])
00084             {
00085                 p->mColour = mColourAdj[0];
00086             } else
00087             if (particle_time >= mTimeAdj[MAX_STAGES - 1])
00088             {
00089                 p->mColour = mColourAdj[MAX_STAGES-1];
00090             } else
00091             {
00092                 for (int i=0;i<MAX_STAGES-1;i++)
00093                 {
00094                     if (particle_time >= mTimeAdj[i] && particle_time < mTimeAdj[i + 1])
00095                     {
00096                         particle_time -= mTimeAdj[i];
00097                         particle_time /= (mTimeAdj[i+1]-mTimeAdj[i]);
00098                         p->mColour.r = ((mColourAdj[i+1].r * particle_time) + (mColourAdj[i].r * (1.0f - particle_time)));
00099                         p->mColour.g = ((mColourAdj[i+1].g * particle_time) + (mColourAdj[i].g * (1.0f - particle_time)));
00100                         p->mColour.b = ((mColourAdj[i+1].b * particle_time) + (mColourAdj[i].b * (1.0f - particle_time)));
00101                         p->mColour.a = ((mColourAdj[i+1].a * particle_time) + (mColourAdj[i].a * (1.0f - particle_time)));
00102                         break;
00103                     }
00104                 }
00105             }
00106         }
00107     }
00108     
00109     //-----------------------------------------------------------------------
00110     void ColourInterpolatorAffector::setColourAdjust(size_t index, ColourValue colour)
00111     {
00112         mColourAdj[index] = colour;
00113     }
00114     //-----------------------------------------------------------------------
00115     ColourValue ColourInterpolatorAffector::getColourAdjust(size_t index) const
00116     {
00117         return mColourAdj[index];
00118     }
00119 
00120 
00121     //-----------------------------------------------------------------------
00122     void ColourInterpolatorAffector::setTimeAdjust(size_t index, Real time)
00123     {
00124         mTimeAdj[index] = time;
00125     }
00126     //-----------------------------------------------------------------------
00127     Real ColourInterpolatorAffector::getTimeAdjust(size_t index) const
00128     {
00129         return mTimeAdj[index];
00130     }
00131     
00132     
00133     //-----------------------------------------------------------------------
00134     //-----------------------------------------------------------------------
00135     //-----------------------------------------------------------------------
00136     // Command objects
00137     //-----------------------------------------------------------------------
00138     //-----------------------------------------------------------------------
00139     String ColourInterpolatorAffector::CmdColourAdjust::doGet(const void* target) const
00140     {
00141         return StringConverter::toString(
00142             static_cast<const ColourInterpolatorAffector*>(target)->getColourAdjust(mIndex) );
00143     }
00144     void ColourInterpolatorAffector::CmdColourAdjust::doSet(void* target, const String& val)
00145     {
00146         static_cast<ColourInterpolatorAffector*>(target)->setColourAdjust(mIndex,
00147             StringConverter::parseColourValue(val));
00148     }
00149     //-----------------------------------------------------------------------
00150     String ColourInterpolatorAffector::CmdTimeAdjust::doGet(const void* target) const
00151     {
00152         return StringConverter::toString(
00153             static_cast<const ColourInterpolatorAffector*>(target)->getTimeAdjust(mIndex) );
00154     }
00155     void ColourInterpolatorAffector::CmdTimeAdjust::doSet(void* target, const String& val)
00156     {
00157         static_cast<ColourInterpolatorAffector*>(target)->setTimeAdjust(mIndex,
00158             StringConverter::parseReal(val));
00159     }
00160 
00161 }
00162 
00163 
00164 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:05 2004