123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "T3D/fx/cameraFXMgr.h"
- #include "platform/profiler.h"
- #include "math/mRandom.h"
- #include "math/mMatrix.h"
- // global cam fx
- CameraFXManager gCamFXMgr;
- //**************************************************************************
- // Camera effect
- //**************************************************************************
- CameraFX::CameraFX()
- {
- mElapsedTime = 0.0;
- mDuration = 1.0;
- }
- //--------------------------------------------------------------------------
- // Update
- //--------------------------------------------------------------------------
- void CameraFX::update( F32 dt )
- {
- mElapsedTime += dt;
- }
- //**************************************************************************
- // Camera shake effect
- //**************************************************************************
- CameraShake::CameraShake()
- {
- mFreq.zero();
- mAmp.zero();
- mStartAmp.zero();
- mTimeOffset.zero();
- mCamFXTrans.identity();
- mFalloff = 10.0;
- remoteControlled = false;
- isAdded = false;
- }
- bool CameraShake::isExpired()
- {
- if ( remoteControlled )
- return false;
- return Parent::isExpired();
- }
- //--------------------------------------------------------------------------
- // Update
- //--------------------------------------------------------------------------
- void CameraShake::update( F32 dt )
- {
- Parent::update( dt );
- if ( !remoteControlled )
- fadeAmplitude();
- else
- mAmp = mStartAmp;
- VectorF camOffset;
- camOffset.x = mAmp.x * sin( M_2PI * (mTimeOffset.x + mElapsedTime) * mFreq.x );
- camOffset.y = mAmp.y * sin( M_2PI * (mTimeOffset.y + mElapsedTime) * mFreq.y );
- camOffset.z = mAmp.z * sin( M_2PI * (mTimeOffset.z + mElapsedTime) * mFreq.z );
- VectorF rotAngles;
- rotAngles.x = camOffset.x * 10.0 * M_PI/180.0;
- rotAngles.y = camOffset.y * 10.0 * M_PI/180.0;
- rotAngles.z = camOffset.z * 10.0 * M_PI/180.0;
- MatrixF rotMatrix( EulerF( rotAngles.x, rotAngles.y, rotAngles.z ) );
- mCamFXTrans = rotMatrix;
- mCamFXTrans.setPosition( camOffset );
- }
- //--------------------------------------------------------------------------
- // Fade out the amplitude over time
- //--------------------------------------------------------------------------
- void CameraShake::fadeAmplitude()
- {
- F32 percentDone = (mElapsedTime / mDuration);
- if( percentDone > 1.0 ) percentDone = 1.0;
- F32 time = 1 + percentDone * mFalloff;
- time = 1 / (time * time);
- mAmp = mStartAmp * time;
- }
- //--------------------------------------------------------------------------
- // Initialize
- //--------------------------------------------------------------------------
- void CameraShake::init()
- {
- mTimeOffset.x = 0.0;
- mTimeOffset.y = gRandGen.randF();
- mTimeOffset.z = gRandGen.randF();
- }
- //**************************************************************************
- // CameraFXManager
- //**************************************************************************
- CameraFXManager::CameraFXManager()
- {
- mCamFXTrans.identity();
- }
- //--------------------------------------------------------------------------
- // Destructor
- //--------------------------------------------------------------------------
- CameraFXManager::~CameraFXManager()
- {
- clear();
- }
- //--------------------------------------------------------------------------
- // Add new effect to currently running list
- //--------------------------------------------------------------------------
- void CameraFXManager::addFX( CameraFX *newFX )
- {
- mFXList.pushFront( newFX );
- }
- void CameraFXManager::removeFX( CameraFX *fx )
- {
- CamFXList::Iterator itr = mFXList.begin();
- for ( ; itr != mFXList.end(); itr++ )
- {
- if ( *itr == fx )
- {
- mFXList.erase( itr );
- return;
- }
- }
-
- return;
- }
- //--------------------------------------------------------------------------
- // Clear all currently running camera effects
- //--------------------------------------------------------------------------
- void CameraFXManager::clear()
- {
- for(CamFXList::Iterator i = mFXList.begin(); i != mFXList.end(); ++i)
- {
- delete *i;
- }
- mFXList.clear();
- }
- //--------------------------------------------------------------------------
- // Update camera effects
- //--------------------------------------------------------------------------
- void CameraFXManager::update( F32 dt )
- {
- PROFILE_SCOPE( CameraFXManager_update );
- mCamFXTrans.identity();
- CamFXList::Iterator cur;
- for(CamFXList::Iterator i = mFXList.begin(); i != mFXList.end(); /*Trickiness*/)
- {
- // Store previous iterator and increment while iterator is still valid.
- cur = i;
- ++i;
- CameraFX * curFX = *cur;
- curFX->update( dt );
- MatrixF fxTrans = curFX->getTrans();
- mCamFXTrans.mul( fxTrans );
- if( curFX->isExpired() )
- {
- delete curFX;
- mFXList.erase( cur );
- }
- }
- }
|