aabtree.h 13 KB

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