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-2005 The OGRE Team
|
---|
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 | */
|
---|
25 | #include "OgreStableHeaders.h"
|
---|
26 | #include "OgreControllerManager.h"
|
---|
27 |
|
---|
28 | #include "OgreLogManager.h"
|
---|
29 | #include "OgreTextureUnitState.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | namespace 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 | {
|
---|
47 |
|
---|
48 | }
|
---|
49 | //-----------------------------------------------------------------------
|
---|
50 | ControllerManager::~ControllerManager()
|
---|
51 | {
|
---|
52 | clearControllers();
|
---|
53 | }
|
---|
54 | //-----------------------------------------------------------------------
|
---|
55 | Controller<Real>* ControllerManager::createController(
|
---|
56 | const ControllerValueRealPtr& src, const ControllerValueRealPtr& dest,
|
---|
57 | const ControllerFunctionRealPtr& func)
|
---|
58 | {
|
---|
59 | Controller<Real>* c = new Controller<Real>(src, dest, func);
|
---|
60 |
|
---|
61 | mControllers.insert(c);
|
---|
62 | return c;
|
---|
63 | }
|
---|
64 | //-----------------------------------------------------------------------
|
---|
65 | void ControllerManager::updateAllControllers(void)
|
---|
66 | {
|
---|
67 | ControllerList::const_iterator ci;
|
---|
68 | for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
|
---|
69 | {
|
---|
70 | (*ci)->update();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | //-----------------------------------------------------------------------
|
---|
74 | void ControllerManager::clearControllers(void)
|
---|
75 | {
|
---|
76 | ControllerList::iterator ci;
|
---|
77 | for (ci = mControllers.begin(); ci != mControllers.end(); ++ci)
|
---|
78 | {
|
---|
79 | delete *ci;
|
---|
80 | }
|
---|
81 | mControllers.clear();
|
---|
82 | }
|
---|
83 | //-----------------------------------------------------------------------
|
---|
84 | const SharedPtr< ControllerValue<Real> >& ControllerManager::getFrameTimeSource(void) const
|
---|
85 | {
|
---|
86 | return mFrameTimeController;
|
---|
87 | }
|
---|
88 | //-----------------------------------------------------------------------
|
---|
89 | Controller<Real>* ControllerManager::createTextureAnimator(TextureUnitState* layer, Real sequenceTime)
|
---|
90 | {
|
---|
91 | SharedPtr< ControllerValue<Real> > texVal(new TextureFrameControllerValue(layer));
|
---|
92 | SharedPtr< ControllerFunction<Real> > animFunc(new AnimationControllerFunction(sequenceTime));
|
---|
93 |
|
---|
94 | return createController(mFrameTimeController, texVal, animFunc);
|
---|
95 | }
|
---|
96 | //-----------------------------------------------------------------------
|
---|
97 | Controller<Real>* ControllerManager::createTextureScroller(TextureUnitState* layer, Real uSpeed, Real vSpeed)
|
---|
98 | {
|
---|
99 | Controller<Real>* ret = 0;
|
---|
100 |
|
---|
101 | // Set up 1 or 2 controllers to manage the scrolling texture
|
---|
102 | if (uSpeed != 0)
|
---|
103 | {
|
---|
104 | SharedPtr< ControllerValue<Real> > uVal;
|
---|
105 | SharedPtr< ControllerFunction<Real> > uFunc;
|
---|
106 |
|
---|
107 | if (uSpeed == vSpeed)
|
---|
108 | {
|
---|
109 | // Cool, we can do both scrolls with a single controller
|
---|
110 | uVal.bind(new TexCoordModifierControllerValue(layer, true, true));
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | // Just do u, v will take a second controller
|
---|
115 | uVal.bind(new TexCoordModifierControllerValue(layer, true));
|
---|
116 | }
|
---|
117 | // Create function: use -speed since we're altering texture coords so they have reverse effect
|
---|
118 | uFunc.bind(new ScaleControllerFunction(-uSpeed, true));
|
---|
119 | ret = createController(mFrameTimeController, uVal, uFunc);
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (vSpeed != 0 && (uSpeed == 0 || vSpeed != uSpeed))
|
---|
123 | {
|
---|
124 | SharedPtr< ControllerValue<Real> > vVal;
|
---|
125 | SharedPtr< ControllerFunction<Real> > vFunc;
|
---|
126 |
|
---|
127 | // Set up a second controller for v scroll
|
---|
128 | vVal.bind(new TexCoordModifierControllerValue(layer, false, true));
|
---|
129 | // Create function: use -speed since we're altering texture coords so they have reverse effect
|
---|
130 | vFunc.bind(new ScaleControllerFunction(-vSpeed, true));
|
---|
131 | ret = createController(mFrameTimeController, vVal, vFunc);
|
---|
132 | }
|
---|
133 |
|
---|
134 | return ret;
|
---|
135 | }
|
---|
136 | //-----------------------------------------------------------------------
|
---|
137 | Controller<Real>* ControllerManager::createTextureRotater(TextureUnitState* layer, Real speed)
|
---|
138 | {
|
---|
139 | SharedPtr< ControllerValue<Real> > val;
|
---|
140 | SharedPtr< ControllerFunction<Real> > func;
|
---|
141 |
|
---|
142 | // Target value is texture coord rotation
|
---|
143 | val.bind(new TexCoordModifierControllerValue(layer, false, false, false, false, true));
|
---|
144 | // Function is simple scale (seconds * speed)
|
---|
145 | // Use -speed since altering texture coords has the reverse visible effect
|
---|
146 | func.bind(new ScaleControllerFunction(-speed, true));
|
---|
147 |
|
---|
148 | return createController(mFrameTimeController, val, func);
|
---|
149 |
|
---|
150 | }
|
---|
151 | //-----------------------------------------------------------------------
|
---|
152 | Controller<Real>* ControllerManager::createTextureWaveTransformer(TextureUnitState* layer,
|
---|
153 | TextureUnitState::TextureTransformType ttype, WaveformType waveType, Real base, Real frequency, Real phase, Real amplitude)
|
---|
154 | {
|
---|
155 | SharedPtr< ControllerValue<Real> > val;
|
---|
156 | SharedPtr< ControllerFunction<Real> > func;
|
---|
157 |
|
---|
158 | switch (ttype)
|
---|
159 | {
|
---|
160 | case TextureUnitState::TT_TRANSLATE_U:
|
---|
161 | // Target value is a u scroll
|
---|
162 | val.bind(new TexCoordModifierControllerValue(layer, true));
|
---|
163 | break;
|
---|
164 | case TextureUnitState::TT_TRANSLATE_V:
|
---|
165 | // Target value is a v scroll
|
---|
166 | val.bind(new TexCoordModifierControllerValue(layer, false, true));
|
---|
167 | break;
|
---|
168 | case TextureUnitState::TT_SCALE_U:
|
---|
169 | // Target value is a u scale
|
---|
170 | val.bind(new TexCoordModifierControllerValue(layer, false, false, true));
|
---|
171 | break;
|
---|
172 | case TextureUnitState::TT_SCALE_V:
|
---|
173 | // Target value is a v scale
|
---|
174 | val.bind(new TexCoordModifierControllerValue(layer, false, false, false, true));
|
---|
175 | break;
|
---|
176 | case TextureUnitState::TT_ROTATE:
|
---|
177 | // Target value is texture coord rotation
|
---|
178 | val.bind(new TexCoordModifierControllerValue(layer, false, false, false, false, true));
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | // Create new wave function for alterations
|
---|
182 | func.bind(new WaveformControllerFunction(waveType, base, frequency, phase, amplitude, true));
|
---|
183 |
|
---|
184 | return createController(mFrameTimeController, val, func);
|
---|
185 | }
|
---|
186 | //-----------------------------------------------------------------------
|
---|
187 | Controller<Real>* ControllerManager::createGpuProgramTimerParam(
|
---|
188 | GpuProgramParameters* params, size_t paramIndex, Real timeFactor)
|
---|
189 | {
|
---|
190 | SharedPtr< ControllerValue<Real> > val;
|
---|
191 | SharedPtr< ControllerFunction<Real> > func;
|
---|
192 |
|
---|
193 | val.bind(new FloatGpuParameterControllerValue(params, paramIndex));
|
---|
194 | func.bind(new ScaleControllerFunction(timeFactor, true));
|
---|
195 |
|
---|
196 | return createController(mFrameTimeController, val, func);
|
---|
197 |
|
---|
198 | }
|
---|
199 | //-----------------------------------------------------------------------
|
---|
200 | void ControllerManager::destroyController(Controller<Real>* controller)
|
---|
201 | {
|
---|
202 | ControllerList::iterator i = mControllers.find(controller);
|
---|
203 | if (i != mControllers.end())
|
---|
204 | {
|
---|
205 | mControllers.erase(i);
|
---|
206 | delete controller;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | //-----------------------------------------------------------------------
|
---|
210 | Real ControllerManager::getTimeFactor(void) const {
|
---|
211 | return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getTimeFactor();
|
---|
212 | }
|
---|
213 | //-----------------------------------------------------------------------
|
---|
214 | void ControllerManager::setTimeFactor(Real tf) {
|
---|
215 | static_cast<FrameTimeControllerValue*>(mFrameTimeController.getPointer())->setTimeFactor(tf);
|
---|
216 | }
|
---|
217 | //-----------------------------------------------------------------------
|
---|
218 | Real ControllerManager::getElapsedTime(void) const
|
---|
219 | {
|
---|
220 | return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getElapsedTime();
|
---|
221 | }
|
---|
222 | //-----------------------------------------------------------------------
|
---|
223 | void ControllerManager::setElapsedTime(Real elapsedTime)
|
---|
224 | {
|
---|
225 | static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setElapsedTime(elapsedTime);
|
---|
226 | }
|
---|
227 | }
|
---|