shadowMapManager.cpp 6.5 KB

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