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-2013 Torus Knot Software Ltd 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ----------------------------------------------------------------------------- 27 */ 28 #include "OgreColourImageAffector.h" 29 #include "OgreParticleSystem.h" 30 #include "OgreStringConverter.h" 31 #include "OgreParticle.h" 32 #include "OgreException.h" 33 #include "OgreResourceGroupManager.h" 34 35 namespace Ogre { 36 37 // init statics 38 ColourImageAffector::CmdImageAdjust ColourImageAffector::msImageCmd; 39 40 //----------------------------------------------------------------------- ColourImageAffector(ParticleSystem * psys)41 ColourImageAffector::ColourImageAffector(ParticleSystem* psys) 42 :ParticleAffector(psys), mColourImageLoaded(false) 43 { 44 mType = "ColourImage"; 45 46 // Init parameters 47 if (createParamDictionary("ColourImageAffector")) 48 { 49 ParamDictionary* dict = getParamDictionary(); 50 51 dict->addParameter(ParameterDef("image", "image where the colours come from", PT_STRING), &msImageCmd); 52 } 53 } 54 //----------------------------------------------------------------------- _initParticle(Particle * pParticle)55 void ColourImageAffector::_initParticle(Particle* pParticle) 56 { 57 if (!mColourImageLoaded) 58 { 59 _loadImage(); 60 } 61 62 pParticle->colour = mColourImage.getColourAt(0, 0, 0); 63 64 } 65 //----------------------------------------------------------------------- _affectParticles(ParticleSystem * pSystem,Real timeElapsed)66 void ColourImageAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed) 67 { 68 Particle* p; 69 ParticleIterator pi = pSystem->_getIterator(); 70 71 if (!mColourImageLoaded) 72 { 73 _loadImage(); 74 } 75 76 int width = (int)mColourImage.getWidth() - 1; 77 78 while (!pi.end()) 79 { 80 p = pi.getNext(); 81 const Real life_time = p->totalTimeToLive; 82 Real particle_time = 1.0f - (p->timeToLive / life_time); 83 84 if (particle_time > 1.0f) 85 particle_time = 1.0f; 86 if (particle_time < 0.0f) 87 particle_time = 0.0f; 88 89 const Real float_index = particle_time * width; 90 const int index = (int)float_index; 91 92 if(index < 0) 93 { 94 p->colour = mColourImage.getColourAt(0, 0, 0); 95 } 96 else if(index >= width) 97 { 98 p->colour = mColourImage.getColourAt(width, 0, 0); 99 } 100 else 101 { 102 // Linear interpolation 103 const Real fract = float_index - (Real)index; 104 const Real to_colour = fract; 105 const Real from_colour = 1.0f - to_colour; 106 107 ColourValue from=mColourImage.getColourAt(index, 0, 0), 108 to=mColourImage.getColourAt(index+1, 0, 0); 109 110 p->colour.r = from.r*from_colour + to.r*to_colour; 111 p->colour.g = from.g*from_colour + to.g*to_colour; 112 p->colour.b = from.b*from_colour + to.b*to_colour; 113 p->colour.a = from.a*from_colour + to.a*to_colour; 114 } 115 } 116 } 117 118 //----------------------------------------------------------------------- setImageAdjust(String name)119 void ColourImageAffector::setImageAdjust(String name) 120 { 121 mColourImageName = name; 122 mColourImageLoaded = false; 123 } 124 //----------------------------------------------------------------------- _loadImage(void)125 void ColourImageAffector::_loadImage(void) 126 { 127 mColourImage.load(mColourImageName, mParent->getResourceGroupName()); 128 129 PixelFormat format = mColourImage.getFormat(); 130 131 if ( !PixelUtil::isAccessible(format) ) 132 { 133 OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Error: Image is not accessible (rgba) image.", 134 "ColourImageAffector::_loadImage" ); 135 } 136 137 mColourImageLoaded = true; 138 } 139 //----------------------------------------------------------------------- getImageAdjust(void) const140 String ColourImageAffector::getImageAdjust(void) const 141 { 142 return mColourImageName; 143 } 144 145 146 //----------------------------------------------------------------------- 147 //----------------------------------------------------------------------- 148 //----------------------------------------------------------------------- 149 // Command objects 150 //----------------------------------------------------------------------- 151 //----------------------------------------------------------------------- doGet(const void * target) const152 String ColourImageAffector::CmdImageAdjust::doGet(const void* target) const 153 { 154 return static_cast<const ColourImageAffector*>(target)->getImageAdjust(); 155 } doSet(void * target,const String & val)156 void ColourImageAffector::CmdImageAdjust::doSet(void* target, const String& val) 157 { 158 static_cast<ColourImageAffector*>(target)->setImageAdjust(val); 159 } 160 161 } 162 163 164 165