sceneManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #ifdef GHOSTSCOPING
  92. mVisibleGhostDistance(GHOSTSCOPING_DEFAULT_DISTANCE_IF_NOT_IN_MISSION),
  93. #endif
  94. mNearClip( 0.1f ),
  95. mAmbientLightColor( ColorF( 0.1f, 0.1f, 0.1f, 1.0f ) ),
  96. mZoneManager( 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. ColorF 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. Point2F projOffset = GFX->getCurrentProjectionOffset();
  189. Point3F eyeOffset = GFX->getStereoEyeOffset();
  190. // Indicate that we're about to start a field
  191. GFX->beginField();
  192. // Render left half of display
  193. RectI leftVP = originalVP;
  194. leftVP.extent.x *= 0.5;
  195. GFX->setViewport(leftVP);
  196. MatrixF leftWorldTrans(true);
  197. leftWorldTrans.setPosition(Point3F(eyeOffset.x, eyeOffset.y, eyeOffset.z));
  198. MatrixF leftWorld(originalWorld);
  199. leftWorld.mulL(leftWorldTrans);
  200. GFX->setWorldMatrix(leftWorld);
  201. Frustum gfxFrustum = GFX->getFrustum();
  202. gfxFrustum.setProjectionOffset(Point2F(projOffset.x, projOffset.y));
  203. GFX->setFrustum(gfxFrustum);
  204. SceneCameraState cameraStateLeft = SceneCameraState::fromGFX();
  205. SceneRenderState renderStateLeft( this, renderState->getScenePassType(), cameraStateLeft );
  206. renderStateLeft.setSceneRenderStyle(SRS_SideBySide);
  207. renderStateLeft.setSceneRenderField(0);
  208. renderSceneNoLights( &renderStateLeft, objectMask, baseObject, baseZone );
  209. // Indicate that we've just finished a field
  210. GFX->endField();
  211. // Indicate that we're about to start a field
  212. GFX->beginField();
  213. // Render right half of display
  214. RectI rightVP = originalVP;
  215. rightVP.extent.x *= 0.5;
  216. rightVP.point.x += rightVP.extent.x;
  217. GFX->setViewport(rightVP);
  218. MatrixF rightWorldTrans(true);
  219. rightWorldTrans.setPosition(Point3F(-eyeOffset.x, eyeOffset.y, eyeOffset.z));
  220. MatrixF rightWorld(originalWorld);
  221. rightWorld.mulL(rightWorldTrans);
  222. GFX->setWorldMatrix(rightWorld);
  223. gfxFrustum = GFX->getFrustum();
  224. gfxFrustum.setProjectionOffset(Point2F(-projOffset.x, projOffset.y));
  225. GFX->setFrustum(gfxFrustum);
  226. SceneCameraState cameraStateRight = SceneCameraState::fromGFX();
  227. SceneRenderState renderStateRight( this, renderState->getScenePassType(), cameraStateRight );
  228. renderStateRight.setSceneRenderStyle(SRS_SideBySide);
  229. renderStateRight.setSceneRenderField(1);
  230. renderSceneNoLights( &renderStateRight, objectMask, baseObject, baseZone );
  231. // Indicate that we've just finished a field
  232. GFX->endField();
  233. // Restore previous values
  234. GFX->setWorldMatrix(originalWorld);
  235. gfxFrustum.clearProjectionOffset();
  236. GFX->setFrustum(gfxFrustum);
  237. GFX->setViewport(originalVP);
  238. }
  239. else
  240. {
  241. renderSceneNoLights( renderState, objectMask, baseObject, baseZone );
  242. }
  243. // Trigger the post-render signal.
  244. PROFILE_START( SceneGraphRender_postRenderSignal );
  245. mCurrentRenderState = renderState;
  246. getPostRenderSignal().trigger( this, renderState );
  247. mCurrentRenderState = NULL;
  248. PROFILE_END();
  249. // Remove the previously registered lights.
  250. PROFILE_START( SceneGraph_unregisterLights);
  251. LIGHTMGR->unregisterAllLights();
  252. PROFILE_END();
  253. }
  254. //-----------------------------------------------------------------------------
  255. void SceneManager::renderSceneNoLights( SceneRenderState* renderState, U32 objectMask, SceneZoneSpace* baseObject, U32 baseZone )
  256. {
  257. // Set the current state.
  258. mCurrentRenderState = renderState;
  259. // Render.
  260. _renderScene( mCurrentRenderState, objectMask, baseObject, baseZone );
  261. #ifdef TORQUE_DEBUG
  262. // If frustum is locked and this is a diffuse pass, render the culling volumes of
  263. // zones that are selected (or the volumes of the outdoor zone if no zone is
  264. // selected).
  265. if( gEditingMission && renderState->isDiffusePass() && smLockDiffuseFrustum )
  266. renderState->getCullingState().debugRenderCullingVolumes();
  267. #endif
  268. mCurrentRenderState = NULL;
  269. }
  270. //-----------------------------------------------------------------------------
  271. void SceneManager::_renderScene( SceneRenderState* state, U32 objectMask, SceneZoneSpace* baseObject, U32 baseZone )
  272. {
  273. AssertFatal( this == gClientSceneGraph, "SceneManager::_buildSceneGraph - Only the client scenegraph can support this call!" );
  274. PROFILE_SCOPE( SceneGraph_batchRenderImages );
  275. // In the editor, override the type mask for diffuse passes.
  276. if( gEditingMission && state->isDiffusePass() )
  277. objectMask = EDITOR_RENDER_TYPEMASK;
  278. // Update the zoning state and traverse zones.
  279. if( getZoneManager() )
  280. {
  281. // Update.
  282. getZoneManager()->updateZoningState();
  283. // If zone culling isn't disabled, traverse the
  284. // zones now.
  285. if( !state->getCullingState().disableZoneCulling() )
  286. {
  287. // Find the start zone if we haven't already.
  288. if( !baseObject )
  289. {
  290. getZoneManager()->findZone( state->getCameraPosition(), baseObject, baseZone );
  291. AssertFatal( baseObject != NULL, "SceneManager::_renderScene - findZone() did not return an object" );
  292. }
  293. // Traverse zones starting in base object.
  294. SceneTraversalState traversalState( &state->getCullingState() );
  295. PROFILE_START( Scene_traverseZones );
  296. baseObject->traverseZones( &traversalState, baseZone );
  297. PROFILE_END();
  298. // Set the scene render box to the area we have traversed.
  299. state->setRenderArea( traversalState.getTraversedArea() );
  300. }
  301. }
  302. // Set the query box for the container query. Never
  303. // make it larger than the frustum's AABB. In the editor,
  304. // always query the full frustum as that gives objects
  305. // the opportunity to render editor visualizations even if
  306. // they are otherwise not in view.
  307. if( !state->getCullingFrustum().getBounds().isOverlapped( state->getRenderArea() ) )
  308. {
  309. // This handles fringe cases like flying backwards into a zone where you
  310. // end up pretty much standing on a zone border and looking directly into
  311. // its "walls". In that case the traversal area will be behind the frustum
  312. // (remember that the camera isn't where visibility starts, it's the near
  313. // distance).
  314. return;
  315. }
  316. Box3F queryBox = state->getCullingFrustum().getBounds();
  317. if( !gEditingMission )
  318. {
  319. queryBox.minExtents.setMax( state->getRenderArea().minExtents );
  320. queryBox.maxExtents.setMin( state->getRenderArea().maxExtents );
  321. }
  322. PROFILE_START( Scene_cullObjects );
  323. //TODO: We should split the codepaths here based on whether the outdoor zone has visible space.
  324. // If it has, we should use the container query-based path.
  325. // If it hasn't, we should fill the object list directly from the zone lists which will usually
  326. // include way fewer objects.
  327. // Gather all objects that intersect the scene render box.
  328. mBatchQueryList.clear();
  329. getContainer()->findObjectList( queryBox, objectMask, &mBatchQueryList );
  330. // Cull the list.
  331. U32 numRenderObjects = state->getCullingState().cullObjects(
  332. mBatchQueryList.address(),
  333. mBatchQueryList.size(),
  334. !state->isDiffusePass() ? SceneCullingState::CullEditorOverrides : 0 // Keep forced editor stuff out of non-diffuse passes.
  335. );
  336. //HACK: If the control object is a Player and it is not in the render list, force
  337. // it into it. This really should be solved by collision bounds being separate from
  338. // object bounds; only because the Player class is using bounds not encompassing
  339. // the actual player object is it that we have this problem in the first place.
  340. // Note that we are forcing the player object into ALL passes here but such
  341. // is the power of proliferation of things done wrong.
  342. GameConnection* connection = GameConnection::getConnectionToServer();
  343. if( connection )
  344. {
  345. Player* player = dynamic_cast< Player* >( connection->getControlObject() );
  346. if( player )
  347. {
  348. mBatchQueryList.setSize( numRenderObjects );
  349. if( !mBatchQueryList.contains( player ) )
  350. {
  351. mBatchQueryList.push_back( player );
  352. numRenderObjects ++;
  353. }
  354. }
  355. }
  356. PROFILE_END();
  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. }