/src/ogre/OgreMain/src/OgreControllerManager.cpp
Line | Count | Source |
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-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreControllerManager.h" |
30 | | |
31 | | #include "OgrePredefinedControllers.h" |
32 | | |
33 | | namespace Ogre { |
34 | | //----------------------------------------------------------------------- |
35 | | template<> ControllerManager* Singleton<ControllerManager>::msSingleton = 0; |
36 | | ControllerManager* ControllerManager::getSingletonPtr(void) |
37 | 0 | { |
38 | 0 | return msSingleton; |
39 | 0 | } |
40 | | ControllerManager& ControllerManager::getSingleton(void) |
41 | 0 | { |
42 | 0 | assert( msSingleton ); return ( *msSingleton ); |
43 | 0 | } |
44 | | //----------------------------------------------------------------------- |
45 | | ControllerManager::ControllerManager() |
46 | 0 | : mFrameTimeController(OGRE_NEW FrameTimeControllerValue()) |
47 | 0 | , mPassthroughFunction(OGRE_NEW PassthroughControllerFunction()) |
48 | 0 | , mLastFrameNumber(0) |
49 | 0 | { |
50 | |
|
51 | 0 | } |
52 | | //----------------------------------------------------------------------- |
53 | | ControllerManager::~ControllerManager() |
54 | 0 | { |
55 | 0 | clearControllers(); |
56 | 0 | } |
57 | | //----------------------------------------------------------------------- |
58 | | ControllerFloat* ControllerManager::createController( |
59 | | const ControllerValueRealPtr& src, const ControllerValueRealPtr& dest, |
60 | | const ControllerFunctionRealPtr& func) |
61 | 0 | { |
62 | 0 | ControllerFloat* c = OGRE_NEW ControllerFloat(src, dest, func); |
63 | |
|
64 | 0 | mControllers.push_back(c); |
65 | 0 | return c; |
66 | 0 | } |
67 | | //----------------------------------------------------------------------- |
68 | | ControllerFloat* ControllerManager::createFrameTimePassthroughController( |
69 | | const ControllerValueRealPtr& dest) |
70 | 0 | { |
71 | 0 | return createController(getFrameTimeSource(), dest, getPassthroughControllerFunction()); |
72 | 0 | } |
73 | | //----------------------------------------------------------------------- |
74 | | void ControllerManager::updateAllControllers(void) |
75 | 0 | { |
76 | | // Only update once per frame |
77 | 0 | unsigned long thisFrameNumber = Root::getSingleton().getNextFrameNumber(); |
78 | 0 | if (thisFrameNumber != mLastFrameNumber) |
79 | 0 | { |
80 | 0 | auto controllersCopy = mControllers; |
81 | 0 | for (auto *ci : controllersCopy) |
82 | 0 | { |
83 | 0 | ci->update(); |
84 | 0 | } |
85 | 0 | mLastFrameNumber = thisFrameNumber; |
86 | 0 | } |
87 | 0 | } |
88 | | //----------------------------------------------------------------------- |
89 | | void ControllerManager::clearControllers(void) |
90 | 0 | { |
91 | 0 | for (auto *ci : mControllers) |
92 | 0 | { |
93 | 0 | OGRE_DELETE ci; |
94 | 0 | } |
95 | 0 | mControllers.clear(); |
96 | 0 | } |
97 | | //----------------------------------------------------------------------- |
98 | | const ControllerValueRealPtr& ControllerManager::getFrameTimeSource(void) const |
99 | 0 | { |
100 | 0 | return mFrameTimeController; |
101 | 0 | } |
102 | | //----------------------------------------------------------------------- |
103 | | const ControllerFunctionRealPtr& ControllerManager::getPassthroughControllerFunction(void) const |
104 | 0 | { |
105 | 0 | return mPassthroughFunction; |
106 | 0 | } |
107 | | //----------------------------------------------------------------------- |
108 | | ControllerFloat* ControllerManager::createTextureAnimator(TextureUnitState* layer, Real sequenceTime) |
109 | 0 | { |
110 | 0 | return createController(mFrameTimeController, TextureFrameControllerValue::create(layer), |
111 | 0 | AnimationControllerFunction::create(sequenceTime)); |
112 | 0 | } |
113 | | //----------------------------------------------------------------------- |
114 | | ControllerFloat* ControllerManager::createTextureUVScroller(TextureUnitState* layer, Real speed) |
115 | 0 | { |
116 | 0 | ControllerFloat* ret = 0; |
117 | |
|
118 | 0 | if (speed != 0) |
119 | 0 | { |
120 | | // We do both scrolls with a single controller |
121 | | // Create function: use -speed since we're altering texture coords so they have reverse effect |
122 | 0 | ret = createController(mFrameTimeController, TexCoordModifierControllerValue::create(layer, true, true), |
123 | 0 | ScaleControllerFunction::create(-speed, true)); |
124 | 0 | } |
125 | |
|
126 | 0 | return ret; |
127 | 0 | } |
128 | | //----------------------------------------------------------------------- |
129 | | ControllerFloat* ControllerManager::createTextureUScroller(TextureUnitState* layer, Real uSpeed) |
130 | 0 | { |
131 | 0 | ControllerFloat* ret = 0; |
132 | |
|
133 | 0 | if (uSpeed != 0) |
134 | 0 | { |
135 | | // Create function: use -speed since we're altering texture coords so they have reverse effect |
136 | 0 | ret = createController(mFrameTimeController, TexCoordModifierControllerValue::create(layer, true), |
137 | 0 | ScaleControllerFunction::create(-uSpeed, true)); |
138 | 0 | } |
139 | |
|
140 | 0 | return ret; |
141 | 0 | } |
142 | | //----------------------------------------------------------------------- |
143 | | ControllerFloat* ControllerManager::createTextureVScroller(TextureUnitState* layer, Real vSpeed) |
144 | 0 | { |
145 | 0 | ControllerFloat* ret = 0; |
146 | |
|
147 | 0 | if (vSpeed != 0) |
148 | 0 | { |
149 | | // Set up a second controller for v scroll |
150 | | // Create function: use -speed since we're altering texture coords so they have reverse effect |
151 | 0 | ret = createController(mFrameTimeController, TexCoordModifierControllerValue::create(layer, false, true), |
152 | 0 | ScaleControllerFunction::create(-vSpeed, true)); |
153 | 0 | } |
154 | |
|
155 | 0 | return ret; |
156 | 0 | } |
157 | | //----------------------------------------------------------------------- |
158 | | ControllerFloat* ControllerManager::createTextureRotater(TextureUnitState* layer, Real speed) |
159 | 0 | { |
160 | | // Target value is texture coord rotation |
161 | | // Function is simple scale (seconds * speed) |
162 | | // Use -speed since altering texture coords has the reverse visible effect |
163 | 0 | return createController(mFrameTimeController, |
164 | 0 | TexCoordModifierControllerValue::create(layer, false, false, false, false, true), |
165 | 0 | ScaleControllerFunction::create(-speed, true)); |
166 | 0 | } |
167 | | //----------------------------------------------------------------------- |
168 | | ControllerFloat* ControllerManager::createTextureWaveTransformer(TextureUnitState* layer, |
169 | | TextureUnitState::TextureTransformType ttype, WaveformType waveType, Real base, Real frequency, Real phase, Real amplitude) |
170 | 0 | { |
171 | 0 | ControllerValueRealPtr val; |
172 | |
|
173 | 0 | switch (ttype) |
174 | 0 | { |
175 | 0 | case TextureUnitState::TT_TRANSLATE_U: |
176 | | // Target value is a u scroll |
177 | 0 | val = TexCoordModifierControllerValue::create(layer, true); |
178 | 0 | break; |
179 | 0 | case TextureUnitState::TT_TRANSLATE_V: |
180 | | // Target value is a v scroll |
181 | 0 | val = TexCoordModifierControllerValue::create(layer, false, true); |
182 | 0 | break; |
183 | 0 | case TextureUnitState::TT_SCALE_U: |
184 | | // Target value is a u scale |
185 | 0 | val = TexCoordModifierControllerValue::create(layer, false, false, true); |
186 | 0 | break; |
187 | 0 | case TextureUnitState::TT_SCALE_V: |
188 | | // Target value is a v scale |
189 | 0 | val = TexCoordModifierControllerValue::create(layer, false, false, false, true); |
190 | 0 | break; |
191 | 0 | case TextureUnitState::TT_ROTATE: |
192 | | // Target value is texture coord rotation |
193 | 0 | val = TexCoordModifierControllerValue::create(layer, false, false, false, false, true); |
194 | 0 | break; |
195 | 0 | } |
196 | | // Create new wave function for alterations |
197 | 0 | return createController(mFrameTimeController, val, |
198 | 0 | WaveformControllerFunction::create(waveType, base, frequency, phase, amplitude, true)); |
199 | 0 | } |
200 | | //----------------------------------------------------------------------- |
201 | | ControllerFloat* ControllerManager::createGpuProgramTimerParam( |
202 | | GpuProgramParametersSharedPtr params, size_t paramIndex, Real timeFactor) |
203 | 0 | { |
204 | 0 | return createController(mFrameTimeController, FloatGpuParameterControllerValue::create(params, paramIndex), |
205 | 0 | ScaleControllerFunction::create(timeFactor, true)); |
206 | 0 | } |
207 | | //----------------------------------------------------------------------- |
208 | | void ControllerManager::destroyController(ControllerFloat* controller) |
209 | 0 | { |
210 | 0 | ControllerList::iterator i = std::find(mControllers.begin(), mControllers.end(), controller); |
211 | 0 | if (i != mControllers.end()) |
212 | 0 | { |
213 | 0 | std::swap(*i, mControllers.back()); |
214 | 0 | mControllers.pop_back(); |
215 | 0 | OGRE_DELETE controller; |
216 | 0 | } |
217 | 0 | } |
218 | | //----------------------------------------------------------------------- |
219 | 0 | Real ControllerManager::getTimeFactor(void) const { |
220 | 0 | return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getTimeFactor(); |
221 | 0 | } |
222 | | //----------------------------------------------------------------------- |
223 | 0 | void ControllerManager::setTimeFactor(Real tf) { |
224 | 0 | static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setTimeFactor(tf); |
225 | 0 | } |
226 | | //----------------------------------------------------------------------- |
227 | 0 | Real ControllerManager::getFrameDelay(void) const { |
228 | 0 | return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getFrameDelay(); |
229 | 0 | } |
230 | | //----------------------------------------------------------------------- |
231 | 0 | void ControllerManager::setFrameDelay(Real fd) { |
232 | 0 | static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setFrameDelay(fd); |
233 | 0 | } |
234 | | //----------------------------------------------------------------------- |
235 | | Real ControllerManager::getElapsedTime(void) const |
236 | 0 | { |
237 | 0 | return static_cast<const FrameTimeControllerValue*>(mFrameTimeController.get())->getElapsedTime(); |
238 | 0 | } |
239 | | //----------------------------------------------------------------------- |
240 | | void ControllerManager::setElapsedTime(Real elapsedTime) |
241 | 0 | { |
242 | 0 | static_cast<FrameTimeControllerValue*>(mFrameTimeController.get())->setElapsedTime(elapsedTime); |
243 | 0 | } |
244 | | } |