[5] | 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 "OgrePredefinedControllers.h" |
---|
| 31 | |
---|
| 32 | #include "OgreRoot.h" |
---|
| 33 | #include "OgreMath.h" |
---|
| 34 | #include "OgreLogManager.h" |
---|
| 35 | #include "OgreTextureUnitState.h" |
---|
| 36 | |
---|
| 37 | namespace Ogre |
---|
| 38 | { |
---|
| 39 | //----------------------------------------------------------------------- |
---|
| 40 | // FrameTimeControllerValue |
---|
| 41 | //----------------------------------------------------------------------- |
---|
| 42 | FrameTimeControllerValue::FrameTimeControllerValue() |
---|
| 43 | { |
---|
| 44 | // Register self |
---|
| 45 | Root::getSingleton().addFrameListener(this); |
---|
| 46 | mFrameTime = 0; |
---|
| 47 | mTimeFactor = 1; |
---|
| 48 | mFrameDelay = 0; |
---|
| 49 | mElapsedTime = 0; |
---|
| 50 | |
---|
| 51 | } |
---|
| 52 | //----------------------------------------------------------------------- |
---|
| 53 | bool FrameTimeControllerValue::frameStarted(const FrameEvent &evt) |
---|
| 54 | { |
---|
| 55 | if(mFrameDelay) |
---|
| 56 | { |
---|
| 57 | // Fixed frame time |
---|
| 58 | mFrameTime = mFrameDelay; |
---|
| 59 | mTimeFactor = mFrameDelay / evt.timeSinceLastFrame; |
---|
| 60 | } |
---|
| 61 | else |
---|
| 62 | { |
---|
| 63 | // Save the time value after applying time factor |
---|
| 64 | mFrameTime = mTimeFactor * evt.timeSinceLastFrame; |
---|
| 65 | } |
---|
| 66 | // Accumulate the elapsed time |
---|
| 67 | mElapsedTime += mFrameTime; |
---|
| 68 | return true; |
---|
| 69 | } |
---|
| 70 | //----------------------------------------------------------------------- |
---|
| 71 | bool FrameTimeControllerValue::frameEnded(const FrameEvent &evt) |
---|
| 72 | { |
---|
| 73 | return true; |
---|
| 74 | } |
---|
| 75 | //----------------------------------------------------------------------- |
---|
| 76 | Real FrameTimeControllerValue::getValue() const |
---|
| 77 | { |
---|
| 78 | return mFrameTime; |
---|
| 79 | } |
---|
| 80 | //----------------------------------------------------------------------- |
---|
| 81 | void FrameTimeControllerValue::setValue(Real value) |
---|
| 82 | { |
---|
| 83 | // Do nothing - value is set from frame listener |
---|
| 84 | } |
---|
| 85 | //----------------------------------------------------------------------- |
---|
| 86 | Real FrameTimeControllerValue::getTimeFactor(void) const { |
---|
| 87 | return mTimeFactor; |
---|
| 88 | } |
---|
| 89 | //----------------------------------------------------------------------- |
---|
| 90 | void FrameTimeControllerValue::setTimeFactor(Real tf) { |
---|
| 91 | if(tf >= 0) |
---|
| 92 | { |
---|
| 93 | mTimeFactor = tf; |
---|
| 94 | mFrameDelay = 0; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | //----------------------------------------------------------------------- |
---|
| 98 | Real FrameTimeControllerValue::getFrameDelay(void) const { |
---|
| 99 | return mFrameDelay; |
---|
| 100 | } |
---|
| 101 | //----------------------------------------------------------------------- |
---|
| 102 | void FrameTimeControllerValue::setFrameDelay(Real fd) { |
---|
| 103 | mTimeFactor = 0; |
---|
| 104 | mFrameDelay = fd; |
---|
| 105 | } |
---|
| 106 | //----------------------------------------------------------------------- |
---|
| 107 | Real FrameTimeControllerValue::getElapsedTime(void) const |
---|
| 108 | { |
---|
| 109 | return mElapsedTime; |
---|
| 110 | } |
---|
| 111 | //----------------------------------------------------------------------- |
---|
| 112 | void FrameTimeControllerValue::setElapsedTime(Real elapsedTime) |
---|
| 113 | { |
---|
| 114 | mElapsedTime = elapsedTime; |
---|
| 115 | } |
---|
| 116 | //----------------------------------------------------------------------- |
---|
| 117 | // TextureFrameControllerValue |
---|
| 118 | //----------------------------------------------------------------------- |
---|
| 119 | TextureFrameControllerValue::TextureFrameControllerValue(TextureUnitState* t) |
---|
| 120 | { |
---|
| 121 | mTextureLayer = t; |
---|
| 122 | } |
---|
| 123 | //----------------------------------------------------------------------- |
---|
| 124 | Real TextureFrameControllerValue::getValue(void) const |
---|
| 125 | { |
---|
| 126 | int numFrames = mTextureLayer->getNumFrames(); |
---|
| 127 | return (mTextureLayer->getCurrentFrame() / numFrames); |
---|
| 128 | } |
---|
| 129 | //----------------------------------------------------------------------- |
---|
| 130 | void TextureFrameControllerValue::setValue(Real value) |
---|
| 131 | { |
---|
| 132 | int numFrames = mTextureLayer->getNumFrames(); |
---|
| 133 | mTextureLayer->setCurrentFrame((int)(value * numFrames) % numFrames); |
---|
| 134 | } |
---|
| 135 | //----------------------------------------------------------------------- |
---|
| 136 | // TexCoordModifierControllerValue |
---|
| 137 | //----------------------------------------------------------------------- |
---|
| 138 | TexCoordModifierControllerValue::TexCoordModifierControllerValue(TextureUnitState* t, |
---|
| 139 | bool translateU, bool translateV, bool scaleU, bool scaleV, bool rotate ) |
---|
| 140 | { |
---|
| 141 | mTextureLayer = t; |
---|
| 142 | mTransU = translateU; |
---|
| 143 | mTransV = translateV; |
---|
| 144 | mScaleU = scaleU; |
---|
| 145 | mScaleV = scaleV; |
---|
| 146 | mRotate = rotate; |
---|
| 147 | } |
---|
| 148 | //----------------------------------------------------------------------- |
---|
| 149 | Real TexCoordModifierControllerValue::getValue() const |
---|
| 150 | { |
---|
| 151 | const Matrix4& pMat = mTextureLayer->getTextureTransform(); |
---|
| 152 | if (mTransU) |
---|
| 153 | { |
---|
| 154 | return pMat[0][3]; |
---|
| 155 | } |
---|
| 156 | else if (mTransV) |
---|
| 157 | { |
---|
| 158 | return pMat[1][3]; |
---|
| 159 | } |
---|
| 160 | else if (mScaleU) |
---|
| 161 | { |
---|
| 162 | return pMat[0][0]; |
---|
| 163 | } |
---|
| 164 | else if (mScaleV) |
---|
| 165 | { |
---|
| 166 | return pMat[1][1]; |
---|
| 167 | } |
---|
| 168 | // Shouldn't get here |
---|
| 169 | return 0; |
---|
| 170 | } |
---|
| 171 | //----------------------------------------------------------------------- |
---|
| 172 | void TexCoordModifierControllerValue::setValue(Real value) |
---|
| 173 | { |
---|
| 174 | if (mTransU) |
---|
| 175 | { |
---|
| 176 | mTextureLayer->setTextureUScroll(value); |
---|
| 177 | } |
---|
| 178 | if (mTransV) |
---|
| 179 | { |
---|
| 180 | mTextureLayer->setTextureVScroll(value); |
---|
| 181 | } |
---|
| 182 | if (mScaleU) |
---|
| 183 | { |
---|
| 184 | if (value >= 0) |
---|
| 185 | { |
---|
| 186 | // Add 1 to scale (+ve scales up) |
---|
| 187 | mTextureLayer->setTextureUScale(1 + value); |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
| 191 | // (-ve scales down) |
---|
| 192 | mTextureLayer->setTextureUScale(1 / -value); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | if (mScaleV) |
---|
| 196 | { |
---|
| 197 | if (value >= 0) |
---|
| 198 | { |
---|
| 199 | // Add 1 to scale (+ve scales up) |
---|
| 200 | mTextureLayer->setTextureVScale(1 + value); |
---|
| 201 | } |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | // (-ve scales down) |
---|
| 205 | mTextureLayer->setTextureVScale(1 / -value); |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | if (mRotate) |
---|
| 209 | { |
---|
| 210 | mTextureLayer->setTextureRotate(Radian(value * Math::TWO_PI)); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | //----------------------------------------------------------------------- |
---|
| 214 | //----------------------------------------------------------------------- |
---|
| 215 | FloatGpuParameterControllerValue::FloatGpuParameterControllerValue( |
---|
| 216 | GpuProgramParameters* params, size_t index) : |
---|
| 217 | mParams(params), mParamIndex(index) |
---|
| 218 | { |
---|
| 219 | } |
---|
| 220 | //----------------------------------------------------------------------- |
---|
| 221 | Real FloatGpuParameterControllerValue::getValue(void) const |
---|
| 222 | { |
---|
| 223 | // do nothing, reading from a set of params not supported |
---|
| 224 | return 0.0f; |
---|
| 225 | } |
---|
| 226 | //----------------------------------------------------------------------- |
---|
| 227 | void FloatGpuParameterControllerValue::setValue(Real val) |
---|
| 228 | { |
---|
| 229 | static Vector4 v4 = Vector4(0,0,0,0); |
---|
| 230 | v4.x = val; |
---|
| 231 | mParams->setConstant(mParamIndex, v4); |
---|
| 232 | } |
---|
| 233 | //----------------------------------------------------------------------- |
---|
| 234 | // PassthroughControllerFunction |
---|
| 235 | //----------------------------------------------------------------------- |
---|
| 236 | PassthroughControllerFunction::PassthroughControllerFunction(bool delta) |
---|
| 237 | : ControllerFunction<Real>(delta) |
---|
| 238 | { |
---|
| 239 | } |
---|
| 240 | //----------------------------------------------------------------------- |
---|
| 241 | Real PassthroughControllerFunction::calculate(Real source) |
---|
| 242 | { |
---|
| 243 | return getAdjustedInput(source); |
---|
| 244 | |
---|
| 245 | } |
---|
| 246 | //----------------------------------------------------------------------- |
---|
| 247 | // AnimationControllerFunction |
---|
| 248 | //----------------------------------------------------------------------- |
---|
| 249 | AnimationControllerFunction::AnimationControllerFunction(Real sequenceTime, Real timeOffset) |
---|
| 250 | : ControllerFunction<Real>(false) |
---|
| 251 | { |
---|
| 252 | mSeqTime = sequenceTime; |
---|
| 253 | mTime = timeOffset; |
---|
| 254 | } |
---|
| 255 | //----------------------------------------------------------------------- |
---|
| 256 | Real AnimationControllerFunction::calculate(Real source) |
---|
| 257 | { |
---|
| 258 | // Assume source is time since last update |
---|
| 259 | mTime += source; |
---|
| 260 | // Wrap |
---|
| 261 | while (mTime >= mSeqTime) mTime -= mSeqTime; |
---|
| 262 | while (mTime < 0) mTime += mSeqTime; |
---|
| 263 | |
---|
| 264 | // Return parametric |
---|
| 265 | return mTime / mSeqTime; |
---|
| 266 | } |
---|
| 267 | //----------------------------------------------------------------------- |
---|
| 268 | void AnimationControllerFunction::setTime(Real timeVal) |
---|
| 269 | { |
---|
| 270 | mTime = timeVal; |
---|
| 271 | } |
---|
| 272 | //----------------------------------------------------------------------- |
---|
| 273 | void AnimationControllerFunction::setSequenceTime(Real seqVal) |
---|
| 274 | { |
---|
| 275 | mSeqTime = seqVal; |
---|
| 276 | } |
---|
| 277 | //----------------------------------------------------------------------- |
---|
| 278 | // ScaleControllerFunction |
---|
| 279 | //----------------------------------------------------------------------- |
---|
| 280 | ScaleControllerFunction::ScaleControllerFunction(Real factor, bool delta) : ControllerFunction<Real>(delta) |
---|
| 281 | { |
---|
| 282 | mScale = factor; |
---|
| 283 | } |
---|
| 284 | //----------------------------------------------------------------------- |
---|
| 285 | Real ScaleControllerFunction::calculate(Real source) |
---|
| 286 | { |
---|
| 287 | return getAdjustedInput(source * mScale); |
---|
| 288 | |
---|
| 289 | } |
---|
| 290 | //----------------------------------------------------------------------- |
---|
| 291 | // WaveformControllerFunction |
---|
| 292 | //----------------------------------------------------------------------- |
---|
| 293 | WaveformControllerFunction::WaveformControllerFunction(WaveformType wType, Real base, Real frequency, Real phase, Real amplitude, bool delta, Real dutyCycle) |
---|
| 294 | :ControllerFunction<Real>(delta) |
---|
| 295 | { |
---|
| 296 | mWaveType = wType; |
---|
| 297 | mBase = base; |
---|
| 298 | mFrequency = frequency; |
---|
| 299 | mPhase = phase; |
---|
| 300 | mAmplitude = amplitude; |
---|
| 301 | mDeltaCount = phase; |
---|
| 302 | mDutyCycle = dutyCycle; |
---|
| 303 | } |
---|
| 304 | //----------------------------------------------------------------------- |
---|
| 305 | Real WaveformControllerFunction::getAdjustedInput(Real input) |
---|
| 306 | { |
---|
| 307 | Real adjusted = ControllerFunction<Real>::getAdjustedInput(input); |
---|
| 308 | |
---|
| 309 | // If not delta, adjust by phase here |
---|
| 310 | // (delta inputs have it adjusted at initialisation) |
---|
| 311 | if (!mDeltaInput) |
---|
| 312 | { |
---|
| 313 | adjusted += mPhase; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | return adjusted; |
---|
| 317 | } |
---|
| 318 | //----------------------------------------------------------------------- |
---|
| 319 | Real WaveformControllerFunction::calculate(Real source) |
---|
| 320 | { |
---|
| 321 | Real input = getAdjustedInput(source * mFrequency); |
---|
| 322 | Real output; |
---|
| 323 | // For simplicity, factor input down to {0,1) |
---|
| 324 | // Use looped subtract rather than divide / round |
---|
| 325 | while (input >= 1.0) |
---|
| 326 | input -= 1.0; |
---|
| 327 | while (input < 0.0) |
---|
| 328 | input += 1.0; |
---|
| 329 | |
---|
| 330 | // Calculate output in -1..1 range |
---|
| 331 | switch (mWaveType) |
---|
| 332 | { |
---|
| 333 | case WFT_SINE: |
---|
| 334 | output = Math::Sin(Radian(input * Math::TWO_PI)); |
---|
| 335 | break; |
---|
| 336 | case WFT_TRIANGLE: |
---|
| 337 | if (input < 0.25) |
---|
| 338 | output = input * 4; |
---|
| 339 | else if (input >= 0.25 && input < 0.75) |
---|
| 340 | output = 1.0 - ((input - 0.25) * 4); |
---|
| 341 | else |
---|
| 342 | output = ((input - 0.75) * 4) - 1.0; |
---|
| 343 | |
---|
| 344 | break; |
---|
| 345 | case WFT_SQUARE: |
---|
| 346 | if (input <= 0.5) |
---|
| 347 | output = 1.0; |
---|
| 348 | else |
---|
| 349 | output = -1.0; |
---|
| 350 | break; |
---|
| 351 | case WFT_SAWTOOTH: |
---|
| 352 | output = (input * 2) - 1; |
---|
| 353 | break; |
---|
| 354 | case WFT_INVERSE_SAWTOOTH: |
---|
| 355 | output = -((input * 2) - 1); |
---|
| 356 | break; |
---|
| 357 | case WFT_PWM: |
---|
| 358 | if( input <= mDutyCycle ) |
---|
| 359 | output = 1.0; |
---|
| 360 | else |
---|
| 361 | output = -1.0; |
---|
| 362 | break; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | // Scale output into 0..1 range and then by base + amplitude |
---|
| 366 | return mBase + ((output + 1.0) * 0.5 * mAmplitude); |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | |
---|