cubeLightShadowMap.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/cubeLightShadowMap.h"
  24. #include "lighting/shadowMap/shadowMapManager.h"
  25. #include "lighting/common/lightMapParams.h"
  26. #include "scene/sceneManager.h"
  27. #include "scene/sceneRenderState.h"
  28. #include "gfx/gfxDevice.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "gfx/gfxDebugEvent.h"
  31. #include "renderInstance/renderPassManager.h"
  32. #include "materials/materialDefinition.h"
  33. #include "gfx/util/gfxFrustumSaver.h"
  34. #include "math/mathUtils.h"
  35. CubeLightShadowMap::CubeLightShadowMap( LightInfo *light )
  36. : Parent( light )
  37. {
  38. }
  39. void CubeLightShadowMap::setShaderParameters( GFXShaderConstBuffer *params,
  40. LightingShaderConstants *lsc )
  41. {
  42. if ( lsc->mTapRotationTexSC->isValid() )
  43. GFX->setTexture( lsc->mTapRotationTexSC->getSamplerRegister(),
  44. SHADOWMGR->getTapRotationTex() );
  45. ShadowMapParams *p = mLight->getExtended<ShadowMapParams>();
  46. if ( lsc->mLightParamsSC->isValid() )
  47. {
  48. Point4F lightParams( mLight->getRange().x,
  49. p->overDarkFactor.x,
  50. 0.0f,
  51. 0.0f );
  52. params->set(lsc->mLightParamsSC, lightParams);
  53. }
  54. // The softness is a factor of the texel size.
  55. params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) );
  56. }
  57. void CubeLightShadowMap::_render( RenderPassManager* renderPass,
  58. const SceneRenderState *diffuseState )
  59. {
  60. PROFILE_SCOPE( CubeLightShadowMap_Render );
  61. const LightMapParams *lmParams = mLight->getExtended<LightMapParams>();
  62. const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true;
  63. const U32 texSize = getBestTexSize();
  64. if (mShadowMapTex.isNull() ||
  65. mTexSize != texSize)
  66. {
  67. mTexSize = texSize;
  68. mShadowMapTex.set(mTexSize, mTexSize,
  69. ShadowMapFormat, &CubeShadowMapProfile,
  70. "CubeLightShadowMap");
  71. mShadowMapDepth = _getDepthTarget(mShadowMapTex->getWidth(), mShadowMapTex->getHeight());
  72. }
  73. // Setup the world to light projection which is used
  74. // in the shader to transform the light vector for the
  75. // shadow lookup.
  76. mWorldToLightProj = mLight->getTransform();
  77. mWorldToLightProj.inverse();
  78. // Set up frustum and visible distance
  79. GFXFrustumSaver fsaver;
  80. GFXTransformSaver saver;
  81. {
  82. F32 left, right, top, bottom;
  83. MathUtils::makeFrustum( &left, &right, &top, &bottom, M_HALFPI_F, 1.0f, 0.1f );
  84. GFX->setFrustum( left, right, bottom, top, 0.1f, mLight->getRange().x );
  85. }
  86. // Render the shadowmap!
  87. GFX->pushActiveRenderTarget();
  88. for( U32 i = 0; i < 6; i++ )
  89. {
  90. // Standard view that will be overridden below.
  91. VectorF vLookatPt(0.0f, 0.0f, 0.0f), vUpVec(0.0f, 0.0f, 0.0f), vRight(0.0f, 0.0f, 0.0f);
  92. switch( i )
  93. {
  94. case 0 : // D3DCUBEMAP_FACE_POSITIVE_X:
  95. vLookatPt = VectorF(1.0f, 0.0f, 0.0f);
  96. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  97. break;
  98. case 1 : // D3DCUBEMAP_FACE_NEGATIVE_X:
  99. vLookatPt = VectorF(-1.0f, 0.0f, 0.0f);
  100. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  101. break;
  102. case 2 : // D3DCUBEMAP_FACE_POSITIVE_Y:
  103. vLookatPt = VectorF(0.0f, 1.0f, 0.0f);
  104. vUpVec = VectorF(0.0f, 0.0f,-1.0f);
  105. break;
  106. case 3 : // D3DCUBEMAP_FACE_NEGATIVE_Y:
  107. vLookatPt = VectorF(0.0f, -1.0f, 0.0f);
  108. vUpVec = VectorF(0.0f, 0.0f, 1.0f);
  109. break;
  110. case 4 : // D3DCUBEMAP_FACE_POSITIVE_Z:
  111. vLookatPt = VectorF(0.0f, 0.0f, 1.0f);
  112. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  113. break;
  114. case 5: // D3DCUBEMAP_FACE_NEGATIVE_Z:
  115. vLookatPt = VectorF(0.0f, 0.0f, -1.0f);
  116. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  117. break;
  118. }
  119. GFXDEBUGEVENT_START( CubeLightShadowMap_Render_Face, ColorI::RED );
  120. // create camera matrix
  121. MatrixF lightMatrix(true);
  122. lightMatrix.LookAt(mLight->getPosition(), vLookatPt, vUpVec);
  123. lightMatrix.inverse();
  124. GFX->setWorldMatrix( lightMatrix );
  125. mTarget->attachTexture(GFXTextureTarget::Color0, mShadowMapTex,0,0, i);
  126. mTarget->attachTexture(GFXTextureTarget::DepthStencil, _getDepthTarget(mShadowMapTex->getWidth(), mShadowMapTex->getHeight()));
  127. GFX->setActiveRenderTarget(mTarget);
  128. GFX->clear( GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 0.0f, 0 );
  129. // Create scene state, prep it
  130. SceneManager* sceneManager = diffuseState->getSceneManager();
  131. SceneRenderState shadowRenderState
  132. (
  133. sceneManager,
  134. SPT_Shadow,
  135. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  136. renderPass
  137. );
  138. shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  139. shadowRenderState.renderNonLightmappedMeshes( true );
  140. shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  141. shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  142. shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  143. sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK );
  144. _debugRender( &shadowRenderState );
  145. // Resolve this face
  146. mTarget->resolve();
  147. GFXDEBUGEVENT_END();
  148. }
  149. GFX->popActiveRenderTarget();
  150. }