cubeLightShadowMap.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. bool CubeLightShadowMap::setTextureStage( U32 currTexFlag, LightingShaderConstants* lsc )
  40. {
  41. if ( currTexFlag == Material::DynamicLight )
  42. {
  43. S32 reg = lsc->mShadowMapSC->getSamplerRegister();
  44. if ( reg != -1 )
  45. GFX->setCubeTexture( reg, mCubemap );
  46. return true;
  47. }
  48. return false;
  49. }
  50. void CubeLightShadowMap::setShaderParameters( GFXShaderConstBuffer *params,
  51. 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,
  60. p->overDarkFactor.x,
  61. 0.0f,
  62. 0.0f );
  63. params->set(lsc->mLightParamsSC, lightParams);
  64. }
  65. // The softness is a factor of the texel size.
  66. params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) );
  67. }
  68. void CubeLightShadowMap::releaseTextures()
  69. {
  70. Parent::releaseTextures();
  71. mCubemap = NULL;
  72. }
  73. void CubeLightShadowMap::_render( RenderPassManager* renderPass,
  74. const SceneRenderState *diffuseState )
  75. {
  76. PROFILE_SCOPE( CubeLightShadowMap_Render );
  77. const LightMapParams *lmParams = mLight->getExtended<LightMapParams>();
  78. const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true;
  79. const U32 texSize = getBestTexSize();
  80. if ( mCubemap.isNull() ||
  81. mTexSize != texSize )
  82. {
  83. mTexSize = texSize;
  84. mCubemap = GFX->createCubemap();
  85. mCubemap->initDynamic( mTexSize, LightShadowMap::ShadowMapFormat );
  86. }
  87. // Setup the world to light projection which is used
  88. // in the shader to transform the light vector for the
  89. // shadow lookup.
  90. mWorldToLightProj = mLight->getTransform();
  91. mWorldToLightProj.inverse();
  92. // Set up frustum and visible distance
  93. GFXFrustumSaver fsaver;
  94. GFXTransformSaver saver;
  95. {
  96. F32 left, right, top, bottom;
  97. MathUtils::makeFrustum( &left, &right, &top, &bottom, M_HALFPI_F, 1.0f, 0.1f );
  98. GFX->setFrustum( left, right, bottom, top, 0.1f, mLight->getRange().x );
  99. }
  100. // Render the shadowmap!
  101. GFX->pushActiveRenderTarget();
  102. for( U32 i = 0; i < 6; i++ )
  103. {
  104. // Standard view that will be overridden below.
  105. VectorF vLookatPt(0.0f, 0.0f, 0.0f), vUpVec(0.0f, 0.0f, 0.0f), vRight(0.0f, 0.0f, 0.0f);
  106. switch( i )
  107. {
  108. case 0 : // D3DCUBEMAP_FACE_POSITIVE_X:
  109. vLookatPt = VectorF(1.0f, 0.0f, 0.0f);
  110. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  111. break;
  112. case 1 : // D3DCUBEMAP_FACE_NEGATIVE_X:
  113. vLookatPt = VectorF(-1.0f, 0.0f, 0.0f);
  114. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  115. break;
  116. case 2 : // D3DCUBEMAP_FACE_POSITIVE_Y:
  117. vLookatPt = VectorF(0.0f, 1.0f, 0.0f);
  118. vUpVec = VectorF(0.0f, 0.0f,-1.0f);
  119. break;
  120. case 3 : // D3DCUBEMAP_FACE_NEGATIVE_Y:
  121. vLookatPt = VectorF(0.0f, -1.0f, 0.0f);
  122. vUpVec = VectorF(0.0f, 0.0f, 1.0f);
  123. break;
  124. case 4 : // D3DCUBEMAP_FACE_POSITIVE_Z:
  125. vLookatPt = VectorF(0.0f, 0.0f, 1.0f);
  126. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  127. break;
  128. case 5: // D3DCUBEMAP_FACE_NEGATIVE_Z:
  129. vLookatPt = VectorF(0.0f, 0.0f, -1.0f);
  130. vUpVec = VectorF(0.0f, 1.0f, 0.0f);
  131. break;
  132. }
  133. GFXDEBUGEVENT_START( CubeLightShadowMap_Render_Face, ColorI::RED );
  134. // create camera matrix
  135. VectorF cross = mCross(vUpVec, vLookatPt);
  136. cross.normalizeSafe();
  137. MatrixF lightMatrix(true);
  138. lightMatrix.setColumn(0, cross);
  139. lightMatrix.setColumn(1, vLookatPt);
  140. lightMatrix.setColumn(2, vUpVec);
  141. lightMatrix.setPosition( mLight->getPosition() );
  142. lightMatrix.inverse();
  143. GFX->setWorldMatrix( lightMatrix );
  144. mTarget->attachTexture(GFXTextureTarget::Color0, mCubemap, i);
  145. mTarget->attachTexture(GFXTextureTarget::DepthStencil, _getDepthTarget( mTexSize, mTexSize ));
  146. GFX->setActiveRenderTarget(mTarget);
  147. GFX->clear( GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 1.0f, 0 );
  148. // Create scene state, prep it
  149. SceneManager* sceneManager = diffuseState->getSceneManager();
  150. SceneRenderState shadowRenderState
  151. (
  152. sceneManager,
  153. SPT_Shadow,
  154. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  155. renderPass
  156. );
  157. shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  158. shadowRenderState.renderNonLightmappedMeshes( true );
  159. shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  160. shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  161. shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  162. sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK );
  163. _debugRender( &shadowRenderState );
  164. // Resolve this face
  165. mTarget->resolve();
  166. GFXDEBUGEVENT_END();
  167. }
  168. GFX->popActiveRenderTarget();
  169. }