123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- //-----------------------------------------------------------------------------
- // 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.
- //-----------------------------------------------------------------------------
- #ifndef _SFXWORLD_H_
- #define _SFXWORLD_H_
- #ifndef _SCOPETRACKER_H_
- #include "util/scopeTracker.h"
- #endif
- #ifndef _TVECTOR_H_
- #include "core/util/tVector.h"
- #endif
- #ifndef _SFXSYSTEM_H_
- #include "sfx/sfxSystem.h"
- #endif
- #ifndef _SFXSOUNDSCAPE_H_
- #include "sfx/sfxSoundscape.h"
- #endif
- #ifndef _SFXAMBIENCE_H_
- #include "sfx/sfxAmbience.h"
- #endif
- // Disable warning about unreferenced
- // local function on VC.
- #ifdef TORQUE_COMPILER_VISUALC
- #pragma warning( disable: 4505 )
- #endif
- //#define DEBUG_SPEW
- /// @file
- /// System for representing and tracking changes to the SFX world of
- /// sounds, sound spaces, occluders, and receivers.
- ///
- /// This is system is abstracted over the number of dimensions it will
- /// work with, so it is usable for both 2D and 3D.
- ///
- /// To put it to use, it has to be connected to the engine's scene
- /// graph structure by deriving suitable types from SFXObject.
- class SFXAmbience;
- /// Property flags for SFXObjects.
- enum SFXObjectFlags
- {
- /// Object occludes sound.
- SFXObjectOccluder = BIT( 0 ),
-
- /// Object connects two ambient zones. Results in a continuous blend
- /// between the two ambient zones as the listener travels through the
- /// portal.
- SFXObjectPortal = BIT( 1 ),
-
- ///
- SFXObjectZone = BIT( 2 ),
-
- /// Object is currently used as listener.
- ///
- /// @note An object used as a listener will automatically have all
- /// its other sound-related functionality (occlusion, zoning) ignored.
- SFXObjectListener = BIT( 3 ),
-
- ///
- SFXObjectEmitter = BIT( 4 ),
- };
- /// Information about an object in the SFX world.
- ///
- /// SFXObjects are:
- ///
- /// - Occluders: blocking sound
- /// - Zones: ambient sound spaces
- /// - Portals: connectors between zones
- /// - Listeners: receiving sound
- ///
- /// Note that emitters are not managed by the SFX world. Instead, the set of
- /// 3D voices active on the device at any one point is defined as the set of
- /// current sound sources.
- ///
- template< S32 NUM_DIMENSIONS >
- class SFXObject : public ScopeTrackerObject< NUM_DIMENSIONS >
- {
- public:
-
- typedef ScopeTrackerObject< NUM_DIMENSIONS > Parent;
-
- protected:
-
- ///
- BitSet32 mFlags;
-
- public:
-
- ///
- SFXObject()
- : Parent() {}
-
- /// Return true if this object is only a sound occluder without any more ambient
- /// sound properties. These kind of objects are not put into the tracking system.
- bool isOccluderOnly() const { return ( isOccluder() && !isZone() ); }
-
- /// Return true if this object is occluding sound.
- bool isOccluder() const { return mFlags.test( SFXObjectOccluder ); }
-
- /// Return true if this object connects two ambient spaces.
- bool isPortal() const { return mFlags.test( SFXObjectPortal ); }
-
- ///
- bool isZone() const { return mFlags.test( SFXObjectZone ); }
-
- /// Return true if this object is currently being used as the listener.
- /// @note All other sound-related properties (occlusion, ambience, etc.) will be ignored
- /// on the listener object.
- bool isListener() const { return mFlags.test( SFXObjectListener ); }
-
- ///
- bool isEmitter() const { return mFlags.test( SFXObjectEmitter ); }
- ///
- void setFlags( U32 bits ) { mFlags.set( bits ); }
-
- ///
- void clearFlags( U32 bits ) { mFlags.clear( bits ); }
-
- /// @name Implementor Interface
- ///
- /// The following methods must be implemented by the client. They are defined here
- /// just for reference. If you don't override them, you'll get link errors.
- ///
- /// @{
-
- /// Return the world-space position of the ears on this object.
- /// This will only be called on objects that are made listeners.
- void getReferenceCenter( F32 pos[ NUM_DIMENSIONS ] ) const;
-
- /// Return the object's bounding box in world-space including its surround zone.
- void getBounds( F32 minBounds[ NUM_DIMENSIONS ], F32 maxBounds[ NUM_DIMENSIONS ] ) const;
- /// Return the object's bounding box in world-space excluding its surround zone.
- void getRealBounds( F32 minBounds[ NUM_DIMENSIONS ], F32 maxBounds[ NUM_DIMENSIONS ] ) const;
-
- /// Return true if the given point is contained in the object's volume.
- bool containsPoint( const F32 point[ NUM_DIMENSIONS ] ) const;
-
- /// Return the ambient space active within this object.
- /// @note Portals cannot have their own ambient spaces.
- SFXAmbience* getAmbience() const;
-
- ///
- String describeSelf() const;
- /// @}
- };
- /// SFXWorld tracks an N-dimensional world of SFXObjects.
- ///
- ///
- /// This class uses two systems as back-ends: occlusion handling is passed on to the
- /// occlusion manager installed on the system and tracking the listener traveling through
- /// the ambient spaces is
- ///
- template< S32 NUM_DIMENSIONS, typename Object >
- class SFXWorld : public ScopeTracker< NUM_DIMENSIONS, Object >
- {
- public:
-
- typedef ScopeTracker< NUM_DIMENSIONS, Object > Parent;
-
- protected:
-
- /// Record for objects that are currently in scope.
- struct Scope
- {
- /// Sort key on scope stack. This is used to establish an ordering between
- /// the ambient spaces that the listener is in concurrently.
- F32 mSortValue;
-
- /// The soundscape instance. Only objects for which the listener actually
- /// is in one of the sound zones, will have an associated soundscape.
- SFXSoundscape* mSoundscape;
-
- /// The object defining this scope. If this is a portal, we transition
- /// between this space and the space above us in the stack.
- Object mObject;
-
- Scope() :mSortValue(0), mSoundscape(NULL) {}
- Scope( F32 sortValue, Object object )
- : mSortValue( sortValue ),
- mSoundscape( NULL ),
- mObject( object ) {}
- };
-
- /// A top-down ordering of all objects that are currently in scope.
- Vector< Scope > mScopeStack;
-
- /// Return the index on the scope stack that is tied to the given object or -1 if
- /// the object is not on the stack.
- S32 _findScope( Object object );
-
- ///
- void _sortScopes();
-
- ///
- F32 _getSortValue( Object object );
- // ScopeTracker.
- virtual void _onScopeIn( Object object );
- virtual void _onScopeOut( Object object );
-
- public:
-
- ///
- SFXWorld();
-
- /// Update the state of the SFX world.
- ///
- /// @note This method may potentially be costly; don't update every frame.
- void update();
-
- // ScopeTracker.
- void notifyChanged( Object object );
- };
- //-----------------------------------------------------------------------------
- template< S32 NUM_DIMENSIONS, class Object >
- SFXWorld< NUM_DIMENSIONS, Object >::SFXWorld()
- {
- VECTOR_SET_ASSOCIATION( mScopeStack );
- }
- //-----------------------------------------------------------------------------
- template< S32 NUM_DIMENSIONS, class Object >
- void SFXWorld< NUM_DIMENSIONS, Object >::update()
- {
- if( !this->mReferenceObject )
- return;
- // Get the reference center (listener) pos.
-
- F32 listenerPos[ NUM_DIMENSIONS ];
- for( U32 n = 0; n < NUM_DIMENSIONS; n ++ )
- listenerPos[ n ] = Deref( this->mReferenceObject ).getMinTrackingNode( n )->getPosition();
- // Check all current scopes.
-
- SFXSoundscapeManager* soundscapeMgr = SFX->getSoundscapeManager();
- for( U32 i = 0; i < mScopeStack.size(); ++ i )
- {
- Scope& scope = mScopeStack[ i ];
-
- if( Deref( scope.mObject ).containsPoint( listenerPos ) )
- {
- // Listener is in the object.
-
- if( !scope.mSoundscape )
- {
- // Add the object's ambient space to the soundscape mix.
-
- SFXAmbience* ambience = Deref( scope.mObject ).getAmbience();
- AssertFatal( ambience != NULL, "SFXWorld::update() - object on stack that does not have an ambient space!" );
-
- // Find the index at which to insert the ambient space. 0 is always
- // the global space and each active soundscape lower down our stack
- // increments by one.
-
- U32 index = 1;
- for( U32 j = 0; j < i; ++ j )
- if( mScopeStack[ j ].mSoundscape )
- ++ index;
-
- scope.mSoundscape = soundscapeMgr->insertSoundscape( index, ambience );
- }
- }
- else
- {
- SFXAmbience* ambience = Deref( scope.mObject ).getAmbience();
- AssertFatal( ambience != NULL, "SFXWorld::update() - object on stack that does not have an ambient space!" );
- // Listener is neither inside object nor in its
- // proximity zone. Kill its ambient soundscape if it
- // has one.
-
- if( scope.mSoundscape )
- {
- soundscapeMgr->removeSoundscape( scope.mSoundscape );
- scope.mSoundscape = NULL;
- }
- }
- }
- }
- //-----------------------------------------------------------------------------
- template< S32 NUM_DIMENSIONS, class Object >
- void SFXWorld< NUM_DIMENSIONS, Object >::notifyChanged( Object object )
- {
- SFXAmbience* ambience = Deref( object ).getAmbience();
- if( !ambience )
- return;
-
- for( U32 i = 0; i < mScopeStack.size(); ++ i )
- {
- Scope& scope = mScopeStack[ i ];
- if( scope.mObject == object )
- {
- if( scope.mSoundscape )
- scope.mSoundscape->setAmbience( ambience );
- break;
- }
- }
- }
- //-----------------------------------------------------------------------------
- template< int NUM_DIMENSIONS, class Object >
- void SFXWorld< NUM_DIMENSIONS, Object >::_onScopeIn( Object object )
- {
- #ifdef DEBUG_SPEW
- Platform::outputDebugString( "[SFXWorld] Object 0x%x in scope now", object );
- #endif
- // Get the object's ambience properties. If it has
- // none, ignore the object.
-
- SFXAmbience* ambience = Deref( object ).getAmbience();
- if( !ambience )
- return;
- // Find where to insert the object into the stack.
-
- typename Vector< Scope >::iterator iter = mScopeStack.begin();
- F32 sortValue = _getSortValue( object );
- while( iter != mScopeStack.end() && iter->mSortValue >= sortValue )
- ++ iter;
- // If the object is already on the scope stack, do not add it again
- S32 index = _findScope(object);
- if (index != -1)
- return;
- // Insert the object into the stack.
- mScopeStack.insert( iter, Scope( sortValue, object ) );
- }
- //-----------------------------------------------------------------------------
- template< S32 NUM_DIMENSIONS, class Object >
- void SFXWorld< NUM_DIMENSIONS, Object >::_onScopeOut( Object object )
- {
- #ifdef DEBUG_SPEW
- Platform::outputDebugString( "[SFXWorld] Object 0x%x out of scope now", object );
- #endif
-
- // Find the object on the stack.
-
- S32 index = _findScope( object );
- if( index == -1 )
- return;
-
- // Remove its soundscape.
-
- Scope& scope = mScopeStack[ index ];
- if( scope.mSoundscape )
- SFX->getSoundscapeManager()->removeSoundscape( scope.mSoundscape );
-
- mScopeStack.erase( index );
- }
- //-----------------------------------------------------------------------------
- template< S32 NUM_DIMENSIONS, class Object >
- F32 SFXWorld< NUM_DIMENSIONS, Object >::_getSortValue( Object object )
- {
- //RDTODO: probably need to work with the overlap here instead of the full volumes
-
- // Get the real object bounds (without the surround zone).
-
- F32 minBounds[ NUM_DIMENSIONS ], maxBounds[ NUM_DIMENSIONS ];
- Deref( object ).getRealBounds( minBounds, maxBounds );
-
- // Get the minimum extent.
-
- F32 minExtent = maxBounds[ 0 ] - minBounds[ 0 ];
- for( U32 n = 1; n < NUM_DIMENSIONS; ++ n )
- minExtent = getMin( minExtent, maxBounds[ n ] - minBounds[ n ] );
-
- return minExtent;
- }
- //-----------------------------------------------------------------------------
- template< S32 NUM_DIMENSIONS, class Object >
- S32 SFXWorld< NUM_DIMENSIONS, Object >::_findScope( Object object )
- {
- for( U32 i = 0; i < mScopeStack.size(); ++ i )
- if( mScopeStack[ i ].mObject == object )
- return i;
-
- return -1;
- }
- #endif // !_SFXWORLD_H_
|