| 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 "OgreColourImageAffector.h" | 
|---|
| 30 | #include "OgreParticleSystem.h" | 
|---|
| 31 | #include "OgreStringConverter.h" | 
|---|
| 32 | #include "OgreParticle.h" | 
|---|
| 33 | #include "OgreException.h" | 
|---|
| 34 | #include "OgreResourceGroupManager.h" | 
|---|
| 35 |  | 
|---|
| 36 | namespace Ogre { | 
|---|
| 37 |      | 
|---|
| 38 |     // init statics | 
|---|
| 39 |         ColourImageAffector::CmdImageAdjust             ColourImageAffector::msImageCmd; | 
|---|
| 40 |  | 
|---|
| 41 |     //----------------------------------------------------------------------- | 
|---|
| 42 |     ColourImageAffector::ColourImageAffector(ParticleSystem* psys) | 
|---|
| 43 |         :ParticleAffector(psys), mColourImageLoaded(false) | 
|---|
| 44 |     { | 
|---|
| 45 |         mType = "ColourImage"; | 
|---|
| 46 |  | 
|---|
| 47 |         // Init parameters | 
|---|
| 48 |         if (createParamDictionary("ColourImageAffector")) | 
|---|
| 49 |         { | 
|---|
| 50 |             ParamDictionary* dict = getParamDictionary(); | 
|---|
| 51 |  | 
|---|
| 52 |                         dict->addParameter(ParameterDef("image", "image where the colours come from", PT_STRING), &msImageCmd); | 
|---|
| 53 |         } | 
|---|
| 54 |     } | 
|---|
| 55 |     //----------------------------------------------------------------------- | 
|---|
| 56 |     void ColourImageAffector::_initParticle(Particle* pParticle) | 
|---|
| 57 |         { | 
|---|
| 58 |         if (!mColourImageLoaded) | 
|---|
| 59 |         { | 
|---|
| 60 |             _loadImage(); | 
|---|
| 61 |         } | 
|---|
| 62 |  | 
|---|
| 63 |         pParticle->colour = mColourImage.getColourAt(0, 0, 0); | 
|---|
| 64 |      | 
|---|
| 65 |         } | 
|---|
| 66 |     //----------------------------------------------------------------------- | 
|---|
| 67 |     void ColourImageAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed) | 
|---|
| 68 |     { | 
|---|
| 69 |         Particle*                       p; | 
|---|
| 70 |                 ParticleIterator        pi                              = pSystem->_getIterator(); | 
|---|
| 71 |  | 
|---|
| 72 |         if (!mColourImageLoaded) | 
|---|
| 73 |         { | 
|---|
| 74 |             _loadImage(); | 
|---|
| 75 |         } | 
|---|
| 76 |  | 
|---|
| 77 |                 int                                width                        = mColourImage.getWidth()  - 1; | 
|---|
| 78 |          | 
|---|
| 79 |                 while (!pi.end()) | 
|---|
| 80 |                 { | 
|---|
| 81 |                         p = pi.getNext(); | 
|---|
| 82 |                         const Real              life_time               = p->totalTimeToLive; | 
|---|
| 83 |                         Real                    particle_time   = 1.0f - (p->timeToLive / life_time);  | 
|---|
| 84 |  | 
|---|
| 85 |                         if (particle_time > 1.0f) | 
|---|
| 86 |                                 particle_time = 1.0f; | 
|---|
| 87 |                         if (particle_time < 0.0f) | 
|---|
| 88 |                                 particle_time = 0.0f; | 
|---|
| 89 |  | 
|---|
| 90 |                         const Real              float_index             = particle_time * width; | 
|---|
| 91 |                         const int               index                   = (int)float_index; | 
|---|
| 92 |  | 
|---|
| 93 |             if(index < 0) | 
|---|
| 94 |             { | 
|---|
| 95 |                                 p->colour = mColourImage.getColourAt(0, 0, 0); | 
|---|
| 96 |             } | 
|---|
| 97 |             else if(index >= width)  | 
|---|
| 98 |             { | 
|---|
| 99 |                 p->colour = mColourImage.getColourAt(width, 0, 0); | 
|---|
| 100 |             } | 
|---|
| 101 |             else | 
|---|
| 102 |             { | 
|---|
| 103 |                 // Linear interpolation | 
|---|
| 104 |                                 const Real              fract           = float_index - (Real)index; | 
|---|
| 105 |                                 const Real              to_colour       = fract; | 
|---|
| 106 |                                 const Real              from_colour     = 1.0f - to_colour; | 
|---|
| 107 |               | 
|---|
| 108 |                 ColourValue from=mColourImage.getColourAt(index, 0, 0), | 
|---|
| 109 |                                                         to=mColourImage.getColourAt(index+1, 0, 0); | 
|---|
| 110 |  | 
|---|
| 111 |                                 p->colour.r = from.r*from_colour + to.r*to_colour; | 
|---|
| 112 |                 p->colour.g = from.g*from_colour + to.g*to_colour; | 
|---|
| 113 |                 p->colour.b = from.b*from_colour + to.b*to_colour; | 
|---|
| 114 |                 p->colour.a = from.a*from_colour + to.a*to_colour; | 
|---|
| 115 |                         } | 
|---|
| 116 |                 } | 
|---|
| 117 |     } | 
|---|
| 118 |      | 
|---|
| 119 |     //----------------------------------------------------------------------- | 
|---|
| 120 |     void ColourImageAffector::setImageAdjust(String name) | 
|---|
| 121 |     { | 
|---|
| 122 |                 mColourImageName = name; | 
|---|
| 123 |         mColourImageLoaded = false; | 
|---|
| 124 |         } | 
|---|
| 125 |     //----------------------------------------------------------------------- | 
|---|
| 126 |     void ColourImageAffector::_loadImage(void) | 
|---|
| 127 |     { | 
|---|
| 128 |         mColourImage.load(mColourImageName, mParent->getResourceGroupName()); | 
|---|
| 129 |  | 
|---|
| 130 |                 PixelFormat     format = mColourImage.getFormat(); | 
|---|
| 131 |  | 
|---|
| 132 |                 if ( !PixelUtil::isAccessible(format) ) | 
|---|
| 133 |                 { | 
|---|
| 134 |                         OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Error: Image is not accessible (rgba) image.", | 
|---|
| 135 |                                         "ColourImageAffector::_loadImage" ); | 
|---|
| 136 |                 } | 
|---|
| 137 |  | 
|---|
| 138 |         mColourImageLoaded = true; | 
|---|
| 139 |         } | 
|---|
| 140 |     //----------------------------------------------------------------------- | 
|---|
| 141 |     String ColourImageAffector::getImageAdjust(void) const | 
|---|
| 142 |     { | 
|---|
| 143 |         return mColourImageName; | 
|---|
| 144 |     } | 
|---|
| 145 |      | 
|---|
| 146 |          | 
|---|
| 147 |         //----------------------------------------------------------------------- | 
|---|
| 148 |     //----------------------------------------------------------------------- | 
|---|
| 149 |     //----------------------------------------------------------------------- | 
|---|
| 150 |     // Command objects | 
|---|
| 151 |     //----------------------------------------------------------------------- | 
|---|
| 152 |     //----------------------------------------------------------------------- | 
|---|
| 153 |     String ColourImageAffector::CmdImageAdjust::doGet(const void* target) const | 
|---|
| 154 |     { | 
|---|
| 155 |         return static_cast<const ColourImageAffector*>(target)->getImageAdjust(); | 
|---|
| 156 |     } | 
|---|
| 157 |     void ColourImageAffector::CmdImageAdjust::doSet(void* target, const String& val) | 
|---|
| 158 |     { | 
|---|
| 159 |         static_cast<ColourImageAffector*>(target)->setImageAdjust(val); | 
|---|
| 160 |     } | 
|---|
| 161 |  | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|