sceneManager.cpp 24 KB

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