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