sceneRenderState.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 culling frustum.
  113. const Frustum& getCullingFrustum() const { return getCullingState().getCullingFrustum(); }
  114. /// Returns the root camera frustum.
  115. const Frustum& getCameraFrustum() const { return getCullingState().getCameraFrustum(); }
  116. /// @}
  117. /// @name Rendering
  118. /// @{
  119. /// Get the AABB around the scene portion that we render.
  120. const Box3F& getRenderArea() const { return mRenderArea; }
  121. /// Set the AABB of the space that should be rendered.
  122. void setRenderArea( const Box3F& area ) { mRenderArea = area; }
  123. /// Batch the given objects to the render pass manager and then
  124. /// render the batched instances.
  125. ///
  126. /// @param objects List of objects.
  127. /// @param numObjects Number of objects in @a objects.
  128. void renderObjects( SceneObject** objects, U32 numObjects );
  129. /// @}
  130. /// @name Lighting
  131. /// @{
  132. /// Return the ambient light color to use for rendering the scene.
  133. ///
  134. /// At the moment, we only support a single global ambient color with which
  135. /// all objects in the scene are rendered. This is because when using
  136. /// Advanced Lighting, we are not resolving light contribution on a per-surface
  137. /// or per-object basis but rather do it globally by gathering light
  138. /// contribution to the whole scene and since the ambient factor is decided
  139. /// by the sun/vector light, it simply becomes a base light level onto which
  140. /// shadowing/lighting is blended based on the shadow maps of the sun/vector
  141. /// light.
  142. ///
  143. /// @return The ambient light color for rendering.
  144. ColorF getAmbientLightColor() const { return mAmbientLightColor; }
  145. /// Set the global ambient light color to render with.
  146. void setAmbientLightColor( const ColorF& color ) { mAmbientLightColor = color; }
  147. /// If true then Advanced Lighting bin draws are disabled during rendering with
  148. /// this scene state.
  149. bool disableAdvancedLightingBins() const { return mDisableAdvancedLightingBins; }
  150. void disableAdvancedLightingBins(bool enabled) { mDisableAdvancedLightingBins = enabled; }
  151. bool renderLightmappedMeshes() const { return mRenderLightmappedMeshes; }
  152. void renderLightmappedMeshes( bool enabled ) { mRenderLightmappedMeshes = enabled; }
  153. bool renderNonLightmappedMeshes() const { return mRenderNonLightmappedMeshes; }
  154. void renderNonLightmappedMeshes( bool enabled ) { mRenderNonLightmappedMeshes = enabled; }
  155. /// @}
  156. /// @name Passes
  157. /// @{
  158. /// Return the RenderPassManager that manages rendering objects batched
  159. /// for this SceneRenderState.
  160. RenderPassManager* getRenderPass() const { return mRenderPass; }
  161. /// Returns the type of scene rendering pass that we're doing.
  162. ScenePassType getScenePassType() const { return mScenePassType; }
  163. /// Returns true if this is a diffuse scene rendering pass.
  164. bool isDiffusePass() const { return mScenePassType == SPT_Diffuse; }
  165. /// Returns true if this is a reflection scene rendering pass.
  166. bool isReflectPass() const { return mScenePassType == SPT_Reflect; }
  167. /// Returns true if this is a shadow scene rendering pass.
  168. bool isShadowPass() const { return mScenePassType == SPT_Shadow; }
  169. /// Returns true if this is not one of the other rendering passes.
  170. bool isOtherPass() const { return mScenePassType >= SPT_Other; }
  171. /// @}
  172. /// @name Render Style
  173. /// @{
  174. /// Get the rendering style used for the scene
  175. SceneRenderStyle getSceneRenderStyle() const { return mSceneRenderStyle; }
  176. /// Set the rendering style used for the scene
  177. void setSceneRenderStyle(SceneRenderStyle style) { mSceneRenderStyle = style; }
  178. /// Get the stereo field being rendered
  179. S32 getSceneRenderField() const { return mRenderField; }
  180. /// Set the stereo field being rendered
  181. void setSceneRenderField(S32 field) { mRenderField = field; }
  182. /// @}
  183. /// @name Transforms, projections, and viewports.
  184. /// @{
  185. /// Return the screen-space viewport rectangle.
  186. const RectI& getViewport() const { return getCullingState().getCameraState().getViewport(); }
  187. /// Return the world->view transform matrix.
  188. const MatrixF& getWorldViewMatrix() const;
  189. /// Return the project transform matrix.
  190. const MatrixF& getProjectionMatrix() const;
  191. /// Returns the actual camera position.
  192. /// @see getDiffuseCameraPosition
  193. const Point3F& getCameraPosition() const { return getCullingState().getCameraState().getViewPosition(); }
  194. /// Returns the camera transform (view->world) this SceneRenderState is using.
  195. const MatrixF& getCameraTransform() const { return getCullingState().getCameraState().getViewWorldMatrix(); }
  196. /// Returns the minimum distance something must be from the camera to not be culled.
  197. F32 getNearPlane() const { return getCullingFrustum().getNearDist(); }
  198. /// Returns the maximum distance something can be from the camera to not be culled.
  199. F32 getFarPlane() const { return getCullingFrustum().getFarDist(); }
  200. /// Returns the camera vector normalized to 1 / far distance.
  201. const Point3F& getVectorEye() const { return mVectorEye; }
  202. /// Returns the possibly overloaded world to screen scale.
  203. /// @see projectRadius
  204. const Point2F& getWorldToScreenScale() const { return mWorldToScreenScale; }
  205. /// Set a new world to screen scale to overload
  206. /// future screen metrics operations.
  207. void setWorldToScreenScale( const Point2F& scale ) { mWorldToScreenScale = scale; }
  208. /// Returns the pixel size of the radius projected to the screen at a desired distance.
  209. ///
  210. /// Internally this uses the stored world to screen scale and viewport extents. This
  211. /// allows the projection to be overloaded in special cases like when rendering shadows
  212. /// or reflections.
  213. ///
  214. /// @see getWorldToScreenScale
  215. /// @see getViewportExtent
  216. F32 projectRadius( F32 dist, F32 radius ) const
  217. {
  218. // We fixup any negative or zero distance
  219. // so we don't get a divide by zero.
  220. dist = dist > 0.0f ? dist : 0.001f;
  221. return ( radius / dist ) * mWorldToScreenScale.y;
  222. }
  223. /// Returns the camera position used during the diffuse rendering pass which may be different
  224. /// from the actual camera position.
  225. ///
  226. /// This is useful when doing level of detail calculations that need to be relative to the
  227. /// diffuse pass.
  228. ///
  229. /// @see getCameraPosition
  230. Point3F getDiffuseCameraPosition() const { return mDiffuseCameraTransform.getPosition(); }
  231. const MatrixF& getDiffuseCameraTransform() const { return mDiffuseCameraTransform; }
  232. /// Set a new diffuse camera transform.
  233. /// @see getDiffuseCameraTransform
  234. void setDiffuseCameraTransform( const MatrixF &mat ) { mDiffuseCameraTransform = mat; }
  235. /// @}
  236. /// @name Material Overrides
  237. /// @{
  238. /// When performing a special render pass like shadows this
  239. /// returns a specialized override material. It can return
  240. /// NULL if the override wants to disable rendering. If
  241. /// there is no override in place then the input material is
  242. /// returned unaltered.
  243. BaseMatInstance* getOverrideMaterial( BaseMatInstance* matInst ) const
  244. {
  245. if ( !matInst || mMatDelegate.empty() )
  246. return matInst;
  247. return mMatDelegate( matInst );
  248. }
  249. /// Returns the optional material override delegate which is
  250. /// used during some special render passes.
  251. /// @see getOverrideMaterial
  252. MatDelegate& getMaterialDelegate() { return mMatDelegate; }
  253. const MatDelegate& getMaterialDelegate() const { return mMatDelegate; }
  254. /// @}
  255. };
  256. #endif // _SCENERENDERSTATE_H_