lightenvironment.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. ** Command & Conquer Generals(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:: Naty_h $*
  29. * *
  30. * $Modtime:: 4/01/01 12:01a $*
  31. * *
  32. * $Revision:: 4 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef LIGHTENVIRONMENT_H
  41. #define LIGHTENVIRONMENT_H
  42. #include "always.h"
  43. #include "vector3.h"
  44. class Matrix3D;
  45. class LightClass;
  46. /**
  47. ** LightEnvironmentClass
  48. ** This class represents an approximation of the local lighting for a given point. The idea
  49. ** is to collect all of the point light sources affecting an object at any given time and
  50. ** create temporary directional light sources representing them. Any distance or directional
  51. ** attenuation will be precalculated into the overall intensity of the light and a vector from
  52. ** the light source to the center of the bounding sphere of the model will be used as the
  53. ** directional component.
  54. ** In addition, the engine will provide the ambient component which will be determined by
  55. ** a combination of the ambient setting for the level and sampling the light maps in the area.
  56. **
  57. ** Notes:
  58. ** - will need fast collection of the lights affecting any one given object
  59. ** - intensity of these lights should take into account attenuation of the original light
  60. ** - intensity should also take into account spot attenuation (no-per vertex atten...)
  61. ** - we need the direction of the lights in eye-space
  62. ** - the ambient light from all lights should be added into the ambient light (not just scene)
  63. */
  64. class LightEnvironmentClass
  65. {
  66. public:
  67. LightEnvironmentClass(void);
  68. ~LightEnvironmentClass(void);
  69. /*
  70. ** Usage (starting from scratch each frame):
  71. ** - Reset the object
  72. ** - Set the scene ambient light (will be derived from lightmap sampling probably)
  73. ** - Add in all overlapping lights, this object will keep the most important ones
  74. ** - When ready to render, call Pre_Render_Update and push into the gerd.
  75. **
  76. ** Usage (caching the lights, only done if the object and the lights are not moving)
  77. ** - Reset and collect the lights once and keep this object around
  78. ** - When ready to render, call Pre_Render_Update and push into the gerd.
  79. */
  80. void Reset(const Vector3 & object_center,const Vector3 & scene_ambient);
  81. void Add_Light(const LightClass & light);
  82. void Pre_Render_Update(const Matrix3D & camera_tm);
  83. /*
  84. ** Accessors
  85. */
  86. const Vector3 & Get_Equivalent_Ambient(void) const { return OutputAmbient; }
  87. void Set_Output_Ambient(Vector3& oa) { OutputAmbient = oa; }
  88. int Get_Light_Count(void) const { return LightCount; }
  89. const Vector3 & Get_Light_Direction(int i) const { return InputLights[i].Direction; }
  90. const Vector3 & Get_Light_Diffuse(int i) const { return InputLights[i].Diffuse; }
  91. bool isPointLight(int i) const {return InputLights[i].m_point;}
  92. float getPointIrad(int i) const {return InputLights[i].m_innerRadius;}
  93. float getPointOrad(int i) const {return InputLights[i].m_outerRadius;}
  94. const Vector3 & getPointDiffuse(int i) const { return InputLights[i].m_diffuse; }
  95. const Vector3 & getPointAmbient(int i) const { return InputLights[i].m_ambient; }
  96. const Vector3 & getPointCenter(int i) const { return InputLights[i].m_center; }
  97. /*
  98. ** Lighting LOD. This is a static setting that is used to convert weak diffuse lights
  99. ** into pure ambient lights.
  100. */
  101. static void Set_Lighting_LOD_Cutoff(float inten);
  102. static float Get_Lighting_LOD_Cutoff(void);
  103. enum { MAX_LIGHTS = 4 }; //Made this public, so other code can tell how many lights are allowed. - MW
  104. protected:
  105. struct InputLightStruct
  106. {
  107. void Init(const LightClass & light,const Vector3 & object_center);
  108. void Init_From_Point_Or_Spot_Light(const LightClass & light,const Vector3 & object_center);
  109. void Init_From_Directional_Light(const LightClass & light,const Vector3 & object_center);
  110. float Contribution(void);
  111. Vector3 Direction;
  112. Vector3 Ambient;
  113. Vector3 Diffuse;
  114. bool DiffuseRejected;
  115. bool m_point;
  116. Vector3 m_center;
  117. float m_innerRadius;
  118. float m_outerRadius;
  119. Vector3 m_ambient;
  120. Vector3 m_diffuse;
  121. };
  122. struct OutputLightStruct
  123. {
  124. void Init(const InputLightStruct & input,const Matrix3D & camera_tm);
  125. Vector3 Direction; // direction to the light.
  126. Vector3 Diffuse; // diffuse color * attenuation
  127. };
  128. /*
  129. ** Member variables
  130. */
  131. int LightCount;
  132. Vector3 ObjectCenter; // center of the object to be lit
  133. InputLightStruct InputLights[MAX_LIGHTS]; // input lights
  134. Vector3 OutputAmbient; // scene ambient + lights' ambients
  135. OutputLightStruct OutputLights[MAX_LIGHTS]; // ouput lights
  136. };
  137. #endif //LIGHTENVIRONMENT_H