sceneRenderState.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "platform/platform.h"
  23. #include "scene/sceneRenderState.h"
  24. #include "renderInstance/renderPassManager.h"
  25. #include "math/util/matrixSet.h"
  26. //-----------------------------------------------------------------------------
  27. SceneRenderState::SceneRenderState( SceneManager* sceneManager,
  28. ScenePassType passType,
  29. const SceneCameraState& view,
  30. RenderPassManager* renderPass /* = NULL */,
  31. bool usePostEffects /* = true */ )
  32. : mSceneManager( sceneManager ),
  33. mScenePassType( passType ),
  34. mRenderPass( renderPass ? renderPass : sceneManager->getDefaultRenderPass() ),
  35. mCullingState( sceneManager, view ),
  36. mUsePostEffects( usePostEffects ),
  37. mRenderLightmappedMeshes( true ),
  38. mRenderNonLightmappedMeshes( true ),
  39. mRenderArea( view.getFrustum().getBounds() ),
  40. mDisableAdvancedLightingBins( false ),
  41. mSceneRenderStyle( SRS_Standard ),
  42. mAmbientLightColor( sceneManager->getAmbientLightColor() )
  43. {
  44. // Setup the default parameters for the screen metrics methods.
  45. mDiffuseCameraTransform = view.getHeadWorldViewMatrix();
  46. mDiffuseCameraTransform.inverse();
  47. // The vector eye is the camera vector with its
  48. // length normalized to 1 / zFar.
  49. getCameraTransform().getColumn( 1, &mVectorEye );
  50. mVectorEye.normalize( 1.0f / getFarPlane() );
  51. // TODO: What about ortho modes? Is near plane ok
  52. // or do i need to remove it... maybe ortho has a near
  53. // plane of 1 and it just works out?
  54. const Frustum& frustum = view.getFrustum();
  55. const RectI& viewport = view.getViewport();
  56. mWorldToScreenScale.set( ( frustum.getNearDist() * viewport.extent.x ) / ( frustum.getNearRight() - frustum.getNearLeft() ),
  57. ( frustum.getNearDist() * viewport.extent.y ) / ( frustum.getNearTop() - frustum.getNearBottom() ) );
  58. // Assign shared matrix data to the render pass.
  59. mRenderPass->assignSharedXform( RenderPassManager::View, view.getWorldViewMatrix() );
  60. mRenderPass->assignSharedXform( RenderPassManager::Projection, view.getProjectionMatrix() );
  61. }
  62. //-----------------------------------------------------------------------------
  63. SceneRenderState::~SceneRenderState()
  64. {
  65. }
  66. //-----------------------------------------------------------------------------
  67. const MatrixF& SceneRenderState::getWorldViewMatrix() const
  68. {
  69. return getRenderPass()->getMatrixSet().getWorldToCamera();
  70. }
  71. //-----------------------------------------------------------------------------
  72. const MatrixF& SceneRenderState::getProjectionMatrix() const
  73. {
  74. return getRenderPass()->getMatrixSet().getCameraToScreen();
  75. }
  76. const MatrixF& SceneRenderState::getInvProjectionMatrix() const
  77. {
  78. return getRenderPass()->getMatrixSet().getScreenToCamera();
  79. }
  80. //-----------------------------------------------------------------------------
  81. void SceneRenderState::renderObjects( SceneObject** objects, U32 numObjects )
  82. {
  83. // Let the objects batch their stuff.
  84. PROFILE_START( SceneRenderState_prepRenderImages );
  85. for( U32 i = 0; i < numObjects; ++ i )
  86. {
  87. SceneObject* object = objects[ i ];
  88. object->prepRenderImage( this );
  89. }
  90. PROFILE_END();
  91. // Render what the objects have batched.
  92. getRenderPass()->renderPass( this );
  93. }