Shadow.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: Shadow.h /////////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, November 2001
  25. // Modified: Mark Wilczynski, February 2002
  26. // Desc: Shadow descriptions
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. #pragma once
  29. #ifndef __SHADOW_H_
  30. #define __SHADOW_H_
  31. //
  32. // skeleton definition of shadow types
  33. //
  34. // shadow bit flags, keep this in sync with TheShadowNames
  35. enum ShadowType
  36. {
  37. SHADOW_NONE = 0x00000000,
  38. SHADOW_DECAL = 0x00000001, //shadow decal applied via modulate blend
  39. SHADOW_VOLUME = 0x00000002,
  40. SHADOW_PROJECTION = 0x00000004,
  41. SHADOW_DYNAMIC_PROJECTION = 0x00000008, //extra setting for shadows which need dynamic updates
  42. SHADOW_DIRECTIONAL_PROJECTION = 0x00000010, //extra setting for shadow decals that rotate with sun direction
  43. SHADOW_ALPHA_DECAL = 0x00000020, //not really for shadows but for other decal uses. Alpha blended.
  44. SHADOW_ADDITIVE_DECAL = 0x00000040 //not really for shadows but for other decal uses. Additive blended.
  45. };
  46. #ifdef DEFINE_SHADOW_NAMES
  47. static const char* TheShadowNames[] =
  48. {
  49. "SHADOW_DECAL",
  50. "SHADOW_VOLUME",
  51. "SHADOW_PROJECTION",
  52. "SHADOW_DYNAMIC_PROJECTION",
  53. "SHADOW_DIRECTIONAL_PROJECTION",
  54. "SHADOW_ALPHA_DECAL",
  55. "SHADOW_ADDITIVE_DECAL",
  56. NULL
  57. };
  58. #endif // end DEFINE_SHADOW_NAMES
  59. #define MAX_SHADOW_LIGHTS 1 //maximum number of shadow casting light sources in scene - support for more than 1 has been dropped from most code.
  60. class RenderObjClass; //forward reference
  61. class RenderCost; //forward reference
  62. //Interface to all shadow objects.
  63. class Shadow
  64. {
  65. public:
  66. struct ShadowTypeInfo
  67. {
  68. char m_ShadowName[64]; //when set, overrides the default model shadow (used mostly for Decals).
  69. ShadowType m_type; //type of shadow
  70. Bool allowUpdates; //whether to update the shadow image when object/light moves.
  71. Bool allowWorldAlign; //whether to align shadow to world geometry or draw as horizontal decal.
  72. Real m_sizeX; //world size of decal projection
  73. Real m_sizeY; //world size of decal projection
  74. Real m_offsetX; //world shift along x axis
  75. Real m_offsetY; //world shift along y axis
  76. };
  77. Shadow(void) : m_diffuse(0xffffffff), m_color(0xffffffff), m_opacity (0x000000ff), m_localAngle(0.0f) {}
  78. ///<if this is set, then no render will occur, even if enableShadowRender() is enabled. Used by Shroud.
  79. void enableShadowInvisible(Bool isEnabled);
  80. void enableShadowRender(Bool isEnabled);
  81. Bool isRenderEnabled(void) {return m_isEnabled;}
  82. Bool isInvisibleEnabled(void) {return m_isInvisibleEnabled;}
  83. virtual void release(void)=0; ///<release this shadow from suitable manager.
  84. void setOpacity(Int value); ///<adjust opacity of decal/shadow
  85. void setColor(Color value);///<adjust ARGB color of decal/shadow
  86. void setAngle(Real angle); ///<adjust orientation around z-axis
  87. void setPosition(Real x, Real y, Real z);
  88. void setSize(Real sizeX, Real sizeY)
  89. {
  90. m_decalSizeX = sizeX;
  91. m_decalSizeY = sizeY;
  92. if (sizeX == 0)
  93. m_oowDecalSizeX = 0;
  94. else
  95. m_oowDecalSizeX = 1.0f/sizeX ;
  96. if (sizeY == 0)
  97. m_oowDecalSizeY = 0;
  98. else
  99. m_oowDecalSizeY = 1.0f/sizeY ;
  100. };
  101. #if defined(_DEBUG) || defined(_INTERNAL)
  102. virtual void getRenderCost(RenderCost & rc) const = 0;
  103. #endif
  104. protected:
  105. Bool m_isEnabled; /// toggle to turn rendering of this shadow on/off.
  106. Bool m_isInvisibleEnabled; /// if set, overrides and causes no rendering.
  107. UnsignedInt m_opacity; ///< value between 0 (transparent) and 255 (opaque)
  108. UnsignedInt m_color; ///< color in ARGB format. (Alpha is ignored).
  109. ShadowType m_type; /// type of projection
  110. Int m_diffuse; /// diffuse color used to tint/fade shadow.
  111. Real m_x,m_y,m_z; /// world position of shadow center when not bound to robj/drawable.
  112. Real m_oowDecalSizeX; /// 1/(world space extent of texture in x direction)
  113. Real m_oowDecalSizeY; /// 1/(world space extent of texture in y direction)
  114. Real m_decalSizeX; /// 1/(world space extent of texture in x direction)
  115. Real m_decalSizeY; /// 1/(world space extent of texture in y direction)
  116. Real m_localAngle; /// yaw or rotation around z-axis of shadow image when not bound to robj/drawable.
  117. };
  118. inline void Shadow::enableShadowRender(Bool isEnabled)
  119. {
  120. m_isEnabled=isEnabled;
  121. }
  122. inline void Shadow::enableShadowInvisible(Bool isEnabled)
  123. {
  124. m_isInvisibleEnabled=isEnabled;
  125. }
  126. ///@todo: Pull these out so casting, etc. is only done for visible decals.
  127. inline void Shadow::setOpacity(Int value)
  128. {
  129. m_opacity=value;
  130. if (m_type & SHADOW_ALPHA_DECAL)
  131. {
  132. m_diffuse = (m_color & 0x00ffffff) + (value << 24);
  133. // m_diffuse = m_color | (value << 24);ML changed
  134. }
  135. else
  136. {
  137. if (m_type & SHADOW_ADDITIVE_DECAL)
  138. {
  139. Real fvalue=(Real)m_opacity/255.0f;
  140. m_diffuse=REAL_TO_INT(((Real)(m_color & 0xff) * fvalue))
  141. |REAL_TO_INT(((Real)((m_color >> 8) & 0xff) * fvalue))
  142. |REAL_TO_INT(((Real)((m_color >> 16) & 0xff) * fvalue));
  143. }
  144. }
  145. }
  146. inline void Shadow::setColor(Color value)
  147. {
  148. m_color = value & 0x00ffffff; //filter out alpha
  149. if (m_type & SHADOW_ALPHA_DECAL)
  150. {
  151. m_diffuse=m_color | (m_opacity << 24);
  152. }
  153. else
  154. {
  155. if (m_type & SHADOW_ADDITIVE_DECAL)
  156. {
  157. Real fvalue=(Real)m_opacity/255.0f;
  158. m_diffuse=REAL_TO_INT(((Real)(m_color & 0xff) * fvalue))
  159. |REAL_TO_INT(((Real)((m_color >> 8) & 0xff) * fvalue))
  160. |REAL_TO_INT(((Real)((m_color >> 16) & 0xff) * fvalue));
  161. }
  162. }
  163. }
  164. inline void Shadow::setPosition(Real x, Real y, Real z)
  165. {
  166. m_x=x; m_y=y; m_z=z;
  167. }
  168. inline void Shadow::setAngle(Real angle)
  169. {
  170. m_localAngle=angle;
  171. }
  172. class Drawable; //forward ref.
  173. class ProjectedShadowManager
  174. {
  175. public:
  176. virtual ~ProjectedShadowManager() { };
  177. virtual Shadow *addDecal(RenderObjClass *, Shadow::ShadowTypeInfo *shadowInfo)=0; ///<add a non-shadow decal
  178. virtual Shadow *addDecal(Shadow::ShadowTypeInfo *shadowInfo)=0; ///<add a non-shadow decal which does not follow an object.
  179. };
  180. extern ProjectedShadowManager *TheProjectedShadowManager;
  181. #endif // __SHADOW_H_