pscene_vis.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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_vis.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 10/23/01 8:35p $*
  31. * *
  32. * $Revision:: 47 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * PhysicsSceneClass::Release_Vis_Resources -- Releases resources used by the VIS code *
  37. * PhysicsSceneClass::Reset_Vis -- Sets the internal vis reset flag. *
  38. * PhysicsSceneClass::Validate_Vis -- Clears the internal vis reset flag *
  39. * PhysicsSceneClass::Internal_Vis_Reset -- Resets the vis system if needed *
  40. * PhysicsSceneClass::Get_Vis_Camera -- returns a pointer (ref-counted) to the vis camera *
  41. * PhysicsSceneClass::Get_Vis_Gerd -- returns a pointer to the VIS Gerd *
  42. * PhysicsSceneClass::Show_Vis_Window -- Makes the vis render window visible to the user *
  43. * PhysicsSceneClass::Is_Vis_Window_Visible -- Returns status of the vis render window *
  44. * PhysicsSceneClass::Get_Static_Light_Count -- returns the number of static lights *
  45. * PhysicsSceneClass::Generate_Vis_For_Light -- generate a PVS for the specified light *
  46. * PhysicsSceneClass::Update_Vis -- Performs a vis sample from the given coord system *
  47. * PhysicsSceneClass::Vis_Render_And_Scan -- Renders the scene and scans for visible objects *
  48. * PhysicsSceneClass::Vis_Debug_Render -- Renders the same way VIS does *
  49. * PhysicsSceneClass::Generate_Vis_Statistics_Report -- Stats about the visibility in the le *
  50. * PhysicsSceneClass::Optimize_Visibility_Data -- combines and removes redundant vis data *
  51. * PhysicsSceneClass::Merge_Vis_Sector_IDs -- Merges two sector ID's *
  52. * PhysicsSceneClass::Merge_Vis_Object_IDs -- combines two vis object ID's *
  53. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  54. #include "pscene.h"
  55. #include "staticaabtreecull.h"
  56. #include "dynamicaabtreecull.h"
  57. #include "staticphys.h"
  58. #include "lightcull.h"
  59. #include "camera.h"
  60. #include "vertmaterial.h"
  61. #include "shader.h"
  62. #include "visrendercontext.h"
  63. #include "visoptimizationcontext.h"
  64. #include "visoptprogress.h"
  65. #include "light.h"
  66. #include "visrasterizer.h"
  67. /************************************************************************************
  68. **
  69. ** PhysicsSceneClass Visibility system
  70. **
  71. ************************************************************************************/
  72. void PhysicsSceneClass::Enable_Vis(bool onoff)
  73. {
  74. VisEnabled = onoff;
  75. }
  76. void PhysicsSceneClass::Invert_Vis(bool onoff)
  77. {
  78. VisInverted = onoff;
  79. }
  80. void PhysicsSceneClass::Set_Vis_Quick_And_Dirty(bool onoff)
  81. {
  82. VisQuickAndDirty = onoff;
  83. }
  84. void PhysicsSceneClass::Enable_Vis_Sector_Display(bool onoff)
  85. {
  86. VisSectorDisplayEnabled = onoff;
  87. }
  88. void PhysicsSceneClass::Enable_Vis_Sector_History_Display(bool onoff)
  89. {
  90. VisSectorHistoryEnabled = onoff;
  91. }
  92. void PhysicsSceneClass::Enable_Vis_Sector_Fallback(bool onoff)
  93. {
  94. VisSectorFallbackEnabled = onoff;
  95. }
  96. void PhysicsSceneClass::Enable_Backface_Occluder_Debug(bool onoff)
  97. {
  98. BackfaceDebugEnabled = onoff;
  99. }
  100. void PhysicsSceneClass::Set_Vis_Grid_Display_Mode(int mode)
  101. {
  102. VisGridDisplayMode = mode;
  103. }
  104. bool PhysicsSceneClass::Is_Vis_Enabled(void)
  105. {
  106. return VisEnabled;
  107. }
  108. bool PhysicsSceneClass::Is_Vis_Inverted(void)
  109. {
  110. return VisInverted;
  111. }
  112. bool PhysicsSceneClass::Is_Vis_Quick_And_Dirty(void)
  113. {
  114. return VisQuickAndDirty;
  115. }
  116. bool PhysicsSceneClass::Is_Vis_Sector_Display_Enabled(void)
  117. {
  118. return VisSectorDisplayEnabled;
  119. }
  120. bool PhysicsSceneClass::Is_Vis_Sector_History_Display_Enabled(void)
  121. {
  122. return VisSectorHistoryEnabled;
  123. }
  124. bool PhysicsSceneClass::Is_Vis_Sector_Fallback_Enabled(void)
  125. {
  126. return VisSectorFallbackEnabled;
  127. }
  128. bool PhysicsSceneClass::Is_Backface_Occluder_Debug_Enabled(void)
  129. {
  130. return BackfaceDebugEnabled;
  131. }
  132. int PhysicsSceneClass::Get_Vis_Grid_Display_Mode(void)
  133. {
  134. return VisGridDisplayMode;
  135. }
  136. void PhysicsSceneClass::Vis_Grid_Debug_Reset_Node(void)
  137. {
  138. DynamicObjVisSystem->Debug_Reset_Node();
  139. }
  140. bool PhysicsSceneClass::Vis_Grid_Debug_Enter_Parent(void)
  141. {
  142. return DynamicObjVisSystem->Debug_Enter_Parent();
  143. }
  144. bool PhysicsSceneClass::Vis_Grid_Debug_Enter_Sibling(void)
  145. {
  146. return DynamicObjVisSystem->Debug_Enter_Sibling();
  147. }
  148. bool PhysicsSceneClass::Vis_Grid_Debug_Enter_Front_Child(void)
  149. {
  150. return DynamicObjVisSystem->Debug_Enter_Front_Child();
  151. }
  152. bool PhysicsSceneClass::Vis_Grid_Debug_Enter_Back_Child(void)
  153. {
  154. return DynamicObjVisSystem->Debug_Enter_Back_Child();
  155. }
  156. bool PhysicsSceneClass::Is_Vis_Sector_Missing(void)
  157. {
  158. return VisSectorMissing;
  159. }
  160. int PhysicsSceneClass::Allocate_Vis_Object_ID(int count /* = 1*/)
  161. {
  162. return VisTableManager.Allocate_Vis_Object_ID(count);
  163. }
  164. int PhysicsSceneClass::Allocate_Vis_Sector_ID(int count /* = 1*/)
  165. {
  166. return VisTableManager.Allocate_Vis_Sector_ID(count);
  167. }
  168. int PhysicsSceneClass::Get_Vis_Table_Size(void)
  169. {
  170. Internal_Vis_Reset();
  171. return VisTableManager.Get_Vis_Table_Size();
  172. }
  173. int PhysicsSceneClass::Get_Vis_Table_Count(void)
  174. {
  175. Internal_Vis_Reset();
  176. return VisTableManager.Get_Vis_Table_Count();
  177. }
  178. void PhysicsSceneClass::Compute_Vis_Sample_Point(const CameraClass & camera,Vector3 * set_point)
  179. {
  180. WWASSERT(set_point != NULL);
  181. if (!VisSamplePointLocked) {
  182. float nearz,farz;
  183. camera.Get_Clip_Planes(nearz,farz);
  184. Matrix3D::Transform_Vector(camera.Get_Transform(),Vector3(0,0,-nearz-0.05f),set_point);
  185. } else {
  186. *set_point = LockedVisSamplePoint;
  187. }
  188. }
  189. VisTableClass * PhysicsSceneClass::Get_Vis_Table(int vis_sector_id)
  190. {
  191. return VisTableManager.Get_Vis_Table(vis_sector_id);
  192. }
  193. VisTableClass * PhysicsSceneClass::Get_Vis_Table(const Vector3 & point)
  194. {
  195. int vis_id = StaticCullingSystem->Get_Vis_Sector_ID(point);
  196. return VisTableManager.Get_Vis_Table(vis_id);
  197. }
  198. VisTableClass * PhysicsSceneClass::Get_Vis_Table(const CameraClass & camera)
  199. {
  200. Vector3 sample_point;
  201. Compute_Vis_Sample_Point(camera,&sample_point);
  202. return Get_Vis_Table(sample_point);
  203. }
  204. VisTableClass * PhysicsSceneClass::Get_Vis_Table_For_Rendering(const CameraClass & camera)
  205. {
  206. // Decompress the visibility table for the current camera view
  207. // Note that if the vis system needs to be reset, we don't want to use any obsolete data
  208. // Also, if a sample point hasn't been given, we will skip visibility.
  209. // Also, if we don't find a vis sector, we'll try to use the last valid one that we had.
  210. int vis_id = -1;
  211. VisTableClass * pvs = NULL;
  212. Vector3 vis_sample_point;
  213. Compute_Vis_Sample_Point(camera,&vis_sample_point);
  214. if ((!VisResetNeeded) && VisEnabled) {
  215. vis_id = StaticCullingSystem->Get_Vis_Sector_ID(vis_sample_point);
  216. if (vis_id == -1) {
  217. VisSectorMissing = true;
  218. if (VisSectorFallbackEnabled) {
  219. vis_id = LastValidVisId;
  220. }
  221. } else {
  222. if ((LastValidVisId != vis_id) && VisSectorDisplayEnabled) {
  223. StaticPhysClass * tile = StaticCullingSystem->Find_Vis_Tile(vis_sample_point);
  224. WWDEBUG_SAY (("Vis Sector: %s\n", tile->Peek_Model ()->Get_Name ()));
  225. }
  226. LastValidVisId = vis_id;
  227. VisSectorMissing = false;
  228. }
  229. if (vis_id != -1) {
  230. pvs = VisTableManager.Get_Vis_Table(LastValidVisId);
  231. }
  232. if ((VisInverted) && (pvs != NULL)) {
  233. VisTableClass * new_pvs = NEW_REF(VisTableClass,(*pvs));
  234. new_pvs->Invert();
  235. REF_PTR_RELEASE(pvs);
  236. pvs = new_pvs;
  237. }
  238. }
  239. return pvs;
  240. }
  241. uint32 PhysicsSceneClass::Get_Dynamic_Object_Vis_ID
  242. (
  243. const AABoxClass & obj_bounds,
  244. int * node_id
  245. )
  246. {
  247. return DynamicObjVisSystem->Get_Dynamic_Object_Vis_ID(obj_bounds,node_id);
  248. }
  249. void PhysicsSceneClass::Debug_Display_Dynamic_Vis_Node(int node_id)
  250. {
  251. if (VisGridDisplayMode == VIS_GRID_DISPLAY_OCCUPIED) {
  252. AABoxClass bounds;
  253. DynamicObjVisSystem->Get_Node_Bounds(node_id,&bounds);
  254. bounds.Extent -= Vector3(5,5,5);
  255. DEBUG_RENDER_AABOX(bounds,Vector3(0,1,1),0.25f);
  256. }
  257. }
  258. void PhysicsSceneClass::Lock_Vis_Sample_Point(bool onoff)
  259. {
  260. VisSamplePointLocked = onoff;
  261. LockedVisSamplePoint = LastCameraPosition;
  262. }
  263. bool PhysicsSceneClass::Is_Vis_Sample_Point_Locked(void)
  264. {
  265. return VisSamplePointLocked;
  266. }
  267. /***********************************************************************************************
  268. * PhysicsSceneClass::Release_Vis_Resources -- Releases resources used by the VIS code *
  269. * *
  270. * INPUT: *
  271. * *
  272. * OUTPUT: *
  273. * *
  274. * WARNINGS: *
  275. * *
  276. * HISTORY: *
  277. * 7/5/2000 gth : Created. *
  278. *=============================================================================================*/
  279. void PhysicsSceneClass::Release_Vis_Resources(void)
  280. {
  281. REF_PTR_RELEASE(VisCamera);
  282. }
  283. /***********************************************************************************************
  284. * PhysicsSceneClass::Reset_Vis -- Sets the internal vis reset flag. *
  285. * *
  286. * INPUT: *
  287. * *
  288. * OUTPUT: *
  289. * *
  290. * WARNINGS: *
  291. * *
  292. * HISTORY: *
  293. * 7/5/2000 gth : Created. *
  294. *=============================================================================================*/
  295. void PhysicsSceneClass::Reset_Vis(bool doitnow)
  296. {
  297. /*
  298. ** defer the actual reset until later.
  299. ** the Update_Vis call and the Save_Map methods will check
  300. ** this bit and do the real reset
  301. */
  302. VisResetNeeded = true;
  303. if (doitnow) {
  304. Internal_Vis_Reset();
  305. }
  306. }
  307. /***********************************************************************************************
  308. * PhysicsSceneClass::Validate_Vis -- Clears the internal vis reset flag *
  309. * *
  310. * This should not normally be called. It is needed when the level editor loads a level. *
  311. * *
  312. * INPUT: *
  313. * *
  314. * OUTPUT: *
  315. * *
  316. * WARNINGS: *
  317. * *
  318. * HISTORY: *
  319. * 7/5/2000 gth : Created. *
  320. *=============================================================================================*/
  321. void PhysicsSceneClass::Validate_Vis(void)
  322. {
  323. /*
  324. ** Clear the reset flag, this is needed after a level is loaded.
  325. ** Normally as static objects are added to the system, the reset
  326. ** flag will be set, but in the case of loading, we are also
  327. ** loading the visibility data...
  328. */
  329. VisResetNeeded = false;
  330. }
  331. /***********************************************************************************************
  332. * PhysicsSceneClass::Internal_Vis_Reset -- Resets the vis system if needed *
  333. * *
  334. * INPUT: *
  335. * *
  336. * OUTPUT: *
  337. * *
  338. * WARNINGS: *
  339. * *
  340. * HISTORY: *
  341. * 7/5/2000 gth : Created. *
  342. *=============================================================================================*/
  343. void PhysicsSceneClass::Internal_Vis_Reset(void)
  344. {
  345. if (VisResetNeeded) {
  346. VisResetNeeded = false;
  347. WWDEBUG_SAY(("Resetting the visibility system.\r\n"));
  348. /*
  349. ** Throw away all visibility data.
  350. */
  351. VisTableManager.Reset();
  352. /*
  353. ** Re-partition the static object culling systems for the heck of it.
  354. ** We don't re-partition the dynamic object culling system since it is
  355. ** a massive preprocessing step to actually generate it.
  356. */
  357. StaticCullingSystem->Re_Partition();
  358. StaticLightingSystem->Re_Partition();
  359. /*
  360. ** Assign Vis IDs. Each system may allocate both vis sector ID's and
  361. ** vis object IDs.
  362. */
  363. StaticCullingSystem->Assign_Vis_IDs();
  364. DynamicObjVisSystem->Assign_Vis_IDs();
  365. StaticLightingSystem->Assign_Vis_IDs();
  366. /*
  367. ** Force all dynamic objects to recompute their visibility id
  368. */
  369. RefPhysListIterator it(&ObjList);
  370. while (!it.Is_Done()) {
  371. DynamicPhysClass * pobj = it.Peek_Obj()->As_DynamicPhysClass();
  372. if (pobj!=NULL) {
  373. pobj->Update_Visibility_Status();
  374. }
  375. it.Next();
  376. }
  377. }
  378. }
  379. /***********************************************************************************************
  380. * PhysicsSceneClass::Get_Vis_Camera -- returns a pointer (ref-counted) to the vis camera *
  381. * *
  382. * The VIS camera is allocated the first time it is needed. Normally this is only done *
  383. * as a pre-processing step inside the level editor. *
  384. * *
  385. * INPUT: *
  386. * *
  387. * OUTPUT: *
  388. * *
  389. * WARNINGS: *
  390. * *
  391. * HISTORY: *
  392. * 7/5/2000 gth : Created. *
  393. *=============================================================================================*/
  394. CameraClass * PhysicsSceneClass::Get_Vis_Camera(void)
  395. {
  396. if (VisCamera) {
  397. VisCamera->Add_Ref();
  398. return VisCamera;
  399. }
  400. VisCamera = new CameraClass;
  401. VisCamera->Set_Clip_Planes(VIS_NEAR_CLIP,VIS_FAR_CLIP);
  402. VisCamera->Set_View_Plane(DEG_TO_RAD(90.0f),DEG_TO_RAD(90.0f));
  403. VisCamera->Set_Viewport(Vector2(0,0),Vector2(1,1));
  404. VisCamera->Add_Ref();
  405. return VisCamera;
  406. }
  407. /***********************************************************************************************
  408. * PhysicsSceneClass::Update_Vis -- Performs a vis sample from the given coord system *
  409. * *
  410. * INPUT: *
  411. * *
  412. * OUTPUT: *
  413. * *
  414. * WARNINGS: *
  415. * *
  416. * HISTORY: *
  417. * 7/5/2000 gth : Created. *
  418. *=============================================================================================*/
  419. VisSampleClass
  420. PhysicsSceneClass::Update_Vis
  421. (
  422. const Matrix3D & camera_tm,
  423. VisDirBitsType direction_bits
  424. )
  425. {
  426. return Update_Vis(camera_tm.Get_Translation(),camera_tm,direction_bits);
  427. }
  428. /***********************************************************************************************
  429. * PhysicsSceneClass::Update_Vis -- Performs a vis sample from the given coord system *
  430. * *
  431. * INPUT: *
  432. * *
  433. * OUTPUT: *
  434. * *
  435. * WARNINGS: *
  436. * *
  437. * HISTORY: *
  438. * 7/5/2000 gth : Created. *
  439. *=============================================================================================*/
  440. VisSampleClass
  441. PhysicsSceneClass::Update_Vis
  442. (
  443. const Vector3 & sample_point,
  444. const Matrix3D & camera_tm,
  445. VisDirBitsType direction_bits,
  446. CameraClass * alternate_camera,
  447. int user_vis_id
  448. )
  449. {
  450. /*
  451. ** If the visibility system has been invalidated, reset it now
  452. */
  453. Internal_Vis_Reset();
  454. /*
  455. ** Look up the vis-sector-id for this vis sample
  456. */
  457. int vis_id = 0;
  458. if (user_vis_id == -1) {
  459. vis_id = StaticCullingSystem->Get_Vis_Sector_ID(sample_point);
  460. } else {
  461. vis_id = user_vis_id;
  462. }
  463. /*
  464. ** Get the existing pvs and make a copy of it that we can
  465. ** modify with the results of our sampling
  466. */
  467. VisTableClass * original_pvs = VisTableManager.Get_Vis_Table(vis_id,true);
  468. if (original_pvs == NULL) {
  469. VisSampleClass vis_sample(camera_tm,direction_bits);
  470. vis_sample.Init_Error();
  471. WWDEBUG_SAY(("Vis Sample Rejected - No Vis Sector or Vis Sector ID not assigned!\r\n"));
  472. return vis_sample;
  473. }
  474. VisTableClass * pvs = new VisTableClass(*original_pvs);
  475. /*
  476. ** Grab the camera, GERD, shader, vertex material...
  477. */
  478. CameraClass * camera = NULL;
  479. if (alternate_camera) { // if user is supplying an alternate camera, use it
  480. camera = alternate_camera;
  481. camera->Add_Ref();
  482. } else { // otherwise use the vis camera
  483. camera = Get_Vis_Camera();
  484. }
  485. WWASSERT (camera != NULL);
  486. /*
  487. ** Create the render context, initialize the GERD, install the Vis shader and vertex material
  488. */
  489. VisRenderContextClass context(*camera,*pvs);
  490. context.Set_Resolution(VIS_RENDER_WIDTH,VIS_RENDER_HEIGHT);
  491. context.Set_Vis_Quick_And_Dirty(VisQuickAndDirty);
  492. /*
  493. ** Evaluate visiblity for the six views from this point
  494. */
  495. VisSampleClass vis_sample(camera_tm,direction_bits);
  496. WWDEBUG_SAY(("Generating Vis for sector %d... ",vis_id));
  497. for (int i=0; i<VIS_DIRECTIONS; i++) {
  498. if (vis_sample.Direction_Enabled((VisDirType)i)) {
  499. if (!vis_sample.Sample_Useless() || (direction_bits & VIS_FORCE_ACCEPT)) {
  500. vis_sample.Set_Cur_Direction((VisDirType)i);
  501. context.Camera.Set_Transform(vis_sample.Get_Camera_Transform((VisDirType)i));
  502. Vis_Render_And_Scan(context,vis_sample);
  503. }
  504. }
  505. }
  506. /*
  507. ** The static culling system needs to propogate the leaf visibility bits through its
  508. ** hierarchy.
  509. */
  510. StaticCullingSystem->Propogate_Hierarchical_Visibility(pvs);
  511. /*
  512. ** Record how many bits this sample actually changed
  513. */
  514. vis_sample.Set_Bits_Changed(original_pvs->Count_Differences(*pvs));
  515. /*
  516. ** Accept the sample if it was not rejected -OR- the user has set the VIS_FORCE_ACCEPT bit
  517. */
  518. bool accept_sample = (!vis_sample.Sample_Rejected()) || (direction_bits & VIS_FORCE_ACCEPT);
  519. if (accept_sample) {
  520. VisTableManager.Update_Vis_Table(vis_id,pvs);
  521. WWDEBUG_SAY(("Done! (%d bits changed) \r\n",vis_sample.Get_Bits_Changed()));
  522. } else {
  523. WWDEBUG_SAY(("Rejected!\r\n"));
  524. }
  525. /*
  526. ** Release assets
  527. */
  528. REF_PTR_RELEASE(camera);
  529. REF_PTR_RELEASE(original_pvs);
  530. REF_PTR_RELEASE(pvs);
  531. return vis_sample;
  532. }
  533. /***********************************************************************************************
  534. * PhysicsSceneClass::Get_Static_Light_Count -- returns the number of static lights *
  535. * *
  536. * Each static light allocates a PVS. This accessor will let you loop over each light *
  537. * and update its PVS. *
  538. * *
  539. * INPUT: *
  540. * *
  541. * OUTPUT: *
  542. * *
  543. * WARNINGS: *
  544. * *
  545. * HISTORY: *
  546. * 7/19/2000 gth : Created. *
  547. *=============================================================================================*/
  548. int PhysicsSceneClass::Get_Static_Light_Count(void)
  549. {
  550. /*
  551. ** Count the lights in the list
  552. */
  553. int counter = 0;
  554. RefPhysListIterator light_it = Get_Static_Light_Iterator();
  555. for (light_it.First();!light_it.Is_Done(); light_it.Next()) {
  556. counter++;
  557. }
  558. return counter;
  559. }
  560. /***********************************************************************************************
  561. * PhysicsSceneClass::Generate_Vis_For_Light -- generate a PVS for the specified light *
  562. * *
  563. * INPUT: *
  564. * *
  565. * OUTPUT: *
  566. * *
  567. * WARNINGS: *
  568. * *
  569. * HISTORY: *
  570. * 7/19/2000 gth : Created. *
  571. *=============================================================================================*/
  572. void PhysicsSceneClass::Generate_Vis_For_Light(int light_index)
  573. {
  574. /*
  575. ** For lights, we're going to use the VIS camera but we're going to tweak the far
  576. ** clip plane to be the outer-radius of the light's falloff sphere.
  577. */
  578. CameraClass * camera = Get_Vis_Camera();
  579. /*
  580. ** Find the specified light. Array-like access into a list...
  581. */
  582. RefPhysListIterator light_it = Get_Static_Light_Iterator();
  583. for (; light_index > 0; light_index--) {
  584. light_it.Next();
  585. }
  586. /*
  587. ** Update vis for the specified light.
  588. */
  589. LightPhysClass * light = light_it.Peek_Obj()->As_LightPhysClass();
  590. if ( (light != NULL) &&
  591. (light->Get_Vis_Sector_ID() != 0xFFFFFFFF) &&
  592. (light->Peek_Model() != NULL) &&
  593. (light->Peek_Model()->Class_ID() == RenderObjClass::CLASSID_LIGHT))
  594. {
  595. /*
  596. ** Perform the vis sample
  597. */
  598. LightClass * lightmodel = (LightClass *)light->Peek_Model();
  599. camera->Set_Clip_Planes(0.01f,lightmodel->Get_Attenuation_Range());
  600. camera->Set_Transform(light->Get_Transform());
  601. Update_Vis( light->Get_Transform().Get_Translation(),
  602. light->Get_Transform(),
  603. (VisDirBitsType)(VIS_ALL | VIS_FORCE_ACCEPT),
  604. camera,
  605. light->Get_Vis_Sector_ID() );
  606. }
  607. /*
  608. ** Done, reset the camera clip planes, release resources
  609. */
  610. camera->Set_Clip_Planes(VIS_NEAR_CLIP,VIS_FAR_CLIP);
  611. REF_PTR_RELEASE(camera);
  612. }
  613. /***********************************************************************************************
  614. * PhysicsSceneClass::Vis_Render_And_Scan -- Renders the scene and scans for visible objects *
  615. * *
  616. * INPUT: *
  617. * *
  618. * OUTPUT: *
  619. * *
  620. * WARNINGS: *
  621. * *
  622. * HISTORY: *
  623. * 7/5/2000 gth : Created. *
  624. *=============================================================================================*/
  625. void PhysicsSceneClass::Vis_Render_And_Scan(VisRenderContextClass & context,VisSampleClass & vis_sample)
  626. {
  627. /*
  628. ** Have the static culling system evaluate visibility for the occluders
  629. ** and build the z-buffer in the process.
  630. */
  631. WWASSERT(context.VisRasterizer != NULL);
  632. context.VisRasterizer->Set_Render_Mode(IDBufferClass::OCCLUDER_MODE);
  633. StaticCullingSystem->Evaluate_Occluder_Visibility(context,vis_sample);
  634. On_Vis_Occluders_Rendered(context,vis_sample);
  635. /*
  636. ** Evaluate the visibility of the non-occluders in all systems
  637. */
  638. if (!vis_sample.Sample_Rejected()) {
  639. context.VisRasterizer->Set_Render_Mode(IDBufferClass::NON_OCCLUDER_MODE);
  640. StaticCullingSystem->Evaluate_Non_Occluder_Visibility(context,vis_sample);
  641. DynamicObjVisSystem->Evaluate_Non_Occluder_Visibility(context);
  642. }
  643. }
  644. /***********************************************************************************************
  645. * PhysicsSceneClass::Generate_Vis_Statistics_Report -- Stats about the visibility in the leve *
  646. * *
  647. * INPUT: *
  648. * *
  649. * OUTPUT: *
  650. * *
  651. * WARNINGS: *
  652. * *
  653. * HISTORY: *
  654. * 7/5/2000 gth : Created. *
  655. *=============================================================================================*/
  656. void PhysicsSceneClass::Generate_Vis_Statistics_Report(DynamicVectorClass<VisSectorStatsClass> & report)
  657. {
  658. /*
  659. ** Loop over each of the vis sectors in the level, adding up how many polygons and
  660. ** and how much texture memory can be seen from each one.
  661. */
  662. VisSectorStatsClass stats;
  663. RefPhysListIterator it(&StaticObjList);
  664. for (it.First(); !it.Is_Done(); it.Next()) {
  665. StaticPhysClass * obj = it.Peek_Obj()->As_StaticPhysClass();
  666. if (obj && obj->Is_Vis_Sector()) {
  667. VisTableClass * vistbl = VisTableManager.Get_Vis_Table(obj->Get_Vis_Sector_ID());
  668. stats.Compute_Stats(obj,vistbl);
  669. report.Add(stats);
  670. REF_PTR_RELEASE(vistbl);
  671. }
  672. }
  673. }
  674. /***********************************************************************************************
  675. * PhysicsSceneClass::Optimize_Visibility_Data -- combines and removes redundant vis data *
  676. * *
  677. * This function does three things. *
  678. * - The dynamic object AABTree removes useless leaf nodes (nodes with visibility identical *
  679. * to their parent) *
  680. * - All vis "objects" are checked and combined if their visibility is nearly the same *
  681. * - All vis "sectors" are checked and combined if their visibility is nearly the same *
  682. * *
  683. * INPUT: *
  684. * progress_status - class the the physics scene will update as the process proceeds. *
  685. * *
  686. * OUTPUT: *
  687. * *
  688. * WARNINGS: *
  689. * *
  690. * HISTORY: *
  691. * 7/5/2000 gth : Created. *
  692. *=============================================================================================*/
  693. void PhysicsSceneClass::Optimize_Visibility_Data(VisOptProgressClass & stats)
  694. {
  695. /*
  696. ** Initialize the progress_status.
  697. ** I'm goint to count one operation per each of the following
  698. ** - each node in the dynamic aabtree for the "Prune_Redundant_Leaf_Nodes" process
  699. ** - each vis object for the "Combine_Redundant_Vis_Objects" process
  700. ** - each vis sector for the "Combine_Redundant_Vis_Sectors" process
  701. */
  702. stats.Reset(DynamicObjVisSystem->Partition_Node_Count() + Get_Vis_Table_Size() + Get_Vis_Table_Count());
  703. stats.Set_Initial_Bit_Count(VisTableManager.Get_Vis_Table_Size() * VisTableManager.Get_Vis_Table_Count());
  704. stats.Set_Initial_Sector_Count(Get_Vis_Table_Count());
  705. stats.Set_Initial_Object_Count(Get_Vis_Table_Size());
  706. stats.Set_Initial_Dynamic_Cell_Count(DynamicObjVisSystem->Partition_Node_Count());
  707. /*
  708. ** Create a VisOptimizationContextClass and optimize the vis data
  709. */
  710. VisOptimizationContextClass optcontext(this,stats);
  711. optcontext.Optimize(&VisTableManager,DynamicObjVisSystem);
  712. /*
  713. ** Reset all of dynamic objects' cached vis-ID and cull node.
  714. */
  715. RefPhysListIterator it(&ObjList);
  716. for (it.First(); !it.Is_Done(); it.Next()) {
  717. DynamicPhysClass * obj = it.Peek_Obj()->As_DynamicPhysClass();
  718. if (obj != NULL) {
  719. obj->Update_Visibility_Status();
  720. }
  721. }
  722. /*
  723. ** Finish the statistics
  724. */
  725. stats.Set_Final_Bit_Count(VisTableManager.Get_Vis_Table_Size() * VisTableManager.Get_Vis_Table_Count());
  726. stats.Set_Final_Sector_Count(Get_Vis_Table_Count());
  727. stats.Set_Final_Object_Count(Get_Vis_Table_Size());
  728. stats.Set_Final_Dynamic_Cell_Count(DynamicObjVisSystem->Partition_Node_Count());
  729. }
  730. /***********************************************************************************************
  731. * PhysicsSceneClass::Merge_Vis_Sector_IDs -- Merges two sector ID's *
  732. * *
  733. * The greater ID will be set equal to the lesser ID. This should only be called by the *
  734. * vis optimization process and relies on a VisOptimizationContext to track the changes *
  735. * to the actual VIS bit vectors. *
  736. * *
  737. * INPUT: *
  738. * *
  739. * OUTPUT: *
  740. * *
  741. * WARNINGS: *
  742. * *
  743. * HISTORY: *
  744. * 7/5/2000 gth : Created. *
  745. *=============================================================================================*/
  746. void PhysicsSceneClass::Merge_Vis_Sector_IDs(uint32 id0,uint32 id1)
  747. {
  748. StaticCullingSystem->Merge_Vis_Sector_IDs(id0,id1);
  749. StaticLightingSystem->Merge_Vis_Sector_IDs(id0,id1);
  750. }
  751. /***********************************************************************************************
  752. * PhysicsSceneClass::Merge_Vis_Object_IDs -- combines two vis object ID's *
  753. * *
  754. * The greater ID will be set equal to the lesser ID. This should only be called by the *
  755. * vis optimization process and relies on a VisOptimizationContext to track the changes *
  756. * to the actual VIS bit vectors. *
  757. * *
  758. * INPUT: *
  759. * *
  760. * OUTPUT: *
  761. * *
  762. * WARNINGS: *
  763. * *
  764. * HISTORY: *
  765. * 7/5/2000 gth : Created. *
  766. *=============================================================================================*/
  767. void PhysicsSceneClass::Merge_Vis_Object_IDs(uint32 id0,uint32 id1)
  768. {
  769. StaticCullingSystem->Merge_Vis_Object_IDs(id0,id1);
  770. DynamicObjVisSystem->Merge_Vis_Object_IDs(id0,id1);
  771. }