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