/
bspleaf.h
438 lines (376 loc) · 13.3 KB
/
bspleaf.h
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/** @file bspleaf.h World map BSP leaf.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#ifndef DENG_WORLD_BSPLEAF_H
#define DENG_WORLD_BSPLEAF_H
#include <QSet>
#include <de/Error>
#include <de/Vector>
#include "MapElement"
#include "Line"
#include "Sector"
#include "Mesh"
#ifdef __CLIENT__
# include "BiasSurface"
#endif
struct polyobj_s;
#ifdef __CLIENT__
class BiasDigest;
#endif
/**
* Represents a leaf in the map's binary space partition (BSP) tree. Each leaf
* defines a two dimensioned convex subspace region (which, may be represented
* by a face (polygon) in the map's half-edge @ref de::Mesh geometry).
*
* On client side a leaf also provides / links to various geometry data assets
* and properties used to visualize the subspace.
*
* Each leaf is attributed to a @ref Sector in the map (with the exception of
* wholly degenerate subspaces which may occur during the partitioning process).
*
* @see http://en.wikipedia.org/wiki/Binary_space_partitioning
*
* @ingroup world
*/
class BspLeaf : public de::MapElement
#ifdef __CLIENT__
, public BiasSurface
#endif
{
DENG2_NO_COPY (BspLeaf)
DENG2_NO_ASSIGN(BspLeaf)
public:
/// Required sector cluster attribution is missing. @ingroup errors
DENG2_ERROR(MissingClusterError);
/// An invalid polygon was specified @ingroup errors
DENG2_ERROR(InvalidPolyError);
/// Required polygon geometry is missing. @ingroup errors
DENG2_ERROR(MissingPolyError);
/*
* Linked-element lists/sets:
*/
typedef QSet<de::Mesh *> Meshes;
typedef QSet<polyobj_s *> Polyobjs;
#ifdef __CLIENT__
typedef QSet<LineSide *> ShadowLines;
#endif
#ifdef __CLIENT__
// Final audio environment characteristics.
typedef uint AudioEnvironmentFactors[NUM_REVERB_DATA];
#endif
public:
/**
* Construct a new BSP leaf and optionally attribute it to @a sector.
* Ownership is unaffected.
*/
explicit BspLeaf(Sector *sector = 0);
/**
* Returns @c true iff the BSP leaf is "degenerate", which is to say that
* no convex face geometry is attributed.
*
* Equivalent to @code !hasPoly() @endcode
*/
inline bool isDegenerate() const { return !hasPoly(); }
/**
* Determines whether a convex face geometry (a polygon) is attributed.
*
* @see poly(), setPoly()
*/
bool hasPoly() const;
/**
* Provides access to the attributed convex face geometry (a polygon).
*
* @see hasPoly(), setPoly()
*/
de::Face const &poly() const;
/**
* Change the convex face geometry attributed to the BSP leaf. Before the
* geometry is accepted it is first conformance tested to ensure that it
* represents a valid, simple convex polygon.
*
* @param polygon New polygon to attribute to the BSP leaf. Ownership is
* unaffected. Can be @c 0 (to clear the attribution).
*
* @see hasPoly(), poly()
*/
void setPoly(de::Face *polygon);
/**
* Assign an additional mesh geometry to the BSP leaf. Such @em extra
* meshes are used to represent geometry which would otherwise result in
* a non-manifold mesh if incorporated in the primary mesh for the map.
*
* @param mesh New mesh to be assigned to the BSP leaf. Ownership of the
* mesh is given to the BspLeaf.
*/
void assignExtraMesh(de::Mesh &mesh);
/**
* Provides access to the set of 'extra' mesh geometries for the BSP leaf.
*
* @see assignExtraMesh()
*/
Meshes const &extraMeshes() const;
/**
* Returns @c true iff a sector cluster is attributed to the BSP leaf. The
* only time a leaf might not be attributed to a sector is if the map geometry
* was @em orphaned by the partitioning algorithm (a bug).
*/
bool hasCluster() const;
/**
* Returns @c true iff a sector (cluster) is attributed to the BSP leaf.
*
* Equivalent to @ref hasCluster()
*/
inline bool hasSector() const { return hasCluster(); }
/**
* Returns the sector cluster attributed to the BSP leaf.
*
* @see hasCluster()
*/
SectorCluster &cluster() const;
/**
* Convenient method of returning the sector of the cluster attributed to
* the BSP leaf.
*
* @see hasSector(), cluster()
*/
inline Sector §or() const { return cluster().sector(); }
/**
* Convenient method returning a pointer to the sector of the cluster
* attributed to the BSP leaf. If not attributed then @c 0 is returned.
*
* @see hasSector(), sector()
*/
inline Sector *sectorPtr() const { return hasSector()? §or() : 0; }
/**
* Change the sector cluster attributed to the BSP leaf.
*
* @param newCluster New sector cluster to attribute to the BSP leaf.
* Ownership is unaffected. Can be @c 0 (to clear the
* attribution).
*
* @see hasCluster(), cluster()
*/
void setCluster(SectorCluster *newCluster);
/**
* Returns the identified @em physical plane of the parent sector. Naturally
* a sector must be attributed for this. Note that this is not the same as
* the "visual" plane which may well be defined by another sector.
*
* @see hasSector()
*
* @param planeIndex Index of the plane to return.
*/
inline Plane &plane(int planeIndex) const { return cluster().plane(planeIndex); }
/**
* Returns the sector plane which defines the physical floor of the BSP leaf.
* @see plane(), visFloor()
*/
inline Plane &floor() const { return plane(Sector::Floor); }
/**
* Returns the sector plane which defines the physical ceiling of the BSP leaf.
* @see plane(), visCeiling()
*/
inline Plane &ceiling() const { return plane(Sector::Ceiling); }
/**
* Returns the identified @em visual sector plane for the BSP leaf (which may
* or may not be the same as the physical plane). Note that a sector must be
* attributed to "this" BSP leaf.
*
* @see hasSector()
*
* @param planeIndex Index of the plane to return.
*/
inline Plane &visPlane(int planeIndex) const { return cluster().visPlane(planeIndex); }
/**
* Returns the sector plane which defines the @em visual floor of the BSP leaf.
* @see hasSector(), floor()
*/
inline Plane &visFloor() const { return visPlane(Sector::Floor); }
/**
* Returns the sector plane which defines the @em visual ceiling of the BSP leaf.
* @see hasSector(), ceiling()
*/
inline Plane &visCeiling() const { return visPlane(Sector::Ceiling); }
#ifdef __CLIENT__
/**
* Convenient method of accessing the visual (i.e., smoothed) height of the
* identified @em visual sector plane.
*
* @param planeIndex Index of the plane to return.
*
* @see visPlane(), Plane::visHeight()
*/
inline coord_t visPlaneHeight(int planeIndex) const {
return visPlane(planeIndex).visHeight();
}
/**
* Convenient method of accessing the visual (i.e., smoothed) height of the
* @em visual floor plane.
*
* @see visPlaneHeight()
*/
inline coord_t visFloorHeight() const { return visPlaneHeight(Sector::Floor); }
/**
* Convenient method of accessing the visual (i.e., smoothed) height of the
* @em visual ceiling plane.
*
* @see visPlaneHeight()
*/
inline coord_t visCeilingHeight() const { return visPlaneHeight(Sector::Ceiling); }
#endif // __CLIENT__
/**
* Add the given @a polyobj to the set of those linked to the BSP leaf.
* Ownership is unaffected. If the polyobj is already linked in this set
* then nothing will happen.
*/
void addOnePolyobj(struct polyobj_s const &polyobj);
/**
* Remove the given @a polyobj from the set of those linked to the BSP leaf.
*
* @return @c true= @a polyobj was linked and subsequently removed.
*/
bool removeOnePolyobj(polyobj_s const &polyobj);
/**
* Provides access to the set of polyobjs linked to the BSP leaf.
*/
Polyobjs const &polyobjs() const;
/**
* Convenient method of returning the total number of polyobjs linked to the
* BSP leaf.
*/
inline int polyobjCount() { return polyobjs().count(); }
/**
* Returns the vector described by the offset from the map coordinate space
* origin to the top most, left most point of the geometry of the BSP leaf.
*
* @see aaBox()
*/
de::Vector2d const &worldGridOffset() const;
/**
* Determines whether the specified @a point in the map coordinate space
* lies within the BSP leaf (according to the edges).
*
* @param point Map space coordinate to test.
*
* @return @c true iff the point lies inside the BSP leaf.
*
* @see http://www.alienryderflex.com/polygon/
*/
bool pointInside(de::Vector2d const &point) const;
/**
* Returns the @em validCount of the BSP leaf. Used by some legacy iteration
* algorithms for marking leafs as processed/visited.
*
* @todo Refactor away.
*/
int validCount() const;
/// @todo Refactor away.
void setValidCount(int newValidCount);
#ifdef __CLIENT__
/**
* Determines whether the BSP leaf has a positive world volume. For this
* to be true the following criteria must be met:
*
* - The polygon geometry is @em not degenerate (see @ref isDegenerate()).
* - A sector is attributed (see @ref hasSector())
* - The height of floor is lower than that of the ceiling plane for the
* attributed sector.
*
* @param useVisualHeights @c true= use the visual (i.e., smoothed) plane
* heights instead of the @em sharp heights.
*/
bool hasWorldVolume(bool useVisualHeights = true) const;
/**
* Returns a pointer to the face geometry half-edge which has been chosen
* for use as the base for a triangle fan GL primitive. May return @c 0 if
* no suitable base was determined.
*/
de::HEdge *fanBase() const;
/**
* Returns the number of vertices needed for a triangle fan GL primitive.
*
* @note When first called after a face geometry is assigned a new 'base'
* half-edge for the triangle fan primitive will be determined.
*
* @see fanBase()
*/
int numFanVertices() const;
/**
* Perform bias lighting for the supplied geometry.
*
* @important It is assumed there are least @ref numFanVertices() elements!
*
* @param group Geometry group identifier.
* @param posCoords World coordinates for each vertex.
* @param colorCoords Final lighting values will be written here.
*/
void lightBiasPoly(int group, de::Vector3f const *posCoords,
de::Vector4f *colorCoords);
void updateBiasAfterGeometryMove(int group);
/**
* Apply bias lighting changes to @em all map element geometries at this
* leaf of the BSP.
*
* @param changes Digest of lighting changes to be applied.
*/
void applyBiasDigest(BiasDigest &changes);
/**
* Recalculate the environmental audio characteristics (reverb) of the BSP leaf.
*/
bool updateReverb();
/**
* Provides access to the final environmental audio characteristics (reverb)
* of the BSP leaf, for efficient accumulation.
*/
AudioEnvironmentFactors const &reverb() const;
/**
* Clear the list of fake radio shadow line sides for the BSP leaf.
*/
void clearShadowLines();
/**
* Add the specified line @a side to the set of fake radio shadow lines for
* the BSP leaf. If the line is already present in this set then nothing
* will happen.
*
* @param side Map line side to add to the set.
*/
void addShadowLine(LineSide &side);
/**
* Provides access to the set of fake radio shadow lines for the BSP leaf.
*/
ShadowLines const &shadowLines() const;
/**
* Returns the frame number of the last time mobj sprite projection was
* performed for the BSP leaf.
*/
int addSpriteCount() const;
/**
* Change the frame number of the last time mobj sprite projection was
* performed for the BSP leaf.
*
* @param newFrameCount New frame number.
*/
void setAddSpriteCount(int newFrameCount);
#endif // __CLIENT__
protected:
int property(DmuArgs &args) const;
private:
DENG2_PRIVATE(d)
};
#endif // DENG_WORLD_BSPLEAF_H