dualParaboloidLightShadowMap.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/dualParaboloidLightShadowMap.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 "gfx/gfxDebugEvent.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 "math/util/matrixSet.h"
  36. DualParaboloidLightShadowMap::DualParaboloidLightShadowMap( LightInfo *light )
  37. : Parent( light )
  38. {
  39. }
  40. void DualParaboloidLightShadowMap::_render( RenderPassManager* renderPass,
  41. const SceneRenderState *diffuseState )
  42. {
  43. PROFILE_SCOPE(DualParaboloidLightShadowMap_render);
  44. const ShadowMapParams *p = mLight->getExtended<ShadowMapParams>();
  45. const LightMapParams *lmParams = mLight->getExtended<LightMapParams>();
  46. const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true;
  47. const U32 texSize = getBestTexSize( 2 );
  48. if ( mShadowMapTex.isNull() ||
  49. mTexSize != texSize )
  50. {
  51. mTexSize = texSize;
  52. mShadowMapTex.set( mTexSize * 2, mTexSize,
  53. ShadowMapFormat, &ShadowMapProfile,
  54. "DualParaboloidLightShadowMap" );
  55. mShadowMapDepth = _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() );
  56. }
  57. GFXFrustumSaver frustSaver;
  58. GFXTransformSaver saver;
  59. // Set and Clear target
  60. GFX->pushActiveRenderTarget();
  61. mTarget->attachTexture(GFXTextureTarget::Color0, mShadowMapTex);
  62. mTarget->attachTexture( GFXTextureTarget::DepthStencil, mShadowMapDepth );
  63. GFX->setActiveRenderTarget(mTarget);
  64. GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI::WHITE, 1.0f, 0);
  65. const bool bUseSinglePassDPM = (p->shadowType == ShadowType_DualParaboloidSinglePass);
  66. // Set up matrix and visible distance
  67. mWorldToLightProj = mLight->getTransform();
  68. mWorldToLightProj.inverse();
  69. const F32 &lightRadius = mLight->getRange().x;
  70. const F32 paraboloidNearPlane = 0.01f;
  71. const F32 renderPosOffset = 0.01f;
  72. // Alter for creation of scene state if this is a single pass map
  73. if(bUseSinglePassDPM)
  74. {
  75. VectorF camDir;
  76. MatrixF temp = mLight->getTransform();
  77. temp.getColumn(1, &camDir);
  78. temp.setPosition(mLight->getPosition() - camDir * (lightRadius + renderPosOffset));
  79. temp.inverse();
  80. GFX->setWorldMatrix(temp);
  81. GFX->setOrtho(-lightRadius, lightRadius, -lightRadius, lightRadius, paraboloidNearPlane, 2.0f * lightRadius, true);
  82. }
  83. else
  84. {
  85. VectorF camDir;
  86. MatrixF temp = mLight->getTransform();
  87. temp.getColumn(1, &camDir);
  88. temp.setPosition(mLight->getPosition() - camDir * renderPosOffset);
  89. temp.inverse();
  90. GFX->setWorldMatrix(temp);
  91. GFX->setOrtho(-lightRadius, lightRadius, -lightRadius, lightRadius, paraboloidNearPlane, lightRadius, true);
  92. }
  93. SceneManager* sceneManager = diffuseState->getSceneManager();
  94. // Front map render
  95. {
  96. SceneRenderState frontMapRenderState
  97. (
  98. sceneManager,
  99. SPT_Shadow,
  100. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  101. renderPass
  102. );
  103. frontMapRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  104. frontMapRenderState.renderNonLightmappedMeshes( true );
  105. frontMapRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  106. frontMapRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  107. frontMapRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  108. if(bUseSinglePassDPM)
  109. {
  110. GFX->setWorldMatrix(mWorldToLightProj);
  111. frontMapRenderState.getRenderPass()->getMatrixSet().setSceneView(mWorldToLightProj);
  112. GFX->setOrtho(-lightRadius, lightRadius, -lightRadius, lightRadius, paraboloidNearPlane, lightRadius, true);
  113. }
  114. GFXDEBUGEVENT_SCOPE( DualParaboloidLightShadowMap_Render_FrontFacingParaboloid, ColorI::RED );
  115. mShadowMapScale.set(0.5f, 1.0f);
  116. mShadowMapOffset.set(-0.5f, 0.0f);
  117. sceneManager->renderSceneNoLights( &frontMapRenderState, SHADOW_TYPEMASK );
  118. _debugRender( &frontMapRenderState );
  119. }
  120. // Back map render
  121. if(!bUseSinglePassDPM)
  122. {
  123. GFXDEBUGEVENT_SCOPE( DualParaboloidLightShadowMap_Render_BackFacingParaboloid, ColorI::RED );
  124. mShadowMapScale.set(0.5f, 1.0f);
  125. mShadowMapOffset.set(0.5f, 0.0f);
  126. // Invert direction on camera matrix
  127. VectorF right, forward;
  128. MatrixF temp = mLight->getTransform();
  129. temp.getColumn( 1, &forward );
  130. temp.getColumn( 0, &right );
  131. forward *= -1.0f;
  132. right *= -1.0f;
  133. temp.setColumn( 1, forward );
  134. temp.setColumn( 0, right );
  135. temp.setPosition(mLight->getPosition() - forward * -renderPosOffset);
  136. temp.inverse();
  137. GFX->setWorldMatrix(temp);
  138. // Create an inverted scene state for the back-map
  139. SceneRenderState backMapRenderState
  140. (
  141. sceneManager,
  142. SPT_Shadow,
  143. SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
  144. renderPass
  145. );
  146. backMapRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
  147. backMapRenderState.renderNonLightmappedMeshes( true );
  148. backMapRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
  149. backMapRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
  150. backMapRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
  151. backMapRenderState.getRenderPass()->getMatrixSet().setSceneView(temp);
  152. // Draw scene
  153. sceneManager->renderSceneNoLights( &backMapRenderState );
  154. _debugRender( &backMapRenderState );
  155. }
  156. mTarget->resolve();
  157. GFX->popActiveRenderTarget();
  158. }