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