shadowMapManager.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. mCurrentDynamicShadowMap(NULL),
  84. mIsActive(false)
  85. {
  86. }
  87. ShadowMapManager::~ShadowMapManager()
  88. {
  89. }
  90. void ShadowMapManager::setLightShadowMapForLight( LightInfo *light )
  91. {
  92. ShadowMapParams *params = light->getExtended<ShadowMapParams>();
  93. if ( params )
  94. {
  95. mCurrentShadowMap = params->getShadowMap();
  96. mCurrentDynamicShadowMap = params->getShadowMap(true);
  97. }
  98. else
  99. {
  100. mCurrentShadowMap = NULL;
  101. mCurrentDynamicShadowMap = NULL;
  102. }
  103. }
  104. void ShadowMapManager::activate()
  105. {
  106. ShadowManager::activate();
  107. if (!getSceneManager())
  108. {
  109. Con::errorf("This world has no scene manager! Shadow manager not activating!");
  110. return;
  111. }
  112. mShadowMapPass = new ShadowMapPass(LIGHTMGR, this);
  113. getSceneManager()->getPreRenderSignal().notify( this, &ShadowMapManager::_onPreRender, 0.01f );
  114. GFXTextureManager::addEventDelegate( this, &ShadowMapManager::_onTextureEvent );
  115. mIsActive = true;
  116. }
  117. void ShadowMapManager::deactivate()
  118. {
  119. GFXTextureManager::removeEventDelegate( this, &ShadowMapManager::_onTextureEvent );
  120. getSceneManager()->getPreRenderSignal().remove( this, &ShadowMapManager::_onPreRender );
  121. SAFE_DELETE(mShadowMapPass);
  122. mTapRotationTex = NULL;
  123. // Clean up our shadow texture memory.
  124. LightShadowMap::releaseAllTextures();
  125. TEXMGR->cleanupPool();
  126. mIsActive = false;
  127. ShadowManager::deactivate();
  128. }
  129. void ShadowMapManager::_onPreRender( SceneManager *sg, const SceneRenderState *state )
  130. {
  131. if ( mShadowMapPass && state->isDiffusePass() )
  132. mShadowMapPass->render( sg, state, (U32)-1 );
  133. }
  134. void ShadowMapManager::_onTextureEvent( GFXTexCallbackCode code )
  135. {
  136. if ( code == GFXZombify )
  137. mTapRotationTex = NULL;
  138. }
  139. GFXTextureObject* ShadowMapManager::getTapRotationTex()
  140. {
  141. if ( mTapRotationTex.isValid() )
  142. return mTapRotationTex;
  143. mTapRotationTex.set( 64, 64, GFXFormatR8G8B8A8, &ShadowMapTexProfile,
  144. "ShadowMapManager::getTapRotationTex" );
  145. GFXLockedRect *rect = mTapRotationTex.lock();
  146. U8 *f = rect->bits;
  147. F32 angle;
  148. for( U32 i = 0; i < 64*64; i++, f += 4 )
  149. {
  150. // We only pack the rotations into the red
  151. // and green channels... the rest are empty.
  152. angle = M_2PI_F * gRandGen.randF();
  153. f[0] = U8_MAX * ( ( 1.0f + mSin( angle ) ) * 0.5f );
  154. f[1] = U8_MAX * ( ( 1.0f + mCos( angle ) ) * 0.5f );
  155. f[2] = 0;
  156. f[3] = 0;
  157. }
  158. mTapRotationTex.unlock();
  159. return mTapRotationTex;
  160. }
  161. void ShadowMapManager::updateShadowDisable()
  162. {
  163. bool disable = false;
  164. if ( ShadowMapPass::smDisableShadowsEditor || ShadowMapPass::smDisableShadowsPref )
  165. disable = true;
  166. if ( disable != ShadowMapPass::smDisableShadows)
  167. {
  168. ShadowMapPass::smDisableShadows = disable;
  169. smShadowDeactivateSignal.trigger();
  170. }
  171. }