W3DShadow.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DShadow.cpp ///////////////////////////////////////////////////////////
  24. //
  25. // Real time shadow representations
  26. //
  27. // Author: Mark Wilczynski, February 2002
  28. //
  29. //
  30. // USER INCLUDES //////////////////////////////////////////////////////////////
  31. #include "always.h"
  32. #include "GameClient/View.h"
  33. #include "WW3D2/Camera.h"
  34. #include "WW3D2/Light.h"
  35. #include "WW3D2/DX8Wrapper.h"
  36. #include "WW3D2/HLod.h"
  37. #include "WW3D2/mesh.h"
  38. #include "WW3D2/meshmdl.h"
  39. #include "Lib/BaseType.h"
  40. #include "W3DDevice/GameClient/W3DGranny.h"
  41. #include "W3DDevice/GameClient/Heightmap.h"
  42. #include "D3dx8math.h"
  43. #include "common/GlobalData.h"
  44. #include "W3DDevice/GameClient/W3DVolumetricShadow.h"
  45. #include "W3DDevice/GameClient/W3DProjectedShadow.h"
  46. #include "W3DDevice/GameClient/W3DShadow.h"
  47. #include "WW3D2/statistics.h"
  48. #include "Common/Debug.h"
  49. #include "Common/PerfTimer.h"
  50. #define SUN_DISTANCE_FROM_GROUND 10000.0f //distance of sun (our only light source).
  51. // Global Variables and Functions /////////////////////////////////////////////
  52. W3DShadowManager *TheW3DShadowManager=NULL;
  53. const FrustumClass *shadowCameraFrustum;
  54. Vector3 LightPosWorld[ MAX_SHADOW_LIGHTS ] =
  55. {
  56. Vector3( 94.0161f, 50.499f, 200.0f)
  57. };
  58. //DECLARE_PERF_TIMER(shadowsRender)
  59. void DoShadows(RenderInfoClass & rinfo, Bool stencilPass)
  60. {
  61. //USE_PERF_TIMER(shadowsRender)
  62. shadowCameraFrustum=&rinfo.Camera.Get_Frustum();
  63. Int projectionCount=0;
  64. //Projected shadows render first because they may fill the stencil buffer
  65. //which will be used by the shadow volumes
  66. if (stencilPass == FALSE && TheW3DProjectedShadowManager)
  67. {
  68. if (TheW3DShadowManager->isShadowScene())
  69. projectionCount=TheW3DProjectedShadowManager->renderShadows(rinfo);
  70. }
  71. if (stencilPass == TRUE && TheW3DVolumetricShadowManager)
  72. {
  73. // TheW3DShadowManager->loadTerrainShadows();
  74. //This function gets called many times by the W3D renderer
  75. //so we use this flag to make sure shadows rendered only once per frame.
  76. if (TheW3DShadowManager->isShadowScene())
  77. TheW3DVolumetricShadowManager->renderShadows(projectionCount);
  78. }
  79. if (TheW3DShadowManager && stencilPass) //reset so no more shadow processing this frame.
  80. TheW3DShadowManager->queueShadows(FALSE);
  81. }
  82. W3DShadowManager::W3DShadowManager( void )
  83. {
  84. DEBUG_ASSERTCRASH(TheW3DVolumetricShadowManager == NULL && TheW3DProjectedShadowManager == NULL,
  85. ("Creating new shadow managers without deleting old ones"));
  86. m_shadowColor = 0x7fa0a0a0;
  87. m_isShadowScene = FALSE;
  88. m_stencilShadowMask = 0; //all bits can be used for storing shadows.
  89. Vector3 lightRay(-TheGlobalData->m_terrainLightPos[0].x,
  90. -TheGlobalData->m_terrainLightPos[0].y, -TheGlobalData->m_terrainLightPos[0].z);
  91. lightRay.Normalize();
  92. LightPosWorld[0]=lightRay*SUN_DISTANCE_FROM_GROUND;
  93. TheW3DVolumetricShadowManager = NEW W3DVolumetricShadowManager;
  94. TheProjectedShadowManager = TheW3DProjectedShadowManager = NEW W3DProjectedShadowManager;
  95. }
  96. W3DShadowManager::~W3DShadowManager( void )
  97. {
  98. delete TheW3DVolumetricShadowManager;
  99. TheW3DVolumetricShadowManager = NULL;
  100. delete TheW3DProjectedShadowManager;
  101. TheProjectedShadowManager = TheW3DProjectedShadowManager = NULL;
  102. }
  103. /** Do one-time initilalization of shadow systems that need to be
  104. active for full duration of game*/
  105. Bool W3DShadowManager::init( void )
  106. {
  107. Bool result=TRUE;
  108. if (TheW3DVolumetricShadowManager && TheW3DVolumetricShadowManager->init())
  109. {
  110. if (TheW3DVolumetricShadowManager->ReAcquireResources())
  111. result = TRUE;
  112. }
  113. if ( TheW3DProjectedShadowManager && TheW3DProjectedShadowManager->init())
  114. {
  115. if (TheW3DProjectedShadowManager->ReAcquireResources())
  116. result = TRUE;
  117. }
  118. return result;
  119. }
  120. /** Do per-map reset. This frees up shadows from all objects since
  121. they may not exist on the next map*/
  122. void W3DShadowManager::Reset( void )
  123. {
  124. if (TheW3DVolumetricShadowManager)
  125. TheW3DVolumetricShadowManager->reset();
  126. if (TheW3DProjectedShadowManager)
  127. TheW3DProjectedShadowManager->reset();
  128. }
  129. Bool W3DShadowManager::ReAcquireResources()
  130. {
  131. Bool result = TRUE;
  132. if (TheW3DVolumetricShadowManager && !TheW3DVolumetricShadowManager->ReAcquireResources())
  133. result = FALSE;
  134. if (TheW3DProjectedShadowManager && !TheW3DProjectedShadowManager->ReAcquireResources())
  135. result = FALSE;
  136. return result;
  137. }
  138. void W3DShadowManager::ReleaseResources(void)
  139. {
  140. if (TheW3DVolumetricShadowManager)
  141. TheW3DVolumetricShadowManager->ReleaseResources();
  142. if (TheW3DProjectedShadowManager)
  143. TheW3DProjectedShadowManager->ReleaseResources();
  144. }
  145. Shadow *W3DShadowManager::addShadow( RenderObjClass *robj, Shadow::ShadowTypeInfo *shadowInfo, Drawable *draw)
  146. {
  147. ShadowType type = SHADOW_VOLUME;
  148. if (shadowInfo)
  149. type = shadowInfo->m_type;
  150. switch(type)
  151. {
  152. case SHADOW_VOLUME:
  153. if (TheW3DVolumetricShadowManager)
  154. return (Shadow *)TheW3DVolumetricShadowManager->addShadow(robj, shadowInfo, draw);
  155. break;
  156. case SHADOW_PROJECTION:
  157. case SHADOW_DECAL:
  158. if (TheW3DProjectedShadowManager)
  159. return (Shadow *)TheW3DProjectedShadowManager->addShadow(robj, shadowInfo, draw);
  160. break;
  161. default:
  162. return NULL;
  163. }
  164. return NULL;
  165. }
  166. void W3DShadowManager::removeShadow(Shadow *shadow)
  167. {
  168. shadow->release();
  169. }
  170. void W3DShadowManager::removeAllShadows(void)
  171. {
  172. if (TheW3DVolumetricShadowManager)
  173. TheW3DVolumetricShadowManager->removeAllShadows();
  174. if (TheW3DProjectedShadowManager)
  175. TheW3DProjectedShadowManager->removeAllShadows();
  176. }
  177. /**Force update of all shadows even when light source and object have not moved*/
  178. void W3DShadowManager::invalidateCachedLightPositions(void)
  179. {
  180. if (TheW3DVolumetricShadowManager)
  181. TheW3DVolumetricShadowManager->invalidateCachedLightPositions();
  182. if (TheW3DProjectedShadowManager)
  183. TheW3DProjectedShadowManager->invalidateCachedLightPositions();
  184. }
  185. Vector3 &W3DShadowManager::getLightPosWorld(Int lightIndex)
  186. {
  187. return LightPosWorld[lightIndex];
  188. }
  189. void W3DShadowManager::setLightPosition(Int lightIndex, Real x, Real y, Real z)
  190. {
  191. if (lightIndex != 0)
  192. return; ///@todo: Add support for multiple lights
  193. LightPosWorld[lightIndex]=Vector3(x,y,z);
  194. }
  195. void W3DShadowManager::setTimeOfDay(TimeOfDay tod)
  196. {
  197. //Ray to light source
  198. const GlobalData::TerrainLighting *ol=&TheGlobalData->m_terrainObjectsLighting[tod][0];
  199. Vector3 lightRay(-ol->lightPos.x,-ol->lightPos.y,-ol->lightPos.z);
  200. lightRay.Normalize();
  201. lightRay *= SUN_DISTANCE_FROM_GROUND;
  202. setLightPosition(0, lightRay.X, lightRay.Y, lightRay.Z);
  203. }