singleLightShadowMap.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 "lighting/shadowMap/singleLightShadowMap.h"
  24. #include "lighting/shadowMap/shadowMapManager.h"
  25. #include "lighting/common/lightMapParams.h"
  26. #include "console/console.h"
  27. #include "scene/sceneManager.h"
  28. #include "scene/sceneRenderState.h"
  29. //#include "scene/sceneReflectPass.h"
  30. #include "gfx/gfxDevice.h"
  31. #include "gfx/util/gfxFrustumSaver.h"
  32. #include "gfx/gfxTransformSaver.h"
  33. #include "renderInstance/renderPassManager.h"
  34. SingleLightShadowMap::SingleLightShadowMap( LightInfo *light )
  35. : LightShadowMap( light )
  36. {
  37. }
  38. SingleLightShadowMap::~SingleLightShadowMap()
  39. {
  40. releaseTextures();
  41. }
  42. void SingleLightShadowMap::_render( RenderPassManager* renderPass,
  43. const SceneRenderState *diffuseState )
  44. {
  45. PROFILE_SCOPE(SingleLightShadowMap_render);
  46. const LightMapParams *lmParams = mLight->getExtended<LightMapParams>();
  47. const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true;
  48. const U32 texSize = getBestTexSize();
  49. if ( mShadowMapTex.isNull() ||
  50. mTexSize != texSize )
  51. {
  52. mTexSize = texSize;
  53. mShadowMapTex.set( mTexSize, mTexSize,
  54. ShadowMapFormat, &ShadowMapProfile,
  55. "SingleLightShadowMap" );
  56. }
  57. GFXFrustumSaver frustSaver;
  58. GFXTransformSaver saver;
  59. MatrixF lightMatrix;
  60. calcLightMatrices( lightMatrix, diffuseState->getCameraFrustum() );
  61. lightMatrix.inverse();
  62. GFX->setWorldMatrix(lightMatrix);
  63. MatrixF lightProj = GFX->getProjectionMatrix();
  64. lightProj.reverseProjection();
  65. mWorldToLightProj = lightProj * lightMatrix;
  66. // Render the shadowmap!
  67. GFX->pushActiveRenderTarget();
  68. mTarget->attachTexture( GFXTextureTarget::Color0, mShadowMapTex );
  69. mTarget->attachTexture( GFXTextureTarget::DepthStencil,
  70. _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) );
  71. GFX->setActiveRenderTarget(mTarget);
  72. GFX->clear(GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 0.0f, 0);
  73. SceneManager* sceneManager = diffuseState->getSceneManager();
  74. SceneRenderState shadowRenderState
  75. (
  76. sceneManager,
  77. SPT_Shadow,
  78. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  79. renderPass
  80. );
  81. shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  82. shadowRenderState.renderNonLightmappedMeshes( true );
  83. shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  84. shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  85. shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  86. sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK );
  87. _debugRender( &shadowRenderState );
  88. mTarget->resolve();
  89. GFX->popActiveRenderTarget();
  90. }
  91. void SingleLightShadowMap::setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc)
  92. {
  93. if ( lsc->mTapRotationTexSC->isValid() )
  94. GFX->setTexture( lsc->mTapRotationTexSC->getSamplerRegister(),
  95. SHADOWMGR->getTapRotationTex() );
  96. ShadowMapParams *p = mLight->getExtended<ShadowMapParams>();
  97. if ( lsc->mLightParamsSC->isValid() )
  98. {
  99. Point4F lightParams( mLight->getRange().x,
  100. p->overDarkFactor.x,
  101. 0.0f,
  102. 0.0f );
  103. params->set(lsc->mLightParamsSC, lightParams);
  104. }
  105. // The softness is a factor of the texel size.
  106. params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) );
  107. }