sceneManager.cpp 25 KB

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