lightcull.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/lightcull.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 4/02/01 12:23p $*
  29. * *
  30. * $Revision:: 18 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "lightcull.h"
  36. #include "lightphys.h"
  37. #include "ww3d.h"
  38. #include "rinfo.h"
  39. #include "chunkio.h"
  40. #include "wwphysids.h"
  41. #include "camera.h"
  42. /*
  43. ** Current version of the lightcull file format
  44. */
  45. const uint32 STATICLIGHTCULL_CURRENT_VERSION = 0x00010000;
  46. /*
  47. ** Chunk Id's used by the phys-aabtree code to save itself into a file
  48. */
  49. enum
  50. {
  51. STATICLIGHTCULL_CHUNK_VERSION = 0x00050001, // version wrapper, contains 32bit version #
  52. STATICLIGHTCULL_CHUNK_PARENT_CLASS_DATA = 0x00050104, // wraps the parent class's save data
  53. };
  54. /*
  55. ** StaticLightCullClass is a derived AABTree which assumes it contains LightPhysClasses
  56. ** these two functions encapsulate some typecasting which happens in a lot
  57. ** of places...
  58. */
  59. inline LightPhysClass * get_first_object(AABTreeNodeClass * node)
  60. {
  61. return (LightPhysClass *)(node->Object);
  62. }
  63. inline LightPhysClass * get_next_object(LightPhysClass * tile)
  64. {
  65. return (LightPhysClass *)(((AABTreeLinkClass *)tile->Get_Cull_Link())->NextObject);
  66. }
  67. /*
  68. ** Implementation of StaticLightCullClass
  69. */
  70. StaticLightCullClass::StaticLightCullClass(void)
  71. {
  72. }
  73. StaticLightCullClass::~StaticLightCullClass(void)
  74. {
  75. }
  76. void StaticLightCullClass::Add_Object(LightPhysClass * obj)
  77. {
  78. WWASSERT(PhysicsSceneClass::Get_Instance() != NULL);
  79. TypedAABTreeCullSystemClass<LightPhysClass>::Add_Object(obj);
  80. // (gth) not resetting vis when adding a new static light
  81. // PhysicsSceneClass::Get_Instance()->Reset_Vis();
  82. }
  83. void StaticLightCullClass::Remove_Object(LightPhysClass * obj)
  84. {
  85. WWASSERT(PhysicsSceneClass::Get_Instance() != NULL);
  86. TypedAABTreeCullSystemClass<LightPhysClass>::Remove_Object(obj);
  87. // (gth) not resetting vis when removing a static light
  88. // PhysicsSceneClass::Get_Instance()->Reset_Vis();
  89. }
  90. void StaticLightCullClass::Update_Culling(CullableClass * obj)
  91. {
  92. WWASSERT(PhysicsSceneClass::Get_Instance() != NULL);
  93. TypedAABTreeCullSystemClass<LightPhysClass>::Update_Culling(obj);
  94. // (gth) not resetting vis when moving a static light
  95. // PhysicsSceneClass::Get_Instance()->Reset_Vis();
  96. }
  97. void StaticLightCullClass::Save_Static_Data(ChunkSaveClass & csave)
  98. {
  99. csave.Begin_Chunk(STATICLIGHTCULL_CHUNK_PARENT_CLASS_DATA);
  100. TypedAABTreeCullSystemClass<LightPhysClass>::Save(csave);
  101. csave.End_Chunk();
  102. }
  103. void StaticLightCullClass::Load_Static_Data(ChunkLoadClass & cload)
  104. {
  105. while(cload.Open_Chunk())
  106. {
  107. switch(cload.Cur_Chunk_ID())
  108. {
  109. case STATICLIGHTCULL_CHUNK_PARENT_CLASS_DATA:
  110. {
  111. TypedAABTreeCullSystemClass<LightPhysClass>::Load(cload);
  112. }
  113. break;
  114. default:
  115. WWDEBUG_SAY(("Unhandled chunk type: %d in LightCullClass::Load\r\n",cload.Cur_Chunk_ID()));
  116. break;
  117. }
  118. cload.Close_Chunk();
  119. }
  120. }
  121. void StaticLightCullClass::Assign_Vis_IDs(void)
  122. {
  123. /*
  124. ** Allocate sector ID's for each light.
  125. */
  126. int nodecount = Partition_Node_Count();
  127. for (int i=0; i<nodecount; i++) {
  128. AABTreeNodeClass * node = IndexedNodes[i];
  129. LightPhysClass * obj = get_first_object(node);
  130. while(obj) {
  131. obj->Set_Vis_Sector_ID(PhysicsSceneClass::Get_Instance()->Allocate_Vis_Sector_ID());
  132. obj = get_next_object(obj);
  133. }
  134. }
  135. }
  136. void StaticLightCullClass::Merge_Vis_Sector_IDs(uint32 id0,uint32 id1)
  137. {
  138. /*
  139. ** Each lightphys object may have a vis sector id.
  140. ** Whenever we encounter one of these with id1, set it to id0.
  141. ** Whenever we encounter one with id > id1, subtract one from it.
  142. */
  143. for (int i=0; i<NodeCount; i++) {
  144. AABTreeNodeClass * node = IndexedNodes[i];
  145. LightPhysClass * obj = get_first_object(node);
  146. while (obj) {
  147. uint32 sector_id = obj->Get_Vis_Sector_ID();
  148. if (sector_id != 0xFFFFFFFF) {
  149. if (sector_id == id1) {
  150. sector_id = id0;
  151. } else if (sector_id > id1) {
  152. sector_id--;
  153. }
  154. obj->Set_Vis_Sector_ID(sector_id);
  155. }
  156. obj = get_next_object(obj);
  157. }
  158. }
  159. }