Torque3D Documentation / _generateds / shadowMapManager.cpp

shadowMapManager.cpp

Engine/source/lighting/shadowMap/shadowMapManager.cpp

More...

Detailed Description

Public Variables

 MODULE_END 
 MODULE_INIT 
 MODULE_SHUTDOWN 

Public Functions

AFTER_MODULE_INIT(Sim )

GFX_ImplementTextureProfile(ShadowMapTexProfile , GFXTextureProfile::DiffuseMap , GFXTextureProfile::PreserveSize</a>|<a href="/coding/class/classgfxtextureprofile/#classgfxtextureprofile_1a09105f0bf717a1f2ae49ceda21b8e82da3dd353f9e8b45762fccd85bd4e7cf25a">GFXTextureProfile::Dynamic , GFXTextureProfile::NONE )

  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/shadowMapManager.h"
 26
 27#include "lighting/shadowMap/shadowMapPass.h"
 28#include "lighting/shadowMap/lightShadowMap.h"
 29#include "materials/materialManager.h"
 30#include "lighting/lightManager.h"
 31#include "core/util/safeDelete.h"
 32#include "scene/sceneRenderState.h"
 33#include "gfx/gfxTextureManager.h"
 34#include "core/module.h"
 35#include "console/consoleTypes.h"
 36
 37
 38GFX_ImplementTextureProfile(ShadowMapTexProfile,
 39                            GFXTextureProfile::DiffuseMap, 
 40                            GFXTextureProfile::PreserveSize | GFXTextureProfile::Dynamic , 
 41                            GFXTextureProfile::NONE);
 42
 43
 44MODULE_BEGIN( ShadowMapManager )
 45#ifndef TORQUE_BASIC_LIGHTING
 46   MODULE_SHUTDOWN_AFTER(Scene)
 47#endif
 48
 49   MODULE_INIT
 50   {
 51      ManagedSingleton< ShadowMapManager >::createSingleton();
 52   }
 53   
 54   MODULE_SHUTDOWN
 55   {
 56      ManagedSingleton< ShadowMapManager >::deleteSingleton();
 57   }
 58
 59MODULE_END;
 60
 61
 62AFTER_MODULE_INIT( Sim )
 63{
 64   Con::addVariable( "$pref::Shadows::textureScalar", 
 65      TypeF32, &LightShadowMap::smShadowTexScalar,
 66      "@brief Used to scale the shadow texture sizes.\n"
 67      "This can reduce the shadow quality and texture memory overhead or increase them.\n"
 68      "@ingroup AdvancedLighting\n" );
 69   Con::NotifyDelegate callabck( &LightShadowMap::releaseAllTextures );
 70   Con::addVariableNotify( "$pref::Shadows::textureScalar", callabck );
 71
 72   Con::addVariable( "$pref::Shadows::disable", 
 73      TypeBool, &ShadowMapPass::smDisableShadowsPref,
 74      "Used to disable all shadow rendering.\n"
 75      "@ingroup AdvancedLighting\n" );
 76
 77   Con::addVariable( "$Shadows::disable", 
 78      TypeBool, &ShadowMapPass::smDisableShadowsEditor,
 79      "Used by the editor to disable all shadow rendering.\n"
 80      "@ingroup AdvancedLighting\n" );
 81
 82   Con::NotifyDelegate shadowCallback( &ShadowMapManager::updateShadowDisable );
 83   Con::addVariableNotify( "$pref::Shadows::disable", shadowCallback );
 84   Con::addVariableNotify( "$Shadows::disable", shadowCallback );
 85
 86   Con::addVariable("$pref::Shadows::teleportDist",
 87      TypeF32, &ShadowMapPass::smShadowsTeleportDist,
 88      "Minimum distance moved per frame to determine that we are teleporting.\n");
 89   Con::addVariableNotify("$pref::Shadows::teleportDist", shadowCallback);
 90
 91   Con::addVariable("$pref::Shadows::turnRate",
 92      TypeF32, &ShadowMapPass::smShadowsTurnRate,
 93      "Minimum angle moved per frame to determine that we are turning quickly.\n");
 94   Con::addVariableNotify("$pref::Shadows::turnRate", shadowCallback);
 95}
 96
 97Signal<void(void)> ShadowMapManager::smShadowDeactivateSignal;
 98
 99
100ShadowMapManager::ShadowMapManager() 
101:  mShadowMapPass(NULL), 
102   mCurrentShadowMap(NULL),
103   mIsActive(false)
104{
105}
106
107ShadowMapManager::~ShadowMapManager()
108{
109}
110
111void ShadowMapManager::setLightShadowMapForLight( LightInfo *light )
112{
113   ShadowMapParams *params = light->getExtended<ShadowMapParams>();
114   if ( params )
115   {
116      mCurrentShadowMap = params->getShadowMap();
117   }
118   else 
119   {
120      mCurrentShadowMap = NULL;
121   }
122}
123
124void ShadowMapManager::activate()
125{
126   ShadowManager::activate();
127
128   if (!getSceneManager())
129   {
130      Con::errorf("This world has no scene manager!  Shadow manager not activating!");
131      return;
132   }
133
134   mShadowMapPass = new ShadowMapPass(LIGHTMGR, this);
135
136   getSceneManager()->getPreRenderSignal().notify( this, &ShadowMapManager::_onPreRender, 0.01f );
137   GFXTextureManager::addEventDelegate( this, &ShadowMapManager::_onTextureEvent );
138
139   mIsActive = true;
140}
141
142void ShadowMapManager::deactivate()
143{
144   GFXTextureManager::removeEventDelegate( this, &ShadowMapManager::_onTextureEvent );
145   getSceneManager()->getPreRenderSignal().remove( this, &ShadowMapManager::_onPreRender );
146
147   SAFE_DELETE(mShadowMapPass);
148   mTapRotationTex = NULL;
149
150   // Clean up our shadow texture memory.
151   LightShadowMap::releaseAllTextures();
152   TEXMGR->cleanupPool();
153
154   mIsActive = false;
155
156   ShadowManager::deactivate();
157}
158
159void ShadowMapManager::_onPreRender( SceneManager *sg, const SceneRenderState *state )
160{
161   if ( mShadowMapPass && state->isDiffusePass() )
162      mShadowMapPass->render( sg, state, (U32)-1 );
163}
164
165void ShadowMapManager::_onTextureEvent( GFXTexCallbackCode code )
166{
167   if ( code == GFXZombify )
168      mTapRotationTex = NULL;
169}
170
171GFXTextureObject* ShadowMapManager::getTapRotationTex()
172{
173   if ( mTapRotationTex.isValid() )
174      return mTapRotationTex;
175
176   mTapRotationTex.set( 64, 64, GFXFormatR8G8B8A8, &ShadowMapTexProfile, 
177                        "ShadowMapManager::getTapRotationTex" );
178
179   GFXLockedRect *rect = mTapRotationTex.lock();
180   U8 *f = rect->bits;
181   F32 angle;
182   for( U32 i = 0; i < 64*64; i++, f += 4 )
183   {         
184      // We only pack the rotations into the red
185      // and green channels... the rest are empty.
186      angle = M_2PI_F * gRandGen.randF();
187      f[0] = U8_MAX * ( ( 1.0f + mSin( angle ) ) * 0.5f );
188      f[1] = U8_MAX * ( ( 1.0f + mCos( angle ) ) * 0.5f );
189      f[2] = 0;
190      f[3] = 0;
191   }
192
193   mTapRotationTex.unlock();
194
195   return mTapRotationTex;
196}
197
198void ShadowMapManager::updateShadowDisable()
199{
200   bool disable = false;
201
202   if ( ShadowMapPass::smDisableShadowsEditor || ShadowMapPass::smDisableShadowsPref )
203      disable = true;
204
205   if ( disable != ShadowMapPass::smDisableShadows)
206   {
207      ShadowMapPass::smDisableShadows = disable;
208      smShadowDeactivateSignal.trigger();
209   }
210}
211