PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Ogre-1.8rc/OgreMain/src/OgrePredefinedControllers.cpp

http://gamekit.googlecode.com/
C++ | 352 lines | 249 code | 14 blank | 89 comment | 27 complexity | 5e5dcc183f82e00d29d8ddde72137029 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, MIT
  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. Copyright (c) 2000-2012 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreStableHeaders.h"
  25. #include "OgrePredefinedControllers.h"
  26. #include "OgreRoot.h"
  27. #include "OgreMath.h"
  28. #include "OgreLogManager.h"
  29. #include "OgreTextureUnitState.h"
  30. namespace Ogre
  31. {
  32. //-----------------------------------------------------------------------
  33. // FrameTimeControllerValue
  34. //-----------------------------------------------------------------------
  35. FrameTimeControllerValue::FrameTimeControllerValue()
  36. {
  37. // Register self
  38. Root::getSingleton().addFrameListener(this);
  39. mFrameTime = 0;
  40. mTimeFactor = 1;
  41. mFrameDelay = 0;
  42. mElapsedTime = 0;
  43. }
  44. //-----------------------------------------------------------------------
  45. bool FrameTimeControllerValue::frameStarted(const FrameEvent &evt)
  46. {
  47. if(mFrameDelay)
  48. {
  49. // Fixed frame time
  50. mFrameTime = mFrameDelay;
  51. mTimeFactor = mFrameDelay / evt.timeSinceLastFrame;
  52. }
  53. else
  54. {
  55. // Save the time value after applying time factor
  56. mFrameTime = mTimeFactor * evt.timeSinceLastFrame;
  57. }
  58. // Accumulate the elapsed time
  59. mElapsedTime += mFrameTime;
  60. return true;
  61. }
  62. //-----------------------------------------------------------------------
  63. bool FrameTimeControllerValue::frameEnded(const FrameEvent &evt)
  64. {
  65. return true;
  66. }
  67. //-----------------------------------------------------------------------
  68. Real FrameTimeControllerValue::getValue() const
  69. {
  70. return mFrameTime;
  71. }
  72. //-----------------------------------------------------------------------
  73. void FrameTimeControllerValue::setValue(Real value)
  74. {
  75. // Do nothing - value is set from frame listener
  76. }
  77. //-----------------------------------------------------------------------
  78. Real FrameTimeControllerValue::getTimeFactor(void) const {
  79. return mTimeFactor;
  80. }
  81. //-----------------------------------------------------------------------
  82. void FrameTimeControllerValue::setTimeFactor(Real tf) {
  83. if(tf >= 0)
  84. {
  85. mTimeFactor = tf;
  86. mFrameDelay = 0;
  87. }
  88. }
  89. //-----------------------------------------------------------------------
  90. Real FrameTimeControllerValue::getFrameDelay(void) const {
  91. return mFrameDelay;
  92. }
  93. //-----------------------------------------------------------------------
  94. void FrameTimeControllerValue::setFrameDelay(Real fd) {
  95. mTimeFactor = 0;
  96. mFrameDelay = fd;
  97. }
  98. //-----------------------------------------------------------------------
  99. Real FrameTimeControllerValue::getElapsedTime(void) const
  100. {
  101. return mElapsedTime;
  102. }
  103. //-----------------------------------------------------------------------
  104. void FrameTimeControllerValue::setElapsedTime(Real elapsedTime)
  105. {
  106. mElapsedTime = elapsedTime;
  107. }
  108. //-----------------------------------------------------------------------
  109. // TextureFrameControllerValue
  110. //-----------------------------------------------------------------------
  111. TextureFrameControllerValue::TextureFrameControllerValue(TextureUnitState* t)
  112. {
  113. mTextureLayer = t;
  114. }
  115. //-----------------------------------------------------------------------
  116. Real TextureFrameControllerValue::getValue(void) const
  117. {
  118. int numFrames = mTextureLayer->getNumFrames();
  119. return ((Real)mTextureLayer->getCurrentFrame() / (Real)numFrames);
  120. }
  121. //-----------------------------------------------------------------------
  122. void TextureFrameControllerValue::setValue(Real value)
  123. {
  124. int numFrames = mTextureLayer->getNumFrames();
  125. mTextureLayer->setCurrentFrame((int)(value * numFrames) % numFrames);
  126. }
  127. //-----------------------------------------------------------------------
  128. // TexCoordModifierControllerValue
  129. //-----------------------------------------------------------------------
  130. TexCoordModifierControllerValue::TexCoordModifierControllerValue(TextureUnitState* t,
  131. bool translateU, bool translateV, bool scaleU, bool scaleV, bool rotate )
  132. {
  133. mTextureLayer = t;
  134. mTransU = translateU;
  135. mTransV = translateV;
  136. mScaleU = scaleU;
  137. mScaleV = scaleV;
  138. mRotate = rotate;
  139. }
  140. //-----------------------------------------------------------------------
  141. Real TexCoordModifierControllerValue::getValue() const
  142. {
  143. const Matrix4& pMat = mTextureLayer->getTextureTransform();
  144. if (mTransU)
  145. {
  146. return pMat[0][3];
  147. }
  148. else if (mTransV)
  149. {
  150. return pMat[1][3];
  151. }
  152. else if (mScaleU)
  153. {
  154. return pMat[0][0];
  155. }
  156. else if (mScaleV)
  157. {
  158. return pMat[1][1];
  159. }
  160. // Shouldn't get here
  161. return 0;
  162. }
  163. //-----------------------------------------------------------------------
  164. void TexCoordModifierControllerValue::setValue(Real value)
  165. {
  166. if (mTransU)
  167. {
  168. mTextureLayer->setTextureUScroll(value);
  169. }
  170. if (mTransV)
  171. {
  172. mTextureLayer->setTextureVScroll(value);
  173. }
  174. if (mScaleU)
  175. {
  176. mTextureLayer->setTextureUScale(value);
  177. }
  178. if (mScaleV)
  179. {
  180. mTextureLayer->setTextureVScale(value);
  181. }
  182. if (mRotate)
  183. {
  184. mTextureLayer->setTextureRotate(Radian(value * Math::TWO_PI));
  185. }
  186. }
  187. //-----------------------------------------------------------------------
  188. //-----------------------------------------------------------------------
  189. FloatGpuParameterControllerValue::FloatGpuParameterControllerValue(
  190. GpuProgramParametersSharedPtr params, size_t index) :
  191. mParams(params), mParamIndex(index)
  192. {
  193. }
  194. //-----------------------------------------------------------------------
  195. Real FloatGpuParameterControllerValue::getValue(void) const
  196. {
  197. // do nothing, reading from a set of params not supported
  198. return 0.0f;
  199. }
  200. //-----------------------------------------------------------------------
  201. void FloatGpuParameterControllerValue::setValue(Real val)
  202. {
  203. Vector4 v4 = Vector4(0,0,0,0);
  204. v4.x = val;
  205. mParams->setConstant(mParamIndex, v4);
  206. }
  207. //-----------------------------------------------------------------------
  208. // PassthroughControllerFunction
  209. //-----------------------------------------------------------------------
  210. PassthroughControllerFunction::PassthroughControllerFunction(bool delta)
  211. : ControllerFunction<Real>(delta)
  212. {
  213. }
  214. //-----------------------------------------------------------------------
  215. Real PassthroughControllerFunction::calculate(Real source)
  216. {
  217. return getAdjustedInput(source);
  218. }
  219. //-----------------------------------------------------------------------
  220. // AnimationControllerFunction
  221. //-----------------------------------------------------------------------
  222. AnimationControllerFunction::AnimationControllerFunction(Real sequenceTime, Real timeOffset)
  223. : ControllerFunction<Real>(false)
  224. {
  225. mSeqTime = sequenceTime;
  226. mTime = timeOffset;
  227. }
  228. //-----------------------------------------------------------------------
  229. Real AnimationControllerFunction::calculate(Real source)
  230. {
  231. // Assume source is time since last update
  232. mTime += source;
  233. // Wrap
  234. while (mTime >= mSeqTime) mTime -= mSeqTime;
  235. while (mTime < 0) mTime += mSeqTime;
  236. // Return parametric
  237. return mTime / mSeqTime;
  238. }
  239. //-----------------------------------------------------------------------
  240. void AnimationControllerFunction::setTime(Real timeVal)
  241. {
  242. mTime = timeVal;
  243. }
  244. //-----------------------------------------------------------------------
  245. void AnimationControllerFunction::setSequenceTime(Real seqVal)
  246. {
  247. mSeqTime = seqVal;
  248. }
  249. //-----------------------------------------------------------------------
  250. // ScaleControllerFunction
  251. //-----------------------------------------------------------------------
  252. ScaleControllerFunction::ScaleControllerFunction(Real factor, bool delta) : ControllerFunction<Real>(delta)
  253. {
  254. mScale = factor;
  255. }
  256. //-----------------------------------------------------------------------
  257. Real ScaleControllerFunction::calculate(Real source)
  258. {
  259. return getAdjustedInput(source * mScale);
  260. }
  261. //-----------------------------------------------------------------------
  262. // WaveformControllerFunction
  263. //-----------------------------------------------------------------------
  264. WaveformControllerFunction::WaveformControllerFunction(WaveformType wType, Real base, Real frequency, Real phase, Real amplitude, bool delta, Real dutyCycle)
  265. :ControllerFunction<Real>(delta)
  266. {
  267. mWaveType = wType;
  268. mBase = base;
  269. mFrequency = frequency;
  270. mPhase = phase;
  271. mAmplitude = amplitude;
  272. mDeltaCount = phase;
  273. mDutyCycle = dutyCycle;
  274. }
  275. //-----------------------------------------------------------------------
  276. Real WaveformControllerFunction::getAdjustedInput(Real input)
  277. {
  278. Real adjusted = ControllerFunction<Real>::getAdjustedInput(input);
  279. // If not delta, adjust by phase here
  280. // (delta inputs have it adjusted at initialisation)
  281. if (!mDeltaInput)
  282. {
  283. adjusted += mPhase;
  284. }
  285. return adjusted;
  286. }
  287. //-----------------------------------------------------------------------
  288. Real WaveformControllerFunction::calculate(Real source)
  289. {
  290. Real input = getAdjustedInput(source * mFrequency);
  291. Real output = 0;
  292. // For simplicity, factor input down to {0,1)
  293. // Use looped subtract rather than divide / round
  294. while (input >= 1.0)
  295. input -= 1.0;
  296. while (input < 0.0)
  297. input += 1.0;
  298. // Calculate output in -1..1 range
  299. switch (mWaveType)
  300. {
  301. case WFT_SINE:
  302. output = Math::Sin(Radian(input * Math::TWO_PI));
  303. break;
  304. case WFT_TRIANGLE:
  305. if (input < 0.25)
  306. output = input * 4;
  307. else if (input >= 0.25 && input < 0.75)
  308. output = 1.0f - ((input - 0.25f) * 4.0f);
  309. else
  310. output = ((input - 0.75f) * 4.0f) - 1.0f;
  311. break;
  312. case WFT_SQUARE:
  313. if (input <= 0.5f)
  314. output = 1.0f;
  315. else
  316. output = -1.0f;
  317. break;
  318. case WFT_SAWTOOTH:
  319. output = (input * 2.0f) - 1.0f;
  320. break;
  321. case WFT_INVERSE_SAWTOOTH:
  322. output = -((input * 2.0f) - 1.0f);
  323. break;
  324. case WFT_PWM:
  325. if( input <= mDutyCycle )
  326. output = 1.0f;
  327. else
  328. output = -1.0f;
  329. break;
  330. }
  331. // Scale output into 0..1 range and then by base + amplitude
  332. return mBase + ((output + 1.0f) * 0.5f * mAmplitude);
  333. }
  334. }