sceneRenderState.cpp 4.3 KB

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