source: OGRE/trunk/ogrenew/OgreMain/src/OgreControllerManager.cpp @ 692

Revision 692, 11.4 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 "OgreStableHeaders.h"
26#include "OgreControllerManager.h"
27
28#include "OgreLogManager.h"
29#include "OgreTextureUnitState.h"
30#include "OgreRoot.h"
31
32namespace Ogre {
33    //-----------------------------------------------------------------------
34    template<> ControllerManager* Singleton<ControllerManager>::ms_Singleton = 0;
35    ControllerManager* ControllerManager::getSingletonPtr(void)
36    {
37        return ms_Singleton;
38    }
39    ControllerManager& ControllerManager::getSingleton(void)
40    { 
41        assert( ms_Singleton );  return ( *ms_Singleton ); 
42    }
43    //-----------------------------------------------------------------------
44    ControllerManager::ControllerManager()
45                : mFrameTimeController(new FrameTimeControllerValue())
46                , mPassthroughFunction(new PassthroughControllerFunction())
47                , mLastFrameNumber(0)
48    {
49
50    }
51    //-----------------------------------------------------------------------
52    ControllerManager::~ControllerManager()
53    {
54        clearControllers();
55    }
56    //-----------------------------------------------------------------------
57    Controller<Real>* ControllerManager::createController(
58        const ControllerValueRealPtr& src, const ControllerValueRealPtr& dest,
59        const ControllerFunctionRealPtr& func)
60    {
61        Controller<Real>* c = new Controller<Real>(src, dest, func);
62
63        mControllers.insert(c);
64        return c;
65    }
66    //-----------------------------------------------------------------------
67    Controller<Real>* ControllerManager::createFrameTimePassthroughController(
68        const ControllerValueRealPtr& dest)
69    {
70        return createController(getFrameTimeSource(), dest, getPassthroughControllerFunction());
71    }
72    //-----------------------------------------------------------------------
73    void ControllerManager::updateAllControllers(void)
74    {
75        // Only update once per frame
76        unsigned long thisFrameNumber = Root::getSingleton().getCurrentFrameNumber();
77        if (thisFrameNumber != mLastFrameNumber)
78        {
79            ControllerList::const_iterator ci;
80            for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
81            {
82                (*ci)->update();
83            }
84            mLastFrameNumber = thisFrameNumber;
85        }
86    }
87    //-----------------------------------------------------------------------
88    void ControllerManager::clearControllers(void)
89    {
90        ControllerList::iterator ci;
91        for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
92        {
93            delete *ci;
94        }
95        mControllers.clear();
96    }
97    //-----------------------------------------------------------------------
98    const ControllerValueRealPtr& ControllerManager::getFrameTimeSource(void) const
99    {
100        return mFrameTimeController;
101    }
102        //-----------------------------------------------------------------------
103        const ControllerFunctionRealPtr& ControllerManager::getPassthroughControllerFunction(void) const
104        {
105                return mPassthroughFunction;
106        }
107    //-----------------------------------------------------------------------
108    Controller<Real>* ControllerManager::createTextureAnimator(TextureUnitState* layer, Real sequenceTime)
109    {
110        SharedPtr< ControllerValue<Real> > texVal(new TextureFrameControllerValue(layer));
111        SharedPtr< ControllerFunction<Real> > animFunc(new AnimationControllerFunction(sequenceTime));
112
113        return createController(mFrameTimeController, texVal, animFunc);
114    }
115    //-----------------------------------------------------------------------
116    Controller<Real>* ControllerManager::createTextureUVScroller(TextureUnitState* layer, Real speed)
117    {
118                Controller<Real>* ret = 0;
119
120                if (speed != 0)
121        {
122                        SharedPtr< ControllerValue<Real> > val;
123                        SharedPtr< ControllerFunction<Real> > func;
124
125                        // We do both scrolls with a single controller
126                        val.bind(new TexCoordModifierControllerValue(layer, true, true));
127                        // Create function: use -speed since we're altering texture coords so they have reverse effect
128            func.bind(new ScaleControllerFunction(-speed, true));
129            ret = createController(mFrameTimeController, val, func);
130                }
131
132                return ret;
133        }
134    //-----------------------------------------------------------------------
135    Controller<Real>* ControllerManager::createTextureUScroller(TextureUnitState* layer, Real uSpeed)
136    {
137        Controller<Real>* ret = 0;
138
139        if (uSpeed != 0)
140        {
141                        SharedPtr< ControllerValue<Real> > uVal;
142                        SharedPtr< ControllerFunction<Real> > uFunc;
143
144                uVal.bind(new TexCoordModifierControllerValue(layer, true));
145            // Create function: use -speed since we're altering texture coords so they have reverse effect
146            uFunc.bind(new ScaleControllerFunction(-uSpeed, true));
147            ret = createController(mFrameTimeController, uVal, uFunc);
148        }
149
150        return ret;
151    }
152        //-----------------------------------------------------------------------
153    Controller<Real>* ControllerManager::createTextureVScroller(TextureUnitState* layer, Real vSpeed)
154    {
155                Controller<Real>* ret = 0;
156
157                if (vSpeed != 0)
158        {
159                        SharedPtr< ControllerValue<Real> > vVal;
160                        SharedPtr< ControllerFunction<Real> > vFunc;
161
162            // Set up a second controller for v scroll
163            vVal.bind(new TexCoordModifierControllerValue(layer, false, true));
164            // Create function: use -speed since we're altering texture coords so they have reverse effect
165            vFunc.bind(new ScaleControllerFunction(-vSpeed, true));
166            ret = createController(mFrameTimeController, vVal, vFunc);
167        }
168
169        return ret;
170    }
171    //-----------------------------------------------------------------------
172    Controller<Real>* ControllerManager::createTextureRotater(TextureUnitState* layer, Real speed)
173    {
174        SharedPtr< ControllerValue<Real> > val;
175        SharedPtr< ControllerFunction<Real> > func;
176
177        // Target value is texture coord rotation
178        val.bind(new TexCoordModifierControllerValue(layer, false, false, false, false, true));
179        // Function is simple scale (seconds * speed)
180        // Use -speed since altering texture coords has the reverse visible effect
181        func.bind(new ScaleControllerFunction(-speed, true));
182
183        return createController(mFrameTimeController, val, func);
184
185    }
186    //-----------------------------------------------------------------------
187    Controller<Real>* ControllerManager::createTextureWaveTransformer(TextureUnitState* layer,
188        TextureUnitState::TextureTransformType ttype, WaveformType waveType, Real base, Real frequency, Real phase, Real amplitude)
189    {
190        SharedPtr< ControllerValue<Real> > val;
191        SharedPtr< ControllerFunction<Real> > func;
192
193        switch (ttype)
194        {
195        case TextureUnitState::TT_TRANSLATE_U:
196            // Target value is a u scroll
197            val.bind(new TexCoordModifierControllerValue(layer, true));
198            break;
199        case TextureUnitState::TT_TRANSLATE_V:
200            // Target value is a v scroll
201            val.bind(new TexCoordModifierControllerValue(layer, false, true));
202            break;
203        case TextureUnitState::TT_SCALE_U:
204            // Target value is a u scale
205            val.bind(new TexCoordModifierControllerValue(layer, false, false, true));
206            break;
207        case TextureUnitState::TT_SCALE_V:
208            // Target value is a v scale
209            val.bind(new TexCoordModifierControllerValue(layer, false, false, false, true));
210            break;
211        case TextureUnitState::TT_ROTATE:
212            // Target value is texture coord rotation
213            val.bind(new TexCoordModifierControllerValue(layer, false, false, false, false, true));
214            break;
215        }
216        // Create new wave function for alterations
217        func.bind(new WaveformControllerFunction(waveType, base, frequency, phase, amplitude, true));
218
219        return createController(mFrameTimeController, val, func);
220    }
221    //-----------------------------------------------------------------------
222    Controller<Real>* ControllerManager::createGpuProgramTimerParam(
223        GpuProgramParameters* params, size_t paramIndex, Real timeFactor)
224    {
225        SharedPtr< ControllerValue<Real> > val;
226        SharedPtr< ControllerFunction<Real> > func;
227
228        val.bind(new FloatGpuParameterControllerValue(params, paramIndex));
229        func.bind(new ScaleControllerFunction(timeFactor, true));
230
231        return createController(mFrameTimeController, val, func);
232
233    }
234    //-----------------------------------------------------------------------
235    void ControllerManager::destroyController(Controller<Real>* controller)
236    {
237        ControllerList::iterator i = mControllers.find(controller);
238        if (i != mControllers.end())
239        {
240            mControllers.erase(i);
241            delete controller;
242        }
243    }
244        //-----------------------------------------------------------------------
245        Real ControllerManager::getTimeFactor(void) const {
246                return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getTimeFactor();
247        }
248        //-----------------------------------------------------------------------
249        void ControllerManager::setTimeFactor(Real tf) {
250                static_cast<FrameTimeControllerValue*>(mFrameTimeController.getPointer())->setTimeFactor(tf);
251        }
252        //-----------------------------------------------------------------------
253        Real ControllerManager::getFrameDelay(void) const {
254                return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getFrameDelay();
255        }
256        //-----------------------------------------------------------------------
257        void ControllerManager::setFrameDelay(Real fd) {
258                static_cast<FrameTimeControllerValue*>(mFrameTimeController.getPointer())->setFrameDelay(fd);
259        }
260        //-----------------------------------------------------------------------
261        Real ControllerManager::getElapsedTime(void) const
262        {
263                return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getElapsedTime();
264        }
265        //-----------------------------------------------------------------------
266        void ControllerManager::setElapsedTime(Real elapsedTime)
267        {
268                static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setElapsedTime(elapsedTime);
269        }
270}
Note: See TracBrowser for help on using the repository browser.