aabtreebuilder.h 6.7 KB

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