sceneManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #ifndef _SCENEMANAGER_H_
  23. #define _SCENEMANAGER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _SCENEZONESPACEMANAGER_H_
  28. #include "scene/zones/sceneZoneSpaceManager.h"
  29. #endif
  30. #ifndef _MRECT_H_
  31. #include "math/mRect.h"
  32. #endif
  33. #ifndef _COLOR_H_
  34. #include "core/color.h"
  35. #endif
  36. #ifndef _INTERPOLATEDCHANGEPROPERTY_H_
  37. #include "util/interpolatedChangeProperty.h"
  38. #endif
  39. #ifndef _GFXTEXTUREHANDLE_H_
  40. #include "gfx/gfxTextureHandle.h"
  41. #endif
  42. #ifndef _FOGSTRUCTS_H_
  43. #include "scene/fogStructs.h"
  44. #endif
  45. #ifndef _TVECTOR_H_
  46. #include "core/util/tVector.h"
  47. #endif
  48. #ifndef _TSIGNAL_H_
  49. #include "core/util/tSignal.h"
  50. #endif
  51. #ifndef _SCENECONTAINER_H_
  52. #include "scene/sceneContainer.h"
  53. #endif
  54. class LightManager;
  55. class SceneRootZone;
  56. class SceneRenderState;
  57. class SceneCameraState;
  58. class SceneZoneSpace;
  59. class NetConnection;
  60. class RenderPassManager;
  61. /// The type of scene pass.
  62. /// @see SceneManager
  63. /// @see SceneRenderState
  64. enum ScenePassType
  65. {
  66. /// The regular diffuse scene pass.
  67. SPT_Diffuse,
  68. /// The scene pass made for reflection rendering.
  69. SPT_Reflect,
  70. /// The scene pass made for shadow map rendering.
  71. SPT_Shadow,
  72. /// A scene pass that isn't one of the other
  73. /// predefined scene pass types.
  74. SPT_Other,
  75. };
  76. /// The type of scene render style
  77. /// @see SceneRenderState
  78. enum SceneRenderStyle
  79. {
  80. /// The regular style of rendering
  81. SRS_Standard,
  82. /// Side-by-side style rendering
  83. SRS_SideBySide,
  84. };
  85. /// An object that manages the SceneObjects belonging to a scene.
  86. class SceneManager
  87. {
  88. public:
  89. /// A signal used to notify of render passes.
  90. typedef Signal< void( SceneManager*, const SceneRenderState* ) > RenderSignal;
  91. /// If true use the last stored locked frustum for culling
  92. /// the diffuse render pass.
  93. /// @see smLockedDiffuseFrustum
  94. static bool smLockDiffuseFrustum;
  95. /// If true, render the AABBs of objects for debugging.
  96. static bool smRenderBoundingBoxes;
  97. //A cache list of objects that made it through culling, so we don't have to attempt to re-test
  98. //visibility of objects later.
  99. Vector< SceneObject* > mRenderedObjectsList;
  100. protected:
  101. /// Whether this is the client-side scene.
  102. bool mIsClient;
  103. /// Manager for the zones in this scene.
  104. SceneZoneSpaceManager* mZoneManager;
  105. // NonClipProjection is the projection matrix without oblique frustum clipping
  106. // applied to it (in reflections)
  107. MatrixF mNonClipProj;
  108. ///
  109. bool mUsePostEffectFog;
  110. /// @see setDisplayTargetResolution
  111. Point2I mDisplayTargetResolution;
  112. /// The currently active render state or NULL if we're
  113. /// not in the process of rendering.
  114. SceneRenderState* mCurrentRenderState;
  115. F32 mVisibleDistance;
  116. F32 mVisibleGhostDistance;
  117. F32 mNearClip;
  118. FogData mFogData;
  119. WaterFogData mWaterFogData;
  120. /// The stored last diffuse pass frustum for locking the cull.
  121. static SceneCameraState smLockedDiffuseCamera;
  122. /// @name Lighting
  123. /// @{
  124. typedef InterpolatedChangeProperty< LinearColorF > AmbientLightInterpolator;
  125. /// Light manager that is active for the scene.
  126. LightManager* mLightManager;
  127. /// Global ambient light level in the scene.
  128. AmbientLightInterpolator mAmbientLightColor;
  129. /// Deactivates the previous light manager and activates the new one.
  130. bool _setLightManager( LightManager *lm );
  131. /// @}
  132. /// @name Rendering
  133. /// @{
  134. /// RenderPassManager for the default render pass. This is set up
  135. /// in script and looked up by getDefaultRenderPass().
  136. mutable RenderPassManager* mDefaultRenderPass;
  137. ///
  138. Vector< SceneObject* > mBatchQueryList;
  139. /// Render scene using the given state.
  140. ///
  141. /// @param state SceneManager render state.
  142. /// @param objectMask Object type mask with which to filter scene objects.
  143. /// @param baseObject Zone manager to start traversal in. If null, the zone manager
  144. /// that contains @a state's camera position will be used.
  145. /// @param baseZone Zone in @a zone manager in which to start traversal. Ignored if
  146. /// @a baseObject is NULL.
  147. void _renderScene( SceneRenderState* state,
  148. U32 objectMask = ( U32 ) -1,
  149. SceneZoneSpace* baseObject = NULL,
  150. U32 baseZone = 0 );
  151. /// Callback for the container query.
  152. static void _batchObjectCallback( SceneObject* object, void* key );
  153. /// @}
  154. public:
  155. SceneManager( bool isClient );
  156. ~SceneManager();
  157. /// Return the SceneContainer for this scene.
  158. SceneContainer* getContainer() const { return mIsClient ? &gClientContainer : &gServerContainer; }
  159. /// Return the manager for the zones in this scene.
  160. /// @note Only client scenes have a zone manager as for the server, no zoning data is kept.
  161. const SceneZoneSpaceManager* getZoneManager() const { return mZoneManager; }
  162. SceneZoneSpaceManager* getZoneManager() { return mZoneManager; }
  163. /// @name SceneObject Management
  164. /// @{
  165. /// Add the given object to the scene.
  166. bool addObjectToScene( SceneObject* object );
  167. /// Remove the given object from the scene.
  168. void removeObjectFromScene( SceneObject* object );
  169. /// Let the scene manager know that the given object has changed its transform or
  170. /// sizing state.
  171. void notifyObjectDirty( SceneObject* object );
  172. /// @}
  173. /// @name Rendering
  174. /// @{
  175. /// Return the default RenderPassManager for the scene.
  176. RenderPassManager* getDefaultRenderPass() const;
  177. /// Set the default render pass for the scene.
  178. void setDefaultRenderPass( RenderPassManager* rpm ) { mDefaultRenderPass = rpm; }
  179. /// Render the scene with the default render pass.
  180. /// @note This uses the current GFX state (transforms, viewport, frustum) to initialize
  181. /// the render state.
  182. void renderScene( ScenePassType passType, U32 objectMask = DEFAULT_RENDER_TYPEMASK );
  183. /// Render the scene with a custom rendering pass.
  184. void renderScene( SceneRenderState *state, U32 objectMask = DEFAULT_RENDER_TYPEMASK, SceneZoneSpace* baseObject = NULL, U32 baseZone = 0 );
  185. /// Render the scene with a custom rendering pass and no lighting set up.
  186. void renderSceneNoLights( SceneRenderState *state, U32 objectMask = DEFAULT_RENDER_TYPEMASK, SceneZoneSpace* baseObject = NULL, U32 baseZone = 0 );
  187. /// Returns the currently active scene state or NULL if no state is currently active.
  188. SceneRenderState* getCurrentRenderState() const { return mCurrentRenderState; }
  189. static RenderSignal& getPreRenderSignal()
  190. {
  191. static RenderSignal theSignal;
  192. return theSignal;
  193. }
  194. static RenderSignal& getPostRenderSignal()
  195. {
  196. static RenderSignal theSignal;
  197. return theSignal;
  198. }
  199. /// @}
  200. /// @name Lighting
  201. /// @{
  202. /// Finds the light manager by name and activates it.
  203. bool setLightManager( const char *lmName );
  204. /// Return the current global ambient light color.
  205. const LinearColorF& getAmbientLightColor() const { return mAmbientLightColor.getCurrentValue(); }
  206. /// Set the time it takes for a new ambient light color to take full effect.
  207. void setAmbientLightTransitionTime( SimTime time ) { mAmbientLightColor.setTransitionTime( time ); }
  208. /// Set the interpolation curve to use for blending from one global ambient light
  209. /// color to a different one.
  210. void setAmbientLightTransitionCurve( const EaseF& ease ) { mAmbientLightColor.setTransitionCurve( ease ); }
  211. /// @}
  212. /// @name Networking
  213. /// @{
  214. /// Set the scoping states of the objects in the scene.
  215. void scopeScene( CameraScopeQuery* query, NetConnection* netConnection );
  216. /// @}
  217. /// @name Fog/Visibility Management
  218. /// @{
  219. void setPostEffectFog( bool enable ) { mUsePostEffectFog = enable; }
  220. bool usePostEffectFog() const { return mUsePostEffectFog; }
  221. /// Accessor for the FogData structure.
  222. const FogData& getFogData() { return mFogData; }
  223. /// Sets the FogData structure.
  224. void setFogData( const FogData &data ) { mFogData = data; }
  225. /// Accessor for the WaterFogData structure.
  226. const WaterFogData& getWaterFogData() { return mWaterFogData; }
  227. /// Sets the WaterFogData structure.
  228. void setWaterFogData( const WaterFogData &data ) { mWaterFogData = data; }
  229. /// Used by LevelInfo to set the default visible distance for
  230. /// rendering the scene.
  231. ///
  232. /// Note this should not be used to alter culling which is
  233. /// controlled by the active frustum when a SceneRenderState is created.
  234. ///
  235. /// @see SceneRenderState
  236. /// @see GameProcessCameraQuery
  237. /// @see LevelInfo
  238. void setVisibleDistance( F32 dist ) { mVisibleDistance = dist; }
  239. /// Returns the default visible distance for the scene.
  240. F32 getVisibleDistance() { return mVisibleDistance; }
  241. void setVisibleGhostDistance( F32 dist ) { mVisibleGhostDistance = dist; }
  242. F32 getVisibleGhostDistance() { return mVisibleGhostDistance;}
  243. /// Used by LevelInfo to set the default near clip plane
  244. /// for rendering the scene.
  245. ///
  246. /// @see GameProcessCameraQuery
  247. /// @see LevelInfo
  248. void setNearClip( F32 nearClip ) { mNearClip = nearClip; }
  249. /// Returns the default near clip distance for the scene.
  250. F32 getNearClip() { return mNearClip; }
  251. /// @}
  252. /// @name dtr Display Target Resolution
  253. ///
  254. /// Some rendering must be targeted at a specific display resolution.
  255. /// This display resolution is distinct from the current RT's size
  256. /// (such as when rendering a reflection to a texture, for instance)
  257. /// so we store the size at which we're going to display the results of
  258. /// the current render.
  259. ///
  260. /// @{
  261. ///
  262. void setDisplayTargetResolution(const Point2I &size);
  263. const Point2I &getDisplayTargetResolution() const;
  264. /// @}
  265. // NonClipProjection is the projection matrix without oblique frustum clipping
  266. // applied to it (in reflections)
  267. void setNonClipProjection( const MatrixF &proj ) { mNonClipProj = proj; }
  268. const MatrixF& getNonClipProjection() const { return mNonClipProj; }
  269. };
  270. //-----------------------------------------------------------------------------
  271. //TODO: these two need to go
  272. /// The client-side scene graph. Not used if the engine is running
  273. /// as a dedicated server.
  274. extern SceneManager* gClientSceneGraph;
  275. /// The server-side scene graph. Not used if the engine is running
  276. /// as a pure client.
  277. extern SceneManager* gServerSceneGraph;
  278. #endif //_SCENEMANAGER_H_