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