cameraFXMgr.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/fx/cameraFXMgr.h"
  24. #include "platform/profiler.h"
  25. #include "math/mRandom.h"
  26. #include "math/mMatrix.h"
  27. // global cam fx
  28. CameraFXManager gCamFXMgr;
  29. //**************************************************************************
  30. // Camera effect
  31. //**************************************************************************
  32. CameraFX::CameraFX()
  33. {
  34. mElapsedTime = 0.0;
  35. mDuration = 1.0;
  36. }
  37. //--------------------------------------------------------------------------
  38. // Update
  39. //--------------------------------------------------------------------------
  40. void CameraFX::update( F32 dt )
  41. {
  42. mElapsedTime += dt;
  43. }
  44. //**************************************************************************
  45. // Camera shake effect
  46. //**************************************************************************
  47. CameraShake::CameraShake()
  48. {
  49. mFreq.zero();
  50. mAmp.zero();
  51. mStartAmp.zero();
  52. mTimeOffset.zero();
  53. mCamFXTrans.identity();
  54. mFalloff = 10.0;
  55. remoteControlled = false;
  56. isAdded = false;
  57. }
  58. bool CameraShake::isExpired()
  59. {
  60. if ( remoteControlled )
  61. return false;
  62. return Parent::isExpired();
  63. }
  64. //--------------------------------------------------------------------------
  65. // Update
  66. //--------------------------------------------------------------------------
  67. void CameraShake::update( F32 dt )
  68. {
  69. Parent::update( dt );
  70. if ( !remoteControlled )
  71. fadeAmplitude();
  72. else
  73. mAmp = mStartAmp;
  74. VectorF camOffset;
  75. camOffset.x = mAmp.x * sin( M_2PI * (mTimeOffset.x + mElapsedTime) * mFreq.x );
  76. camOffset.y = mAmp.y * sin( M_2PI * (mTimeOffset.y + mElapsedTime) * mFreq.y );
  77. camOffset.z = mAmp.z * sin( M_2PI * (mTimeOffset.z + mElapsedTime) * mFreq.z );
  78. VectorF rotAngles;
  79. rotAngles.x = camOffset.x * 10.0 * M_PI/180.0;
  80. rotAngles.y = camOffset.y * 10.0 * M_PI/180.0;
  81. rotAngles.z = camOffset.z * 10.0 * M_PI/180.0;
  82. MatrixF rotMatrix( EulerF( rotAngles.x, rotAngles.y, rotAngles.z ) );
  83. mCamFXTrans = rotMatrix;
  84. mCamFXTrans.setPosition( camOffset );
  85. }
  86. //--------------------------------------------------------------------------
  87. // Fade out the amplitude over time
  88. //--------------------------------------------------------------------------
  89. void CameraShake::fadeAmplitude()
  90. {
  91. F32 percentDone = (mElapsedTime / mDuration);
  92. if( percentDone > 1.0 ) percentDone = 1.0;
  93. F32 time = 1 + percentDone * mFalloff;
  94. time = 1 / (time * time);
  95. mAmp = mStartAmp * time;
  96. }
  97. //--------------------------------------------------------------------------
  98. // Initialize
  99. //--------------------------------------------------------------------------
  100. void CameraShake::init()
  101. {
  102. mTimeOffset.x = 0.0;
  103. mTimeOffset.y = gRandGen.randF();
  104. mTimeOffset.z = gRandGen.randF();
  105. }
  106. //**************************************************************************
  107. // CameraFXManager
  108. //**************************************************************************
  109. CameraFXManager::CameraFXManager()
  110. {
  111. mCamFXTrans.identity();
  112. }
  113. //--------------------------------------------------------------------------
  114. // Destructor
  115. //--------------------------------------------------------------------------
  116. CameraFXManager::~CameraFXManager()
  117. {
  118. clear();
  119. }
  120. //--------------------------------------------------------------------------
  121. // Add new effect to currently running list
  122. //--------------------------------------------------------------------------
  123. void CameraFXManager::addFX( CameraFX *newFX )
  124. {
  125. mFXList.pushFront( newFX );
  126. }
  127. void CameraFXManager::removeFX( CameraFX *fx )
  128. {
  129. CamFXList::Iterator itr = mFXList.begin();
  130. for ( ; itr != mFXList.end(); itr++ )
  131. {
  132. if ( *itr == fx )
  133. {
  134. mFXList.erase( itr );
  135. return;
  136. }
  137. }
  138. return;
  139. }
  140. //--------------------------------------------------------------------------
  141. // Clear all currently running camera effects
  142. //--------------------------------------------------------------------------
  143. void CameraFXManager::clear()
  144. {
  145. for(CamFXList::Iterator i = mFXList.begin(); i != mFXList.end(); ++i)
  146. {
  147. delete *i;
  148. }
  149. mFXList.clear();
  150. }
  151. //--------------------------------------------------------------------------
  152. // Update camera effects
  153. //--------------------------------------------------------------------------
  154. void CameraFXManager::update( F32 dt )
  155. {
  156. PROFILE_SCOPE( CameraFXManager_update );
  157. mCamFXTrans.identity();
  158. CamFXList::Iterator cur;
  159. for(CamFXList::Iterator i = mFXList.begin(); i != mFXList.end(); /*Trickiness*/)
  160. {
  161. // Store previous iterator and increment while iterator is still valid.
  162. cur = i;
  163. ++i;
  164. CameraFX * curFX = *cur;
  165. curFX->update( dt );
  166. MatrixF fxTrans = curFX->getTrans();
  167. mCamFXTrans.mul( fxTrans );
  168. if( curFX->isExpired() )
  169. {
  170. delete curFX;
  171. mFXList.erase( cur );
  172. }
  173. }
  174. }