singleLightShadowMap.cpp
Engine/source/lighting/shadowMap/singleLightShadowMap.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "lighting/shadowMap/singleLightShadowMap.h" 26#include "lighting/shadowMap/shadowMapManager.h" 27#include "lighting/common/lightMapParams.h" 28#include "console/console.h" 29#include "scene/sceneManager.h" 30#include "scene/sceneRenderState.h" 31//#include "scene/sceneReflectPass.h" 32#include "gfx/gfxDevice.h" 33#include "gfx/util/gfxFrustumSaver.h" 34#include "gfx/gfxTransformSaver.h" 35#include "renderInstance/renderPassManager.h" 36 37SingleLightShadowMap::SingleLightShadowMap( LightInfo *light ) 38 : LightShadowMap( light ) 39{ 40} 41 42SingleLightShadowMap::~SingleLightShadowMap() 43{ 44 releaseTextures(); 45} 46 47void SingleLightShadowMap::_render( RenderPassManager* renderPass, 48 const SceneRenderState *diffuseState ) 49{ 50 PROFILE_SCOPE(SingleLightShadowMap_render); 51 52 const LightMapParams *lmParams = mLight->getExtended<LightMapParams>(); 53 const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true; 54 55 const U32 texSize = getBestTexSize(); 56 57 if ( mShadowMapTex.isNull() || 58 mTexSize != texSize ) 59 { 60 mTexSize = texSize; 61 62 mShadowMapTex.set( mTexSize, mTexSize, 63 ShadowMapFormat, &ShadowMapProfile, 64 "SingleLightShadowMap" ); 65 } 66 67 GFXFrustumSaver frustSaver; 68 GFXTransformSaver saver; 69 70 MatrixF lightMatrix; 71 calcLightMatrices( lightMatrix, diffuseState->getCameraFrustum() ); 72 lightMatrix.inverse(); 73 GFX->setWorldMatrix(lightMatrix); 74 75 const MatrixF& lightProj = GFX->getProjectionMatrix(); 76 mWorldToLightProj = lightProj * lightMatrix; 77 78 // Render the shadowmap! 79 GFX->pushActiveRenderTarget(); 80 mTarget->attachTexture( GFXTextureTarget::Color0, mShadowMapTex ); 81 mTarget->attachTexture( GFXTextureTarget::DepthStencil, 82 _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) ); 83 GFX->setActiveRenderTarget(mTarget); 84 GFX->clear(GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 1.0f, 0); 85 86 SceneManager* sceneManager = diffuseState->getSceneManager(); 87 88 SceneRenderState shadowRenderState 89 ( 90 sceneManager, 91 SPT_Shadow, 92 SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ), 93 renderPass 94 ); 95 96 shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial ); 97 shadowRenderState.renderNonLightmappedMeshes( true ); 98 shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry ); 99 shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() ); 100 shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() ); 101 102 sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK ); 103 104 _debugRender( &shadowRenderState ); 105 106 mTarget->resolve(); 107 GFX->popActiveRenderTarget(); 108} 109 110void SingleLightShadowMap::setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc) 111{ 112 if ( lsc->mTapRotationTexSC->isValid() ) 113 GFX->setTexture( lsc->mTapRotationTexSC->getSamplerRegister(), 114 SHADOWMGR->getTapRotationTex() ); 115 116 ShadowMapParams *p = mLight->getExtended<ShadowMapParams>(); 117 118 if ( lsc->mLightParamsSC->isValid() ) 119 { 120 Point4F lightParams( mLight->getRange().x, 121 p->overDarkFactor.x, 122 0.0f, 123 0.0f ); 124 params->set(lsc->mLightParamsSC, lightParams); 125 } 126 127 // The softness is a factor of the texel size. 128 params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) ); 129} 130