aabtree.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/aabtree.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 6/14/01 9:42a $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef AABTREE_H
  39. #define AABTREE_H
  40. #include "always.h"
  41. #include "refcount.h"
  42. #include "simplevec.h"
  43. #include "vector3.h"
  44. #include "vector3i.h"
  45. #include "aaplane.h"
  46. #include "bittype.h"
  47. #include "colmath.h"
  48. #include "wwdebug.h"
  49. #include "aabtreebuilder.h"
  50. #include "obbox.h"
  51. #include <tri.h>
  52. #include <float.h>
  53. class MeshClass;
  54. class CameraClass;
  55. class RayCollisionTestClass;
  56. class AABoxCollisionTestClass;
  57. class OBBoxCollisionTestClass;
  58. class OBBoxIntersectionTestClass;
  59. class ChunkLoadClass;
  60. class ChunkSaveClass;
  61. class MeshGeometryClass;
  62. class OBBoxClass;
  63. class ChunkLoadClass;
  64. struct BoxRayAPTContextStruct;
  65. #define AABTREE_LEAF_FLAG 0x80000000
  66. /*
  67. ** AABTreeClass
  68. ** This class encapsulates an Axis-Aligned Bounding Box Tree for a mesh. This tree
  69. ** can be used to perform hierarchical culling for collision detection. Note that
  70. ** this class is constructed using the AABTreeBuilderClass; these two classes are
  71. ** very tightly coupled. Pretty much the only code which needs to know about the AABTreeClass
  72. ** is in MeshGeometryClass. I moved these out into a separate file just to reduce the
  73. ** size of meshmdl.cpp.
  74. */
  75. class AABTreeClass : public RefCountClass
  76. {
  77. public:
  78. AABTreeClass(void);
  79. AABTreeClass(AABTreeBuilderClass * builder);
  80. AABTreeClass(const AABTreeClass & that);
  81. ~AABTreeClass(void);
  82. void Load_W3D(ChunkLoadClass & cload);
  83. int Get_Node_Count(void) { return NodeCount; }
  84. int Get_Poly_Count(void) { return PolyCount; }
  85. int Compute_Ram_Size(void);
  86. void Generate_APT(const OBBoxClass & box,SimpleDynVecClass<uint32> & apt);
  87. void Generate_APT(const OBBoxClass & box,const Vector3 & viewdir,SimpleDynVecClass<uint32> & apt);
  88. bool Cast_Ray(RayCollisionTestClass & raytest);
  89. int Cast_Semi_Infinite_Axis_Aligned_Ray(const Vector3 & start_point,
  90. int axis_dir, unsigned char & flags);
  91. bool Cast_AABox(AABoxCollisionTestClass & boxtest);
  92. bool Cast_OBBox(OBBoxCollisionTestClass & boxtest);
  93. bool Intersect_OBBox(OBBoxIntersectionTestClass & boxtest);
  94. private:
  95. AABTreeClass & operator = (const AABTreeClass & that);
  96. void Read_Poly_Indices(ChunkLoadClass & cload);
  97. void Read_Nodes(ChunkLoadClass & cload);
  98. void Build_Tree_Recursive(AABTreeBuilderClass::CullNodeStruct * node,int &curpolyindex);
  99. void Reset(void);
  100. void Set_Mesh(MeshGeometryClass * mesh);
  101. void Update_Bounding_Boxes(void);
  102. void Update_Min_Max(int index,Vector3 & min,Vector3 & max);
  103. /*
  104. ** CullNodeStruct - the culling tree is built out of an array of these structures
  105. ** They contain the extents of an axis-aligned box, indices to children nodes,
  106. ** and indices into the polygon array
  107. ** (05/22/2000 gth - changed this structure to support either child nodes -or-
  108. ** a polygon array but not both at the same time. Also switched to 32bit indices
  109. ** so that the code doesn't become useless as quickly )
  110. */
  111. struct CullNodeStruct
  112. {
  113. Vector3 Min;
  114. Vector3 Max;
  115. uint32 FrontOrPoly0;
  116. uint32 BackOrPolyCount;
  117. // accessors
  118. inline bool Is_Leaf(void);
  119. inline int Get_Back_Child(void); // returns index of back child (only call for non-LEAFs!!!)
  120. inline int Get_Front_Child(void); // returns index of front child (only call for non-LEAFs!!!)
  121. inline int Get_Poly0(void); // returns index of first polygon (only call on LEAFs)
  122. inline int Get_Poly_Count(void); // returns polygon count (only call on LEAFs)
  123. // initialization
  124. inline void Set_Front_Child(uint32 index);
  125. inline void Set_Back_Child(uint32 index);
  126. inline void Set_Poly0(uint32 index);
  127. inline void Set_Poly_Count(uint32 count);
  128. };
  129. /*
  130. ** OBBoxAPTContextStruct - this is a temporary datastructure used in building
  131. ** an APT by culling the mesh to an oriented bounding box.
  132. */
  133. struct OBBoxAPTContextStruct
  134. {
  135. OBBoxAPTContextStruct(const OBBoxClass & box,SimpleDynVecClass<uint32> & apt) :
  136. Box(box), APT(apt)
  137. { }
  138. OBBoxClass Box;
  139. SimpleDynVecClass<uint32> & APT;
  140. };
  141. /**
  142. ** OBBoxRayAPTContextStruct - temporary datastructure used in building an APT
  143. ** by culling the mesh to a oriented box and eliminating backfaces to a ray.
  144. */
  145. struct OBBoxRayAPTContextStruct
  146. {
  147. OBBoxRayAPTContextStruct(const OBBoxClass & box,const Vector3 & viewdir,SimpleDynVecClass<uint32> & apt) :
  148. Box(box),
  149. ViewVector(viewdir),
  150. APT(apt)
  151. { }
  152. OBBoxClass Box;
  153. Vector3 ViewVector;
  154. SimpleDynVecClass<uint32> & APT;
  155. };
  156. void Generate_OBBox_APT_Recursive(CullNodeStruct * node,OBBoxAPTContextStruct & context);
  157. void Generate_OBBox_APT_Recursive(CullNodeStruct * node, OBBoxRayAPTContextStruct & context);
  158. bool Cast_Ray_Recursive(CullNodeStruct * node,RayCollisionTestClass & raytest);
  159. int Cast_Semi_Infinite_Axis_Aligned_Ray_Recursive(CullNodeStruct * node, const Vector3 & start_point,
  160. int axis_r, int axis_1, int axis_2, int direction, unsigned char & flags);
  161. bool Cast_AABox_Recursive(CullNodeStruct * node,AABoxCollisionTestClass & boxtest);
  162. bool Cast_OBBox_Recursive(CullNodeStruct * node,OBBoxCollisionTestClass & boxtest);
  163. bool Intersect_OBBox_Recursive(CullNodeStruct * node,OBBoxIntersectionTestClass & boxtest);
  164. bool Cast_Ray_To_Polys(CullNodeStruct * node,RayCollisionTestClass & raytest);
  165. int Cast_Semi_Infinite_Axis_Aligned_Ray_To_Polys(CullNodeStruct * node, const Vector3 & start_point,
  166. int axis_r, int axis_1, int axis_2, int direction, unsigned char & flags);
  167. bool Cast_AABox_To_Polys(CullNodeStruct * node,AABoxCollisionTestClass & boxtest);
  168. bool Cast_OBBox_To_Polys(CullNodeStruct * node,OBBoxCollisionTestClass & boxtest);
  169. bool Intersect_OBBox_With_Polys(CullNodeStruct * node,OBBoxIntersectionTestClass & boxtest);
  170. void Update_Bounding_Boxes_Recursive(CullNodeStruct * node);
  171. int NodeCount; // number of nodes in the tree
  172. CullNodeStruct * Nodes; // array of nodes
  173. int PolyCount; // number of polygons in the parent mesh (and the number of indexes in our array)
  174. uint32 * PolyIndices; // linear array of polygon indices, nodes index into this array
  175. MeshGeometryClass * Mesh; // pointer to the parent mesh (non-ref-counted; we are a member of this mesh)
  176. friend class MeshClass;
  177. friend class MeshGeometryClass;
  178. friend class AuxMeshDataClass;
  179. friend class AABTreeBuilderClass;
  180. };
  181. inline int AABTreeClass::Compute_Ram_Size(void)
  182. {
  183. return NodeCount * sizeof(CullNodeStruct) +
  184. PolyCount * sizeof(int) +
  185. sizeof(AABTreeClass);
  186. }
  187. inline bool AABTreeClass::Cast_Ray(RayCollisionTestClass & raytest)
  188. {
  189. WWASSERT(Nodes != NULL);
  190. return Cast_Ray_Recursive(&(Nodes[0]),raytest);
  191. }
  192. inline int AABTreeClass::Cast_Semi_Infinite_Axis_Aligned_Ray(const Vector3 & start_point,
  193. int axis_dir, unsigned char & flags)
  194. {
  195. // These tables translate between the axis_dir representation (which is an integer in which 0
  196. // indicates a ray along the positive x axis, 1 along the negative x axis, 2 the positive y
  197. // axis, 3 negative y axis, 4 positive z axis, 5 negative z axis) and a four-integer
  198. // representation (axis_r is the axis number - 0, 1 or 2 - of the axis along which the ray is
  199. // cast; axis_1 and axis_2 are the axis numbers of the other two axes; direction is 0 for
  200. // negative and 1 for positive direction of the ray).
  201. static const int axis_r[6] = { 0, 0, 1, 1, 2, 2 };
  202. static const int axis_1[6] = { 1, 1, 2, 2, 0, 0 };
  203. static const int axis_2[6] = { 2, 2, 0, 0, 1, 1 };
  204. static const int direction[6] = { 1, 0, 1, 0, 1, 0 };
  205. WWASSERT(Nodes != NULL);
  206. WWASSERT(axis_dir >= 0);
  207. WWASSERT(axis_dir < 6);
  208. // The functions called after this point will 'or' bits into this variable, so it needs to
  209. // be initialized here to TRI_RAYCAST_FLAG_NONE.
  210. flags = TRI_RAYCAST_FLAG_NONE;
  211. return Cast_Semi_Infinite_Axis_Aligned_Ray_Recursive(&(Nodes[0]), start_point,
  212. axis_r[axis_dir], axis_1[axis_dir], axis_2[axis_dir], direction[axis_dir], flags);
  213. }
  214. inline bool AABTreeClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  215. {
  216. WWASSERT(Nodes != NULL);
  217. return Cast_AABox_Recursive(&(Nodes[0]),boxtest);
  218. }
  219. inline bool AABTreeClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  220. {
  221. WWASSERT(Nodes != NULL);
  222. return Cast_OBBox_Recursive(&(Nodes[0]),boxtest);
  223. }
  224. inline bool AABTreeClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  225. {
  226. WWASSERT(Nodes != NULL);
  227. return Intersect_OBBox_Recursive(&(Nodes[0]),boxtest);
  228. }
  229. inline void AABTreeClass::Update_Bounding_Boxes(void)
  230. {
  231. WWASSERT(Nodes != NULL);
  232. Update_Bounding_Boxes_Recursive(&(Nodes[0]));
  233. }
  234. /***********************************************************************************************
  235. AABTreeClass::CullNodeStruct implementation
  236. These nodes can be either leaf nodes or non-leaf nodes. If they are leaf nodes, they
  237. will contain an index to their first polygon index and a polygon count. If they are
  238. non-leafs they will contain indices to their front and back children. Since I'm re-using
  239. the same variables for the child indices and the polygon indices, you have to call
  240. the Is_Leaf function then only call the appropriate functions. The flag indicating whether
  241. this node is a leaf is stored in the MSB of the FrontOrPoly0 variable. It will always
  242. be stripped off by these accessor functions
  243. ***********************************************************************************************/
  244. inline bool AABTreeClass::CullNodeStruct::Is_Leaf(void)
  245. {
  246. return ((FrontOrPoly0 & AABTREE_LEAF_FLAG) != 0);
  247. }
  248. inline int AABTreeClass::CullNodeStruct::Get_Front_Child(void)
  249. {
  250. WWASSERT(!Is_Leaf());
  251. return FrontOrPoly0; // we shouldn't be calling this on a leaf and the leaf bit should be zero...
  252. }
  253. inline int AABTreeClass::CullNodeStruct::Get_Back_Child(void)
  254. {
  255. WWASSERT(!Is_Leaf());
  256. return BackOrPolyCount;
  257. }
  258. inline int AABTreeClass::CullNodeStruct::Get_Poly0(void)
  259. {
  260. WWASSERT(Is_Leaf());
  261. return (FrontOrPoly0 & ~AABTREE_LEAF_FLAG);
  262. }
  263. inline int AABTreeClass::CullNodeStruct::Get_Poly_Count(void)
  264. {
  265. WWASSERT(Is_Leaf());
  266. return BackOrPolyCount;
  267. }
  268. inline void AABTreeClass::CullNodeStruct::Set_Front_Child(uint32 index)
  269. {
  270. WWASSERT(index < 0x7FFFFFFF);
  271. FrontOrPoly0 = index;
  272. }
  273. inline void AABTreeClass::CullNodeStruct::Set_Back_Child(uint32 index)
  274. {
  275. WWASSERT(index < 0x7FFFFFFF);
  276. BackOrPolyCount = index;
  277. }
  278. inline void AABTreeClass::CullNodeStruct::Set_Poly0(uint32 index)
  279. {
  280. WWASSERT(index < 0x7FFFFFFF);
  281. FrontOrPoly0 = (index | AABTREE_LEAF_FLAG);
  282. }
  283. inline void AABTreeClass::CullNodeStruct::Set_Poly_Count(uint32 count)
  284. {
  285. WWASSERT(count < 0x7FFFFFFF);
  286. BackOrPolyCount = count;
  287. }
  288. #endif