paraboloidLightShadowMap.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/paraboloidLightShadowMap.h"
  24. #include "lighting/common/lightMapParams.h"
  25. #include "lighting/shadowMap/shadowMapManager.h"
  26. #include "math/mathUtils.h"
  27. #include "scene/sceneManager.h"
  28. #include "scene/sceneRenderState.h"
  29. //#include "scene/sceneReflectPass.h"
  30. #include "gfx/gfxDevice.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "gfx/util/gfxFrustumSaver.h"
  33. #include "renderInstance/renderPassManager.h"
  34. #include "materials/materialDefinition.h"
  35. #include "gui/controls/guiBitmapCtrl.h"
  36. ParaboloidLightShadowMap::ParaboloidLightShadowMap( LightInfo *light )
  37. : Parent( light ),
  38. mShadowMapScale( 1, 1 ),
  39. mShadowMapOffset( 0, 0 )
  40. {
  41. }
  42. ParaboloidLightShadowMap::~ParaboloidLightShadowMap()
  43. {
  44. releaseTextures();
  45. }
  46. ShadowType ParaboloidLightShadowMap::getShadowType() const
  47. {
  48. const ShadowMapParams *params = mLight->getExtended<ShadowMapParams>();
  49. return params->shadowType;
  50. }
  51. void ParaboloidLightShadowMap::setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc)
  52. {
  53. if ( lsc->mTapRotationTexSC->isValid() )
  54. GFX->setTexture( lsc->mTapRotationTexSC->getSamplerRegister(),
  55. SHADOWMGR->getTapRotationTex() );
  56. ShadowMapParams *p = mLight->getExtended<ShadowMapParams>();
  57. if ( lsc->mLightParamsSC->isValid() )
  58. {
  59. Point4F lightParams( mLight->getRange().x, p->overDarkFactor.x, 0.0f, 0.0f);
  60. params->set( lsc->mLightParamsSC, lightParams );
  61. }
  62. // Atlasing parameters (only used in the dual case, set here to use same shaders)
  63. params->setSafe( lsc->mAtlasScaleSC, mShadowMapScale );
  64. params->setSafe( lsc->mAtlasXOffsetSC, mShadowMapOffset );
  65. // The softness is a factor of the texel size.
  66. params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) );
  67. }
  68. void ParaboloidLightShadowMap::_render( RenderPassManager* renderPass,
  69. const SceneRenderState *diffuseState )
  70. {
  71. PROFILE_SCOPE(ParaboloidLightShadowMap_render);
  72. const LightMapParams *lmParams = mLight->getExtended<LightMapParams>();
  73. const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true;
  74. const U32 texSize = getBestTexSize();
  75. if ( mShadowMapTex.isNull() ||
  76. mTexSize != texSize )
  77. {
  78. mTexSize = texSize;
  79. mShadowMapTex.set( mTexSize, mTexSize,
  80. ShadowMapFormat, &ShadowMapProfile,
  81. "ParaboloidLightShadowMap" );
  82. }
  83. GFXFrustumSaver frustSaver;
  84. GFXTransformSaver saver;
  85. // Render the shadowmap!
  86. GFX->pushActiveRenderTarget();
  87. // Calc matrix and set up visible distance
  88. mWorldToLightProj = mLight->getTransform();
  89. mWorldToLightProj.inverse();
  90. GFX->setWorldMatrix(mWorldToLightProj);
  91. const F32 &lightRadius = mLight->getRange().x;
  92. GFX->setOrtho(-lightRadius, lightRadius, -lightRadius, lightRadius, 1.0f, lightRadius, true);
  93. // Set up target
  94. mTarget->attachTexture( GFXTextureTarget::Color0, mShadowMapTex );
  95. mTarget->attachTexture( GFXTextureTarget::DepthStencil,
  96. _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) );
  97. GFX->setActiveRenderTarget(mTarget);
  98. GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 1.0f, 0);
  99. // Create scene state, prep it
  100. SceneManager* sceneManager = diffuseState->getSceneManager();
  101. SceneRenderState shadowRenderState
  102. (
  103. sceneManager,
  104. SPT_Shadow,
  105. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  106. renderPass
  107. );
  108. shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  109. shadowRenderState.renderNonLightmappedMeshes( true );
  110. shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  111. shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  112. shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  113. sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK );
  114. _debugRender( &shadowRenderState );
  115. mTarget->resolve();
  116. GFX->popActiveRenderTarget();
  117. }