sfx3DWorld.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "T3D/sfx/sfx3DWorld.h"
  23. #include "T3D/portal.h"
  24. #include "T3D/shapeBase.h"
  25. #include "ts/tsShapeInstance.h"
  26. #include "sfx/sfxSystem.h"
  27. #include "math/mBox.h"
  28. #include "core/module.h"
  29. #include "T3D/gameBase/gameConnection.h"
  30. //#define DEBUG_SPEW
  31. MODULE_BEGIN( SFX3D )
  32. MODULE_INIT_AFTER( Scene )
  33. MODULE_SHUTDOWN_BEFORE( Scene )
  34. MODULE_INIT
  35. {
  36. if( !Con::getBoolVariable( "$SFX::noSFXWorld", false ) )
  37. gSFX3DWorld = new SFX3DWorld;
  38. }
  39. MODULE_SHUTDOWN
  40. {
  41. if( gSFX3DWorld )
  42. SAFE_DELETE( gSFX3DWorld );
  43. }
  44. MODULE_END;
  45. SFX3DWorld* gSFX3DWorld;
  46. //=============================================================================
  47. // SFX3DObject.
  48. //=============================================================================
  49. //-----------------------------------------------------------------------------
  50. SFX3DObject::SFX3DObject( SFX3DWorld* world, SceneObject* object )
  51. : Parent( world, object )
  52. {
  53. if( mObject->isOccludingSound() )
  54. setFlags( SFXObjectOccluder );
  55. if( dynamic_cast< Portal* >( mObject ) )
  56. setFlags( SFXObjectPortal );
  57. if( object->getSoundAmbience() )
  58. setFlags( SFXObjectZone );
  59. }
  60. //-----------------------------------------------------------------------------
  61. void SFX3DObject::getEarTransform( MatrixF& transform ) const
  62. {
  63. // If it's not a ShapeBase, just use the object transform.
  64. ShapeBase* shape = dynamic_cast< ShapeBase* >( mObject );
  65. if ( !shape )
  66. {
  67. transform = mObject->getTransform();
  68. return;
  69. }
  70. // It it's ShapeBase, use the earNode transform if one was defined.
  71. // Otherwise, use the camera transform.
  72. TSShapeInstance* shapeInstance = shape->getShapeInstance();
  73. if ( !shapeInstance )
  74. {
  75. // Just in case.
  76. transform = mObject->getTransform();
  77. return;
  78. }
  79. ShapeBaseData* datablock = dynamic_cast< ShapeBaseData* >( shape->getDataBlock() );
  80. AssertFatal( datablock, "SFX3DObject::getEarTransform() - shape without ShapeBaseData datablock!" );
  81. // Get the transform for the ear node.
  82. const S32 earNode = datablock->earNode;
  83. if ( earNode != -1 && earNode != datablock->eyeNode )
  84. {
  85. transform = shape->getTransform();
  86. transform *= shapeInstance->mNodeTransforms[ earNode ];
  87. }
  88. else
  89. {
  90. GameConnection* connection = dynamic_cast<GameConnection *>(NetConnection::getConnectionToServer());
  91. if ( !connection || !connection->getControlCameraTransform( 0.0f, &transform ) )
  92. transform = mObject->getTransform();
  93. }
  94. }
  95. //-----------------------------------------------------------------------------
  96. void SFX3DObject::getReferenceCenter( F32 position[ 3 ] ) const
  97. {
  98. MatrixF transform;
  99. getEarTransform( transform );
  100. Point3F pos = transform.getPosition();
  101. AssertFatal( TypeTraits< F32 >::MIN <= pos.x && pos.x <= TypeTraits< F32 >::MAX,
  102. "SFX3DObject::getReferenceCenter - invalid float in reference center X position" );
  103. AssertFatal( TypeTraits< F32 >::MIN <= pos.y && pos.y <= TypeTraits< F32 >::MAX,
  104. "SFX3DObject::getReferenceCenter - invalid float in reference center Y position" );
  105. AssertFatal( TypeTraits< F32 >::MIN <= pos.z && pos.z <= TypeTraits< F32 >::MAX,
  106. "SFX3DObject::getReferenceCenter - invalid float in reference center Z position" );
  107. dMemcpy( position, &pos.x, sizeof( F32 ) * 3 );
  108. }
  109. //-----------------------------------------------------------------------------
  110. void SFX3DObject::getBounds( F32 minBounds[ 3 ], F32 maxBounds[ 3 ] ) const
  111. {
  112. getRealBounds( minBounds, maxBounds );
  113. }
  114. //-----------------------------------------------------------------------------
  115. void SFX3DObject::getRealBounds( F32 minBounds[ 3 ], F32 maxBounds[ 3 ] ) const
  116. {
  117. const Box3F& worldBox = mObject->getWorldBox();
  118. dMemcpy( minBounds, &worldBox.minExtents.x, sizeof( F32 ) * 3 );
  119. dMemcpy( maxBounds, &worldBox.maxExtents.x, sizeof( F32 ) * 3 );
  120. }
  121. //-----------------------------------------------------------------------------
  122. SFXAmbience* SFX3DObject::getAmbience() const
  123. {
  124. return mObject->getSoundAmbience();
  125. }
  126. //-----------------------------------------------------------------------------
  127. bool SFX3DObject::containsPoint( const F32 point[ 3 ] ) const
  128. {
  129. return mObject->containsPoint( *( reinterpret_cast< const Point3F* >( &point[ 0 ] ) ) );
  130. }
  131. //-----------------------------------------------------------------------------
  132. String SFX3DObject::describeSelf() const
  133. {
  134. return mObject->describeSelf();
  135. }
  136. //=============================================================================
  137. // SFX3DWorld.
  138. //=============================================================================
  139. //-----------------------------------------------------------------------------
  140. SFX3DWorld::SFX3DWorld()
  141. : Parent( true, TYPEMASK )
  142. {
  143. }
  144. //-----------------------------------------------------------------------------
  145. bool SFX3DWorld::_isTrackableObject( SceneObject* object ) const
  146. {
  147. if( !Parent::_isTrackableObject( object ) )
  148. return false;
  149. // We are only interested in occluders, zones, and portals.
  150. if( object->isOccludingSound() )
  151. return true;
  152. else if( object->getSoundAmbience() )
  153. return true;
  154. else if( dynamic_cast< Portal* >( object ) )
  155. return true;
  156. return false;
  157. }
  158. //-----------------------------------------------------------------------------
  159. void SFX3DWorld::update()
  160. {
  161. mSFXWorld.update();
  162. }
  163. //-----------------------------------------------------------------------------
  164. void SFX3DWorld::registerObject( SceneObject* object )
  165. {
  166. if( !_isTrackableObject( object ) )
  167. return;
  168. // Construct a new scene object link.
  169. SFX3DObject* sfxObject = mChunker.alloc();
  170. constructInPlace( sfxObject, this, object );
  171. // Register it with the SFX world.
  172. #ifdef DEBUG_SPEW
  173. Platform::outputDebugString( "[SFX3DWorld] Registering %i:%s as 0x%x",
  174. object->getId(), object->getClassName(), sfxObject );
  175. #endif
  176. mSFXWorld.registerObject( sfxObject );
  177. }
  178. //-----------------------------------------------------------------------------
  179. void SFX3DWorld::unregisterObject( SceneObject* object )
  180. {
  181. SFX3DObject* sfxObject = dynamic_cast< SFX3DObject* >( SFX3DObject::getLinkForTracker( this, object ) );
  182. if( !sfxObject )
  183. return;
  184. #ifdef DEBUG_SPEW
  185. Platform::outputDebugString( "[SFX3DWorld] Unregistering %i:%s (was 0x%x)",
  186. object->getId(), object->getClassName(), sfxObject );
  187. #endif
  188. // Remove the object from the SFX world.
  189. if( sfxObject->isListener() )
  190. mSFXWorld.setReferenceObject( NULL );
  191. else
  192. mSFXWorld.unregisterObject( sfxObject );
  193. // Destroy the scene object link.
  194. destructInPlace( sfxObject );
  195. mChunker.free( sfxObject );
  196. }
  197. //-----------------------------------------------------------------------------
  198. void SFX3DWorld::updateObject( SceneObjectLink* object )
  199. {
  200. SFX3DObject* sfxObject = dynamic_cast< SFX3DObject* >( object );
  201. AssertFatal( sfxObject, "SFX3DWorld::updateObject - invalid object type" );
  202. mSFXWorld.updateObject( sfxObject );
  203. // If this is the listener object, update its
  204. // properties on the SFX system.
  205. if( sfxObject->isListener() )
  206. {
  207. SFXListenerProperties listener;
  208. sfxObject->getEarTransform( listener.getTransform() );
  209. listener.getVelocity() = sfxObject->getObject()->getVelocity();
  210. SFX->setListener( 0, listener );
  211. }
  212. }
  213. //-----------------------------------------------------------------------------
  214. SceneObject* SFX3DWorld::getListener() const
  215. {
  216. SFX3DObject* object = mSFXWorld.getReferenceObject();
  217. if( !object )
  218. return NULL;
  219. return object->getObject();
  220. }
  221. //-----------------------------------------------------------------------------
  222. void SFX3DWorld::setListener( SceneObject* object )
  223. {
  224. SFX3DObject* oldListener = mSFXWorld.getReferenceObject();
  225. // If it's the same object as our current listener,
  226. // return.
  227. if( oldListener && oldListener->getObject() == object )
  228. return;
  229. // Create a SFX3DObject for the given SceneObject.
  230. SFX3DObject* sfxObject = NULL;
  231. if( object )
  232. {
  233. AssertFatal( !dynamic_cast< SFX3DObject* >( SFX3DObject::getLinkForTracker( this, object ) ),
  234. "SFX3DWorld::setListener - listener objects must not be registered for tracking" );
  235. sfxObject = mChunker.alloc();
  236. constructInPlace( sfxObject, this, object );
  237. sfxObject->setFlags( SFXObjectListener );
  238. }
  239. #ifdef DEBUG_SPEW
  240. if( object )
  241. Platform::outputDebugString( "[SFX3DWorld] Listener is now %i:%s (%s)",
  242. object->getId(), object->getClassName(), object->getName() );
  243. else
  244. Platform::outputDebugString( "[SFX3DWorld] Unsetting listener" );
  245. #endif
  246. // Make this object the center of our SFX world.
  247. mSFXWorld.setReferenceObject( sfxObject );
  248. // Remove the tracking links from the old listener so we
  249. // don't see further updates on it.
  250. if( oldListener )
  251. {
  252. destructInPlace( oldListener );
  253. mChunker.free( oldListener );
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. void SFX3DWorld::notifyChanged( SceneObject* object )
  258. {
  259. SFX3DObject* sfxObject = dynamic_cast< SFX3DObject* >( SFX3DObject::getLinkForTracker( this, object ) );
  260. if( !sfxObject && _isTrackableObject( object ) )
  261. registerObject( object );
  262. else if( sfxObject && !_isTrackableObject( object ) )
  263. unregisterObject( object );
  264. else if( sfxObject )
  265. mSFXWorld.notifyChanged( sfxObject );
  266. }
  267. //-----------------------------------------------------------------------------
  268. void SFX3DWorld::debugDump()
  269. {
  270. mSFXWorld.debugDump();
  271. }