sceneRenderState.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /// When doing stereo rendering, the current field that is being rendered
  60. S32 mRenderField;
  61. /// The render pass which we are setting up with this scene state.
  62. RenderPassManager* mRenderPass;
  63. /// Culling state of the scene.
  64. SceneCullingState mCullingState;
  65. /// The optional material override delegate.
  66. MatDelegate mMatDelegate;
  67. ///
  68. MatrixF mDiffuseCameraTransform;
  69. /// The world to screen space scalar used for LOD calculations.
  70. Point2F mWorldToScreenScale;
  71. /// The AABB that encloses the space in the scene that we render.
  72. Box3F mRenderArea;
  73. /// The camera vector normalized to 1 / far dist.
  74. Point3F mVectorEye;
  75. /// Global ambient light color.
  76. ColorF mAmbientLightColor;
  77. /// Forces bin based post effects to be disabled
  78. /// during rendering with this scene state.
  79. bool mUsePostEffects;
  80. /// Disables AdvancedLighting bin draws during rendering with this scene state.
  81. bool mDisableAdvancedLightingBins;
  82. /// If true (default) lightmapped meshes should be rendered.
  83. bool mRenderLightmappedMeshes;
  84. /// If true (default) non-lightmapped meshes should be rendered.
  85. bool mRenderNonLightmappedMeshes;
  86. public:
  87. /// Construct a new SceneRenderState.
  88. ///
  89. /// @param sceneManager SceneManager rendered in this SceneRenderState.
  90. /// @param passType Type of rendering pass that the SceneRenderState is for.
  91. /// @param view The view that is being rendered
  92. /// @param renderPass The render pass which is being set up by this SceneRenderState. If NULL,
  93. /// then Scene::getDefaultRenderPass() is used.
  94. /// @param usePostEffect Whether PostFX are enabled in the rendering pass.
  95. SceneRenderState( SceneManager* sceneManager,
  96. ScenePassType passType,
  97. const SceneCameraState& view = SceneCameraState::fromGFX(),
  98. RenderPassManager* renderPass = NULL,
  99. bool usePostEffects = true );
  100. ~SceneRenderState();
  101. /// Return the SceneManager that is being rendered in this SceneRenderState.
  102. SceneManager* getSceneManager() const { return mSceneManager; }
  103. /// If true then bin based post effects are disabled
  104. /// during rendering with this scene state.
  105. bool usePostEffects() const { return mUsePostEffects; }
  106. void usePostEffects( bool value ) { mUsePostEffects = value; }
  107. /// @name Culling
  108. /// @{
  109. /// Return the culling state for the scene.
  110. const SceneCullingState& getCullingState() const { return mCullingState; }
  111. SceneCullingState& getCullingState() { return mCullingState; }
  112. /// Returns the root frustum.
  113. const Frustum& getFrustum() const { return getCullingState().getFrustum(); }
  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. ColorF getAmbientLightColor() const { return mAmbientLightColor; }
  143. /// Set the global ambient light color to render with.
  144. void setAmbientLightColor( const ColorF& 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. /// Get the stereo field being rendered
  177. S32 getSceneRenderField() const { return mRenderField; }
  178. /// Set the stereo field being rendered
  179. void setSceneRenderField(S32 field) { mRenderField = field; }
  180. /// @}
  181. /// @name Transforms, projections, and viewports.
  182. /// @{
  183. /// Return the screen-space viewport rectangle.
  184. const RectI& getViewport() const { return getCullingState().getCameraState().getViewport(); }
  185. /// Return the world->view transform matrix.
  186. const MatrixF& getWorldViewMatrix() const;
  187. /// Return the project transform matrix.
  188. const MatrixF& getProjectionMatrix() const;
  189. /// Returns the actual camera position.
  190. /// @see getDiffuseCameraPosition
  191. const Point3F& getCameraPosition() const { return getCullingState().getCameraState().getViewPosition(); }
  192. /// Returns the camera transform (view->world) this SceneRenderState is using.
  193. const MatrixF& getCameraTransform() const { return getCullingState().getCameraState().getViewWorldMatrix(); }
  194. /// Returns the minimum distance something must be from the camera to not be culled.
  195. F32 getNearPlane() const { return getFrustum().getNearDist(); }
  196. /// Returns the maximum distance something can be from the camera to not be culled.
  197. F32 getFarPlane() const { return getFrustum().getFarDist(); }
  198. /// Returns the camera vector normalized to 1 / far distance.
  199. const Point3F& getVectorEye() const { return mVectorEye; }
  200. /// Returns the possibly overloaded world to screen scale.
  201. /// @see projectRadius
  202. const Point2F& getWorldToScreenScale() const { return mWorldToScreenScale; }
  203. /// Set a new world to screen scale to overload
  204. /// future screen metrics operations.
  205. void setWorldToScreenScale( const Point2F& scale ) { mWorldToScreenScale = scale; }
  206. /// Returns the pixel size of the radius projected to the screen at a desired distance.
  207. ///
  208. /// Internally this uses the stored world to screen scale and viewport extents. This
  209. /// allows the projection to be overloaded in special cases like when rendering shadows
  210. /// or reflections.
  211. ///
  212. /// @see getWorldToScreenScale
  213. /// @see getViewportExtent
  214. F32 projectRadius( F32 dist, F32 radius ) const
  215. {
  216. // We fixup any negative or zero distance
  217. // so we don't get a divide by zero.
  218. dist = dist > 0.0f ? dist : 0.001f;
  219. return ( radius / dist ) * mWorldToScreenScale.y;
  220. }
  221. /// Returns the camera position used during the diffuse rendering pass which may be different
  222. /// from the actual camera position.
  223. ///
  224. /// This is useful when doing level of detail calculations that need to be relative to the
  225. /// diffuse pass.
  226. ///
  227. /// @see getCameraPosition
  228. Point3F getDiffuseCameraPosition() const { return mDiffuseCameraTransform.getPosition(); }
  229. const MatrixF& getDiffuseCameraTransform() const { return mDiffuseCameraTransform; }
  230. /// Set a new diffuse camera transform.
  231. /// @see getDiffuseCameraTransform
  232. void setDiffuseCameraTransform( const MatrixF &mat ) { mDiffuseCameraTransform = mat; }
  233. /// @}
  234. /// @name Material Overrides
  235. /// @{
  236. /// When performing a special render pass like shadows this
  237. /// returns a specialized override material. It can return
  238. /// NULL if the override wants to disable rendering. If
  239. /// there is no override in place then the input material is
  240. /// returned unaltered.
  241. BaseMatInstance* getOverrideMaterial( BaseMatInstance* matInst ) const
  242. {
  243. if ( !matInst || mMatDelegate.empty() )
  244. return matInst;
  245. return mMatDelegate( matInst );
  246. }
  247. /// Returns the optional material override delegate which is
  248. /// used during some special render passes.
  249. /// @see getOverrideMaterial
  250. MatDelegate& getMaterialDelegate() { return mMatDelegate; }
  251. const MatDelegate& getMaterialDelegate() const { return mMatDelegate; }
  252. /// @}
  253. };
  254. #endif // _SCENERENDERSTATE_H_