sceneManager.cpp 25 KB

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