singleLightShadowMap.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. const MatrixF& lightProj = GFX->getProjectionMatrix();
  64. mWorldToLightProj = lightProj * lightMatrix;
  65. // Render the shadowmap!
  66. GFX->pushActiveRenderTarget();
  67. mTarget->attachTexture( GFXTextureTarget::Color0, mShadowMapTex );
  68. mTarget->attachTexture( GFXTextureTarget::DepthStencil,
  69. _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) );
  70. GFX->setActiveRenderTarget(mTarget);
  71. GFX->clear(GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 1.0f, 0);
  72. SceneManager* sceneManager = diffuseState->getSceneManager();
  73. SceneRenderState shadowRenderState
  74. (
  75. sceneManager,
  76. SPT_Shadow,
  77. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  78. renderPass
  79. );
  80. shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  81. shadowRenderState.renderNonLightmappedMeshes( true );
  82. shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  83. shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  84. shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  85. sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK );
  86. _debugRender( &shadowRenderState );
  87. mTarget->resolve();
  88. GFX->popActiveRenderTarget();
  89. }
  90. void SingleLightShadowMap::setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc)
  91. {
  92. if ( lsc->mTapRotationTexSC->isValid() )
  93. GFX->setTexture( lsc->mTapRotationTexSC->getSamplerRegister(),
  94. SHADOWMGR->getTapRotationTex() );
  95. ShadowMapParams *p = mLight->getExtended<ShadowMapParams>();
  96. if ( lsc->mLightParamsSC->isValid() )
  97. {
  98. Point4F lightParams( mLight->getRange().x,
  99. p->overDarkFactor.x,
  100. 0.0f,
  101. 0.0f );
  102. params->set(lsc->mLightParamsSC, lightParams);
  103. }
  104. // The softness is a factor of the texel size.
  105. params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) );
  106. }