Torque3D Documentation / _generateds / paraboloidLightShadowMap.cpp

paraboloidLightShadowMap.cpp

Engine/source/lighting/shadowMap/paraboloidLightShadowMap.cpp

More...

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/paraboloidLightShadowMap.h"
 26#include "lighting/common/lightMapParams.h"
 27#include "lighting/shadowMap/shadowMapManager.h"
 28#include "math/mathUtils.h"
 29#include "scene/sceneManager.h"
 30#include "scene/sceneRenderState.h"
 31//#include "scene/sceneReflectPass.h"
 32#include "gfx/gfxDevice.h"
 33#include "gfx/gfxTransformSaver.h"
 34#include "gfx/util/gfxFrustumSaver.h"
 35#include "renderInstance/renderPassManager.h"
 36#include "materials/materialDefinition.h"
 37#include "gui/controls/guiBitmapCtrl.h"
 38
 39ParaboloidLightShadowMap::ParaboloidLightShadowMap( LightInfo *light )
 40   :  Parent( light ),
 41      mShadowMapScale( 1, 1 ),
 42      mShadowMapOffset( 0, 0 )
 43{   
 44}
 45
 46ParaboloidLightShadowMap::~ParaboloidLightShadowMap()
 47{
 48   releaseTextures();
 49}
 50
 51ShadowType ParaboloidLightShadowMap::getShadowType() const
 52{
 53   const ShadowMapParams *params = mLight->getExtended<ShadowMapParams>();
 54   return params->shadowType;
 55}
 56
 57void ParaboloidLightShadowMap::setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc)
 58{
 59   if ( lsc->mTapRotationTexSC->isValid() )
 60      GFX->setTexture( lsc->mTapRotationTexSC->getSamplerRegister(), 
 61                        SHADOWMGR->getTapRotationTex() );
 62
 63   ShadowMapParams *p = mLight->getExtended<ShadowMapParams>();
 64   if ( lsc->mLightParamsSC->isValid() )
 65   {
 66      Point4F lightParams( mLight->getRange().x, p->overDarkFactor.x, 0.0f, 0.0f);
 67      params->set( lsc->mLightParamsSC, lightParams );
 68   }
 69
 70   // Atlasing parameters (only used in the dual case, set here to use same shaders)
 71   params->setSafe( lsc->mAtlasScaleSC, mShadowMapScale );
 72   params->setSafe( lsc->mAtlasXOffsetSC, mShadowMapOffset );
 73
 74   // The softness is a factor of the texel size.
 75   params->setSafe( lsc->mShadowSoftnessConst, p->shadowSoftness * ( 1.0f / mTexSize ) );
 76}
 77
 78void ParaboloidLightShadowMap::_render(   RenderPassManager* renderPass,
 79                                          const SceneRenderState *diffuseState )
 80{
 81   PROFILE_SCOPE(ParaboloidLightShadowMap_render);
 82
 83   const LightMapParams *lmParams = mLight->getExtended<LightMapParams>();
 84   const bool bUseLightmappedGeometry = lmParams ? !lmParams->representedInLightmap || lmParams->includeLightmappedGeometryInShadow : true;
 85
 86   const U32 texSize = getBestTexSize();
 87
 88   if (  mShadowMapTex.isNull() || 
 89         mTexSize != texSize )
 90   {
 91      mTexSize = texSize;
 92
 93      mShadowMapTex.set(   mTexSize, mTexSize, 
 94                           ShadowMapFormat, &ShadowMapProfile, 
 95                           "ParaboloidLightShadowMap" );
 96   }
 97
 98   GFXFrustumSaver frustSaver;
 99   GFXTransformSaver saver;
100   
101   // Render the shadowmap!
102   GFX->pushActiveRenderTarget();
103
104   // Calc matrix and set up visible distance
105   mWorldToLightProj = mLight->getTransform();
106   mWorldToLightProj.inverse();
107   GFX->setWorldMatrix(mWorldToLightProj);
108
109   const F32 &lightRadius = mLight->getRange().x;
110   GFX->setOrtho(-lightRadius, lightRadius, -lightRadius, lightRadius, 1.0f, lightRadius, true);
111
112   // Set up target
113   mTarget->attachTexture( GFXTextureTarget::Color0, mShadowMapTex );
114   mTarget->attachTexture( GFXTextureTarget::DepthStencil, 
115      _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) );
116   GFX->setActiveRenderTarget(mTarget);
117   GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 1.0f, 0);
118
119   // Create scene state, prep it
120   SceneManager* sceneManager = diffuseState->getSceneManager();
121   
122   SceneRenderState shadowRenderState
123   (
124      sceneManager,
125      SPT_Shadow,
126      SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ),
127      renderPass
128   );
129
130   shadowRenderState.getMaterialDelegate().bind( this, &LightShadowMap::getShadowMaterial );
131   shadowRenderState.renderNonLightmappedMeshes( true );
132   shadowRenderState.renderLightmappedMeshes( bUseLightmappedGeometry );
133   shadowRenderState.setDiffuseCameraTransform( diffuseState->getCameraTransform() );
134   shadowRenderState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() );
135
136   sceneManager->renderSceneNoLights( &shadowRenderState, SHADOW_TYPEMASK );
137
138   _debugRender( &shadowRenderState );
139 
140   mTarget->resolve();
141   GFX->popActiveRenderTarget();   
142}
143