dynamicaabtreecull.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/dynamicaabtreecull.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 7/24/01 1:26p $*
  31. * *
  32. * $Revision:: 7 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef DYNAMICAABTREECULL_H
  41. #define DYNAMICAABTREECULL_H
  42. #include "always.h"
  43. #include "physaabtreecull.h"
  44. #include "physlist.h"
  45. class VisTableClass;
  46. class VisRenderContextClass;
  47. class VisOptimizationContextClass;
  48. class AABoxRenderObjClass;
  49. /**
  50. ** DynamicAABTreeCullClass
  51. ** This is a specialized AABTree which is designed to be used with dynamic objects. It
  52. ** contains both a uniform voxelization of the level and special "seed" boxes which
  53. ** are generated from a pathfind floodfill. Whenever a dynamic object moves, it is
  54. ** re-inserted into the smallest leaf node in the tree. So this is basically an AAB-Tree
  55. ** that partitions the level paying special attention to the areas that you seed
  56. ** it with. All nodes in the tree are assigned a visiblity id and are tracked in the
  57. ** vis system.
  58. **
  59. ** This AABTree also over-rides the Add_Object and Update_Culling function to behave differently.
  60. ** The tree is constructed with a set of boxes which cover all of the space in the level.
  61. ** These boxes are inflated by the MaxObjRadius parameter. When inserting objects into the
  62. ** tree we test their center-point against the non-inflated boxes and insert the object into
  63. ** the smallest leaf node that we can find.
  64. */
  65. class DynamicAABTreeCullClass : public PhysAABTreeCullClass
  66. {
  67. public:
  68. DynamicAABTreeCullClass(PhysicsSceneClass * pscene);
  69. ~DynamicAABTreeCullClass(void);
  70. /*
  71. ** DynamicAABTreeCullClass over-rides Add_Object and Update_Culling because it
  72. ** uses custom logic for determining which cell an object should end up in.
  73. */
  74. virtual void Add_Object(PhysClass * obj);
  75. virtual void Update_Culling(CullableClass * obj);
  76. /*
  77. ** Objects will request the visibility ID from this system.
  78. ** Note that if you pass in a pointer to your previous node_id, the lookup will be faster and this
  79. ** variable will be modified with the updated node id. The first time this method is called,
  80. ** initialize the node_id to zero.
  81. */
  82. uint32 Get_Dynamic_Object_Vis_ID(const AABoxClass & obj_bounds,int * node_id = NULL);
  83. /*
  84. ** DynamicObjCullClass adds a new re-partitioning interface to AABTreeCullSystemClass
  85. ** This interface allows the user to supply the grid parameters and the virtual occludees
  86. */
  87. void Re_Partition( DynamicVectorClass<AABoxClass> * virtual_occludees,
  88. const Vector3 & grid_min,
  89. const Vector3 & grid_max,
  90. const Vector3 & min_grid_cell_size,
  91. int max_grid_cell_count,
  92. float max_obj_radius );
  93. /*
  94. ** DynamicObjCullClass adds a new collect function which takes the pvs data
  95. ** into account. It also puts objects into one of two lists, separating
  96. ** world-space-meshes from the other objects.
  97. */
  98. void Collect_Visible_Objects( const FrustumClass & frustum,
  99. VisTableClass * pvs,
  100. RefPhysListClass & visobjlist );
  101. /*
  102. ** Debugging
  103. ** Render the visible leaf cells.
  104. ** Tree visualization, walk the tree, rendering all leaf cells of current node.
  105. */
  106. enum DisplayModeType {
  107. DISPLAY_NONE = 0,
  108. DISPLAY_ACTUAL_BOXES,
  109. DISPLAY_CENTERS,
  110. DISPLAY_OCCUPIED,
  111. };
  112. void Render_Visible_Cells(RenderInfoClass & rinfo,VisTableClass * pvs,DisplayModeType mode);
  113. void Debug_Reset_Node(void) { DebugIterator.Reset(); }
  114. bool Debug_Enter_Parent(void) { return DebugIterator.Enter_Parent(); }
  115. bool Debug_Enter_Sibling(void) { return DebugIterator.Enter_Sibling(); }
  116. bool Debug_Enter_Front_Child(void) { return DebugIterator.Enter_Front_Child(); }
  117. bool Debug_Enter_Back_Child(void) { return DebugIterator.Enter_Back_Child(); }
  118. /*
  119. ** Visibility Preprocesing. For this culling system, visibility of all of the nodes is tracked
  120. ** while the objects inside the culling system are ignored because they are assumed to be
  121. ** dynamic. For this reason, there are no occluders in the dynamic object culling system.
  122. */
  123. void Assign_Vis_IDs(void);
  124. void Evaluate_Non_Occluder_Visibility(VisRenderContextClass & context);
  125. void Prune_Redundant_Leaf_Nodes(VisOptimizationContextClass & context);
  126. void Merge_Vis_Object_IDs(uint32 id0,uint32 id1);
  127. /*
  128. ** Save/Load system.
  129. */
  130. virtual void Save_Static_Data(ChunkSaveClass & csave);
  131. virtual void Load_Static_Data(ChunkLoadClass & cload);
  132. protected:
  133. /*
  134. ** VisObjCollectContextClass - this object is passed into the recursive function that collects
  135. ** the visible objects each frame.
  136. */
  137. class VisObjCollectContextClass
  138. {
  139. public:
  140. VisObjCollectContextClass(const FrustumClass & frustum,VisTableClass & pvs,RefPhysListClass & visobjlist) :
  141. Frustum(frustum),
  142. PVS(pvs),
  143. VisObjList(visobjlist),
  144. PlanesPassed(0)
  145. {
  146. }
  147. const FrustumClass & Frustum;
  148. VisTableClass & PVS;
  149. RefPhysListClass & VisObjList;
  150. int PlanesPassed;
  151. };
  152. /*
  153. ** Internal functions
  154. */
  155. void collect_visible_objects_recursive(AABTreeNodeClass * node,VisObjCollectContextClass & context);
  156. void collect_visible_objects_no_hvis_recursive(AABTreeNodeClass * node,VisObjCollectContextClass & context);
  157. void render_visible_cells_recursive(AABTreeNodeClass * node,RenderInfoClass & rinfo,VisTableClass * pvs,DisplayModeType mode);
  158. void evaluate_non_occluder_visibility_recursive(AABTreeNodeClass * node,VisRenderContextClass & context);
  159. bool subtree_is_visible(AABTreeNodeClass * node,VisRenderContextClass & context);
  160. void set_tree_visibility(AABTreeNodeClass * node,VisRenderContextClass & context,bool onoff);
  161. int find_insertion_node(CullableClass * obj);
  162. void find_optimal_node(AABTreeNodeClass * node,const AABoxClass & box,int & node_index);
  163. void find_optimal_deflated_node(AABTreeNodeClass * node,const Vector3 & center,int & node_index);
  164. bool deflated_box_contains_point(const AABoxClass & box,const Vector3 & point);
  165. void prune_redundant_leaf_nodes_recursive(AABTreeNodeClass * node,VisOptimizationContextClass & context);
  166. void prune_child(AABTreeNodeClass * parent,AABTreeNodeClass * child,VisOptimizationContextClass & context);
  167. AABoxRenderObjClass * get_render_box(void);
  168. /*
  169. ** Members
  170. */
  171. float MaxObjRadius; // Object "inflation" radius used when the tree was created.
  172. AABoxRenderObjClass * RenderBox; // Used to render nodes when debugging
  173. AABTreeIterator DebugIterator; // Used to walk the tree while debugging. Render_Visible.. starts from this node
  174. /*
  175. ** Making Physics Scene a friend so that some of the more ugly vis-system interfaces
  176. ** can be accessed by it without exposing them to everyone else...
  177. */
  178. friend class PhysicsSceneClass;
  179. };
  180. /*
  181. ** Inlines
  182. */
  183. inline bool DynamicAABTreeCullClass::deflated_box_contains_point
  184. (
  185. const AABoxClass & box,
  186. const Vector3 & point
  187. )
  188. {
  189. if (WWMath::Fabs(point.X - box.Center.X) > box.Extent.X - MaxObjRadius) return false;
  190. if (WWMath::Fabs(point.Y - box.Center.Y) > box.Extent.Y - MaxObjRadius) return false;
  191. if (WWMath::Fabs(point.Z - box.Center.Z) > box.Extent.Z - MaxObjRadius) return false;
  192. return true;
  193. }
  194. #endif //DYNAMICAABTREECULL_H