aabtree.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. ** Command & Conquer Generals(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 W3DMPO, public RefCountClass
  76. {
  77. W3DMPO_GLUE(AABTreeClass)
  78. public:
  79. AABTreeClass(void);
  80. AABTreeClass(AABTreeBuilderClass * builder);
  81. AABTreeClass(const AABTreeClass & that);
  82. ~AABTreeClass(void);
  83. void Load_W3D(ChunkLoadClass & cload);
  84. int Get_Node_Count(void) { return NodeCount; }
  85. int Get_Poly_Count(void) { return PolyCount; }
  86. int Compute_Ram_Size(void);
  87. void Generate_APT(const OBBoxClass & box,SimpleDynVecClass<uint32> & apt);
  88. void Generate_APT(const OBBoxClass & box,const Vector3 & viewdir,SimpleDynVecClass<uint32> & apt);
  89. bool Cast_Ray(RayCollisionTestClass & raytest);
  90. int Cast_Semi_Infinite_Axis_Aligned_Ray(const Vector3 & start_point,
  91. int axis_dir, unsigned char & flags);
  92. bool Cast_AABox(AABoxCollisionTestClass & boxtest);
  93. bool Cast_OBBox(OBBoxCollisionTestClass & boxtest);
  94. bool Intersect_OBBox(OBBoxIntersectionTestClass & boxtest);
  95. private:
  96. AABTreeClass & operator = (const AABTreeClass & that);
  97. void Read_Poly_Indices(ChunkLoadClass & cload);
  98. void Read_Nodes(ChunkLoadClass & cload);
  99. void Build_Tree_Recursive(AABTreeBuilderClass::CullNodeStruct * node,int &curpolyindex);
  100. void Reset(void);
  101. void Set_Mesh(MeshGeometryClass * mesh);
  102. void Update_Bounding_Boxes(void);
  103. void Update_Min_Max(int index,Vector3 & min,Vector3 & max);
  104. /*
  105. ** CullNodeStruct - the culling tree is built out of an array of these structures
  106. ** They contain the extents of an axis-aligned box, indices to children nodes,
  107. ** and indices into the polygon array
  108. ** (05/22/2000 gth - changed this structure to support either child nodes -or-
  109. ** a polygon array but not both at the same time. Also switched to 32bit indices
  110. ** so that the code doesn't become useless as quickly )
  111. */
  112. struct CullNodeStruct
  113. {
  114. Vector3 Min;
  115. Vector3 Max;
  116. uint32 FrontOrPoly0;
  117. uint32 BackOrPolyCount;
  118. // accessors
  119. inline bool Is_Leaf(void);
  120. inline int Get_Back_Child(void); // returns index of back child (only call for non-LEAFs!!!)
  121. inline int Get_Front_Child(void); // returns index of front child (only call for non-LEAFs!!!)
  122. inline int Get_Poly0(void); // returns index of first polygon (only call on LEAFs)
  123. inline int Get_Poly_Count(void); // returns polygon count (only call on LEAFs)
  124. // initialization
  125. inline void Set_Front_Child(uint32 index);
  126. inline void Set_Back_Child(uint32 index);
  127. inline void Set_Poly0(uint32 index);
  128. inline void Set_Poly_Count(uint32 count);
  129. };
  130. /*
  131. ** OBBoxAPTContextStruct - this is a temporary datastructure used in building
  132. ** an APT by culling the mesh to an oriented bounding box.
  133. */
  134. struct OBBoxAPTContextStruct
  135. {
  136. OBBoxAPTContextStruct(const OBBoxClass & box,SimpleDynVecClass<uint32> & apt) :
  137. Box(box), APT(apt)
  138. { }
  139. OBBoxClass Box;
  140. SimpleDynVecClass<uint32> & APT;
  141. };
  142. /**
  143. ** OBBoxRayAPTContextStruct - temporary datastructure used in building an APT
  144. ** by culling the mesh to a oriented box and eliminating backfaces to a ray.
  145. */
  146. struct OBBoxRayAPTContextStruct
  147. {
  148. OBBoxRayAPTContextStruct(const OBBoxClass & box,const Vector3 & viewdir,SimpleDynVecClass<uint32> & apt) :
  149. Box(box),
  150. ViewVector(viewdir),
  151. APT(apt)
  152. { }
  153. OBBoxClass Box;
  154. Vector3 ViewVector;
  155. SimpleDynVecClass<uint32> & APT;
  156. };
  157. void Generate_OBBox_APT_Recursive(CullNodeStruct * node,OBBoxAPTContextStruct & context);
  158. void Generate_OBBox_APT_Recursive(CullNodeStruct * node, OBBoxRayAPTContextStruct & context);
  159. bool Cast_Ray_Recursive(CullNodeStruct * node,RayCollisionTestClass & raytest);
  160. int Cast_Semi_Infinite_Axis_Aligned_Ray_Recursive(CullNodeStruct * node, const Vector3 & start_point,
  161. int axis_r, int axis_1, int axis_2, int direction, unsigned char & flags);
  162. bool Cast_AABox_Recursive(CullNodeStruct * node,AABoxCollisionTestClass & boxtest);
  163. bool Cast_OBBox_Recursive(CullNodeStruct * node,OBBoxCollisionTestClass & boxtest);
  164. bool Intersect_OBBox_Recursive(CullNodeStruct * node,OBBoxIntersectionTestClass & boxtest);
  165. bool Cast_Ray_To_Polys(CullNodeStruct * node,RayCollisionTestClass & raytest);
  166. int Cast_Semi_Infinite_Axis_Aligned_Ray_To_Polys(CullNodeStruct * node, const Vector3 & start_point,
  167. int axis_r, int axis_1, int axis_2, int direction, unsigned char & flags);
  168. bool Cast_AABox_To_Polys(CullNodeStruct * node,AABoxCollisionTestClass & boxtest);
  169. bool Cast_OBBox_To_Polys(CullNodeStruct * node,OBBoxCollisionTestClass & boxtest);
  170. bool Intersect_OBBox_With_Polys(CullNodeStruct * node,OBBoxIntersectionTestClass & boxtest);
  171. void Update_Bounding_Boxes_Recursive(CullNodeStruct * node);
  172. int NodeCount; // number of nodes in the tree
  173. CullNodeStruct * Nodes; // array of nodes
  174. int PolyCount; // number of polygons in the parent mesh (and the number of indexes in our array)
  175. uint32 * PolyIndices; // linear array of polygon indices, nodes index into this array
  176. MeshGeometryClass * Mesh; // pointer to the parent mesh (non-ref-counted; we are a member of this mesh)
  177. friend class MeshClass;
  178. friend class MeshGeometryClass;
  179. friend class AuxMeshDataClass;
  180. friend class AABTreeBuilderClass;
  181. };
  182. inline int AABTreeClass::Compute_Ram_Size(void)
  183. {
  184. return NodeCount * sizeof(CullNodeStruct) +
  185. PolyCount * sizeof(int) +
  186. sizeof(AABTreeClass);
  187. }
  188. inline bool AABTreeClass::Cast_Ray(RayCollisionTestClass & raytest)
  189. {
  190. WWASSERT(Nodes != NULL);
  191. return Cast_Ray_Recursive(&(Nodes[0]),raytest);
  192. }
  193. inline int AABTreeClass::Cast_Semi_Infinite_Axis_Aligned_Ray(const Vector3 & start_point,
  194. int axis_dir, unsigned char & flags)
  195. {
  196. // These tables translate between the axis_dir representation (which is an integer in which 0
  197. // indicates a ray along the positive x axis, 1 along the negative x axis, 2 the positive y
  198. // axis, 3 negative y axis, 4 positive z axis, 5 negative z axis) and a four-integer
  199. // representation (axis_r is the axis number - 0, 1 or 2 - of the axis along which the ray is
  200. // cast; axis_1 and axis_2 are the axis numbers of the other two axes; direction is 0 for
  201. // negative and 1 for positive direction of the ray).
  202. static const int axis_r[6] = { 0, 0, 1, 1, 2, 2 };
  203. static const int axis_1[6] = { 1, 1, 2, 2, 0, 0 };
  204. static const int axis_2[6] = { 2, 2, 0, 0, 1, 1 };
  205. static const int direction[6] = { 1, 0, 1, 0, 1, 0 };
  206. WWASSERT(Nodes != NULL);
  207. WWASSERT(axis_dir >= 0);
  208. WWASSERT(axis_dir < 6);
  209. // The functions called after this point will 'or' bits into this variable, so it needs to
  210. // be initialized here to TRI_RAYCAST_FLAG_NONE.
  211. flags = TRI_RAYCAST_FLAG_NONE;
  212. return Cast_Semi_Infinite_Axis_Aligned_Ray_Recursive(&(Nodes[0]), start_point,
  213. axis_r[axis_dir], axis_1[axis_dir], axis_2[axis_dir], direction[axis_dir], flags);
  214. }
  215. inline bool AABTreeClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  216. {
  217. WWASSERT(Nodes != NULL);
  218. return Cast_AABox_Recursive(&(Nodes[0]),boxtest);
  219. }
  220. inline bool AABTreeClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  221. {
  222. WWASSERT(Nodes != NULL);
  223. return Cast_OBBox_Recursive(&(Nodes[0]),boxtest);
  224. }
  225. inline bool AABTreeClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  226. {
  227. WWASSERT(Nodes != NULL);
  228. return Intersect_OBBox_Recursive(&(Nodes[0]),boxtest);
  229. }
  230. inline void AABTreeClass::Update_Bounding_Boxes(void)
  231. {
  232. WWASSERT(Nodes != NULL);
  233. Update_Bounding_Boxes_Recursive(&(Nodes[0]));
  234. }
  235. /***********************************************************************************************
  236. AABTreeClass::CullNodeStruct implementation
  237. These nodes can be either leaf nodes or non-leaf nodes. If they are leaf nodes, they
  238. will contain an index to their first polygon index and a polygon count. If they are
  239. non-leafs they will contain indices to their front and back children. Since I'm re-using
  240. the same variables for the child indices and the polygon indices, you have to call
  241. the Is_Leaf function then only call the appropriate functions. The flag indicating whether
  242. this node is a leaf is stored in the MSB of the FrontOrPoly0 variable. It will always
  243. be stripped off by these accessor functions
  244. ***********************************************************************************************/
  245. inline bool AABTreeClass::CullNodeStruct::Is_Leaf(void)
  246. {
  247. return ((FrontOrPoly0 & AABTREE_LEAF_FLAG) != 0);
  248. }
  249. inline int AABTreeClass::CullNodeStruct::Get_Front_Child(void)
  250. {
  251. WWASSERT(!Is_Leaf());
  252. return FrontOrPoly0; // we shouldn't be calling this on a leaf and the leaf bit should be zero...
  253. }
  254. inline int AABTreeClass::CullNodeStruct::Get_Back_Child(void)
  255. {
  256. WWASSERT(!Is_Leaf());
  257. return BackOrPolyCount;
  258. }
  259. inline int AABTreeClass::CullNodeStruct::Get_Poly0(void)
  260. {
  261. WWASSERT(Is_Leaf());
  262. return (FrontOrPoly0 & ~AABTREE_LEAF_FLAG);
  263. }
  264. inline int AABTreeClass::CullNodeStruct::Get_Poly_Count(void)
  265. {
  266. WWASSERT(Is_Leaf());
  267. return BackOrPolyCount;
  268. }
  269. inline void AABTreeClass::CullNodeStruct::Set_Front_Child(uint32 index)
  270. {
  271. WWASSERT(index < 0x7FFFFFFF);
  272. FrontOrPoly0 = index;
  273. }
  274. inline void AABTreeClass::CullNodeStruct::Set_Back_Child(uint32 index)
  275. {
  276. WWASSERT(index < 0x7FFFFFFF);
  277. BackOrPolyCount = index;
  278. }
  279. inline void AABTreeClass::CullNodeStruct::Set_Poly0(uint32 index)
  280. {
  281. WWASSERT(index < 0x7FFFFFFF);
  282. FrontOrPoly0 = (index | AABTREE_LEAF_FLAG);
  283. }
  284. inline void AABTreeClass::CullNodeStruct::Set_Poly_Count(uint32 count)
  285. {
  286. WWASSERT(count < 0x7FFFFFFF);
  287. BackOrPolyCount = count;
  288. }
  289. #endif