W3DProjectedShadow.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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: W3DProjectedShadow.h ///////////////////////////////////////////////////////////
  24. //
  25. // Real time shadow projection through textures.
  26. //
  27. // Author: Mark Wilczynski, February 2002
  28. //
  29. //
  30. ///////////////////////////////////////////////////////////////////////////////
  31. #pragma once
  32. #ifndef __W3D_PROJECTED_SHADOW_H_
  33. #define __W3D_PROJECTED_SHADOW_H_
  34. #include "GameClient/Shadow.h"
  35. class W3DShadowTexture; //forward reference
  36. class W3DShadowTextureManager; //forward reference
  37. class Drawable; //forward reference
  38. class W3DProjectedShadow; //forward reference.
  39. class TexProjectClass;
  40. class W3DProjectedShadowManager : public ProjectedShadowManager
  41. {
  42. /* enum ShadowTextureType {
  43. STT_STATIC = 0x0001,
  44. STT_DYNAMIC = 0x0002,
  45. STT_SHARED
  46. };
  47. */
  48. public:
  49. W3DProjectedShadowManager( void );
  50. ~W3DProjectedShadowManager( void );
  51. Bool init(void); ///<allocate one-time shadow assets for length of entire game.
  52. void reset(void); ///<free all existing shadows - ready for next map.
  53. void shutdown(void); ///<free all assets prior to shutdown of entire game.
  54. Int renderShadows(RenderInfoClass & rinfo); ///<iterate over each object and render its shadow onto affected objects.
  55. void ReleaseResources(void); ///<release device dependent D3D resources.
  56. Bool ReAcquireResources(void); ///<allocate device dependent D3D resources.
  57. void invalidateCachedLightPositions(void); ///<forces shadows to update regardless of last lightposition
  58. virtual Shadow *addDecal(RenderObjClass *robj, Shadow::ShadowTypeInfo *shadowInfo); ///<add a non-shadow decal
  59. virtual Shadow *addDecal(Shadow::ShadowTypeInfo *shadowInfo); ///<add a non-shadow decal which does not follow an object.
  60. W3DProjectedShadow *addShadow( RenderObjClass *robj, Shadow::ShadowTypeInfo *shadowInfo, Drawable *draw); ///<add a new shadow with texture of given name or that of robj.
  61. W3DProjectedShadow *createDecalShadow( Shadow::ShadowTypeInfo *shadowInfo); ///<add a new shadow with texture of given name or that of robj.
  62. void removeShadow (W3DProjectedShadow *shadow);
  63. void removeAllShadows(void); ///< Remove all shadows.
  64. TextureClass *getRenderTarget(void) { return m_dynamicRenderTarget;}
  65. SpecialRenderInfoClass *getRenderContext(void) { return m_shadowContext;}
  66. void updateRenderTargetTextures(void); ///<render into any textures that need updating.
  67. void queueDecal(W3DProjectedShadow *shadow); ///<add shadow decal to render list - decal conforms to terrain.
  68. void queueSimpleDecal(W3DProjectedShadow *shadow); ///< add shadow decal to render list - decal floats on terrain.
  69. void flushDecals(W3DShadowTexture *texture, ShadowType type); ///<empty queue by rendering all decals with given texture
  70. private:
  71. W3DProjectedShadow *m_shadowList;
  72. W3DProjectedShadow *m_decalList;
  73. TextureClass *m_dynamicRenderTarget; ///<offscreen video memory texture used to render all shadow textures.
  74. Bool m_renderTargetHasAlpha; ///<does render target have destination alpha support?
  75. CameraClass *m_shadowCamera; ///<camera used to render all shadow textures - configured by projector
  76. LightEnvironmentClass m_shadowLightEnv;
  77. SpecialRenderInfoClass *m_shadowContext;
  78. W3DShadowTextureManager *m_W3DShadowTextureManager;
  79. Int m_numDecalShadows; ///< number of decal shadows in the system.
  80. Int m_numProjectionShadows; ///< number of projected shadows in the system.
  81. Int renderProjectedTerrainShadow(W3DProjectedShadow *shadow, AABoxClass &box); ///<render shadow on map terrain.
  82. };
  83. extern W3DProjectedShadowManager *TheW3DProjectedShadowManager;
  84. /** Object for maintaining and updating an object's shadow texture.
  85. */
  86. class W3DProjectedShadow : public Shadow
  87. {
  88. friend class W3DProjectedShadowManager;
  89. public:
  90. W3DProjectedShadow(void);
  91. ~W3DProjectedShadow(void);
  92. void setRenderObject( RenderObjClass *robj) {m_robj=robj;}
  93. void setObjPosHistory(Vector3 &pos) {m_lastObjPosition=pos;} ///<position of object when projection matrix was updated.
  94. void setTexture(Int lightIndex,W3DShadowTexture *texture) {m_shadowTexture[lightIndex]=texture;} ///<textur with light's shadow
  95. void update(void); ///<updates the texture and/or projection parameters when the object or light moves.
  96. void init(void); ///<allocates local member variables used for projection
  97. void updateTexture(Vector3 &lightPos); ///<updates the shadow texture image using render object and given light position.
  98. void updateProjectionParameters(const Matrix3D &cameraXform); ///<recompute projection matrix - needed when light or object moves.
  99. TexProjectClass *getShadowProjector(void) {return m_shadowProjector;}
  100. #if defined(_DEBUG) || defined(_INTERNAL)
  101. virtual void getRenderCost(RenderCost & rc) const;
  102. #endif
  103. W3DShadowTexture *getTexture(Int lightIndex) {return m_shadowTexture[lightIndex];}
  104. protected:
  105. W3DShadowTexture *m_shadowTexture[MAX_SHADOW_LIGHTS]; ///<cached shadow data
  106. TexProjectClass *m_shadowProjector; ///<object used to generate texture and projection matrix.
  107. RenderObjClass *m_robj; ///<render object used to cast the shadow.
  108. Vector3 m_lastObjPosition; ///<position of object at time of projection matrix update.
  109. W3DProjectedShadow *m_next; /// for the shadow manager list
  110. Bool m_allowWorldAlign; /// wrap shadow around world geometry - else align perpendicular to local z-axis.
  111. Real m_decalOffsetU; /// texture coordinate offset so not centered at object origin.
  112. Real m_decalOffsetV; /// texture coordinate offset so not centered at object origin.
  113. Int m_flags; /// custom rendering flags
  114. virtual void release(void) {TheW3DProjectedShadowManager->removeShadow(this);} ///<release shadow from manager
  115. };
  116. #endif //__W3D_PROJECTED_SHADOW_H_