2
0

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