shadowMapManager.cpp 5.9 KB

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