sceneManager.h 12 KB

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