aabtreebuilder.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/aabtreebuilder.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 11/24/01 5:49p $*
  31. * *
  32. * $Revision:: 2 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef AABTREEBUILDER_H
  41. #define AABTREEBUILDER_H
  42. #include "always.h"
  43. #include "vector3.h"
  44. #include "vector3i.h"
  45. #include "aaplane.h"
  46. #include "bittype.h"
  47. #include "meshgeometry.h"
  48. #include <float.h>
  49. class AABTreeClass;
  50. class ChunkSaveClass;
  51. struct W3dMeshAABTreeNode;
  52. /*
  53. ** AABTreeBuilderClass
  54. ** This class serves simply to build AABTreeClasses. It first builds a tree
  55. ** which uses an easier to manage data structure (but uses more memory). Then
  56. ** the tree is converted into the representation used in the AABTreeClass.
  57. */
  58. class AABTreeBuilderClass
  59. {
  60. public:
  61. AABTreeBuilderClass(void);
  62. ~AABTreeBuilderClass(void);
  63. void Build_AABTree(int polycount,TriIndex * polys,int vertcount,Vector3 * verts);
  64. void Export(ChunkSaveClass & csave);
  65. int Node_Count(void);
  66. int Poly_Count(void);
  67. enum
  68. {
  69. MIN_POLYS_PER_NODE = 4,
  70. SMALL_VERTEX = -100000,
  71. BIG_VERTEX = 100000
  72. };
  73. private:
  74. /*
  75. ** This CullNodeStruct is used in building the AABTree. It is much more
  76. ** wasteful in terms of memory footprint and number of allocations than the
  77. ** streamlined version found in the actual AABTreeClass.
  78. */
  79. struct CullNodeStruct
  80. {
  81. CullNodeStruct(void) : Index(0),Min(0,0,0),Max(0,0,0),Front(NULL),Back(NULL),PolyCount(0),PolyIndices(NULL) {}
  82. ~CullNodeStruct(void)
  83. {
  84. if (Front) { delete Front; }
  85. if (Back) { delete Back; }
  86. if (PolyIndices) { delete[] PolyIndices; }
  87. }
  88. int Index;
  89. Vector3 Min;
  90. Vector3 Max;
  91. CullNodeStruct * Front;
  92. CullNodeStruct * Back;
  93. int PolyCount;
  94. int * PolyIndices;
  95. };
  96. /*
  97. ** SplitChoiceStruct - encapsulates the results of evaluating the suitability of a partition
  98. */
  99. struct SplitChoiceStruct
  100. {
  101. SplitChoiceStruct(void) :
  102. Cost(FLT_MAX),
  103. FrontCount(0),
  104. BackCount(0),
  105. BMin(BIG_VERTEX,BIG_VERTEX,BIG_VERTEX),
  106. BMax(SMALL_VERTEX,SMALL_VERTEX,SMALL_VERTEX),
  107. FMin(BIG_VERTEX,BIG_VERTEX,BIG_VERTEX),
  108. FMax(SMALL_VERTEX,SMALL_VERTEX,SMALL_VERTEX),
  109. Plane(AAPlaneClass::XNORMAL,0)
  110. {
  111. }
  112. float Cost; // try to minimize this!
  113. int FrontCount; // number of polys in front of the plane
  114. int BackCount; // number of polys behind the plane
  115. Vector3 BMin; // min of the bounding box of the "back" child
  116. Vector3 BMax; // max of the bounding box of the "back" child
  117. Vector3 FMin; // min of the bounding box of the "front" child
  118. Vector3 FMax; // max of the bounding box of the "front" child
  119. AAPlaneClass Plane; // partitioning plane
  120. };
  121. struct SplitArraysStruct
  122. {
  123. SplitArraysStruct(void) :
  124. FrontCount(0),
  125. BackCount(0),
  126. FrontPolys(NULL),
  127. BackPolys(NULL)
  128. {
  129. }
  130. int FrontCount;
  131. int BackCount;
  132. int * FrontPolys;
  133. int * BackPolys;
  134. };
  135. enum OverlapType
  136. {
  137. POS = 0x01,
  138. NEG = 0x02,
  139. ON = 0x04,
  140. BOTH = 0x08,
  141. OUTSIDE = POS,
  142. INSIDE = NEG,
  143. OVERLAPPED = BOTH,
  144. FRONT = POS,
  145. BACK = NEG,
  146. };
  147. /*
  148. ** Internal functions
  149. */
  150. void Reset();
  151. void Build_Tree(CullNodeStruct * node,int polycount,int * polyindices);
  152. SplitChoiceStruct Select_Splitting_Plane(int polycount,int * polyindices);
  153. SplitChoiceStruct Compute_Plane_Score(int polycont,int * polyindices,const AAPlaneClass & plane);
  154. void Split_Polys(int polycount,int * polyindices,const SplitChoiceStruct & sc,SplitArraysStruct * arrays);
  155. OverlapType Which_Side(const AAPlaneClass & plane,int poly_index);
  156. void Compute_Bounding_Box(CullNodeStruct * node);
  157. int Assign_Index(CullNodeStruct * node,int index);
  158. int Node_Count_Recursive(CullNodeStruct * node,int curcount);
  159. void Update_Min(int poly_index,Vector3 & set_min);
  160. void Update_Max(int poly_index,Vector3 & set_max);
  161. void Update_Min_Max(int poly_index, Vector3 & set_min, Vector3 & set_max);
  162. void Build_W3D_AABTree_Recursive(CullNodeStruct * node,
  163. W3dMeshAABTreeNode * w3dnodes,
  164. uint32 * poly_indices,
  165. int & cur_node,
  166. int & cur_poly);
  167. /*
  168. ** Tree
  169. */
  170. CullNodeStruct * Root;
  171. int CurPolyIndex;
  172. /*
  173. ** Mesh data
  174. */
  175. int PolyCount;
  176. TriIndex * Polys;
  177. int VertCount;
  178. Vector3 * Verts;
  179. friend class AABTreeClass;
  180. };
  181. #endif //AABTREEBUILDER_H