lightenvironment.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/lightenvironment.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Kenny Mitchell $*
  29. * *
  30. * $Modtime:: 06/27/02 9:23a $*
  31. * *
  32. * $Revision:: 5 $*
  33. * *
  34. * 06/27/02 KM Shader system light environment updates *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #if defined(_MSC_VER)
  39. #pragma once
  40. #endif
  41. #ifndef LIGHTENVIRONMENT_H
  42. #define LIGHTENVIRONMENT_H
  43. #include "always.h"
  44. #include "vector3.h"
  45. class Matrix3D;
  46. class LightClass;
  47. /**
  48. ** LightEnvironmentClass
  49. ** This class represents an approximation of the local lighting for a given point. The idea
  50. ** is to collect all of the point light sources affecting an object at any given time and
  51. ** create temporary directional light sources representing them. Any distance or directional
  52. ** attenuation will be precalculated into the overall intensity of the light and a vector from
  53. ** the light source to the center of the bounding sphere of the model will be used as the
  54. ** directional component.
  55. ** In addition, the engine will provide the ambient component which will be determined by
  56. ** a combination of the ambient setting for the level and sampling the light maps in the area.
  57. **
  58. ** Notes:
  59. ** - will need fast collection of the lights affecting any one given object
  60. ** - intensity of these lights should take into account attenuation of the original light
  61. ** - intensity should also take into account spot attenuation (no-per vertex atten...)
  62. ** - we need the direction of the lights in eye-space
  63. ** - the ambient light from all lights should be added into the ambient light (not just scene)
  64. */
  65. class LightEnvironmentClass
  66. {
  67. public:
  68. LightEnvironmentClass(void);
  69. ~LightEnvironmentClass(void);
  70. /*
  71. ** Usage (starting from scratch each frame):
  72. ** - Reset the object
  73. ** - Set the scene ambient light (will be derived from lightmap sampling probably)
  74. ** - Add in all overlapping lights, this object will keep the most important ones
  75. ** - When ready to render, call Pre_Render_Update and push into the gerd.
  76. **
  77. ** Usage (caching the lights, only done if the object and the lights are not moving)
  78. ** - Reset and collect the lights once and keep this object around
  79. ** - When ready to render, call Pre_Render_Update and push into the gerd.
  80. */
  81. void Reset(const Vector3 & object_center,const Vector3 & scene_ambient);
  82. void Add_Light(const LightClass & light);
  83. void Pre_Render_Update(const Matrix3D & camera_tm);
  84. void Add_Fill_Light(void);
  85. void Calculate_Fill_Light(void);
  86. void Set_Fill_Intensity(float intensity) { FillIntensity = intensity; }
  87. /*
  88. ** Accessors
  89. */
  90. const Vector3 & Get_Equivalent_Ambient(void) const { return OutputAmbient; }
  91. void Set_Output_Ambient(Vector3& oa) { OutputAmbient = oa; }
  92. int Get_Light_Count(void) const { return LightCount; }
  93. const Vector3 & Get_Light_Direction(int i) const { return InputLights[i].Direction; }
  94. const Vector3 & Get_Light_Diffuse(int i) const { return InputLights[i].Diffuse; }
  95. bool isPointLight(int i) const {return InputLights[i].m_point;}
  96. float getPointIrad(int i) const {return InputLights[i].m_innerRadius;}
  97. float getPointOrad(int i) const {return InputLights[i].m_outerRadius;}
  98. const Vector3 & getPointDiffuse(int i) const { return InputLights[i].m_diffuse; }
  99. const Vector3 & getPointAmbient(int i) const { return InputLights[i].m_ambient; }
  100. const Vector3 & getPointCenter(int i) const { return InputLights[i].m_center; }
  101. /*
  102. ** Lighting LOD. This is a static setting that is used to convert weak diffuse lights
  103. ** into pure ambient lights.
  104. */
  105. static void Set_Lighting_LOD_Cutoff(float inten);
  106. static float Get_Lighting_LOD_Cutoff(void);
  107. static int Get_Max_Lights() { return MAX_LIGHTS; }
  108. enum { MAX_LIGHTS = 4 }; //Made this public, so other code can tell how many lights are allowed. - MW
  109. inline bool operator== (const LightEnvironmentClass& that) const
  110. {
  111. if (LightCount!=that.LightCount) return false;
  112. bool dif=!(ObjectCenter==that.ObjectCenter);
  113. dif|=OutputAmbient!=that.OutputAmbient;
  114. for (int i=0;i<LightCount;++i) {
  115. dif|=!(OutputLights[i].Diffuse==that.OutputLights[i].Diffuse);
  116. dif|=!(OutputLights[i].Direction==that.OutputLights[i].Direction);
  117. if (dif) return false;
  118. }
  119. return true;
  120. }
  121. protected:
  122. struct InputLightStruct
  123. {
  124. void Init(const LightClass & light,const Vector3 & object_center);
  125. void Init_From_Point_Or_Spot_Light(const LightClass & light,const Vector3 & object_center);
  126. void Init_From_Directional_Light(const LightClass & light,const Vector3 & object_center);
  127. float Contribution(void);
  128. Vector3 Direction;
  129. Vector3 Ambient;
  130. Vector3 Diffuse;
  131. bool DiffuseRejected;
  132. bool m_point;
  133. Vector3 m_center;
  134. float m_innerRadius;
  135. float m_outerRadius;
  136. Vector3 m_ambient;
  137. Vector3 m_diffuse;
  138. };
  139. struct OutputLightStruct
  140. {
  141. void Init(const InputLightStruct & input,const Matrix3D & camera_tm);
  142. Vector3 Direction; // direction to the light.
  143. Vector3 Diffuse; // diffuse color * attenuation
  144. };
  145. /*
  146. ** Member variables
  147. */
  148. int LightCount;
  149. Vector3 ObjectCenter; // center of the object to be lit
  150. InputLightStruct InputLights[MAX_LIGHTS]; // Sorted list of input lights from the greatest contributor to the least
  151. Vector3 OutputAmbient; // scene ambient + lights' ambients
  152. OutputLightStruct OutputLights[MAX_LIGHTS]; // ouput lights
  153. InputLightStruct FillLight; // Used to store the calculated fill light
  154. float FillIntensity; // Used to determine how strong the fill light should be
  155. };
  156. #endif //LIGHTENVIRONMENT_H