sceneRenderState.cpp 4.9 KB

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