source: OGRE/trunk/ogrenew/PlugIns/ParticleFX/src/OgreColourImageAffector.cpp @ 692

Revision 692, 5.5 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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