Skip to content

Commit

Permalink
BspLeaf: Added getter methods for all BspLeaf property values
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 12, 2013
1 parent f159256 commit f8a673c
Show file tree
Hide file tree
Showing 33 changed files with 910 additions and 737 deletions.
150 changes: 128 additions & 22 deletions doomsday/client/include/map/bspleaf.h
Expand Up @@ -40,13 +40,19 @@ class Sector;
class HEdge;

/**
* Map BSP leaf.
* Two dimensional convex polygon describing a @em leaf in a binary space
* partition tree (BSP).
*
* @see http://en.wikipedia.org/wiki/Binary_space_partitioning
*
* @ingroup map
*/
class BspLeaf : public de::MapElement
{
public:
/// Required sector attribution is missing. @ingroup errors
DENG2_ERROR(MissingSectorError);

/// The referenced geometry group does not exist. @ingroup errors
DENG2_ERROR(UnknownGeometryGroupError);

Expand All @@ -58,50 +64,130 @@ class BspLeaf : public de::MapElement

public: /// @todo Make private:
/// First HEdge in this leaf.
HEdge *hedge;
HEdge *_hedge;

/// @ref bspLeafFlags.
int flags;
int _flags;

/// Unique. Set when saving the BSP.
uint index;
uint _index;

/// Frame number of last R_AddSprites.
int addSpriteCount;
int _addSpriteCount;

int validCount;
int _validCount;

/// Number of HEdge's in this leaf.
uint hedgeCount;
uint _hedgeCount;

Sector *sector;
Sector *_sector;

/// First polyobj in this leaf. Can be @c NULL.
struct polyobj_s *polyObj;
/// First Polyobj in this leaf. Can be @c NULL.
struct polyobj_s *_polyObj;

/// HEdge whose vertex to use as the base for a trifan. If @c NULL then midPoint is used instead.
HEdge *fanBase;
/// HEdge whose vertex to use as the base for a trifan.
/// If @c NULL then midPoint will be used instead.
HEdge *_fanBase;

struct shadowlink_s *shadows;
struct shadowlink_s *_shadows;

/// Vertex bounding box in the map coordinate space.
AABoxd aaBox;
AABoxd _aaBox;

/// Center of vertices.
coord_t midPoint[2];
coord_t _center[2];

/// Offset to align the top left of materials in the built geometry to the map coordinate space grid.
coord_t worldGridOffset[2];
/// Offset to align the top left of materials in the built geometry to the
/// map coordinate space grid.
coord_t _worldGridOffset[2];

/// [sector->planeCount] size.
struct biassurface_s **bsuf;
struct biassurface_s **_bsuf;

uint reverb[NUM_REVERB_DATA];
uint _reverb[NUM_REVERB_DATA];

public:
BspLeaf();
~BspLeaf();

/**
* Returns the axis-aligned bounding box which encompases all the vertexes
* which define the geometry of the BSP leaf in map coordinate space units.
*/
AABoxd const &aaBox() const;

/**
* Returns the point described by the average origin coordinates of all the
* vertexes which define the geometry of the BSP leaf in map coordinate space
* units.
*/
vec2d_t const &center() const;

/**
* Returns a pointer to the first HEdge of the BSP leaf; otherwise @c 0.
*/
HEdge *firstHEdge() const;

/**
* Returns the total number of half-edges in the BSP leaf.
*/
uint hedgeCount() const;

/**
* Returns @c true iff a sector is attributed to the BSP leaf. The only time
* a leaf might not be attributed to a sector is if the leaf was @em orphaned
* by the partitioning algorithm (a bug).
*/
bool hasSector() const;

/**
* Returns the sector attributed to the BSP leaf.
*
* @see hasSector()
*/
Sector &sector() const;

/**
* Returns a pointer to the sector attributed to the BSP leaf; otherwise @c 0.
*
* @see hasSector()
*/
inline Sector *sectorPtr() const { return hasSector()? &sector() : 0; }

/**
* Returns @c true iff there is at least one polyobj linked with the BSP leaf.
*/
inline bool hasPolyobj() { return !!firstPolyobj(); }

/**
* Returns a pointer to the first polyobj linked to the BSP leaf; otherwise @c 0.
*/
struct polyobj_s *firstPolyobj() const;

/**
* Returns the @ref bspLeafFlags of the BSP leaf.
*/
int flags() const;

/**
* Returns the original index of the BSP leaf.
*/
uint origIndex() const;

/**
* Returns the frame number of the last time sprites were projected for the
* BSP leaf.
*/
int addSpriteCount() const;

/**
* Returns the @em validCount of the BSP leaf. Used by some legacy iteration
* algorithms for marking leafs as processed/visited.
*
* @todo Refactor me away.
*/
int validCount() const;

/**
* Retrieve the bias surface for specified geometry @a groupId
*
Expand All @@ -110,17 +196,17 @@ class BspLeaf : public de::MapElement
biassurface_t &biasSurfaceForGeometryGroup(uint groupId);

/**
* Update the BspLeaf's map space axis-aligned bounding box to encompass
* Update the BSP leaf's map space axis-aligned bounding box to encompass
* the points defined by it's vertices.
*/
void updateAABox();

/**
* Update the mid point in the map coordinate space.
* Update the center point in the map coordinate space.
*
* @pre Axis-aligned bounding box must have been initialized.
*/
void updateMidPoint();
void updateCenter();

/**
* Update the world grid offset.
Expand All @@ -129,6 +215,26 @@ class BspLeaf : public de::MapElement
*/
void updateWorldGridOffset();

/**
* Returns a pointer to the HEdge of the BSP lead which has been chosen for
* use as the base for a triangle fan geometry. May return @c 0 if no suitable
* base is configured.
*/
HEdge *fanBase() const;

/**
* Returns the first shadowlink_t associated with the BSP leaf; otherwise @c 0.
*/
struct shadowlink_s *firstShadowLink() const;

/**
* 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()
*/
vec2d_t const &worldGridOffset() const;

/**
* Get a property value, selected by DMU_* name.
*
Expand Down
14 changes: 13 additions & 1 deletion doomsday/client/include/map/p_players.h
Expand Up @@ -45,7 +45,19 @@ int P_LocalToConsole(int localPlayer);
int P_ConsoleToLocal(int playerNum);
int P_GetDDPlayerIdx(ddplayer_t *ddpl);

boolean P_IsInVoid(player_t *p);
/**
* Do we THINK the given (camera) player is currently in the void.
* The method used to test this is to compare the position of the mobj
* each time it is linked into a BSP leaf.
*
* @note Cannot be 100% accurate so best not to use it for anything critical...
*
* @param player The player to test.
*
* @return @c true if the player is thought to be in the void.
*/
boolean P_IsInVoid(player_t *p);

short P_LookDirToShort(float lookDir);
float P_ShortToLookDir(short s);

Expand Down
69 changes: 35 additions & 34 deletions doomsday/client/src/audio/s_environ.cpp
Expand Up @@ -132,13 +132,13 @@ static void setBspLeafSectorOwner(ownerlist_t* ownerList, BspLeaf* bspLeaf)
ownerList->head = node;
}

static void findBspLeafsAffectingSector(GameMap* map, uint secIDX)
static void findBspLeafsAffectingSector(GameMap *map, uint secIDX)
{
Sector* sec = GameMap_Sector(map, secIDX);
Sector *sec = GameMap_Sector(map, secIDX);
if(!sec || !sec->lineDefCount) return;

ownerlist_t bspLeafOwnerList;
memset(&bspLeafOwnerList, 0, sizeof(bspLeafOwnerList));
std::memset(&bspLeafOwnerList, 0, sizeof(bspLeafOwnerList));

AABoxd aaBox = sec->aaBox;
aaBox.minX -= 128;
Expand All @@ -151,14 +151,14 @@ static void findBspLeafsAffectingSector(GameMap* map, uint secIDX)

for(uint i = 0; i < map->numBspLeafs; ++i)
{
BspLeaf* bspLeaf = GameMap_BspLeaf(map, i);
BspLeaf *bspLeaf = GameMap_BspLeaf(map, i);

// Is this BSP leaf close enough?
if(bspLeaf->sector == sec || // leaf is IN this sector
(bspLeaf->midPoint[VX] > aaBox.minX &&
bspLeaf->midPoint[VY] > aaBox.minY &&
bspLeaf->midPoint[VX] < aaBox.maxX &&
bspLeaf->midPoint[VY] < aaBox.maxY))
if(bspLeaf->sectorPtr() == sec || // leaf is IN this sector
(bspLeaf->center()[VX] > aaBox.minX &&
bspLeaf->center()[VY] > aaBox.minY &&
bspLeaf->center()[VX] < aaBox.maxX &&
bspLeaf->center()[VY] < aaBox.maxY))
{
// It will contribute to the reverb settings of this sector.
setBspLeafSectorOwner(&bspLeafOwnerList, bspLeaf);
Expand All @@ -169,15 +169,15 @@ static void findBspLeafsAffectingSector(GameMap* map, uint secIDX)
sec->numReverbBspLeafAttributors = bspLeafOwnerList.count;
if(sec->numReverbBspLeafAttributors)
{
sec->reverbBspLeafs = (BspLeaf**)
Z_Malloc((sec->numReverbBspLeafAttributors + 1) * sizeof(BspLeaf*), PU_MAPSTATIC, 0);
sec->reverbBspLeafs = (BspLeaf **)
Z_Malloc((sec->numReverbBspLeafAttributors + 1) * sizeof(BspLeaf *), PU_MAPSTATIC, 0);

BspLeaf** ptr = sec->reverbBspLeafs;
ownernode_t* node = bspLeafOwnerList.head;
BspLeaf **ptr = sec->reverbBspLeafs;
ownernode_t *node = bspLeafOwnerList.head;
for(uint i = 0; i < sec->numReverbBspLeafAttributors; ++i, ptr++)
{
ownernode_t* next = node->next;
*ptr = (BspLeaf*) node->data;
ownernode_t *next = node->next;
*ptr = (BspLeaf *) node->data;

if(i < map->sectorCount() - 1)
{
Expand Down Expand Up @@ -214,30 +214,31 @@ void S_DetermineBspLeafsAffectingSectorReverb(GameMap* map)
<< (Timer_RealMilliseconds() - startTime) / 1000.0f;
}

static boolean calcBspLeafReverb(BspLeaf* bspLeaf)
static boolean calcBspLeafReverb(BspLeaf *bspLeaf)
{
DENG2_ASSERT(bspLeaf);

if(!bspLeaf->sector || isDedicated)
if(bspLeaf->hasSector() || isDedicated)
{
bspLeaf->reverb[SRD_SPACE] = bspLeaf->reverb[SRD_VOLUME] =
bspLeaf->reverb[SRD_DECAY] = bspLeaf->reverb[SRD_DAMPING] = 0;
bspLeaf->_reverb[SRD_SPACE] = bspLeaf->_reverb[SRD_VOLUME] =
bspLeaf->_reverb[SRD_DECAY] = bspLeaf->_reverb[SRD_DAMPING] = 0;
return false;
}

float envSpaceAccum[NUM_AUDIO_ENVIRONMENT_CLASSES];
std::memset(&envSpaceAccum, 0, sizeof(envSpaceAccum));

// Space is the rough volume of the BSP leaf (bounding box).
bspLeaf->reverb[SRD_SPACE] =
(int) (bspLeaf->sector->SP_ceilheight - bspLeaf->sector->SP_floorheight) *
(bspLeaf->aaBox.maxX - bspLeaf->aaBox.minX) *
(bspLeaf->aaBox.maxY - bspLeaf->aaBox.minY);
bspLeaf->_reverb[SRD_SPACE] =
(int) (bspLeaf->sector().SP_ceilheight - bspLeaf->sector().SP_floorheight) *
(bspLeaf->aaBox().maxX - bspLeaf->aaBox().minX) *
(bspLeaf->aaBox().maxY - bspLeaf->aaBox().minY);

float total = 0;
// The other reverb properties can be found out by taking a look at the
// materials of all surfaces in the BSP leaf.
HEdge* hedge = bspLeaf->hedge;
HEdge *base = bspLeaf->firstHEdge();
HEdge *hedge = base;
do
{
if(hedge->lineDef && HEDGE_SIDEDEF(hedge) && HEDGE_SIDEDEF(hedge)->SW_middlematerial)
Expand All @@ -251,13 +252,13 @@ static boolean calcBspLeafReverb(BspLeaf* bspLeaf)

envSpaceAccum[env] += hedge->length;
}
} while((hedge = hedge->next) != bspLeaf->hedge);
} while((hedge = hedge->next) != base);

if(!total)
{
// Huh?
bspLeaf->reverb[SRD_VOLUME] = bspLeaf->reverb[SRD_DECAY] =
bspLeaf->reverb[SRD_DAMPING] = 0;
bspLeaf->_reverb[SRD_VOLUME] = bspLeaf->_reverb[SRD_DECAY] =
bspLeaf->_reverb[SRD_DAMPING] = 0;
return false;
}

Expand All @@ -274,23 +275,23 @@ static boolean calcBspLeafReverb(BspLeaf* bspLeaf)
v += envSpaceAccum[i] * envInfo[i].volumeMul;
}
if(v > 255) v = 255;
bspLeaf->reverb[SRD_VOLUME] = v;
bspLeaf->_reverb[SRD_VOLUME] = v;

// Decay time.
for(i = 0, v = 0; i < NUM_AUDIO_ENVIRONMENT_CLASSES; ++i)
{
v += envSpaceAccum[i] * envInfo[i].decayMul;
}
if(v > 255) v = 255;
bspLeaf->reverb[SRD_DECAY] = v;
bspLeaf->_reverb[SRD_DECAY] = v;

// High frequency damping.
for(i = 0, v = 0; i < NUM_AUDIO_ENVIRONMENT_CLASSES; ++i)
{
v += envSpaceAccum[i] * envInfo[i].dampingMul;
}
if(v > 255) v = 255;
bspLeaf->reverb[SRD_DAMPING] = v;
bspLeaf->_reverb[SRD_DAMPING] = v;

/* DEBUG_Message(("bspLeaf %04i: vol:%3i sp:%3i dec:%3i dam:%3i\n",
GET_BSPLEAF_IDX(bspLeaf), bspLeaf->reverb[SRD_VOLUME],
Expand Down Expand Up @@ -322,11 +323,11 @@ static void calculateSectorReverb(Sector *sec)

if(calcBspLeafReverb(bspLeaf))
{
sec->reverb[SRD_SPACE] += bspLeaf->reverb[SRD_SPACE];
sec->reverb[SRD_SPACE] += bspLeaf->_reverb[SRD_SPACE];

sec->reverb[SRD_VOLUME] += bspLeaf->reverb[SRD_VOLUME] / 255.0f * bspLeaf->reverb[SRD_SPACE];
sec->reverb[SRD_DECAY] += bspLeaf->reverb[SRD_DECAY] / 255.0f * bspLeaf->reverb[SRD_SPACE];
sec->reverb[SRD_DAMPING] += bspLeaf->reverb[SRD_DAMPING] / 255.0f * bspLeaf->reverb[SRD_SPACE];
sec->reverb[SRD_VOLUME] += bspLeaf->_reverb[SRD_VOLUME] / 255.0f * bspLeaf->_reverb[SRD_SPACE];
sec->reverb[SRD_DECAY] += bspLeaf->_reverb[SRD_DECAY] / 255.0f * bspLeaf->_reverb[SRD_SPACE];
sec->reverb[SRD_DAMPING] += bspLeaf->_reverb[SRD_DAMPING] / 255.0f * bspLeaf->_reverb[SRD_SPACE];
}
}

Expand Down

0 comments on commit f8a673c

Please sign in to comment.