physgridcull.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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/physgridcull.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 8/13/00 12:13p $*
  29. * *
  30. * $Revision:: 28 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "physgridcull.h"
  36. #include "wwdebug.h"
  37. #include "wwprofile.h"
  38. #include "pscene.h"
  39. #include "physcoltest.h"
  40. #include "physinttest.h"
  41. #include "ww3d.h"
  42. #include "phys.h"
  43. #include "camera.h"
  44. #include "boxrobj.h"
  45. #include "vistable.h"
  46. #include "visrendercontext.h"
  47. #include "chunkio.h"
  48. #include "iostruct.h"
  49. #include "colmathinlines.h"
  50. #define NEW_CAST_FUNCTIONS 1
  51. /*
  52. ** Constants
  53. */
  54. const int MAX_PHYSGRID_CELLS = 2048;
  55. const float MIN_PHYSGRID_CELL_DIMENSION = 60.0f;
  56. /*
  57. ** Persist save/load system
  58. */
  59. enum
  60. {
  61. PHYSGRID_CHUNK_VARIABLES = 0x00770001, // variables wrapper, contains micro-chunks
  62. PHYSGRID_CHUNK_PARENT_CLASS = 0x00770104 // wraps the parent class's save data
  63. };
  64. /*******************************************************************************************************
  65. **
  66. ** PhysGridCullClass Implementation
  67. **
  68. *******************************************************************************************************/
  69. PhysGridCullClass::PhysGridCullClass(PhysicsSceneClass * scene) :
  70. Scene(scene)
  71. {
  72. }
  73. PhysGridCullClass::~PhysGridCullClass(void)
  74. {
  75. }
  76. void PhysGridCullClass::Re_Partition(const Vector3 & min,const Vector3 & max,float objdim)
  77. {
  78. /*
  79. ** I want very coarse culling for this grid so force some of the constants.
  80. */
  81. TerminationCellCount = MAX_PHYSGRID_CELLS;
  82. MinCellSize.Set(MIN_PHYSGRID_CELL_DIMENSION,MIN_PHYSGRID_CELL_DIMENSION,MIN_PHYSGRID_CELL_DIMENSION);
  83. /*
  84. ** Tell the parent class to repartition
  85. */
  86. TypedGridCullSystemClass<PhysClass>::Re_Partition(min,max,objdim);
  87. WWASSERT(Scene != NULL);
  88. }
  89. void PhysGridCullClass::Collect_Visible_Objects(const FrustumClass & frustum,VisTableClass * pvs,RefPhysListClass & visobjlist)
  90. {
  91. Reset_Collection();
  92. Collect_Visible_Objects(frustum,pvs);
  93. PhysClass * obj;
  94. for ( obj = Get_First_Collected_Object();
  95. obj != NULL;
  96. obj = Get_Next_Collected_Object(obj))
  97. {
  98. visobjlist.Add(obj);
  99. }
  100. }
  101. void PhysGridCullClass::Collect_Visible_Objects(const FrustumClass & frustum,VisTableClass * pvs)
  102. {
  103. if (pvs == NULL) {
  104. Collect_Objects(frustum);
  105. return;
  106. }
  107. /*
  108. ** Collect all objects in the frustum that are in visible grid cells.
  109. */
  110. VolumeStruct vol;
  111. init_volume(frustum,&vol);
  112. if (!vol.Is_Empty()) {
  113. int delta_x = vol.Max[0] - vol.Min[0];
  114. int i,j,k;
  115. int address = map_indices_to_address(vol.Min[0],vol.Min[1],vol.Min[2]);
  116. for (k=vol.Min[2]; k<vol.Max[2]; k++) {
  117. for (j=vol.Min[1]; j<vol.Max[1]; j++) {
  118. for (i=vol.Min[0]; i<vol.Max[0]; i++) {
  119. GRIDCULL_NODE_TRIVIALLY_ACCEPTED;
  120. collect_visible_objects_in_leaf(frustum,*pvs,address);
  121. address++;
  122. }
  123. address -= delta_x;
  124. address += CellCount[0];
  125. }
  126. address = map_indices_to_address(vol.Min[0],vol.Min[1],k+1);
  127. }
  128. }
  129. /*
  130. ** Collect the objects in the no-grid-list
  131. */
  132. collect_objects_in_leaf(frustum,NoGridList);
  133. }
  134. inline void PhysGridCullClass::collect_visible_objects_in_leaf
  135. (
  136. const FrustumClass & frustum,
  137. VisTableClass & pvs,
  138. int address
  139. )
  140. {
  141. CullableClass * head = Cells[address];
  142. if (head != NULL) {
  143. /*
  144. ** Add all visible objects to the collection
  145. */
  146. GridListIterator it(head);
  147. for (; !it.Is_Done(); it.Next()) {
  148. PhysClass * obj = (PhysClass *)it.Peek_Obj();
  149. if ( (pvs.Get_Bit(obj->Get_Vis_Object_ID()) != 0) &&
  150. (CollisionMath::Overlap_Test(frustum,obj->Get_Cull_Box()) != CollisionMath::OUTSIDE) )
  151. {
  152. Add_To_Collection(obj);
  153. }
  154. }
  155. }
  156. }
  157. bool PhysGridCullClass::Cast_Ray(PhysRayCollisionTestClass & raytest)
  158. {
  159. #if NEW_CAST_FUNCTIONS
  160. Reset_Collection();
  161. AABoxClass bounds;
  162. bounds.Init(raytest.Ray);
  163. Collect_Objects(bounds);
  164. bool res = false;
  165. PhysClass * obj = Get_First_Collected_Object();
  166. PhysicsSceneClass * scene = PhysicsSceneClass::Get_Instance();
  167. while (obj) {
  168. if (
  169. scene->Do_Groups_Collide(obj->Get_Collision_Group(),raytest.CollisionGroup) &&
  170. !obj->Is_Ignore_Me()
  171. )
  172. {
  173. res |= obj->Cast_Ray(raytest);
  174. }
  175. obj = Get_Next_Collected_Object(obj);
  176. }
  177. return res;
  178. #else
  179. bool res = false;
  180. // hierarchically cull the objects in the grid
  181. VolumeStruct vol;
  182. init_volume(raytest.Ray,&vol);
  183. if (!vol.Is_Empty()) {
  184. res |= cast_ray_recursive(raytest,vol);
  185. }
  186. // linearly cull the objects in the NoGridList
  187. PhysicsSceneClass * scene = PhysicsSceneClass::Get_Instance();
  188. for (GridListIterator it(NoGridList); !it.Is_Done(); it.Next()) {
  189. PhysClass * obj = (PhysClass *)it.Peek_Obj();
  190. if (
  191. scene->Do_Groups_Collide(obj->Get_Collision_Group(),raytest.CollisionGroup) &&
  192. !obj->Is_Ignore_Me()
  193. )
  194. {
  195. res |= obj->Cast_Ray(raytest);
  196. }
  197. }
  198. return res;
  199. #endif
  200. }
  201. bool PhysGridCullClass::cast_ray_recursive
  202. (
  203. PhysRayCollisionTestClass & raytest,
  204. const GridCullSystemClass::VolumeStruct & vol
  205. )
  206. {
  207. AABoxClass box;
  208. compute_box(vol,&box);
  209. // does the ray enter this volume?
  210. if (raytest.Cull(box)) {
  211. return false;
  212. }
  213. // if we have recursed to a leaf, linearly check the objects in this leaf
  214. // otherwise, divide this box and recurse.
  215. bool res = false;
  216. if (vol.Is_Leaf()) {
  217. PhysClass * head = (PhysClass*)Cells[map_indices_to_address(vol.Min[0],vol.Min[1],vol.Min[2])];
  218. for (GridListIterator it(head); !it.Is_Done(); it.Next()) {
  219. PhysClass * obj = (PhysClass*)it.Peek_Obj();
  220. if (
  221. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),raytest.CollisionGroup) &&
  222. !obj->Is_Ignore_Me()
  223. )
  224. {
  225. res |= obj->Cast_Ray(raytest);
  226. }
  227. }
  228. return res;
  229. }
  230. // Not in a leaf, divide this volume and continue!
  231. VolumeStruct vol0,vol1;
  232. vol.Split(vol0,vol1);
  233. res |= cast_ray_recursive(raytest,vol0);
  234. res |= cast_ray_recursive(raytest,vol1);
  235. return res;
  236. }
  237. bool PhysGridCullClass::Cast_AABox(PhysAABoxCollisionTestClass & boxtest)
  238. {
  239. #if NEW_CAST_FUNCTIONS
  240. Reset_Collection();
  241. AABoxClass bounds;
  242. bounds.Init_Min_Max(boxtest.SweepMin,boxtest.SweepMax);
  243. Collect_Objects(bounds);
  244. bool res = false;
  245. PhysClass * obj = Get_First_Collected_Object();
  246. PhysicsSceneClass * scene = PhysicsSceneClass::Get_Instance();
  247. while (obj) {
  248. if (
  249. scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  250. !obj->Is_Ignore_Me()
  251. )
  252. {
  253. res |= obj->Cast_AABox(boxtest);
  254. }
  255. obj = Get_Next_Collected_Object(obj);
  256. }
  257. return res;
  258. #else
  259. bool res = false;
  260. // hierarchically cull the objects in the grid
  261. VolumeStruct vol;
  262. init_volume(boxtest.SweepMin,boxtest.SweepMax,&vol);
  263. if (!vol.Is_Empty()) {
  264. res |= cast_aabox_recursive(boxtest,vol);
  265. }
  266. // linearly cull the objects in the NoGridList
  267. for (GridListIterator it(NoGridList); !it.Is_Done(); it.Next()) {
  268. PhysClass * obj = (PhysClass*)it.Peek_Obj();
  269. if (
  270. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  271. !obj->Is_Ignore_Me()
  272. )
  273. {
  274. res |= obj->Cast_AABox(boxtest);
  275. }
  276. }
  277. return res;
  278. #endif
  279. }
  280. bool PhysGridCullClass::cast_aabox_recursive
  281. (
  282. PhysAABoxCollisionTestClass & boxtest,
  283. const GridCullSystemClass::VolumeStruct & vol
  284. )
  285. {
  286. AABoxClass box;
  287. compute_box(vol,&box);
  288. // does the ray enter this volume?
  289. if (boxtest.Cull(box)) {
  290. return false;
  291. }
  292. // if we have recursed to a leaf, linearly check the objects in this leaf
  293. // otherwise, divide this box and recurse.
  294. bool res = false;
  295. if (vol.Is_Leaf()) {
  296. PhysClass * head = (PhysClass*)Cells[map_indices_to_address(vol.Min[0],vol.Min[1],vol.Min[2])];
  297. for (GridListIterator it(head); !it.Is_Done(); it.Next()) {
  298. PhysClass * obj = (PhysClass*)it.Peek_Obj();
  299. if (
  300. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  301. !obj->Is_Ignore_Me()
  302. )
  303. {
  304. res |= obj->Cast_AABox(boxtest);
  305. }
  306. }
  307. return res;
  308. }
  309. // Not in a leaf, divide this volume and continue!
  310. VolumeStruct vol0,vol1;
  311. vol.Split(vol0,vol1);
  312. res |= cast_aabox_recursive(boxtest,vol0);
  313. res |= cast_aabox_recursive(boxtest,vol1);
  314. return res;
  315. }
  316. bool PhysGridCullClass::Cast_OBBox(PhysOBBoxCollisionTestClass & boxtest)
  317. {
  318. #if NEW_CAST_FUNCTIONS
  319. Reset_Collection();
  320. AABoxClass bounds;
  321. bounds.Init_Min_Max(boxtest.SweepMin,boxtest.SweepMax);
  322. Collect_Objects(bounds);
  323. bool res = false;
  324. PhysClass * obj = Get_First_Collected_Object();
  325. PhysicsSceneClass * scene = PhysicsSceneClass::Get_Instance();
  326. while (obj) {
  327. if (
  328. scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  329. !obj->Is_Ignore_Me()
  330. )
  331. {
  332. res |= obj->Cast_OBBox(boxtest);
  333. }
  334. obj = Get_Next_Collected_Object(obj);
  335. }
  336. return res;
  337. #else
  338. bool res = false;
  339. // hierarchically cull the objects in the grid
  340. VolumeStruct vol;
  341. init_volume(boxtest.SweepMin,boxtest.SweepMax,&vol);
  342. if (!vol.Is_Empty()) {
  343. res |= cast_obbox_recursive(boxtest,vol);
  344. }
  345. // linearly cull the objects in the NoGridList
  346. for (GridListIterator it(NoGridList); !it.Is_Done(); it.Next()) {
  347. PhysClass * obj = (PhysClass*)it.Peek_Obj();
  348. if (
  349. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  350. !obj->Is_Ignore_Me()
  351. )
  352. {
  353. res |= obj->Cast_OBBox(boxtest);
  354. }
  355. }
  356. return res;
  357. #endif
  358. }
  359. bool PhysGridCullClass::cast_obbox_recursive
  360. (
  361. PhysOBBoxCollisionTestClass & boxtest,
  362. const GridCullSystemClass::VolumeStruct & vol
  363. )
  364. {
  365. AABoxClass box;
  366. compute_box(vol,&box);
  367. // does the swept box enter this volume?
  368. if (boxtest.Cull(box)) {
  369. return false;
  370. }
  371. // if we have recursed to a leaf, linearly check the objects in this leaf
  372. // otherwise, divide this box and recurse.
  373. bool res = false;
  374. if (vol.Is_Leaf()) {
  375. PhysClass * head = (PhysClass*)Cells[map_indices_to_address(vol.Min[0],vol.Min[1],vol.Min[2])];
  376. for (GridListIterator it(head); !it.Is_Done(); it.Next()) {
  377. PhysClass * obj = (PhysClass*)it.Peek_Obj();
  378. if (
  379. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  380. !obj->Is_Ignore_Me()
  381. )
  382. {
  383. res |= obj->Cast_OBBox(boxtest);
  384. }
  385. }
  386. return res;
  387. }
  388. // Not in a leaf, divide this volume and continue!
  389. VolumeStruct vol0,vol1;
  390. vol.Split(vol0,vol1);
  391. res |= cast_obbox_recursive(boxtest,vol0);
  392. res |= cast_obbox_recursive(boxtest,vol1);
  393. return res;
  394. }
  395. bool PhysGridCullClass::Intersection_Test(PhysAABoxIntersectionTestClass & boxtest)
  396. {
  397. Reset_Collection();
  398. Collect_Objects(boxtest.Box);
  399. bool res = false;
  400. PhysClass * obj = Get_First_Collected_Object();
  401. while (obj) {
  402. if (
  403. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  404. !obj->Is_Ignore_Me()
  405. )
  406. {
  407. res |= obj->Intersection_Test(boxtest);
  408. }
  409. obj = Get_Next_Collected_Object(obj);
  410. }
  411. return res;
  412. }
  413. bool PhysGridCullClass::Intersection_Test(PhysOBBoxIntersectionTestClass & boxtest)
  414. {
  415. Reset_Collection();
  416. Collect_Objects(boxtest.BoundingBox);
  417. bool res = false;
  418. PhysClass * obj = Get_First_Collected_Object();
  419. while (obj) {
  420. if (
  421. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
  422. !obj->Is_Ignore_Me()
  423. )
  424. {
  425. res |= obj->Intersection_Test(boxtest);
  426. }
  427. obj = Get_Next_Collected_Object(obj);
  428. }
  429. return res;
  430. }
  431. bool PhysGridCullClass::Intersection_Test(PhysMeshIntersectionTestClass & meshtest)
  432. {
  433. Reset_Collection();
  434. Collect_Objects(meshtest.BoundingBox);
  435. bool res = false;
  436. PhysClass * obj = Get_First_Collected_Object();
  437. while (obj) {
  438. if (
  439. Scene->Do_Groups_Collide(obj->Get_Collision_Group(),meshtest.CollisionGroup) &&
  440. !obj->Is_Ignore_Me()
  441. )
  442. {
  443. res |= obj->Intersection_Test(meshtest);
  444. }
  445. obj = Get_Next_Collected_Object(obj);
  446. }
  447. return res;
  448. }
  449. void PhysGridCullClass::Load_Static_Data(ChunkLoadClass & cload)
  450. {
  451. while(cload.Open_Chunk())
  452. {
  453. switch(cload.Cur_Chunk_ID()) {
  454. /* (gth) no variables for now...
  455. case PHYSGRID_CHUNK_VARIABLES:
  456. Load_Static_Variables(cload);
  457. break;
  458. */
  459. case PHYSGRID_CHUNK_PARENT_CLASS:
  460. TypedGridCullSystemClass<PhysClass>::Load(cload);
  461. break;
  462. }
  463. cload.Close_Chunk();
  464. }
  465. }
  466. void PhysGridCullClass::Load_Static_Variables(ChunkLoadClass & cload)
  467. {
  468. /* (gth) no variables for now...
  469. uint32 version;
  470. while (cload.Open_Micro_Chunk()) {
  471. switch(cload.Cur_Micro_Chunk_ID()) {
  472. }
  473. cload.Close_Micro_Chunk();
  474. }
  475. */
  476. }
  477. void PhysGridCullClass::Save_Static_Data(ChunkSaveClass & csave)
  478. {
  479. /* (gth) no variables for now...
  480. csave.Begin_Chunk(PHYSGRID_CHUNK_VARIABLES);
  481. Save_Static_Variables(csave);
  482. csave.End_Chunk();
  483. */
  484. csave.Begin_Chunk(PHYSGRID_CHUNK_PARENT_CLASS);
  485. TypedGridCullSystemClass<PhysClass>::Save(csave);
  486. csave.End_Chunk();
  487. }
  488. void PhysGridCullClass::Save_Static_Variables(ChunkSaveClass & csave)
  489. {
  490. /* (gth) no variables for now...
  491. uint32 version = PHYSGRID_CURRENT_VERSION;
  492. WRITE_MICRO_CHUNK(csave,PHYSGRID_VARIABLE_VERSION,version);
  493. WRITE_MICRO_CHUNK(csave,PHYSGRID_VARIABLE_DUMMYVISID,DummyVisId);
  494. WRITE_MICRO_CHUNK(csave,PHYSGRID_VARIABLE_BASEVISID,BaseVisId);
  495. */
  496. }