shadowMapManager.cpp 6.1 KB

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