sfx3DWorld.cpp 11 KB

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