Skip to content

Commit

Permalink
LightGrid: Observe changes to sector lighting properties
Browse files Browse the repository at this point in the history
Removed the the now obsolete mechanism for updating the light grid
from DMU and unused sector properties.
  • Loading branch information
danij-deng committed May 21, 2013
1 parent 0b5966e commit 318b24d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 87 deletions.
12 changes: 3 additions & 9 deletions doomsday/client/include/map/sector.h
Expand Up @@ -61,11 +61,11 @@ class Sector : public de::MapElement,
DENG2_ERROR(MissingPlaneError);

DENG2_DEFINE_AUDIENCE(LightLevelChange,
void lightLevelChanged(Sector &sector, float oldLightLevel))
void sectorLightLevelChanged(Sector &sector, float oldLightLevel))

DENG2_DEFINE_AUDIENCE(LightColorChange,
void lightColorChanged(Sector &sector, de::Vector3f const &oldLightColor,
int changedComponents /*bit-field (0x1=Red, 0x2=Green, 0x4=Blue)*/))
void sectorLightColorChanged(Sector &sector, de::Vector3f const &oldLightColor,
int changedComponents /*bit-field (0x1=Red, 0x2=Green, 0x4=Blue)*/))

static float const DEFAULT_LIGHT_LEVEL; ///< 1.f
static de::Vector3f const DEFAULT_LIGHT_COLOR; ///< red=1.f green=1.f, blue=1.f
Expand Down Expand Up @@ -96,15 +96,9 @@ class Sector : public de::MapElement,
/// Ambient light level in the sector.
float _lightLevel;

/// Old ambient light level in the sector. For smoothing.
float _oldLightLevel;

/// Ambient light color in the sector.
de::Vector3f _lightColor;

/// Old ambient light color in the sector. For smoothing.
de::Vector3f _oldLightColor;

/// Head of the linked list of mobjs "in" the sector (not owned).
struct mobj_s *_mobjList;

Expand Down
7 changes: 0 additions & 7 deletions doomsday/client/include/render/lightgrid.h
Expand Up @@ -103,13 +103,6 @@ class LightGrid
*/
float evaluateLightLevel(Vector3d const &point);

/**
* To be called when the ambient lighting properties in the sector change.
*
* @todo Replace with internal de::Observers based mechanism.
*/
void sectorChanged(Sector &sector);

/**
* Draw the light grid debug visual.
*/
Expand Down
42 changes: 0 additions & 42 deletions doomsday/client/src/map/p_dmu.cpp
Expand Up @@ -823,27 +823,6 @@ void DMU_SetValue(valuetype_t valueType, void *dst, setargs_t const *args,
}
}

static void updateSector(Sector &sector, bool forceUpdate = false)
{
#ifdef __CLIENT__
// Check if there are any lightlevel or color changes.
if(forceUpdate ||
(sector._lightLevel != sector._oldLightLevel ||
sector._lightColor[0] != sector._oldLightColor[0] ||
sector._lightColor[1] != sector._oldLightColor[1] ||
sector._lightColor[2] != sector._oldLightColor[2]))
{
sector._oldLightLevel = sector._lightLevel;
sector._oldLightColor = sector._lightColor;

if(theMap->hasLightGrid())
{
theMap->lightGrid().sectorChanged(sector);
}
}
#endif
}

/**
* Only those properties that are writable by outside parties (such as games)
* are included here. Attempting to set a non-writable property causes a
Expand All @@ -856,9 +835,6 @@ static void setProperty(MapElement *elem, setargs_t &args)
{
DENG_ASSERT(elem != 0);

Sector *updateSector1 = 0, *updateSector2 = 0;
Plane *updatePlane = 0;

/**
* @par Algorithm
* When setting a property, reference resolution is done hierarchically so
Expand Down Expand Up @@ -890,8 +866,6 @@ static void setProperty(MapElement *elem, setargs_t &args)

if(args.type == DMU_SECTOR)
{
updateSector1 = elem->castTo<Sector>();

if(args.modifiers & DMU_FLOOR_OF_SECTOR)
{
elem = &elem->castTo<Sector>()->floor();
Expand All @@ -906,8 +880,6 @@ static void setProperty(MapElement *elem, setargs_t &args)

if(args.type == DMU_LINE)
{
elem->castTo<Line>();

if(args.modifiers & DMU_FRONT_OF_LINE)
{
elem = &elem->castTo<Line>()->front();
Expand All @@ -922,8 +894,6 @@ static void setProperty(MapElement *elem, setargs_t &args)

if(args.type == DMU_SIDE)
{
elem->castTo<Line::Side>();

if(args.modifiers & DMU_TOP_OF_SIDE)
{
elem = &elem->castTo<Line::Side>()->top();
Expand All @@ -943,8 +913,6 @@ static void setProperty(MapElement *elem, setargs_t &args)

if(args.type == DMU_PLANE)
{
updatePlane = elem->castTo<Plane>();

switch(args.prop)
{
case DMU_MATERIAL:
Expand Down Expand Up @@ -981,16 +949,6 @@ static void setProperty(MapElement *elem, setargs_t &args)
// Write the property value(s).
/// @throws MapElement::WritePropertyError If the requested property is not writable.
elem->setProperty(args);

/// @todo Replace with de::Observers based updates.
if(updatePlane)
updateSector1 = &updatePlane->sector();

if(updateSector1)
updateSector(*updateSector1);

if(updateSector2)
updateSector(*updateSector2);
}

void DMU_GetValue(valuetype_t valueType, void const *src, setargs_t *args, uint index)
Expand Down
8 changes: 4 additions & 4 deletions doomsday/client/src/map/sector.cpp
Expand Up @@ -88,7 +88,7 @@ DENG2_PIMPL(Sector)
{
DENG2_FOR_PUBLIC_AUDIENCE(LightLevelChange, i)
{
i->lightLevelChanged(self, oldLightLevel);
i->sectorLightLevelChanged(self, oldLightLevel);
}
}

Expand All @@ -97,7 +97,7 @@ DENG2_PIMPL(Sector)
{
DENG2_FOR_PUBLIC_AUDIENCE(LightColorChange, i)
{
i->lightColorChanged(self, oldLightColor, changedComponents);
i->sectorLightColorChanged(self, oldLightColor, changedComponents);
}
}

Expand All @@ -116,8 +116,8 @@ DENG2_PIMPL(Sector)

Sector::Sector(float lightLevel, Vector3f const &lightColor)
: MapElement(DMU_SECTOR),
_lightLevel(lightLevel), _oldLightLevel(_lightLevel),
_lightColor(lightColor), _oldLightColor(_lightColor),
_lightLevel(lightLevel),
_lightColor(lightColor),
d(new Instance(this))
{
_frameFlags = 0;
Expand Down
80 changes: 55 additions & 25 deletions doomsday/client/src/render/lightgrid.cpp
Expand Up @@ -30,6 +30,7 @@
#include "de_console.h"

#include "BspLeaf"
#include "Sector"
#include "map/gamemap.h"
#include "map/p_maputil.h" // P_IsPointInBspLeaf
#include "map/p_players.h" // viewPlayer
Expand Down Expand Up @@ -341,7 +342,9 @@ static inline bool isNullBlock(LightBlock const &block) {
return &block == &nullBlock;
}

DENG2_PIMPL(LightGrid)
DENG2_PIMPL(LightGrid),
DENG2_OBSERVES(Sector, LightColorChange),
DENG2_OBSERVES(Sector, LightLevelChange)
{
/// Map for which we provide an ambient lighting grid.
GameMap &map;
Expand All @@ -358,7 +361,7 @@ DENG2_PIMPL(LightGrid)
/// The grid of LightBlocks.
Blocks grid;

/// Set to @a true when a full update is needed.
/// Set to @c true when a full update is needed.
bool needUpdate;

Instance(Public *i, GameMap &map)
Expand All @@ -369,6 +372,14 @@ DENG2_PIMPL(LightGrid)

~Instance()
{
foreach(LightBlock *block, grid)
{
if(!block) continue;
Sector &sector = block->sector();
sector.audienceForLightLevelChange -= this;
sector.audienceForLightColorChange -= this;
}

qDeleteAll(grid);
}

Expand All @@ -389,7 +400,7 @@ DENG2_PIMPL(LightGrid)
int y = de::round<int>((point.y - origin.y) / blockSize);

return Ref(de::clamp(1, x, dimensions.x - 2),
de::clamp(1, y, dimensions.y - 2));
de::clamp(1, y, dimensions.y - 2));
}

/**
Expand Down Expand Up @@ -618,8 +629,6 @@ DENG2_PIMPL(LightGrid)
for(int y = 0; y < dimensions.y; ++y)
for(int x = 0; x < dimensions.x; ++x)
{
Vector2d off(x * blockSize, y * blockSize);

/**
* Pick the sector at each of the sample points.
* @todo We don't actually need the blkSampleSectors array
Expand Down Expand Up @@ -681,6 +690,10 @@ DENG2_PIMPL(LightGrid)

// There is now one more block.
numBlocks++;

// We want notification when the sector light properties change.
sector->audienceForLightLevelChange += this;
sector->audienceForLightColorChange += this;
}

LOG_INFO("%i light blocks (%u bytes).")
Expand Down Expand Up @@ -788,6 +801,42 @@ DENG2_PIMPL(LightGrid)
// How much time did we spend?
LOG_INFO(String("LightGrid::initialize: Done in %1 seconds.").arg(begunAt.since(), 0, 'g', 2));
}

/**
* To be called when the ambient lighting properties in the sector change.
*/
void sectorChanged(Sector &sector)
{
Sector::LightGridData &lgData = sector._lightGridData;
if(!lgData.changedBlockCount && !lgData.blockCount)
return;

// Mark changed blocks and contributors.
for(uint i = 0; i < lgData.changedBlockCount; ++i)
{
lightBlock(lgData.blocks[i]).markChanged();
}

for(uint i = 0; i < lgData.blockCount; ++i)
{
lightBlock(lgData.blocks[i]).markChanged(true /* is-contributor */);
}

needUpdate = true;
}

/// Observes Sector LightLevelChange.
void sectorLightLevelChanged(Sector &sector, float /*oldLightLevel*/)
{
sectorChanged(sector);
}

/// Observes Sector LightColorChange.
void sectorLightColorChanged(Sector &sector, Vector3f const & /*oldLightColor*/,
int /*changedComponents*/)
{
sectorChanged(sector);
}
};

LightGrid::LightGrid(GameMap &map)
Expand Down Expand Up @@ -827,31 +876,12 @@ float LightGrid::evaluateLightLevel(Vector3d const &point)
return (color.x + color.y + color.z) / 3;
}

void LightGrid::sectorChanged(Sector &sector)
{
Sector::LightGridData &lgData = sector._lightGridData;
if(!lgData.changedBlockCount && !lgData.blockCount) return;

// Mark changed blocks and contributors.
for(uint i = 0; i < lgData.changedBlockCount; ++i)
{
d->lightBlock(lgData.blocks[i]).markChanged();
}

for(uint i = 0; i < lgData.blockCount; ++i)
{
d->lightBlock(lgData.blocks[i]).markChanged(true /* is-contributor */);
}

d->needUpdate = true;
}

void LightGrid::markAllForUpdate()
{
// Mark all blocks and contributors.
foreach(Sector *sector, d->map.sectors())
{
sectorChanged(*sector);
d->sectorChanged(*sector);
}
}

Expand Down

0 comments on commit 318b24d

Please sign in to comment.