W3DVolumetricShadow.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #pragma once
  24. #ifndef __W3DVOLUMETRICSHADOW_H_
  25. #define __W3DVOLUMETRICSHADOW_H_
  26. #include "matrix4.h"
  27. #include "W3DDevice/GameClient/W3DBufferManager.h"
  28. #include "GameClient/Shadow.h"
  29. ///@todo Make the 100 below a 'better' number. Was 32, increased because of overcomplex models.
  30. #define MAX_SHADOW_CASTER_MESHES 160 //number of meshes allowed in animated hierarchy (must be <256 since index is a byte).
  31. class W3DShadowGeometry; //forward reference
  32. class W3DShadowGeometryManager; //forward reference
  33. struct Geometry; //forward reference
  34. struct PolyNeighbor; //forward reference
  35. class W3DVolumetricShadow; //forward reference
  36. class Drawable; //forward reference
  37. struct W3DVolumetricShadowRenderTask : public W3DBufferManager::W3DRenderTask
  38. {
  39. W3DVolumetricShadow *m_parentShadow; ///<main casting object to which this volume belongs.
  40. UnsignedByte m_meshIndex; ///<mesh index of volume within parent to render.
  41. UnsignedByte m_lightIndex; ///<light index of volume within parent to render.
  42. };
  43. // ShadowManager -------------------------------------------------------------
  44. class W3DVolumetricShadowManager
  45. {
  46. public:
  47. W3DVolumetricShadowManager( void );
  48. ~W3DVolumetricShadowManager( void );
  49. Bool init( void ); ///<initialize resources used by manager, must have valid D3D device.
  50. // shadow list management
  51. void reset( void );
  52. W3DVolumetricShadow* addShadow( RenderObjClass *robj, Shadow::ShadowTypeInfo *shadowInfo, Drawable *draw); ///< adds shadow caster to rendering system.
  53. void removeShadow(W3DVolumetricShadow *shadow); ///< removed shadow from rendering system and frees its resources.
  54. void removeAllShadows(void); ///< Remove all shadows.
  55. /// queues up a dynamic shadow caster for rendering - only used internally by shadow system.
  56. void addDynamicShadowTask(W3DVolumetricShadowRenderTask *task)
  57. { W3DBufferManager::W3DRenderTask *oldTask=m_dynamicShadowVolumesToRender;
  58. m_dynamicShadowVolumesToRender=task;
  59. m_dynamicShadowVolumesToRender->m_nextTask=oldTask;
  60. }
  61. void invalidateCachedLightPositions(void); ///<forces shadow volumes to update regardless of last lightposition
  62. void loadTerrainShadows(void);
  63. // rendering
  64. void renderShadows( Bool forceStencilFill );
  65. void ReleaseResources(void);
  66. Bool ReAcquireResources(void);
  67. protected:
  68. // to render the stencil buffer polygon to the screen
  69. void renderStencilShadows( void );
  70. W3DVolumetricShadow *m_shadowList;
  71. W3DVolumetricShadowRenderTask *m_dynamicShadowVolumesToRender;
  72. W3DShadowGeometryManager *m_W3DShadowGeometryManager;
  73. }; // end class W3DVolumetricShadowManager
  74. extern W3DVolumetricShadowManager *TheW3DVolumetricShadowManager;
  75. // W3DVolumetricShadow ---------------------------------------------------------------------
  76. class W3DVolumetricShadow : public Shadow
  77. {
  78. friend class W3DVolumetricShadowManager;
  79. enum
  80. {
  81. SHADOW_DYNAMIC = 0x1 //flag indicating a dynamic shadow caster (animated mesh).
  82. };
  83. public:
  84. W3DVolumetricShadow( void );
  85. ~W3DVolumetricShadow( void );
  86. protected:
  87. virtual void release(void) {TheW3DVolumetricShadowManager->removeShadow(this);} ///<release shadow from manager
  88. #if defined(_DEBUG) || defined(_INTERNAL)
  89. virtual void getRenderCost(RenderCost & rc) const;
  90. #endif
  91. // tie in geometry and transformation for this shadow
  92. void SetGeometry( W3DShadowGeometry *geometry );
  93. void setShadowLengthScale(Real value) {m_shadowLengthScale = value;}
  94. void updateOptimalExtrusionPadding(void); ///<for immobile objects, figure out the length of extrusion needed to hit ground - expensive!
  95. void setOptimalExtrusionPadding(Real value) {m_extraExtrusionPadding=value;}
  96. const W3DShadowGeometry *getGeometry(void) {return m_geometry;}
  97. void setRenderObject( RenderObjClass *robj) {assert(m_robj==NULL); m_robj=robj;}
  98. void setRenderObjExtent ( Real extent) { m_robjExtent = extent; }
  99. // called once per frame, updates shadow volume when necessary
  100. void Update();
  101. void updateVolumes(Real zoffset); ///<update shadow volumes of all meshes in this model
  102. void updateMeshVolume(Int meshIndex, Int lightIndex, const Matrix3D *meshXform, const AABoxClass &meshBox, float floorZ);///<update shadow volume of this mesh.
  103. // rendering interface
  104. void RenderVolume(Int meshIndex, Int lightIndex); ///<renders a specifc volume from the model hierarchy
  105. ///render single mesh which could belong to a larger hierarchy (optimized for static meshes).
  106. void RenderMeshVolume(Int meshIndex, Int lightIndex, const Matrix3D *meshXform);
  107. ///render single mesh which could belong to a larger hierarchy (optimized for animated meshes).
  108. void RenderDynamicMeshVolume(Int meshIndex, Int lightIndex, const Matrix3D *meshXform);
  109. void RenderMeshVolumeBounds(Int meshIndex, Int lightIndex, const Matrix3D *meshXform); ///<render bounding volume around shadow volume - for debug use.
  110. void setLightPosHistory(Int lightIndex, Int meshIndex, Vector3 &pos) {m_lightPosHistory[lightIndex][meshIndex]=pos;} ///<updates the last position of light
  111. W3DVolumetricShadow *m_next; /// for the shadow manager list
  112. // silhouette tools
  113. void buildSilhouette(Int meshIndex, Vector3 *lightPosWorld);
  114. void addSilhouetteEdge(Int meshIndex, PolyNeighbor *visible, PolyNeighbor *hidden );
  115. void addNeighborlessEdges(Int meshIndex, PolyNeighbor *us );
  116. void addSilhouetteIndices(Int meshIndex, Short edgeStart, Short edgeEnd );
  117. Bool allocateSilhouette(Int meshIndex, Int numVertices ); // allocate memory for sil
  118. void deleteSilhouette(Int meshIndex ); // resets and frees silhouette memory
  119. void resetSilhouette( Int meshIndex ); // reset silhouette to empty
  120. // shadow volume access
  121. void constructVolume( Vector3 *lightPos, Real shadowExtrudeDistance, Int volumeIndex, Int meshIndex );
  122. void constructVolumeVB( Vector3 *lightPosObject,Real shadowExtrudeDistance, Int volumeIndex, Int meshIndex );
  123. Bool allocateShadowVolume( Int volumeIndex, Int meshIndex ); // allocate mem
  124. void deleteShadowVolume( Int volumeIndex ); // delete all volume data
  125. void resetShadowVolume( Int volumeIndex, Int meshIndex ); // reset shadow volume
  126. // this is our current geometry we will derive the shadow from
  127. W3DShadowGeometry *m_geometry;
  128. RenderObjClass *m_robj; ///<render object belonging to model casting shadow
  129. Real m_shadowLengthScale; ///<scale factor used to reduce/clamp shadow length
  130. Real m_robjExtent; ///<maximum possible horizontal reach of shadow from center of object.
  131. Real m_extraExtrusionPadding; ///<amount to extrude shadow beyond where it would normally stop (ground level below object).
  132. //
  133. // this geometry is our actual shadow volume data, we have an array
  134. // of them so we can have separate shadow volume for each light source
  135. // casting shadows
  136. //
  137. static Geometry m_tempShadowVolume; //scratch buffer to use by all shadows during volume construction.
  138. Geometry *m_shadowVolume[ MAX_SHADOW_LIGHTS ][MAX_SHADOW_CASTER_MESHES];
  139. W3DBufferManager::W3DVertexBufferSlot *m_shadowVolumeVB[ MAX_SHADOW_LIGHTS ][MAX_SHADOW_CASTER_MESHES];
  140. W3DBufferManager::W3DIndexBufferSlot *m_shadowVolumeIB[ MAX_SHADOW_LIGHTS ][MAX_SHADOW_CASTER_MESHES];
  141. W3DVolumetricShadowRenderTask m_shadowVolumeRenderTask[ MAX_SHADOW_LIGHTS ][MAX_SHADOW_CASTER_MESHES];
  142. Int m_shadowVolumeCount[MAX_SHADOW_CASTER_MESHES]; // how man shadows are valid in m_shadowVolume
  143. Vector3 m_lightPosHistory[ MAX_SHADOW_LIGHTS ][MAX_SHADOW_CASTER_MESHES];
  144. Matrix4x4 m_objectXformHistory[ MAX_SHADOW_LIGHTS ][MAX_SHADOW_CASTER_MESHES];
  145. // silhouette building space
  146. Short *m_silhouetteIndex[MAX_SHADOW_CASTER_MESHES]; // silhouette vertex index list, edges occur
  147. // as disjoint pairs. The acutal size of this
  148. // piece of memory must accomodate #vertices*2
  149. Short m_numSilhouetteIndices[MAX_SHADOW_CASTER_MESHES]; // total number of edge indices in the index
  150. // array, these are pairs and therefore
  151. // always a multiple of two
  152. Short m_maxSilhouetteEntries[MAX_SHADOW_CASTER_MESHES]; // how big the silhouette index can hold max
  153. Int m_numIndicesPerMesh[MAX_SHADOW_CASTER_MESHES]; ///<silhouette indices from each mesh.
  154. }; // end class W3DVolumetricShadow
  155. #endif //__W3DVOLUMETRICSHADOW_H_