sfx3DWorld.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. GameConnection* connection = dynamic_cast<GameConnection *>(NetConnection::getConnectionToServer());
  77. if ( !connection || !connection->getControlCameraTransform( 0.0f, &transform ) )
  78. transform = mObject->getTransform();
  79. return;
  80. }
  81. ShapeBaseData* datablock = dynamic_cast< ShapeBaseData* >( shape->getDataBlock() );
  82. AssertFatal( datablock, "SFX3DObject::getEarTransform() - shape without ShapeBaseData datablock!" );
  83. // Get the transform for the ear node.
  84. const S32 earNode = datablock->earNode;
  85. if ( earNode != -1 && earNode != datablock->eyeNode )
  86. {
  87. transform = shape->getTransform();
  88. transform *= shapeInstance->mNodeTransforms[ earNode ];
  89. }
  90. else
  91. {
  92. GameConnection* connection = dynamic_cast<GameConnection *>(NetConnection::getConnectionToServer());
  93. if ( !connection || !connection->getControlCameraTransform( 0.0f, &transform ) )
  94. transform = mObject->getTransform();
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. void SFX3DObject::getReferenceCenter( F32 position[ 3 ] ) const
  99. {
  100. MatrixF transform;
  101. getEarTransform( transform );
  102. Point3F pos = transform.getPosition();
  103. AssertFatal( TypeTraits< F32 >::MIN <= pos.x && pos.x <= TypeTraits< F32 >::MAX,
  104. "SFX3DObject::getReferenceCenter - invalid float in reference center X position" );
  105. AssertFatal( TypeTraits< F32 >::MIN <= pos.y && pos.y <= TypeTraits< F32 >::MAX,
  106. "SFX3DObject::getReferenceCenter - invalid float in reference center Y position" );
  107. AssertFatal( TypeTraits< F32 >::MIN <= pos.z && pos.z <= TypeTraits< F32 >::MAX,
  108. "SFX3DObject::getReferenceCenter - invalid float in reference center Z position" );
  109. dMemcpy( position, &pos.x, sizeof( F32 ) * 3 );
  110. }
  111. //-----------------------------------------------------------------------------
  112. void SFX3DObject::getBounds( F32 minBounds[ 3 ], F32 maxBounds[ 3 ] ) const
  113. {
  114. getRealBounds( minBounds, maxBounds );
  115. }
  116. //-----------------------------------------------------------------------------
  117. void SFX3DObject::getRealBounds( F32 minBounds[ 3 ], F32 maxBounds[ 3 ] ) const
  118. {
  119. const Box3F& worldBox = mObject->getWorldBox();
  120. dMemcpy( minBounds, &worldBox.minExtents.x, sizeof( F32 ) * 3 );
  121. dMemcpy( maxBounds, &worldBox.maxExtents.x, sizeof( F32 ) * 3 );
  122. }
  123. //-----------------------------------------------------------------------------
  124. SFXAmbience* SFX3DObject::getAmbience() const
  125. {
  126. return mObject->getSoundAmbience();
  127. }
  128. //-----------------------------------------------------------------------------
  129. bool SFX3DObject::containsPoint( const F32 point[ 3 ] ) const
  130. {
  131. return mObject->containsPoint( *( reinterpret_cast< const Point3F* >( &point[ 0 ] ) ) );
  132. }
  133. //-----------------------------------------------------------------------------
  134. String SFX3DObject::describeSelf() const
  135. {
  136. return mObject->describeSelf();
  137. }
  138. //=============================================================================
  139. // SFX3DWorld.
  140. //=============================================================================
  141. //-----------------------------------------------------------------------------
  142. SFX3DWorld::SFX3DWorld()
  143. : Parent( true, TYPEMASK )
  144. {
  145. }
  146. //-----------------------------------------------------------------------------
  147. bool SFX3DWorld::_isTrackableObject( SceneObject* object ) const
  148. {
  149. if( !Parent::_isTrackableObject( object ) )
  150. return false;
  151. // We are only interested in occluders, zones, and portals.
  152. if( object->isOccludingSound() )
  153. return true;
  154. else if( object->getSoundAmbience() )
  155. return true;
  156. else if( dynamic_cast< Portal* >( object ) )
  157. return true;
  158. return false;
  159. }
  160. //-----------------------------------------------------------------------------
  161. void SFX3DWorld::update()
  162. {
  163. mSFXWorld.update();
  164. }
  165. //-----------------------------------------------------------------------------
  166. void SFX3DWorld::registerObject( SceneObject* object )
  167. {
  168. if( !_isTrackableObject( object ) )
  169. return;
  170. // Construct a new scene object link.
  171. SFX3DObject* sfxObject = mChunker.alloc();
  172. constructInPlace( sfxObject, this, object );
  173. // Register it with the SFX world.
  174. #ifdef DEBUG_SPEW
  175. Platform::outputDebugString( "[SFX3DWorld] Registering %i:%s as 0x%x",
  176. object->getId(), object->getClassName(), sfxObject );
  177. #endif
  178. mSFXWorld.registerObject( sfxObject );
  179. }
  180. //-----------------------------------------------------------------------------
  181. void SFX3DWorld::unregisterObject( SceneObject* object )
  182. {
  183. SFX3DObject* sfxObject = dynamic_cast< SFX3DObject* >( SFX3DObject::getLinkForTracker( this, object ) );
  184. if( !sfxObject )
  185. return;
  186. #ifdef DEBUG_SPEW
  187. Platform::outputDebugString( "[SFX3DWorld] Unregistering %i:%s (was 0x%x)",
  188. object->getId(), object->getClassName(), sfxObject );
  189. #endif
  190. // Remove the object from the SFX world.
  191. if( sfxObject->isListener() )
  192. mSFXWorld.setReferenceObject( NULL );
  193. else
  194. mSFXWorld.unregisterObject( sfxObject );
  195. // Destroy the scene object link.
  196. destructInPlace( sfxObject );
  197. mChunker.free( sfxObject );
  198. }
  199. //-----------------------------------------------------------------------------
  200. void SFX3DWorld::updateObject( SceneObjectLink* object )
  201. {
  202. SFX3DObject* sfxObject = dynamic_cast< SFX3DObject* >( object );
  203. AssertFatal( sfxObject, "SFX3DWorld::updateObject - invalid object type" );
  204. mSFXWorld.updateObject( sfxObject );
  205. // If this is the listener object, update its
  206. // properties on the SFX system.
  207. if( sfxObject->isListener() )
  208. {
  209. SFXListenerProperties listener;
  210. sfxObject->getEarTransform( listener.getTransform() );
  211. listener.getVelocity() = sfxObject->getObject()->getVelocity();
  212. SFX->setListener( 0, listener );
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. SceneObject* SFX3DWorld::getListener() const
  217. {
  218. SFX3DObject* object = mSFXWorld.getReferenceObject();
  219. if( !object )
  220. return NULL;
  221. return object->getObject();
  222. }
  223. //-----------------------------------------------------------------------------
  224. void SFX3DWorld::setListener( SceneObject* object )
  225. {
  226. SFX3DObject* oldListener = mSFXWorld.getReferenceObject();
  227. // If it's the same object as our current listener,
  228. // return.
  229. if( oldListener && oldListener->getObject() == object )
  230. return;
  231. // Create a SFX3DObject for the given SceneObject.
  232. SFX3DObject* sfxObject = NULL;
  233. if( object )
  234. {
  235. AssertFatal( !dynamic_cast< SFX3DObject* >( SFX3DObject::getLinkForTracker( this, object ) ),
  236. "SFX3DWorld::setListener - listener objects must not be registered for tracking" );
  237. sfxObject = mChunker.alloc();
  238. constructInPlace( sfxObject, this, object );
  239. sfxObject->setFlags( SFXObjectListener );
  240. }
  241. #ifdef DEBUG_SPEW
  242. if( object )
  243. Platform::outputDebugString( "[SFX3DWorld] Listener is now %i:%s (%s)",
  244. object->getId(), object->getClassName(), object->getName() );
  245. else
  246. Platform::outputDebugString( "[SFX3DWorld] Unsetting listener" );
  247. #endif
  248. // Make this object the center of our SFX world.
  249. mSFXWorld.setReferenceObject( sfxObject );
  250. // Remove the tracking links from the old listener so we
  251. // don't see further updates on it.
  252. if( oldListener )
  253. {
  254. destructInPlace( oldListener );
  255. mChunker.free( oldListener );
  256. }
  257. }
  258. //-----------------------------------------------------------------------------
  259. void SFX3DWorld::notifyChanged( SceneObject* object )
  260. {
  261. SFX3DObject* sfxObject = dynamic_cast< SFX3DObject* >( SFX3DObject::getLinkForTracker( this, object ) );
  262. if( !sfxObject && _isTrackableObject( object ) )
  263. registerObject( object );
  264. else if( sfxObject && !_isTrackableObject( object ) )
  265. unregisterObject( object );
  266. else if( sfxObject )
  267. mSFXWorld.notifyChanged( sfxObject );
  268. }
  269. //-----------------------------------------------------------------------------
  270. void SFX3DWorld::debugDump()
  271. {
  272. mSFXWorld.debugDump();
  273. }