forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathculltreenode.cpp
258 lines (219 loc) · 7.15 KB
/
culltreenode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "stdafx.h"
#include "CullTreeNode.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
// dvs: decide how this code should be organized
bool BoxesIntersect(Vector const &mins1, Vector const &maxs1, Vector const &mins2, Vector const &maxs2);
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CCullTreeNode::CCullTreeNode(void)
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CCullTreeNode::~CCullTreeNode(void)
{
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pChild -
//-----------------------------------------------------------------------------
void CCullTreeNode::AddCullTreeChild(CCullTreeNode *pChild)
{
m_Children.AddToTail(pChild);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pObject -
//-----------------------------------------------------------------------------
void CCullTreeNode::AddCullTreeObject(CMapClass *pObject)
{
// First make sure the object isn't already in this node.
// If it's already here, bail out.
if ( m_Objects.Find( pObject ) != -1 )
return;
// Add the object.
m_Objects.AddToTail(pObject);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pObject -
//-----------------------------------------------------------------------------
void CCullTreeNode::AddCullTreeObjectRecurse(CMapClass *pObject)
{
//
// If the object intersects this node, add it to this node and recurse,
// testing each of our children in the same fashion.
//
Vector ObjMins;
Vector ObjMaxs;
pObject->GetCullBox(ObjMins, ObjMaxs);
if (BoxesIntersect(ObjMins, ObjMaxs, bmins, bmaxs))
{
int nChildCount = GetChildCount();
if (nChildCount != 0)
{
// dvs: we should split when appropriate!
// otherwise the tree becomes less optimal over time.
for (int nChild = 0; nChild < nChildCount; nChild++)
{
CCullTreeNode *pChild = GetCullTreeChild(nChild);
pChild->AddCullTreeObjectRecurse(pObject);
}
}
else
{
AddCullTreeObject(pObject);
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes all objects from this node.
//-----------------------------------------------------------------------------
void CCullTreeNode::RemoveAllCullTreeObjects(void)
{
m_Objects.RemoveAll();
}
//-----------------------------------------------------------------------------
// Purpose: Removes all objects from this branch of the tree recursively.
//-----------------------------------------------------------------------------
void CCullTreeNode::RemoveAllCullTreeObjectsRecurse(void)
{
RemoveAllCullTreeObjects();
int nChildCount = GetChildCount();
for (int nChild = 0; nChild < nChildCount; nChild++)
{
CCullTreeNode *pChild = GetCullTreeChild(nChild);
pChild->RemoveAllCullTreeObjectsRecurse();
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes all instances of a given object from this node.
// Input : pObject -
//-----------------------------------------------------------------------------
void CCullTreeNode::RemoveCullTreeObject(CMapClass *pObject)
{
// Remove occurrence of pObject from the array
m_Objects.FindAndFastRemove( pObject );
// make sure it's not in there twice
Assert( m_Objects.Find( pObject) == -1 );
}
//-----------------------------------------------------------------------------
// Purpose: Removes all instances of a given object from this node.
// Input : pObject -
//-----------------------------------------------------------------------------
void CCullTreeNode::RemoveCullTreeObjectRecurse(CMapClass *pObject)
{
RemoveCullTreeObject(pObject);
for (int nChild = 0; nChild < m_Children.Count(); nChild++)
{
CCullTreeNode *pChild = m_Children[nChild];
pChild->RemoveCullTreeObjectRecurse(pObject);
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes all instances of a given object from this node.
// Input : pObject -
//-----------------------------------------------------------------------------
CCullTreeNode *CCullTreeNode::FindCullTreeObjectRecurse(CMapClass *pObject)
{
for (int i = 0; i < m_Objects.Count(); i++)
{
CMapClass *pCurrent = m_Objects[i];
if (pCurrent == pObject)
{
return(this);
}
}
int nChildCount = GetChildCount();
for (int nChild = 0; nChild < nChildCount; nChild++)
{
CCullTreeNode *pChild = GetCullTreeChild(nChild);
CCullTreeNode *pFound = pChild->FindCullTreeObjectRecurse(pObject);
if (pFound != NULL)
{
return(pFound);
}
}
return(NULL);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pObject -
//-----------------------------------------------------------------------------
void CCullTreeNode::UpdateCullTreeObject(CMapClass *pObject)
{
Vector mins;
Vector maxs;
pObject->GetCullBox(mins, maxs);
if (!BoxesIntersect(mins, maxs, bmins, bmaxs))
{
RemoveCullTreeObject(pObject);
}
else
{
AddCullTreeObject(pObject);
}
}
//-----------------------------------------------------------------------------
// Purpose: Updates the culling tree due to a change in the bounding box of a
// given object within the tree. The object is added to any leaf nodes
// that it now intersects, and is removed from any leaf nodes that it
// no longer intersects.
// Input : pObject - The object whose bounding box has changed.
//-----------------------------------------------------------------------------
void CCullTreeNode::UpdateCullTreeObjectRecurse(CMapClass *pObject)
{
int nChildCount = GetChildCount();
if (nChildCount != 0)
{
for (int nChild = 0; nChild < nChildCount; nChild++)
{
CCullTreeNode *pChild = GetCullTreeChild(nChild);
pChild->UpdateCullTreeObjectRecurse(pObject);
}
}
else
{
UpdateCullTreeObject(pObject);
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pObject - The object whose bounding box has changed.
//-----------------------------------------------------------------------------
void CCullTreeNode::UpdateAllCullTreeObjectsRecurse(void)
{
int nChildCount = GetChildCount();
if (nChildCount != 0)
{
for (int nChild = 0; nChild < nChildCount; nChild++)
{
CCullTreeNode *pChild = GetCullTreeChild(nChild);
pChild->UpdateAllCullTreeObjectsRecurse();
}
}
else
{
int nObjectCount = GetObjectCount();
for (int nObject = 0; nObject < nObjectCount; nObject++)
{
CMapClass *pObject = GetCullTreeObject(nObject);
Vector mins;
Vector maxs;
pObject->GetCullBox(mins, maxs);
if (!BoxesIntersect(mins, maxs, bmins, bmaxs))
{
RemoveCullTreeObject(pObject);
}
}
}
}