sceneRenderState.cpp 4.5 KB

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