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

# OgreColourImageAffector.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 "OgreColourImageAffector.h"
    00026 #include "OgreParticleSystem.h"
    00027 #include "OgreStringConverter.h"
    00028 #include "OgreParticle.h"
    00029 #include "OgreException.h"
    00030
    00031
    00032 namespace Ogre {
    00033
    00034     // init statics
    00035     ColourImageAffector::CmdImageAdjust     ColourImageAffector::msImageCmd;
    00036
    00037     //-----------------------------------------------------------------------
    00038     ColourImageAffector::ColourImageAffector()
    00039     {
    00040         mType = "ColourImage";
    00041
    00042         // Init parameters
    00043         if (createParamDictionary("ColourImageAffector"))
    00044         {
    00045             ParamDictionary* dict = getParamDictionary();
    00046
    00047             dict->addParameter(ParameterDef("image", "image where the colours come from", PT_STRING), &msImageCmd);
    00048         }
    00049     }
    00050     //-----------------------------------------------------------------------
    00051     void ColourImageAffector::_initParticle(Particle* pParticle)
    00052     {
    00053         const uchar*        data            = mColourImage.getData();
    00054         const Real          div_255         = 1.0f / 255.f;
    00055
    00056         pParticle->mColour.r = data[0] * div_255;
    00057         pParticle->mColour.g = data[1] * div_255;
    00058         pParticle->mColour.b = data[2] * div_255;
    00059         pParticle->mColour.a = data[3] * div_255;
    00060
    00061     }
    00062     //-----------------------------------------------------------------------
    00063     void ColourImageAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
    00064     {
    00065         Particle*           p;
    00066         ParticleIterator    pi              = pSystem->_getIterator();
    00067
    00068         Real                width           = mColourImage.getWidth()  - 1;
    00069         Real                height          = mColourImage.getHeight() - 1;
    00070         const uchar*        data            = mColourImage.getData();
    00071
    00072         while (!pi.end())
    00073         {
    00074             p = pi.getNext();
    00075             const Real      life_time       = p->mTotalTimeToLive;
    00076             Real            particle_time   = 1.0f - (p->mTimeToLive / life_time);
    00077
    00078             if (particle_time > 1.0f)
    00079                 particle_time = 1.0f;
    00080             if (particle_time < 0.0f)
    00081                 particle_time = 0.0f;
    00082
    00083             const Real      float_index     = particle_time * width;
    00084             const int       index           = (int)float_index;
    00085             const int       position        = index * 4;
    00086             const Real      div_255         = 1.0f / 255.f;
    00087
    00088             if (index <= 0 || index >= width)
    00089             {
    00090                 p->mColour.r = (data[position + 0] * div_255);
    00091                 p->mColour.g = (data[position + 1] * div_255);
    00092                 p->mColour.b = (data[position + 2] * div_255);
    00093                 p->mColour.a = (data[position + 3] * div_255);
    00094             } else
    00095             {
    00096                 const Real      fract       = float_index - (Real)index;
    00097                 const Real      to_color    = fract * div_255;
    00098                 const Real      from_color  = (div_255 - to_color);
    00099
    00100                 p->mColour.r = (data[position + 0] * from_color) + (data[position + 4] * to_color);
    00101                 p->mColour.g = (data[position + 1] * from_color) + (data[position + 5] * to_color);
    00102                 p->mColour.b = (data[position + 2] * from_color) + (data[position + 6] * to_color);
    00103                 p->mColour.a = (data[position + 3] * from_color) + (data[position + 7] * to_color);
    00104             }
    00105         }
    00106     }
    00107
    00108     //-----------------------------------------------------------------------
    00109     void ColourImageAffector::setImageAdjust(String name)
    00110     {
    00111         mColourImageName = name;
    00112         mColourImage.load(name);
    00113
    00114         PixelFormat format = mColourImage.getFormat();
    00115
    00116         if ( format != PF_A8R8G8B8 )
    00117         {
    00118             Except( Exception::ERR_INVALIDPARAMS, "Error: Image is not a rgba image.",
    00119                     "ColourImageAffector::setImageAdjust" );
    00120         }
    00121     }
    00122     //-----------------------------------------------------------------------
    00123     String ColourImageAffector::getImageAdjust(void) const
    00124     {
    00125         return mColourImageName;
    00126     }
    00127
    00128
    00129     //-----------------------------------------------------------------------
    00130     //-----------------------------------------------------------------------
    00131     //-----------------------------------------------------------------------
    00132     // Command objects
    00133     //-----------------------------------------------------------------------
    00134     //-----------------------------------------------------------------------
    00135     String ColourImageAffector::CmdImageAdjust::doGet(const void* target) const
    00136     {
    00137         return static_cast<const ColourImageAffector*>(target)->getImageAdjust();
    00138     }
    00139     void ColourImageAffector::CmdImageAdjust::doSet(void* target, const String& val)
    00140     {
    00141         static_cast<ColourImageAffector*>(target)->setImageAdjust(val);
    00142     }
    00143
    00144 }
    00145
    00146
    00147

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:17 2004