sceneRenderState.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 _SCENERENDERSTATE_H_
  23. #define _SCENERENDERSTATE_H_
  24. #ifndef _COLOR_H_
  25. #include "core/color.h"
  26. #endif
  27. #ifndef _SCENEMANAGER_H_
  28. #include "scene/sceneManager.h"
  29. #endif
  30. #ifndef _SCENECULLINGSTATE_H_
  31. #include "scene/culling/sceneCullingState.h"
  32. #endif
  33. #ifndef _UTIL_DELEGATE_H_
  34. #include "core/util/delegate.h"
  35. #endif
  36. class SceneObject;
  37. class RenderPassManager;
  38. class BaseMatInstance;
  39. /// The SceneRenderState describes the state of the scene being rendered.
  40. ///
  41. /// It keeps track of the information that objects need to render properly with regard to
  42. /// the camera position, any fog information, viewing frustum, the global environment
  43. /// map for reflections, viewable distance, etc.
  44. ///
  45. /// It also owns the current culling state.
  46. class SceneRenderState
  47. {
  48. public:
  49. /// The delegate used for material overrides.
  50. /// @see getOverrideMaterial
  51. typedef Delegate< BaseMatInstance*( BaseMatInstance* ) > MatDelegate;
  52. protected:
  53. /// SceneManager being rendered in this state.
  54. SceneManager* mSceneManager;
  55. /// The type of scene render pass we're doing.
  56. ScenePassType mScenePassType;
  57. /// The render style being performed
  58. SceneRenderStyle mSceneRenderStyle;
  59. /// The render pass which we are setting up with this scene state.
  60. RenderPassManager* mRenderPass;
  61. /// Culling state of the scene.
  62. SceneCullingState mCullingState;
  63. /// The optional material override delegate.
  64. MatDelegate mMatDelegate;
  65. ///
  66. MatrixF mDiffuseCameraTransform;
  67. /// The world to screen space scalar used for LOD calculations.
  68. Point2F mWorldToScreenScale;
  69. /// The AABB that encloses the space in the scene that we render.
  70. Box3F mRenderArea;
  71. /// The camera vector normalized to 1 / far dist.
  72. Point3F mVectorEye;
  73. /// Global ambient light color.
  74. LinearColorF mAmbientLightColor;
  75. /// Forces bin based post effects to be disabled
  76. /// during rendering with this scene state.
  77. bool mUsePostEffects;
  78. /// Disables AdvancedLighting bin draws during rendering with this scene state.
  79. bool mDisableAdvancedLightingBins;
  80. /// If true (default) lightmapped meshes should be rendered.
  81. bool mRenderLightmappedMeshes;
  82. /// If true (default) non-lightmapped meshes should be rendered.
  83. bool mRenderNonLightmappedMeshes;
  84. public:
  85. /// Construct a new SceneRenderState.
  86. ///
  87. /// @param sceneManager SceneManager rendered in this SceneRenderState.
  88. /// @param passType Type of rendering pass that the SceneRenderState is for.
  89. /// @param view The view that is being rendered
  90. /// @param renderPass The render pass which is being set up by this SceneRenderState. If NULL,
  91. /// then Scene::getDefaultRenderPass() is used.
  92. /// @param usePostEffect Whether PostFX are enabled in the rendering pass.
  93. SceneRenderState( SceneManager* sceneManager,
  94. ScenePassType passType,
  95. const SceneCameraState& view = SceneCameraState::fromGFX(),
  96. RenderPassManager* renderPass = NULL,
  97. bool usePostEffects = true );
  98. ~SceneRenderState();
  99. /// Return the SceneManager that is being rendered in this SceneRenderState.
  100. SceneManager* getSceneManager() const { return mSceneManager; }
  101. /// If true then bin based post effects are disabled
  102. /// during rendering with this scene state.
  103. bool usePostEffects() const { return mUsePostEffects; }
  104. void usePostEffects( bool value ) { mUsePostEffects = value; }
  105. /// @name Culling
  106. /// @{
  107. /// Return the culling state for the scene.
  108. const SceneCullingState& getCullingState() const { return mCullingState; }
  109. SceneCullingState& getCullingState() { return mCullingState; }
  110. /// Returns the root culling frustum.
  111. const Frustum& getCullingFrustum() const { return getCullingState().getCullingFrustum(); }
  112. /// Returns the root camera frustum.
  113. const Frustum& getCameraFrustum() const { return getCullingState().getCameraFrustum(); }
  114. /// @}
  115. /// @name Rendering
  116. /// @{
  117. /// Get the AABB around the scene portion that we render.
  118. const Box3F& getRenderArea() const { return mRenderArea; }
  119. /// Set the AABB of the space that should be rendered.
  120. void setRenderArea( const Box3F& area ) { mRenderArea = area; }
  121. /// Batch the given objects to the render pass manager and then
  122. /// render the batched instances.
  123. ///
  124. /// @param objects List of objects.
  125. /// @param numObjects Number of objects in @a objects.
  126. void renderObjects( SceneObject** objects, U32 numObjects );
  127. /// @}
  128. /// @name Lighting
  129. /// @{
  130. /// Return the ambient light color to use for rendering the scene.
  131. ///
  132. /// At the moment, we only support a single global ambient color with which
  133. /// all objects in the scene are rendered. This is because when using
  134. /// Advanced Lighting, we are not resolving light contribution on a per-surface
  135. /// or per-object basis but rather do it globally by gathering light
  136. /// contribution to the whole scene and since the ambient factor is decided
  137. /// by the sun/vector light, it simply becomes a base light level onto which
  138. /// shadowing/lighting is blended based on the shadow maps of the sun/vector
  139. /// light.
  140. ///
  141. /// @return The ambient light color for rendering.
  142. LinearColorF getAmbientLightColor() const { return mAmbientLightColor; }
  143. /// Set the global ambient light color to render with.
  144. void setAmbientLightColor( const LinearColorF& color ) { mAmbientLightColor = color; }
  145. /// If true then Advanced Lighting bin draws are disabled during rendering with
  146. /// this scene state.
  147. bool disableAdvancedLightingBins() const { return mDisableAdvancedLightingBins; }
  148. void disableAdvancedLightingBins(bool enabled) { mDisableAdvancedLightingBins = enabled; }
  149. bool renderLightmappedMeshes() const { return mRenderLightmappedMeshes; }
  150. void renderLightmappedMeshes( bool enabled ) { mRenderLightmappedMeshes = enabled; }
  151. bool renderNonLightmappedMeshes() const { return mRenderNonLightmappedMeshes; }
  152. void renderNonLightmappedMeshes( bool enabled ) { mRenderNonLightmappedMeshes = enabled; }
  153. /// @}
  154. /// @name Passes
  155. /// @{
  156. /// Return the RenderPassManager that manages rendering objects batched
  157. /// for this SceneRenderState.
  158. RenderPassManager* getRenderPass() const { return mRenderPass; }
  159. /// Returns the type of scene rendering pass that we're doing.
  160. ScenePassType getScenePassType() const { return mScenePassType; }
  161. /// Returns true if this is a diffuse scene rendering pass.
  162. bool isDiffusePass() const { return mScenePassType == SPT_Diffuse; }
  163. /// Returns true if this is a reflection scene rendering pass.
  164. bool isReflectPass() const { return mScenePassType == SPT_Reflect; }
  165. /// Returns true if this is a shadow scene rendering pass.
  166. bool isShadowPass() const { return mScenePassType == SPT_Shadow; }
  167. /// Returns true if this is not one of the other rendering passes.
  168. bool isOtherPass() const { return mScenePassType >= SPT_Other; }
  169. /// @}
  170. /// @name Render Style
  171. /// @{
  172. /// Get the rendering style used for the scene
  173. SceneRenderStyle getSceneRenderStyle() const { return mSceneRenderStyle; }
  174. /// Set the rendering style used for the scene
  175. void setSceneRenderStyle(SceneRenderStyle style) { mSceneRenderStyle = style; }
  176. /// @}
  177. /// @name Transforms, projections, and viewports.
  178. /// @{
  179. /// Return the screen-space viewport rectangle.
  180. const RectI& getViewport() const { return getCullingState().getCameraState().getViewport(); }
  181. /// Return the world->view transform matrix.
  182. const MatrixF& getWorldViewMatrix() const;
  183. /// Return the project transform matrix.
  184. const MatrixF& getProjectionMatrix() const;
  185. /// Return the inverse project transform matrix.
  186. const MatrixF& getInvProjectionMatrix() const;
  187. /// Returns the actual camera position.
  188. /// @see getDiffuseCameraPosition
  189. const Point3F& getCameraPosition() const { return getCullingState().getCameraState().getViewPosition(); }
  190. /// Returns the camera transform (view->world) this SceneRenderState is using.
  191. const MatrixF& getCameraTransform() const { return getCullingState().getCameraState().getViewWorldMatrix(); }
  192. /// Returns the minimum distance something must be from the camera to not be culled.
  193. F32 getNearPlane() const { return getCullingFrustum().getNearDist(); }
  194. /// Returns the maximum distance something can be from the camera to not be culled.
  195. F32 getFarPlane() const { return getCullingFrustum().getFarDist(); }
  196. /// Returns the camera vector normalized to 1 / far distance.
  197. const Point3F& getVectorEye() const { return mVectorEye; }
  198. /// Returns the possibly overloaded world to screen scale.
  199. /// @see projectRadius
  200. const Point2F& getWorldToScreenScale() const { return mWorldToScreenScale; }
  201. /// Set a new world to screen scale to overload
  202. /// future screen metrics operations.
  203. void setWorldToScreenScale( const Point2F& scale ) { mWorldToScreenScale = scale; }
  204. /// Returns the pixel size of the radius projected to the screen at a desired distance.
  205. ///
  206. /// Internally this uses the stored world to screen scale and viewport extents. This
  207. /// allows the projection to be overloaded in special cases like when rendering shadows
  208. /// or reflections.
  209. ///
  210. /// @see getWorldToScreenScale
  211. /// @see getViewportExtent
  212. F32 projectRadius( F32 dist, F32 radius ) const
  213. {
  214. // We fixup any negative or zero distance
  215. // so we don't get a divide by zero.
  216. dist = dist > 0.0f ? dist : 0.001f;
  217. return ( radius / dist ) * mWorldToScreenScale.y;
  218. }
  219. /// Returns the camera position used during the diffuse rendering pass which may be different
  220. /// from the actual camera position.
  221. ///
  222. /// This is useful when doing level of detail calculations that need to be relative to the
  223. /// diffuse pass.
  224. ///
  225. /// @see getCameraPosition
  226. Point3F getDiffuseCameraPosition() const { return mDiffuseCameraTransform.getPosition(); }
  227. const MatrixF& getDiffuseCameraTransform() const { return mDiffuseCameraTransform; }
  228. /// Set a new diffuse camera transform.
  229. /// @see getDiffuseCameraTransform
  230. void setDiffuseCameraTransform( const MatrixF &mat ) { mDiffuseCameraTransform = mat; }
  231. /// @}
  232. /// @name Material Overrides
  233. /// @{
  234. /// When performing a special render pass like shadows this
  235. /// returns a specialized override material. It can return
  236. /// NULL if the override wants to disable rendering. If
  237. /// there is no override in place then the input material is
  238. /// returned unaltered.
  239. BaseMatInstance* getOverrideMaterial( BaseMatInstance* matInst ) const
  240. {
  241. if ( !matInst || mMatDelegate.empty() )
  242. return matInst;
  243. return mMatDelegate( matInst );
  244. }
  245. /// Returns the optional material override delegate which is
  246. /// used during some special render passes.
  247. /// @see getOverrideMaterial
  248. MatDelegate& getMaterialDelegate() { return mMatDelegate; }
  249. const MatDelegate& getMaterialDelegate() const { return mMatDelegate; }
  250. /// @}
  251. };
  252. #endif // _SCENERENDERSTATE_H_