sceneManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 "platform/platform.h"
  23. #include "scene/sceneManager.h"
  24. #include "scene/sceneObject.h"
  25. #include "scene/zones/sceneTraversalState.h"
  26. #include "scene/sceneRenderState.h"
  27. #include "scene/zones/sceneRootZone.h"
  28. #include "scene/zones/sceneZoneSpace.h"
  29. #include "lighting/lightManager.h"
  30. #include "renderInstance/renderPassManager.h"
  31. #include "gfx/gfxDevice.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "gfx/gfxDebugEvent.h"
  34. #include "console/engineAPI.h"
  35. #include "sim/netConnection.h"
  36. #include "T3D/gameBase/gameConnection.h"
  37. // For player object bounds workaround.
  38. #include "T3D/player.h"
  39. extern bool gEditingMission;
  40. MODULE_BEGIN( Scene )
  41. MODULE_INIT_AFTER( Sim )
  42. MODULE_SHUTDOWN_BEFORE( Sim )
  43. MODULE_INIT
  44. {
  45. // Client scene.
  46. gClientSceneGraph = new SceneManager( true );
  47. // Server scene.
  48. gServerSceneGraph = new SceneManager( false );
  49. Con::addVariable( "$Scene::lockCull", TypeBool, &SceneManager::smLockDiffuseFrustum,
  50. "Debug tool which locks the frustum culling to the current camera location.\n"
  51. "@ingroup Rendering\n" );
  52. Con::addVariable( "$Scene::disableTerrainOcclusion", TypeBool, &SceneCullingState::smDisableTerrainOcclusion,
  53. "Used to disable the somewhat expensive terrain occlusion testing.\n"
  54. "@ingroup Rendering\n" );
  55. Con::addVariable( "$Scene::disableZoneCulling", TypeBool, &SceneCullingState::smDisableZoneCulling,
  56. "If true, zone culling will be disabled and the scene contents will only be culled against the root frustum.\n\n"
  57. "@ingroup Rendering\n" );
  58. Con::addVariable( "$Scene::renderBoundingBoxes", TypeBool, &SceneManager::smRenderBoundingBoxes,
  59. "If true, the bounding boxes of objects will be displayed.\n\n"
  60. "@ingroup Rendering" );
  61. Con::addVariable( "$Scene::maxOccludersPerZone", TypeS32, &SceneCullingState::smMaxOccludersPerZone,
  62. "Maximum number of occluders that will be concurrently allowed into the scene culling state of any given zone.\n\n"
  63. "@ingroup Rendering" );
  64. Con::addVariable( "$Scene::occluderMinWidthPercentage", TypeF32, &SceneCullingState::smOccluderMinWidthPercentage,
  65. "TODO\n\n"
  66. "@ingroup Rendering" );
  67. Con::addVariable( "$Scene::occluderMinHeightPercentage", TypeF32, &SceneCullingState::smOccluderMinHeightPercentage,
  68. "TODO\n\n"
  69. "@ingroup Rendering" );
  70. }
  71. MODULE_SHUTDOWN
  72. {
  73. SAFE_DELETE( gClientSceneGraph );
  74. SAFE_DELETE( gServerSceneGraph );
  75. }
  76. MODULE_END;
  77. bool SceneManager::smRenderBoundingBoxes;
  78. bool SceneManager::smLockDiffuseFrustum = false;
  79. SceneCameraState SceneManager::smLockedDiffuseCamera = SceneCameraState( RectI(), Frustum(), MatrixF(), MatrixF() );
  80. SceneManager* gClientSceneGraph = NULL;
  81. SceneManager* gServerSceneGraph = NULL;
  82. //-----------------------------------------------------------------------------
  83. SceneManager::SceneManager( bool isClient )
  84. : mLightManager( NULL ),
  85. mCurrentRenderState( NULL ),
  86. mIsClient( isClient ),
  87. mUsePostEffectFog( true ),
  88. mDisplayTargetResolution( 0, 0 ),
  89. mDefaultRenderPass( NULL ),
  90. mVisibleDistance( 500.f ),
  91. mNearClip( 0.1f ),
  92. mAmbientLightColor( ColorF( 0.1f, 0.1f, 0.1f, 1.0f ) ),
  93. mZoneManager( NULL )
  94. {
  95. VECTOR_SET_ASSOCIATION( mBatchQueryList );
  96. // For the client, create a zone manager.
  97. if( isClient )
  98. {
  99. mZoneManager = new SceneZoneSpaceManager( getContainer() );
  100. // Add the root zone to the scene.
  101. addObjectToScene( mZoneManager->getRootZone() );
  102. }
  103. }
  104. //-----------------------------------------------------------------------------
  105. SceneManager::~SceneManager()
  106. {
  107. SAFE_DELETE( mZoneManager );
  108. if( mLightManager )
  109. mLightManager->deactivate();
  110. }
  111. //-----------------------------------------------------------------------------
  112. void SceneManager::renderScene( ScenePassType passType, U32 objectMask )
  113. {
  114. SceneCameraState cameraState = SceneCameraState::fromGFX();
  115. // Handle frustum locking.
  116. const bool lockedFrustum = ( smLockDiffuseFrustum && passType == SPT_Diffuse );
  117. if( lockedFrustum )
  118. cameraState = smLockedDiffuseCamera;
  119. else if( passType == SPT_Diffuse )
  120. {
  121. // Store the camera state so if we lock, this will become the
  122. // locked state.
  123. smLockedDiffuseCamera = cameraState;
  124. }
  125. // Create the render state.
  126. SceneRenderState renderState( this, passType, cameraState );
  127. // If we have locked the frustum, reset the view transform
  128. // on the render pass which the render state has just set
  129. // to the view matrix corresponding to the locked frustum. For
  130. // rendering, however, we need the true view matrix from the
  131. // GFX state.
  132. if( lockedFrustum )
  133. {
  134. RenderPassManager* rpm = renderState.getRenderPass();
  135. rpm->assignSharedXform( RenderPassManager::View, GFX->getWorldMatrix() );
  136. }
  137. // Render.
  138. renderScene( &renderState, objectMask );
  139. }
  140. //-----------------------------------------------------------------------------
  141. void SceneManager::renderScene( SceneRenderState* renderState, U32 objectMask, SceneZoneSpace* baseObject, U32 baseZone )
  142. {
  143. PROFILE_SCOPE( SceneGraph_renderScene );
  144. // Get the lights for rendering the scene.
  145. PROFILE_START( SceneGraph_registerLights );
  146. LIGHTMGR->registerGlobalLights( &renderState->getCullingFrustum(), false );
  147. PROFILE_END();
  148. // If its a diffuse pass, update the current ambient light level.
  149. // To do that find the starting zone and determine whether it has a custom
  150. // ambient light color. If so, pass it on to the ambient light manager.
  151. // If not, use the ambient light color of the sunlight.
  152. //
  153. // Note that we retain the starting zone information here and pass it
  154. // on to renderSceneNoLights so that we don't need to look it up twice.
  155. if( renderState->isDiffusePass() )
  156. {
  157. if( !baseObject && getZoneManager() )
  158. {
  159. getZoneManager()->findZone( renderState->getCameraPosition(), baseObject, baseZone );
  160. AssertFatal( baseObject != NULL, "SceneManager::renderScene - findZone() did not return an object" );
  161. }
  162. ColorF zoneAmbient;
  163. if( baseObject && baseObject->getZoneAmbientLightColor( baseZone, zoneAmbient ) )
  164. mAmbientLightColor.setTargetValue( zoneAmbient );
  165. else
  166. {
  167. const LightInfo* sunlight = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  168. if( sunlight )
  169. mAmbientLightColor.setTargetValue( sunlight->getAmbient() );
  170. }
  171. renderState->setAmbientLightColor( mAmbientLightColor.getCurrentValue() );
  172. }
  173. // Trigger the pre-render signal.
  174. PROFILE_START( SceneGraph_preRenderSignal);
  175. mCurrentRenderState = renderState;
  176. getPreRenderSignal().trigger( this, renderState );
  177. mCurrentRenderState = NULL;
  178. PROFILE_END();
  179. // Render the scene.
  180. if(GFX->getCurrentRenderStyle() == GFXDevice::RS_StereoSideBySide)
  181. {
  182. // Store previous values
  183. RectI originalVP = GFX->getViewport();
  184. MatrixF originalWorld = GFX->getWorldMatrix();
  185. Point2F projOffset = GFX->getCurrentProjectionOffset();
  186. Point3F eyeOffset = GFX->getStereoEyeOffset();
  187. // Indicate that we're about to start a field
  188. GFX->beginField();
  189. // Render left half of display
  190. RectI leftVP = originalVP;
  191. leftVP.extent.x *= 0.5;
  192. GFX->setViewport(leftVP);
  193. MatrixF leftWorldTrans(true);
  194. leftWorldTrans.setPosition(Point3F(eyeOffset.x, eyeOffset.y, eyeOffset.z));
  195. MatrixF leftWorld(originalWorld);
  196. leftWorld.mulL(leftWorldTrans);
  197. GFX->setWorldMatrix(leftWorld);
  198. Frustum gfxFrustum = GFX->getFrustum();
  199. gfxFrustum.setProjectionOffset(Point2F(projOffset.x, projOffset.y));
  200. GFX->setFrustum(gfxFrustum);
  201. SceneCameraState cameraStateLeft = SceneCameraState::fromGFX();
  202. SceneRenderState renderStateLeft( this, renderState->getScenePassType(), cameraStateLeft );
  203. renderStateLeft.setSceneRenderStyle(SRS_SideBySide);
  204. renderStateLeft.setSceneRenderField(0);
  205. renderSceneNoLights( &renderStateLeft, objectMask, baseObject, baseZone );
  206. // Indicate that we've just finished a field
  207. GFX->endField();
  208. // Indicate that we're about to start a field
  209. GFX->beginField();
  210. // Render right half of display
  211. RectI rightVP = originalVP;
  212. rightVP.extent.x *= 0.5;
  213. rightVP.point.x += rightVP.extent.x;
  214. GFX->setViewport(rightVP);
  215. MatrixF rightWorldTrans(true);
  216. rightWorldTrans.setPosition(Point3F(-eyeOffset.x, eyeOffset.y, eyeOffset.z));
  217. MatrixF rightWorld(originalWorld);
  218. rightWorld.mulL(rightWorldTrans);
  219. GFX->setWorldMatrix(rightWorld);
  220. gfxFrustum = GFX->getFrustum();
  221. gfxFrustum.setProjectionOffset(Point2F(-projOffset.x, projOffset.y));
  222. GFX->setFrustum(gfxFrustum);
  223. SceneCameraState cameraStateRight = SceneCameraState::fromGFX();
  224. SceneRenderState renderStateRight( this, renderState->getScenePassType(), cameraStateRight );
  225. renderStateRight.setSceneRenderStyle(SRS_SideBySide);
  226. renderStateRight.setSceneRenderField(1);
  227. renderSceneNoLights( &renderStateRight, objectMask, baseObject, baseZone );
  228. // Indicate that we've just finished a field
  229. GFX->endField();
  230. // Restore previous values
  231. GFX->setWorldMatrix(originalWorld);
  232. gfxFrustum.clearProjectionOffset();
  233. GFX->setFrustum(gfxFrustum);
  234. GFX->setViewport(originalVP);
  235. }
  236. else
  237. {
  238. renderSceneNoLights( renderState, objectMask, baseObject, baseZone );
  239. }
  240. // Trigger the post-render signal.
  241. PROFILE_START( SceneGraphRender_postRenderSignal );
  242. mCurrentRenderState = renderState;
  243. getPostRenderSignal().trigger( this, renderState );
  244. mCurrentRenderState = NULL;
  245. PROFILE_END();
  246. // Remove the previously registered lights.
  247. PROFILE_START( SceneGraph_unregisterLights);
  248. LIGHTMGR->unregisterAllLights();
  249. PROFILE_END();
  250. }
  251. //-----------------------------------------------------------------------------
  252. void SceneManager::renderSceneNoLights( SceneRenderState* renderState, U32 objectMask, SceneZoneSpace* baseObject, U32 baseZone )
  253. {
  254. // Set the current state.
  255. mCurrentRenderState = renderState;
  256. // Render.
  257. _renderScene( mCurrentRenderState, objectMask, baseObject, baseZone );
  258. #ifdef TORQUE_DEBUG
  259. // If frustum is locked and this is a diffuse pass, render the culling volumes of
  260. // zones that are selected (or the volumes of the outdoor zone if no zone is
  261. // selected).
  262. if( gEditingMission && renderState->isDiffusePass() && smLockDiffuseFrustum )
  263. renderState->getCullingState().debugRenderCullingVolumes();
  264. #endif
  265. mCurrentRenderState = NULL;
  266. }
  267. //-----------------------------------------------------------------------------
  268. void SceneManager::_renderScene( SceneRenderState* state, U32 objectMask, SceneZoneSpace* baseObject, U32 baseZone )
  269. {
  270. AssertFatal( this == gClientSceneGraph, "SceneManager::_buildSceneGraph - Only the client scenegraph can support this call!" );
  271. PROFILE_SCOPE( SceneGraph_batchRenderImages );
  272. // In the editor, override the type mask for diffuse passes.
  273. if( gEditingMission && state->isDiffusePass() )
  274. objectMask = EDITOR_RENDER_TYPEMASK;
  275. // Update the zoning state and traverse zones.
  276. if( getZoneManager() )
  277. {
  278. // Update.
  279. getZoneManager()->updateZoningState();
  280. // If zone culling isn't disabled, traverse the
  281. // zones now.
  282. if( !state->getCullingState().disableZoneCulling() )
  283. {
  284. // Find the start zone if we haven't already.
  285. if( !baseObject )
  286. {
  287. getZoneManager()->findZone( state->getCameraPosition(), baseObject, baseZone );
  288. AssertFatal( baseObject != NULL, "SceneManager::_renderScene - findZone() did not return an object" );
  289. }
  290. // Traverse zones starting in base object.
  291. SceneTraversalState traversalState( &state->getCullingState() );
  292. PROFILE_START( Scene_traverseZones );
  293. baseObject->traverseZones( &traversalState, baseZone );
  294. PROFILE_END();
  295. // Set the scene render box to the area we have traversed.
  296. state->setRenderArea( traversalState.getTraversedArea() );
  297. }
  298. }
  299. // Set the query box for the container query. Never
  300. // make it larger than the frustum's AABB. In the editor,
  301. // always query the full frustum as that gives objects
  302. // the opportunity to render editor visualizations even if
  303. // they are otherwise not in view.
  304. if( !state->getCullingFrustum().getBounds().isOverlapped( state->getRenderArea() ) )
  305. {
  306. // This handles fringe cases like flying backwards into a zone where you
  307. // end up pretty much standing on a zone border and looking directly into
  308. // its "walls". In that case the traversal area will be behind the frustum
  309. // (remember that the camera isn't where visibility starts, it's the near
  310. // distance).
  311. return;
  312. }
  313. Box3F queryBox = state->getCullingFrustum().getBounds();
  314. if( !gEditingMission )
  315. {
  316. queryBox.minExtents.setMax( state->getRenderArea().minExtents );
  317. queryBox.maxExtents.setMin( state->getRenderArea().maxExtents );
  318. }
  319. PROFILE_START( Scene_cullObjects );
  320. //TODO: We should split the codepaths here based on whether the outdoor zone has visible space.
  321. // If it has, we should use the container query-based path.
  322. // If it hasn't, we should fill the object list directly from the zone lists which will usually
  323. // include way fewer objects.
  324. // Gather all objects that intersect the scene render box.
  325. mBatchQueryList.clear();
  326. getContainer()->findObjectList( queryBox, objectMask, &mBatchQueryList );
  327. // Cull the list.
  328. U32 numRenderObjects = state->getCullingState().cullObjects(
  329. mBatchQueryList.address(),
  330. mBatchQueryList.size(),
  331. !state->isDiffusePass() ? SceneCullingState::CullEditorOverrides : 0 // Keep forced editor stuff out of non-diffuse passes.
  332. );
  333. //HACK: If the control object is a Player and it is not in the render list, force
  334. // it into it. This really should be solved by collision bounds being separate from
  335. // object bounds; only because the Player class is using bounds not encompassing
  336. // the actual player object is it that we have this problem in the first place.
  337. // Note that we are forcing the player object into ALL passes here but such
  338. // is the power of proliferation of things done wrong.
  339. GameConnection* connection = GameConnection::getConnectionToServer();
  340. if( connection )
  341. {
  342. Player* player = dynamic_cast< Player* >( connection->getControlObject() );
  343. if( player )
  344. {
  345. mBatchQueryList.setSize( numRenderObjects );
  346. if( !mBatchQueryList.contains( player ) )
  347. {
  348. mBatchQueryList.push_back( player );
  349. numRenderObjects ++;
  350. }
  351. }
  352. }
  353. PROFILE_END();
  354. // Render the remaining objects.
  355. PROFILE_START( Scene_renderObjects );
  356. state->renderObjects( mBatchQueryList.address(), numRenderObjects );
  357. PROFILE_END();
  358. // Render bounding boxes, if enabled.
  359. if( smRenderBoundingBoxes && state->isDiffusePass() )
  360. {
  361. GFXDEBUGEVENT_SCOPE( Scene_renderBoundingBoxes, ColorI::WHITE );
  362. GameBase* cameraObject = 0;
  363. if( connection )
  364. cameraObject = connection->getCameraObject();
  365. GFXStateBlockDesc desc;
  366. desc.setFillModeWireframe();
  367. desc.setZReadWrite( true, false );
  368. for( U32 i = 0; i < numRenderObjects; ++ i )
  369. {
  370. SceneObject* object = mBatchQueryList[ i ];
  371. // Skip global bounds object.
  372. if( object->isGlobalBounds() )
  373. continue;
  374. // Skip camera object as we're viewing the scene from it.
  375. if( object == cameraObject )
  376. continue;
  377. const Box3F& worldBox = object->getWorldBox();
  378. GFX->getDrawUtil()->drawObjectBox(
  379. desc,
  380. Point3F( worldBox.len_x(), worldBox.len_y(), worldBox.len_z() ),
  381. worldBox.getCenter(),
  382. MatrixF::Identity,
  383. ColorI::WHITE
  384. );
  385. }
  386. }
  387. }
  388. //-----------------------------------------------------------------------------
  389. struct ScopingInfo
  390. {
  391. Point3F scopePoint;
  392. F32 scopeDist;
  393. F32 scopeDistSquared;
  394. NetConnection* connection;
  395. };
  396. static void _scopeCallback( SceneObject* object, void* data )
  397. {
  398. if( !object->isScopeable() )
  399. return;
  400. ScopingInfo* info = reinterpret_cast< ScopingInfo* >( data );
  401. NetConnection* connection = info->connection;
  402. F32 difSq = ( object->getWorldSphere().center - info->scopePoint ).lenSquared();
  403. if( difSq < info->scopeDistSquared )
  404. {
  405. // Not even close, it's in...
  406. connection->objectInScope( object );
  407. }
  408. else
  409. {
  410. // Check a little more closely...
  411. F32 realDif = mSqrt( difSq );
  412. if( realDif - object->getWorldSphere().radius < info->scopeDist)
  413. connection->objectInScope( object );
  414. }
  415. }
  416. void SceneManager::scopeScene( CameraScopeQuery* query, NetConnection* netConnection )
  417. {
  418. PROFILE_SCOPE( SceneGraph_scopeScene );
  419. // Note that this method does not use the zoning information in the scene
  420. // to scope objects. The reason is that with the way that scoping is implemented
  421. // in the networking layer--i.e. by killing off ghosts of objects that are out
  422. // of scope--, it doesn't make sense to let, for example, all objects in the outdoor
  423. // zone go out of scope, just because there is no exterior portal that is visible from
  424. // the current camera viewpoint (in any direction).
  425. //
  426. // So, we perform a simple box query on the area covered by the camera query
  427. // and then scope in everything that is in range.
  428. // Set up scoping info.
  429. ScopingInfo info;
  430. info.scopePoint = query->pos;
  431. info.scopeDist = query->visibleDistance;
  432. info.scopeDistSquared = info.scopeDist * info.scopeDist;
  433. info.connection = netConnection;
  434. // Scope all objects in the query area.
  435. Box3F area( query->visibleDistance );
  436. area.setCenter( query->pos );
  437. getContainer()->findObjects( area, 0xFFFFFFFF, _scopeCallback, &info );
  438. }
  439. //-----------------------------------------------------------------------------
  440. bool SceneManager::addObjectToScene( SceneObject* object )
  441. {
  442. AssertFatal( !object->mSceneManager, "SceneManager::addObjectToScene - Object already part of a scene" );
  443. // Mark the object as belonging to us.
  444. object->mSceneManager = this;
  445. // Register with managers except its the root zone.
  446. if( !dynamic_cast< SceneRootZone* >( object ) )
  447. {
  448. // Add to container.
  449. getContainer()->addObject( object );
  450. // Register the object with the zone manager.
  451. if( getZoneManager() )
  452. getZoneManager()->registerObject( object );
  453. }
  454. // Notify the object.
  455. return object->onSceneAdd();
  456. }
  457. //-----------------------------------------------------------------------------
  458. void SceneManager::removeObjectFromScene( SceneObject* obj )
  459. {
  460. AssertFatal( obj, "SceneManager::removeObjectFromScene - Object is not declared" );
  461. AssertFatal( obj->getSceneManager() == this, "SceneManager::removeObjectFromScene - Object not part of SceneManager" );
  462. // Notify the object.
  463. obj->onSceneRemove();
  464. // Remove the object from the container.
  465. if( getContainer() )
  466. getContainer()->removeObject( obj );
  467. // Remove the object from the zoning system.
  468. if( getZoneManager() )
  469. getZoneManager()->unregisterObject( obj );
  470. // Clear out the reference to us.
  471. obj->mSceneManager = NULL;
  472. }
  473. //-----------------------------------------------------------------------------
  474. void SceneManager::notifyObjectDirty( SceneObject* object )
  475. {
  476. // Update container state.
  477. if( object->mContainer )
  478. object->mContainer->checkBins( object );
  479. // Mark zoning state as dirty.
  480. if( getZoneManager() )
  481. getZoneManager()->notifyObjectChanged( object );
  482. }
  483. //-----------------------------------------------------------------------------
  484. void SceneManager::setDisplayTargetResolution( const Point2I &size )
  485. {
  486. mDisplayTargetResolution = size;
  487. }
  488. //-----------------------------------------------------------------------------
  489. const Point2I & SceneManager::getDisplayTargetResolution() const
  490. {
  491. return mDisplayTargetResolution;
  492. }
  493. //-----------------------------------------------------------------------------
  494. bool SceneManager::setLightManager( const char* lmName )
  495. {
  496. LightManager *lm = LightManager::findByName( lmName );
  497. if ( !lm )
  498. return false;
  499. return _setLightManager( lm );
  500. }
  501. //-----------------------------------------------------------------------------
  502. bool SceneManager::_setLightManager( LightManager* lm )
  503. {
  504. // Avoid unnecessary work reinitializing materials.
  505. if ( lm == mLightManager )
  506. return true;
  507. // Make sure its valid... else fail!
  508. if ( !lm->isCompatible() )
  509. return false;
  510. // We only deactivate it... all light managers are singletons
  511. // and will manager their own lifetime.
  512. if ( mLightManager )
  513. mLightManager->deactivate();
  514. mLightManager = lm;
  515. if ( mLightManager )
  516. mLightManager->activate( this );
  517. return true;
  518. }
  519. //-----------------------------------------------------------------------------
  520. RenderPassManager* SceneManager::getDefaultRenderPass() const
  521. {
  522. if( !mDefaultRenderPass )
  523. {
  524. Sim::findObject( "DiffuseRenderPassManager", mDefaultRenderPass );
  525. AssertISV( mDefaultRenderPass, "SceneManager::_setDefaultRenderPass - No DiffuseRenderPassManager defined! Must be set up in script!" );
  526. }
  527. return mDefaultRenderPass;
  528. }
  529. //=============================================================================
  530. // Console API.
  531. //=============================================================================
  532. // MARK: ---- Console API ----
  533. //-----------------------------------------------------------------------------
  534. DefineConsoleFunction( sceneDumpZoneStates, void, ( bool updateFirst ), ( true ),
  535. "Dump the current zoning states of all zone spaces in the scene to the console.\n\n"
  536. "@param updateFirst If true, zoning states are brought up to date first; if false, the zoning states "
  537. "are dumped as is.\n\n"
  538. "@note Only valid on the client.\n"
  539. "@ingroup Game" )
  540. {
  541. if( !gClientSceneGraph )
  542. {
  543. Con::errorf( "sceneDumpZoneStates - Only valid on client!" );
  544. return;
  545. }
  546. SceneZoneSpaceManager* manager = gClientSceneGraph->getZoneManager();
  547. if( !manager )
  548. {
  549. Con::errorf( "sceneDumpZoneStates - Scene is not using zones!" );
  550. return;
  551. }
  552. manager->dumpZoneStates( updateFirst );
  553. }
  554. //-----------------------------------------------------------------------------
  555. DefineConsoleFunction( sceneGetZoneOwner, SceneObject*, ( U32 zoneId ), ( true ),
  556. "Return the SceneObject that contains the given zone.\n\n"
  557. "@param zoneId ID of zone.\n"
  558. "@return A SceneObject or NULL if the given @a zoneId is invalid.\n\n"
  559. "@note Only valid on the client.\n"
  560. "@ingroup Game" )
  561. {
  562. if( !gClientSceneGraph )
  563. {
  564. Con::errorf( "sceneGetZoneOwner - Only valid on client!" );
  565. return NULL;
  566. }
  567. SceneZoneSpaceManager* manager = gClientSceneGraph->getZoneManager();
  568. if( !manager )
  569. {
  570. Con::errorf( "sceneGetZoneOwner - Scene is not using zones!" );
  571. return NULL;
  572. }
  573. if( !manager->isValidZoneId( zoneId ) )
  574. {
  575. Con::errorf( "sceneGetZoneOwner - Invalid zone ID: %i", zoneId );
  576. return NULL;
  577. }
  578. return manager->getZoneOwner( zoneId );
  579. }