sceneManager.h 12 KB

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