sceneRenderState.h 12 KB

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