source: OGRE/trunk/ogrenew/PlugIns/ParticleFX/src/OgreColourInterpolatorAffector.cpp @ 690

Revision 690, 6.4 KB checked in by mattausch, 18 years ago (diff)

added ogre 1.07 main

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4        (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreColourInterpolatorAffector.h"
26#include "OgreParticleSystem.h"
27#include "OgreStringConverter.h"
28#include "OgreParticle.h"
29
30
31namespace Ogre {
32   
33    // init statics
34    ColourInterpolatorAffector::CmdColourAdjust         ColourInterpolatorAffector::msColourCmd[MAX_STAGES];
35    ColourInterpolatorAffector::CmdTimeAdjust           ColourInterpolatorAffector::msTimeCmd[MAX_STAGES];
36
37    //-----------------------------------------------------------------------
38    ColourInterpolatorAffector::ColourInterpolatorAffector(ParticleSystem* psys)
39        : ParticleAffector(psys)
40    {
41                for (int i=0;i<MAX_STAGES;i++)
42                {
43                        // set default colour to transparent grey, transparent since we might not want to display the particle here
44                        // grey because when a colour component is 0.5f the maximum difference to another colour component is 0.5f
45                        mColourAdj[i]   = ColourValue(0.5f, 0.5f, 0.5f, 0.0f);
46                        mTimeAdj[i]             = 1.0f;
47                }
48
49        mType = "ColourInterpolator";
50
51        // Init parameters
52        if (createParamDictionary("ColourInterpolatorAffector"))
53        {
54            ParamDictionary* dict = getParamDictionary();
55
56                        for (int i=0;i<MAX_STAGES;i++)
57                        {
58                                msColourCmd[i].mIndex   = i;
59                                msTimeCmd[i].mIndex             = i;
60
61                                StringUtil::StrStreamType stage;
62                                stage << i;
63                                String  colour_title    = String("colour") + stage.str();
64                                String  time_title              = String("time") + stage.str();
65                                String  colour_descr    = String("Stage ") + stage.str() + String(" colour.");
66                                String  time_descr              = String("Stage ") + stage.str() + String(" time.");
67
68                                dict->addParameter(ParameterDef(colour_title, colour_descr, PT_COLOURVALUE), &msColourCmd[i]);
69                                dict->addParameter(ParameterDef(time_title,   time_descr,   PT_REAL),            &msTimeCmd[i]);
70                        }
71        }
72    }
73    //-----------------------------------------------------------------------
74    void ColourInterpolatorAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed)
75    {
76        Particle*                       p;
77                ParticleIterator        pi                              = pSystem->_getIterator();
78
79
80                while (!pi.end())
81        {
82            p = pi.getNext();
83                        const Real              life_time               = p->totalTimeToLive;
84                        Real                    particle_time   = 1.0f - (p->timeToLive / life_time);
85
86                        if (particle_time <= mTimeAdj[0])
87                        {
88                                p->colour = mColourAdj[0];
89                        } else
90                        if (particle_time >= mTimeAdj[MAX_STAGES - 1])
91                        {
92                                p->colour = mColourAdj[MAX_STAGES-1];
93                        } else
94                        {
95                                for (int i=0;i<MAX_STAGES-1;i++)
96                                {
97                                        if (particle_time >= mTimeAdj[i] && particle_time < mTimeAdj[i + 1])
98                                        {
99                                                particle_time -= mTimeAdj[i];
100                                                particle_time /= (mTimeAdj[i+1]-mTimeAdj[i]);
101                                                p->colour.r = ((mColourAdj[i+1].r * particle_time) + (mColourAdj[i].r * (1.0f - particle_time)));
102                                                p->colour.g = ((mColourAdj[i+1].g * particle_time) + (mColourAdj[i].g * (1.0f - particle_time)));
103                                                p->colour.b = ((mColourAdj[i+1].b * particle_time) + (mColourAdj[i].b * (1.0f - particle_time)));
104                                                p->colour.a = ((mColourAdj[i+1].a * particle_time) + (mColourAdj[i].a * (1.0f - particle_time)));
105                                                break;
106                                        }
107                                }
108                        }
109                }
110    }
111   
112        //-----------------------------------------------------------------------
113    void ColourInterpolatorAffector::setColourAdjust(size_t index, ColourValue colour)
114    {
115        mColourAdj[index] = colour;
116    }
117    //-----------------------------------------------------------------------
118    ColourValue ColourInterpolatorAffector::getColourAdjust(size_t index) const
119    {
120        return mColourAdj[index];
121    }
122
123
124    //-----------------------------------------------------------------------
125    void ColourInterpolatorAffector::setTimeAdjust(size_t index, Real time)
126    {
127        mTimeAdj[index] = time;
128    }
129    //-----------------------------------------------------------------------
130    Real ColourInterpolatorAffector::getTimeAdjust(size_t index) const
131    {
132        return mTimeAdj[index];
133    }
134   
135       
136        //-----------------------------------------------------------------------
137    //-----------------------------------------------------------------------
138    //-----------------------------------------------------------------------
139    // Command objects
140    //-----------------------------------------------------------------------
141    //-----------------------------------------------------------------------
142    String ColourInterpolatorAffector::CmdColourAdjust::doGet(const void* target) const
143    {
144        return StringConverter::toString(
145            static_cast<const ColourInterpolatorAffector*>(target)->getColourAdjust(mIndex) );
146    }
147    void ColourInterpolatorAffector::CmdColourAdjust::doSet(void* target, const String& val)
148    {
149        static_cast<ColourInterpolatorAffector*>(target)->setColourAdjust(mIndex,
150            StringConverter::parseColourValue(val));
151    }
152        //-----------------------------------------------------------------------
153    String ColourInterpolatorAffector::CmdTimeAdjust::doGet(const void* target) const
154    {
155        return StringConverter::toString(
156            static_cast<const ColourInterpolatorAffector*>(target)->getTimeAdjust(mIndex) );
157    }
158    void ColourInterpolatorAffector::CmdTimeAdjust::doSet(void* target, const String& val)
159    {
160        static_cast<ColourInterpolatorAffector*>(target)->setTimeAdjust(mIndex,
161            StringConverter::parseReal(val));
162    }
163
164}
165
166
167
Note: See TracBrowser for help on using the repository browser.