sceneManager.cpp 25 KB

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