Torque3D Documentation / _generateds / cameraFXMgr.cpp

cameraFXMgr.cpp

Engine/source/T3D/fx/cameraFXMgr.cpp

More...

Public Variables

Detailed Description

Public Variables

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