sceneManager.h 11 KB

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