pscene_lighting.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. ** Command & Conquer Renegade(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/wwphys/pscene_lighting.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 2/28/02 1:41p $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * PhysicsSceneClass::Is_Sun_Light_Enabled -- Returns true if the sun-light is enabled *
  37. * PhysicsSceneClass::Enable_Sun_Light -- Enable/Disable the sun-light *
  38. * PhysicsSceneClass::Get_Sun_Light -- Returns pointer to the sun-light object *
  39. * PhysicsSceneClass::Set_Sun_Light_Orientation -- Set the orientation of the sun-ligth *
  40. * PhysicsSceneClass::Get_Sun_Light_Orientation -- returns the sun-light orientation *
  41. * PhysicsSceneClass::Get_Sun_Light_Vector -- returns the sun-light vector *
  42. * PhysicsSceneClass::Reset_Sun_Light -- Resets the sun-light to default settings *
  43. * PhysicsSceneClass::Set_Lighting_LOD_Cutoff -- Sets the LOD cutoff for lighting *
  44. * PhysicsSceneClass::Get_Lighting_LOD_Cutoff -- returns the LOD cutoff for lighting *
  45. * PhysicsSceneClass::Compute_Static_Lighting -- Compute the static lighting approximation *
  46. * PhysicsSceneClass::Invalidate_Lighting_Caches -- invalidate lighting caches in the given *
  47. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  48. #include "pscene.h"
  49. #include "rendobj.h"
  50. #include "phys.h"
  51. #include "staticphys.h"
  52. #include "dx8renderer.h"
  53. #include "simplevec.h"
  54. #include "vp.h"
  55. #include "lightphys.h"
  56. #include "light.h"
  57. #include "lightcull.h"
  58. /***********************************************************************************************
  59. * PhysicsSceneClass::Set_Lighting_LOD_Cutoff -- Sets the LOD cutoff for lighting *
  60. * *
  61. * All lights below this intensity become purely ambient *
  62. * *
  63. * INPUT: *
  64. * *
  65. * OUTPUT: *
  66. * *
  67. * WARNINGS: *
  68. * *
  69. * HISTORY: *
  70. * 8/10/2000 gth : Created. *
  71. *=============================================================================================*/
  72. void PhysicsSceneClass::Set_Lighting_LOD_Cutoff(float intensity)
  73. {
  74. LightEnvironmentClass::Set_Lighting_LOD_Cutoff(intensity);
  75. }
  76. /***********************************************************************************************
  77. * PhysicsSceneClass::Get_Lighting_LOD_Cutoff -- returns the LOD cutoff for lighting *
  78. * *
  79. * All lights below the returned intensity are being converted into pure ambient *
  80. * *
  81. * INPUT: *
  82. * *
  83. * OUTPUT: *
  84. * *
  85. * WARNINGS: *
  86. * *
  87. * HISTORY: *
  88. * 8/10/2000 gth : Created. *
  89. *=============================================================================================*/
  90. float PhysicsSceneClass::Get_Lighting_LOD_Cutoff(void)
  91. {
  92. return LightEnvironmentClass::Get_Lighting_LOD_Cutoff();
  93. }
  94. /***********************************************************************************************
  95. * PhysicsSceneClass::Is_Sun_Light_Enabled -- Returns true if the sun-light is enabled *
  96. * *
  97. * INPUT: *
  98. * *
  99. * OUTPUT: *
  100. * *
  101. * WARNINGS: *
  102. * *
  103. * HISTORY: *
  104. * 7/7/2000 gth : Created. *
  105. *=============================================================================================*/
  106. bool PhysicsSceneClass::Is_Sun_Light_Enabled(void)
  107. {
  108. return UseSun;
  109. }
  110. /***********************************************************************************************
  111. * PhysicsSceneClass::Enable_Sun_Light -- Enable/Disable the sun-light *
  112. * *
  113. * INPUT: *
  114. * *
  115. * OUTPUT: *
  116. * *
  117. * WARNINGS: *
  118. * *
  119. * HISTORY: *
  120. * 7/7/2000 gth : Created. *
  121. *=============================================================================================*/
  122. void PhysicsSceneClass::Enable_Sun_Light(bool onoff)
  123. {
  124. UseSun = onoff;
  125. }
  126. /***********************************************************************************************
  127. * PhysicsSceneClass::Get_Sun_Light -- Returns pointer to the sun-light object *
  128. * *
  129. * INPUT: *
  130. * *
  131. * OUTPUT: *
  132. * *
  133. * WARNINGS: *
  134. * *
  135. * HISTORY: *
  136. * 7/7/2000 gth : Created. *
  137. *=============================================================================================*/
  138. LightClass * PhysicsSceneClass::Get_Sun_Light(void)
  139. {
  140. WWASSERT(SunLight);
  141. SunLight->Add_Ref();
  142. return SunLight;
  143. }
  144. /***********************************************************************************************
  145. * PhysicsSceneClass::Set_Sun_Light_Orientation -- Set the orientation of the sun-ligth *
  146. * *
  147. * INPUT: *
  148. * *
  149. * OUTPUT: *
  150. * *
  151. * WARNINGS: *
  152. * *
  153. * HISTORY: *
  154. * 7/7/2000 gth : Created. *
  155. *=============================================================================================*/
  156. void PhysicsSceneClass::Set_Sun_Light_Orientation(float yaw,float pitch)
  157. {
  158. SunYaw = yaw;
  159. SunPitch = pitch;
  160. Matrix3D tm(1);
  161. tm.Rotate_Z(yaw);
  162. tm.Rotate_Y(DEG_TO_RADF(90.0f) - pitch);
  163. tm.Rotate_X(DEG_TO_RADF(180.0f));
  164. SunLight->Set_Transform(tm);
  165. }
  166. /***********************************************************************************************
  167. * PhysicsSceneClass::Get_Sun_Light_Orientation -- returns the sun-light orientation *
  168. * *
  169. * INPUT: *
  170. * *
  171. * OUTPUT: *
  172. * *
  173. * WARNINGS: *
  174. * *
  175. * HISTORY: *
  176. * 7/7/2000 gth : Created. *
  177. *=============================================================================================*/
  178. void PhysicsSceneClass::Get_Sun_Light_Orientation(float * set_yaw,float * set_pitch)
  179. {
  180. *set_yaw = SunYaw;
  181. *set_pitch = SunPitch;
  182. }
  183. /***********************************************************************************************
  184. * PhysicsSceneClass::Get_Sun_Light_Vector -- returns the sun-light vector *
  185. * *
  186. * INPUT: *
  187. * *
  188. * OUTPUT: *
  189. * *
  190. * WARNINGS: *
  191. * *
  192. * HISTORY: *
  193. * 7/7/2000 gth : Created. *
  194. *=============================================================================================*/
  195. void PhysicsSceneClass::Get_Sun_Light_Vector(Vector3 * set_vector)
  196. {
  197. WWASSERT(set_vector != NULL);
  198. const Matrix3D & tm = SunLight->Get_Transform();
  199. *set_vector = tm * Vector3(0,0,1);
  200. }
  201. /***********************************************************************************************
  202. * PhysicsSceneClass::Reset_Sun_Light -- Resets the sun-light to default settings *
  203. * *
  204. * INPUT: *
  205. * *
  206. * OUTPUT: *
  207. * *
  208. * WARNINGS: *
  209. * *
  210. * HISTORY: *
  211. * 7/7/2000 gth : Created. *
  212. *=============================================================================================*/
  213. void PhysicsSceneClass::Reset_Sun_Light(void)
  214. {
  215. UseSun = false;
  216. SunLight->Set_Transform(Matrix3D(1));
  217. SunLight->Set_Ambient(Vector3(0,0,0));
  218. SunLight->Set_Diffuse(Vector3(1,1,1));
  219. SunLight->Set_Specular(Vector3(0,0,0));
  220. SunLight->Set_Flag(LightClass::NEAR_ATTENUATION,false);
  221. SunLight->Set_Flag(LightClass::FAR_ATTENUATION,false);
  222. Set_Sun_Light_Orientation(DEG_TO_RADF(0.0f),DEG_TO_RADF(70.0f));
  223. }
  224. /***********************************************************************************************
  225. * PhysicsSceneClass::Compute_Static_Lighting -- Compute the static lighting approximation *
  226. * *
  227. * INPUT: *
  228. * *
  229. * OUTPUT: *
  230. * *
  231. * WARNINGS: *
  232. * *
  233. * HISTORY: *
  234. * 9/25/2000 gth : Created. *
  235. *=============================================================================================*/
  236. void PhysicsSceneClass::Compute_Static_Lighting
  237. (
  238. LightEnvironmentClass * light_env,
  239. const Vector3 & obj_center,
  240. bool use_sun,
  241. int vis_object_id
  242. )
  243. {
  244. WWASSERT(light_env != NULL);
  245. light_env->Reset(obj_center,Get_Ambient_Light());
  246. /*
  247. ** Add in the sun
  248. */
  249. if (use_sun) {
  250. light_env->Add_Light(*SunLight);
  251. }
  252. /*
  253. ** Add in the static lights affecting this object
  254. */
  255. StaticLightingSystem->Reset_Collection();
  256. StaticLightingSystem->Collect_Objects(obj_center);
  257. LightPhysClass * light = StaticLightingSystem->Get_First_Collected_Object();
  258. while (light != NULL) {
  259. if ((light->Is_Disabled() == false) && (light->Is_Vis_Object_Visible(vis_object_id))) {
  260. LightClass * light_obj = (LightClass *)light->Peek_Model();
  261. light_env->Add_Light(*light_obj);
  262. }
  263. light = StaticLightingSystem->Get_Next_Collected_Object(light);
  264. }
  265. }
  266. /***********************************************************************************************
  267. * PhysicsSceneClass::Invalidate_Lighting_Caches -- invalidate lighting caches in the given bo *
  268. * *
  269. * This function finds all physics objects that contain cached lighting information within *
  270. * the given bounds and invalidates their caches. This is used when a light source in the *
  271. * level changes state (e.g. when the buildings lose power, etc) *
  272. * *
  273. * INPUT: *
  274. * box - all objects overlapping the specified box will have thier lighting cache reset *
  275. * *
  276. * OUTPUT: *
  277. * *
  278. * WARNINGS: *
  279. * *
  280. * HISTORY: *
  281. * 9/26/2000 gth : Created. *
  282. *=============================================================================================*/
  283. void PhysicsSceneClass::Invalidate_Lighting_Caches(const AABoxClass & box)
  284. {
  285. NonRefPhysListClass list;
  286. Collect_Objects(box,true,true,&list);
  287. NonRefPhysListIterator it(&list);
  288. for (it.First(); !it.Is_Done(); it.Next()) {
  289. PhysClass * obj = it.Peek_Obj();
  290. if (!obj->Is_Pre_Lit()) {
  291. obj->Invalidate_Static_Lighting_Cache();
  292. }
  293. }
  294. }