Torque3D Documentation / _generateds / sceneRenderState.h

sceneRenderState.h

Engine/source/scene/sceneRenderState.h

More...

Classes:

class

The SceneRenderState describes the state of the scene being rendered.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _SCENERENDERSTATE_H_
 25#define _SCENERENDERSTATE_H_
 26
 27#ifndef _COLOR_H_
 28#include "core/color.h"
 29#endif
 30
 31#ifndef _SCENEMANAGER_H_
 32#include "scene/sceneManager.h"
 33#endif
 34
 35#ifndef _SCENECULLINGSTATE_H_
 36#include "scene/culling/sceneCullingState.h"
 37#endif
 38
 39#ifndef _UTIL_DELEGATE_H_
 40#include "core/util/delegate.h"
 41#endif
 42
 43
 44class SceneObject;
 45class RenderPassManager;
 46class BaseMatInstance;
 47
 48
 49
 50/// The SceneRenderState describes the state of the scene being rendered.
 51///
 52/// It keeps track of the information that objects need to render properly with regard to
 53/// the camera position, any fog information, viewing frustum, the global environment
 54/// map for reflections, viewable distance, etc.
 55///
 56/// It also owns the current culling state.
 57class SceneRenderState
 58{
 59   public:
 60
 61      /// The delegate used for material overrides.
 62      /// @see getOverrideMaterial
 63      typedef Delegate< BaseMatInstance*( BaseMatInstance* ) > MatDelegate;
 64
 65   protected:
 66
 67      /// SceneManager being rendered in this state.
 68      SceneManager* mSceneManager;
 69
 70      /// The type of scene render pass we're doing.
 71      ScenePassType mScenePassType;
 72
 73      /// The render style being performed
 74      SceneRenderStyle mSceneRenderStyle;
 75
 76      /// The render pass which we are setting up with this scene state.
 77      RenderPassManager* mRenderPass;
 78
 79      /// Culling state of the scene.
 80      SceneCullingState mCullingState;
 81
 82      /// The optional material override delegate.
 83      MatDelegate mMatDelegate;
 84
 85      ///
 86      MatrixF mDiffuseCameraTransform;
 87
 88      /// The world to screen space scalar used for LOD calculations.
 89      Point2F mWorldToScreenScale;
 90
 91      /// The AABB that encloses the space in the scene that we render.
 92      Box3F mRenderArea;
 93
 94      /// The camera vector normalized to 1 / far dist.
 95      Point3F mVectorEye;
 96
 97      /// Global ambient light color.
 98      LinearColorF mAmbientLightColor;
 99
100      /// Forces bin based post effects to be disabled
101      /// during rendering with this scene state.
102      bool mUsePostEffects;
103
104      /// Disables AdvancedLighting bin draws during rendering with this scene state.
105      bool mDisableAdvancedLightingBins;
106
107      /// If true (default) lightmapped meshes should be rendered.
108      bool mRenderLightmappedMeshes;
109
110      /// If true (default) non-lightmapped meshes should be rendered.
111      bool mRenderNonLightmappedMeshes;
112
113   public:
114
115      /// Construct a new SceneRenderState.
116      ///
117      /// @param sceneManager SceneManager rendered in this SceneRenderState.
118      /// @param passType Type of rendering pass that the SceneRenderState is for.
119      /// @param view The view that is being rendered
120      /// @param renderPass The render pass which is being set up by this SceneRenderState.  If NULL,
121      ///   then Scene::getDefaultRenderPass() is used.
122      /// @param usePostEffect Whether PostFX are enabled in the rendering pass.
123      SceneRenderState( SceneManager* sceneManager,
124                        ScenePassType passType,
125                        const SceneCameraState& view = SceneCameraState::fromGFX(),
126                        RenderPassManager* renderPass = NULL,
127                        bool usePostEffects = true );
128
129      ~SceneRenderState();
130
131      /// Return the SceneManager that is being rendered in this SceneRenderState.
132      SceneManager* getSceneManager() const { return mSceneManager; }
133
134      /// If true then bin based post effects are disabled 
135      /// during rendering with this scene state.
136      bool usePostEffects() const { return mUsePostEffects; }
137      void usePostEffects( bool value ) { mUsePostEffects = value; }
138
139      /// @name Culling
140      /// @{
141
142      /// Return the culling state for the scene.
143      const SceneCullingState& getCullingState() const { return mCullingState; }
144      SceneCullingState& getCullingState() { return mCullingState; }
145
146      /// Returns the root culling frustum.
147      const Frustum& getCullingFrustum() const { return getCullingState().getCullingFrustum(); }
148
149      /// Returns the root camera frustum.
150      const Frustum& getCameraFrustum() const { return getCullingState().getCameraFrustum(); }
151
152      /// @}
153
154      /// @name Rendering
155      /// @{
156
157      /// Get the AABB around the scene portion that we render.
158      const Box3F& getRenderArea() const { return mRenderArea; }
159
160      /// Set the AABB of the space that should be rendered.
161      void setRenderArea( const Box3F& area ) { mRenderArea = area; }
162
163      /// Batch the given objects to the render pass manager and then
164      /// render the batched instances.
165      ///
166      /// @param objects List of objects.
167      /// @param numObjects Number of objects in @a objects.
168      void renderObjects( SceneObject** objects, U32 numObjects );
169
170      /// @}
171
172      /// @name Lighting
173      /// @{
174
175      /// Return the ambient light color to use for rendering the scene.
176      ///
177      /// At the moment, we only support a single global ambient color with which
178      /// all objects in the scene are rendered.  This is because when using
179      /// Advanced Lighting, we are not resolving light contribution on a per-surface
180      /// or per-object basis but rather do it globally by gathering light
181      /// contribution to the whole scene and since the ambient factor is decided
182      /// by the sun/vector light, it simply becomes a base light level onto which
183      /// shadowing/lighting is blended based on the shadow maps of the sun/vector
184      /// light.
185      ///
186      /// @return The ambient light color for rendering.
187      LinearColorF getAmbientLightColor() const { return mAmbientLightColor; }
188
189      /// Set the global ambient light color to render with.
190      void setAmbientLightColor( const LinearColorF& color ) { mAmbientLightColor = color; }
191
192      /// If true then Advanced Lighting bin draws are disabled during rendering with
193      /// this scene state.
194      bool disableAdvancedLightingBins() const { return mDisableAdvancedLightingBins; }
195      void disableAdvancedLightingBins(bool enabled) { mDisableAdvancedLightingBins = enabled; }
196
197      bool renderLightmappedMeshes() const { return mRenderLightmappedMeshes; }
198      void renderLightmappedMeshes( bool enabled ) { mRenderLightmappedMeshes = enabled; }
199
200      bool renderNonLightmappedMeshes() const { return mRenderNonLightmappedMeshes; }
201      void renderNonLightmappedMeshes( bool enabled ) { mRenderNonLightmappedMeshes = enabled; }
202
203      /// @}
204
205      /// @name Passes
206      /// @{
207
208      /// Return the RenderPassManager that manages rendering objects batched
209      /// for this SceneRenderState.
210      RenderPassManager* getRenderPass() const { return mRenderPass; }
211
212      /// Returns the type of scene rendering pass that we're doing.
213      ScenePassType getScenePassType() const { return mScenePassType; }
214
215      /// Returns true if this is a diffuse scene rendering pass.
216      bool isDiffusePass() const { return mScenePassType == SPT_Diffuse; }
217
218      /// Returns true if this is a reflection scene rendering pass.
219      bool isReflectPass() const { return mScenePassType == SPT_Reflect; }
220
221      /// Returns true if this is a shadow scene rendering pass.
222      bool isShadowPass() const { return mScenePassType == SPT_Shadow; }
223
224      /// Returns true if this is not one of the other rendering passes.
225      bool isOtherPass() const { return mScenePassType >= SPT_Other; }
226
227      /// @}
228
229      /// @name Render Style
230      /// @{
231
232      /// Get the rendering style used for the scene
233      SceneRenderStyle getSceneRenderStyle() const { return mSceneRenderStyle; }
234
235      /// Set the rendering style used for the scene
236      void setSceneRenderStyle(SceneRenderStyle style) { mSceneRenderStyle = style; }
237
238      /// @}
239
240      /// @name Transforms, projections, and viewports.
241      /// @{
242
243      /// Return the screen-space viewport rectangle.
244      const RectI& getViewport() const { return getCullingState().getCameraState().getViewport(); }
245
246      /// Return the world->view transform matrix.
247      const MatrixF& getWorldViewMatrix() const;
248
249      /// Return the project transform matrix.
250      const MatrixF& getProjectionMatrix() const;
251      /// Return the inverse project transform matrix.
252      const MatrixF& getInvProjectionMatrix() const;
253
254      /// Returns the actual camera position.
255      /// @see getDiffuseCameraPosition
256      const Point3F& getCameraPosition() const { return getCullingState().getCameraState().getViewPosition(); }
257
258      /// Returns the camera transform (view->world) this SceneRenderState is using.
259      const MatrixF& getCameraTransform() const { return getCullingState().getCameraState().getViewWorldMatrix(); }
260
261      /// Returns the minimum distance something must be from the camera to not be culled.
262      F32 getNearPlane() const { return getCullingFrustum().getNearDist();   }
263
264      /// Returns the maximum distance something can be from the camera to not be culled.
265      F32 getFarPlane() const { return getCullingFrustum().getFarDist();    }
266
267      /// Returns the camera vector normalized to 1 / far distance.
268      const Point3F& getVectorEye() const { return mVectorEye; }
269
270      /// Returns the possibly overloaded world to screen scale.
271      /// @see projectRadius
272      const Point2F& getWorldToScreenScale() const { return mWorldToScreenScale; }
273
274      /// Set a new world to screen scale to overload
275      /// future screen metrics operations.
276      void setWorldToScreenScale( const Point2F& scale ) { mWorldToScreenScale = scale; }
277
278      /// Returns the pixel size of the radius projected to the screen at a desired distance.
279      /// 
280      /// Internally this uses the stored world to screen scale and viewport extents.  This
281      /// allows the projection to be overloaded in special cases like when rendering shadows
282      /// or reflections.
283      ///
284      /// @see getWorldToScreenScale
285      /// @see getViewportExtent
286      F32 projectRadius( F32 dist, F32 radius ) const
287      {
288         // We fixup any negative or zero distance 
289         // so we don't get a divide by zero.
290         dist = dist > 0.0f ? dist : 0.001f;
291         return ( radius / dist ) * mWorldToScreenScale.y;
292      }
293
294      /// Returns the camera position used during the diffuse rendering pass which may be different
295      /// from the actual camera position.
296      ///
297      /// This is useful when doing level of detail calculations that need to be relative to the
298      /// diffuse pass.
299      ///
300      /// @see getCameraPosition
301      Point3F getDiffuseCameraPosition() const { return mDiffuseCameraTransform.getPosition(); }
302      const MatrixF& getDiffuseCameraTransform() const { return mDiffuseCameraTransform; }
303
304      /// Set a new diffuse camera transform.
305      /// @see getDiffuseCameraTransform
306      void setDiffuseCameraTransform( const MatrixF &mat ) { mDiffuseCameraTransform = mat; }
307
308      /// @}
309
310      /// @name Material Overrides
311      /// @{
312
313      /// When performing a special render pass like shadows this
314      /// returns a specialized override material.  It can return 
315      /// NULL if the override wants to disable rendering.  If 
316      /// there is no override in place then the input material is
317      /// returned unaltered.
318      BaseMatInstance* getOverrideMaterial( BaseMatInstance* matInst ) const
319      {
320         if ( !matInst || mMatDelegate.empty() )
321            return matInst;
322
323         return mMatDelegate( matInst );
324      }
325
326      /// Returns the optional material override delegate which is
327      /// used during some special render passes.
328      /// @see getOverrideMaterial
329      MatDelegate& getMaterialDelegate() { return mMatDelegate; }
330      const MatDelegate& getMaterialDelegate() const { return mMatDelegate; }
331
332      /// @}
333};
334
335#endif // _SCENERENDERSTATE_H_
336