Skip to content

Commit

Permalink
Refactor: Moved material origin interpolation into Surface
Browse files Browse the repository at this point in the history
Also fixed a few issues with the implementation and took advantage
of the new features in de::Vector2<>.
  • Loading branch information
danij-deng committed Apr 10, 2013
1 parent b05558f commit c6c15e3
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 141 deletions.
61 changes: 39 additions & 22 deletions doomsday/client/include/map/surface.h
Expand Up @@ -25,7 +25,6 @@

#include <de/Observers>
#include <de/Vector>
#include <de/vector1.h> /// @todo remove me

#include "Material"
#ifdef __CLIENT__
Expand Down Expand Up @@ -63,29 +62,30 @@ class Surface : public de::MapElement

static float const DEFAULT_OPACITY; ///< 1.f
static de::Vector3f const DEFAULT_TINT_COLOR; ///< red=1.f, green=1.f, blue=1.f
static int const MAX_SMOOTH_MATERIAL_MOVE; ///< 8, $smoothmatoffset: Maximum speed for a smoothed material offset.

#ifdef __CLIENT__
struct DecorSource
{
vec3d_t origin; ///< World coordinates of the decoration.
de::Vector3d origin; ///< World coordinates of the decoration.
BspLeaf *bspLeaf;
/// @todo $revise-texture-animation reference by index.
de::MaterialSnapshot::Decoration const *decor;
};
#endif // __CLIENT__

public:
/// [X, Y] Planar offset to surface material origin.
vec2f_t _offset;
/// @em Sharp origin of the surface material.
de::Vector2f _materialOrigin;

/// Old [X, Y] Planar material origin offset. For smoothing.
vec2f_t _oldOffset[2];
/// Old @em sharp origin of the surface material, for smoothing.
de::Vector2f _oldMaterialOrigin[2];

/// Smoothed [X, Y] Planar material origin offset.
vec2f_t _visOffset;
/// Smoothed origin of the surface material.
de::Vector2f _visMaterialOrigin;

/// Smoother [X, Y] Planar material origin offset delta.
vec2f_t _visOffsetDelta;
/// Delta between the @sharp and smoothed origin of the surface material.
de::Vector2f _visMaterialOriginDelta;

#ifdef __CLIENT__
/// @todo Does not belong here - move to the map renderer.
Expand Down Expand Up @@ -194,25 +194,23 @@ class Surface : public de::MapElement
/**
* Returns the material origin offset for the surface.
*/
const_pvec2f_t &materialOrigin() const;
de::Vector2f const &materialOrigin() const;

/**
* Change the material origin offset for the surface.
*
* @param newOrigin New origin offset in map coordinate space units.
*/
bool setMaterialOrigin(const_pvec2f_t newOrigin);
bool setMaterialOrigin(de::Vector2f const &newOrigin);

/**
* @copydoc setMaterialOrigin()
*
* @param x New X origin offset in map coordinate space units.
* @param y New Y origin offset in map coordinate space units.
*/
inline bool setMaterialOrigin(float x, float y)
{
float newOrigin[2] = { x, y};
return setMaterialOrigin(newOrigin);
inline bool setMaterialOrigin(float x, float y) {
return setMaterialOrigin(de::Vector2f(x, y));
}

/**
Expand All @@ -235,15 +233,34 @@ class Surface : public de::MapElement
*
* @see setMaterialOrigin()
*/
const_pvec2f_t &visMaterialOrigin() const;
de::Vector2f const &visMaterialOrigin() const;

/**
* Returns the delta between current material origin and the interpolated
* visual origin of the material in the map coordinate space.
*
* @see setMaterialOrigin(), visMaterialOrigin()
*/
const_pvec2f_t &visMaterialOriginDelta() const;
de::Vector2f const &visMaterialOriginDelta() const;

/**
* Interpolate the visible material origin.
*
* @see visMaterialOrigin()
*/
void lerpVisMaterialOrigin();

/**
* Reset the surface's material origin tracking.
*
* @see visMaterialOrigin()
*/
void resetVisMaterialOrigin();

/**
* Roll the surface's material origin tracking buffer.
*/
void updateMaterialOriginTracking();

/**
* Returns the sound emitter for the surface.
Expand Down Expand Up @@ -423,6 +440,10 @@ class Surface : public de::MapElement
void markAsNeedingDecorationUpdate();
#endif // __CLIENT__

/// @return @c true= is owned by some element of the Map geometry.
/// @deprecated Unnecessary; refactor away.
bool isAttachedToMap() const;

/**
* Get a property value, selected by DMU_* name.
*
Expand All @@ -439,10 +460,6 @@ class Surface : public de::MapElement
*/
int setProperty(setargs_t const &args);

/// @return @c true= is owned by some element of the Map geometry.
/// @deprecated Unnecessary; refactor away.
bool isAttachedToMap() const;

/**
* Helper function for determining whether the surface is owned by a
* Line which is itself owned by a Polyobj.
Expand Down
104 changes: 21 additions & 83 deletions doomsday/client/src/map/gamemap.cpp
Expand Up @@ -37,9 +37,6 @@
/// Size of Blockmap blocks in map units. Must be an integer power of two.
#define MAPBLOCKUNITS (128)

/// $smoothmatoffset: Maximum speed for a smoothed material offset.
#define MAX_SMOOTH_MATERIAL_MOVE (8)

using namespace de;

DENG2_PIMPL(GameMap)
Expand Down Expand Up @@ -227,14 +224,6 @@ DENG2_PIMPL(GameMap)
foreach(Sector *sector, self._sectors)
foreach(Plane *plane, sector->planes())
{
// Set target heights for each plane.
plane->_targetHeight =
plane->_oldHeight[0] =
plane->_oldHeight[1] =
plane->_visHeight = plane->_height;

plane->_visHeightDelta = 0;

plane->surface().updateSoundEmitterOrigin();

#ifdef __CLIENT__
Expand Down Expand Up @@ -1697,59 +1686,33 @@ BspLeaf *GameMap::bspLeafAtPoint(const_pvec2d_t const point) const

void GameMap::lerpScrollingSurfaces(bool resetNextViewer)
{
SurfaceSet::iterator it = d->scrollingSurfaces.begin();
while(it != d->scrollingSurfaces.end())
if(resetNextViewer)
{
Surface &suf = **it;

if(resetNextViewer)
// Reset the surface material origin trackers.
foreach(Surface *surface, d->scrollingSurfaces)
{
// Reset the material offset trackers.
// X Offset.
suf._visOffsetDelta[0] = 0;
suf._oldOffset[0][0] = suf._oldOffset[0][1] = suf._offset[0];

// Y Offset.
suf._visOffsetDelta[1] = 0;
suf._oldOffset[1][0] = suf._oldOffset[1][1] = suf._offset[1];

#ifdef __CLIENT__
suf.markAsNeedingDecorationUpdate();
#endif

it++;
surface->resetVisMaterialOrigin();
}
// While the game is paused there is no need to calculate any
// visual material offsets.
else //if(!clientPaused)
{
// Set the visible material offsets.
// X Offset.
suf._visOffsetDelta[0] =
suf._oldOffset[0][0] * (1 - frameTimePos) +
suf._offset[0] * frameTimePos - suf._offset[0];

// Y Offset.
suf._visOffsetDelta[1] =
suf._oldOffset[1][0] * (1 - frameTimePos) +
suf._offset[1] * frameTimePos - suf._offset[1];

// Visible material offset.
suf._visOffset[0] = suf._offset[0] + suf._visOffsetDelta[0];
suf._visOffset[1] = suf._offset[1] + suf._visOffsetDelta[1];
// Tracked movement is now all done.
d->scrollingSurfaces.clear();
}
// While the game is paused there is no need to calculate any
// visual material origin offsets $smoothmaterialorigin.
else //if(!clientPaused)
{
// Set the visible origins.
QMutableSetIterator<Surface *> iter(d->scrollingSurfaces);
while(iter.hasNext())
{
Surface *surface = iter.next();

#ifdef __CLIENT__
suf.markAsNeedingDecorationUpdate();
#endif
surface->lerpVisMaterialOrigin();

// Has this material reached its destination?
if(suf._visOffset[0] == suf._offset[0] && suf._visOffset[1] == suf._offset[1])
{
it = d->scrollingSurfaces.erase(it);
}
else
// Has this plane reached its destination?
if(surface->visMaterialOrigin() == surface->materialOrigin())
{
it++;
iter.remove();
}
}
}
Expand All @@ -1759,32 +1722,7 @@ void GameMap::updateScrollingSurfaces()
{
foreach(Surface *surface, d->scrollingSurfaces)
{
// X Offset
surface->_oldOffset[0][0] = surface->_oldOffset[0][1];
surface->_oldOffset[0][1] = surface->_offset[0];

if(surface->_oldOffset[0][0] != surface->_oldOffset[0][1])
{
if(de::abs(surface->_oldOffset[0][0] - surface->_oldOffset[0][1]) >=
MAX_SMOOTH_MATERIAL_MOVE)
{
// Too fast: make an instantaneous jump.
surface->_oldOffset[0][0] = surface->_oldOffset[0][1];
}
}

// Y Offset
surface->_oldOffset[1][0] = surface->_oldOffset[1][1];
surface->_oldOffset[1][1] = surface->_offset[1];
if(surface->_oldOffset[1][0] != surface->_oldOffset[1][1])
{
if(de::abs(surface->_oldOffset[1][0] - surface->_oldOffset[1][1]) >=
MAX_SMOOTH_MATERIAL_MOVE)
{
// Too fast: make an instantaneous jump.
surface->_oldOffset[1][0] = surface->_oldOffset[1][1];
}
}
surface->updateMaterialOriginTracking();
}
}

Expand Down
9 changes: 4 additions & 5 deletions doomsday/client/src/map/r_world.cpp
Expand Up @@ -110,7 +110,7 @@ boolean R_FindBottomTop(SideDefSection section, int lineFlags,
if(matOffset)
{
Surface const &suf = frontDef->middle();
V2f_Copy(matOffset, suf.visMaterialOrigin());
V2f_Set(matOffset, suf.visMaterialOrigin().x, suf.visMaterialOrigin().y);
if(unpegBottom)
{
matOffset[1] -= *hi - *low;
Expand Down Expand Up @@ -138,7 +138,7 @@ boolean R_FindBottomTop(SideDefSection section, int lineFlags,

if(matOffset)
{
V2f_Copy(matOffset, suf->visMaterialOrigin());
V2f_Set(matOffset, suf->visMaterialOrigin().x, suf->visMaterialOrigin().y);
if(!unpegTop)
{
// Align with normal middle texture.
Expand Down Expand Up @@ -167,7 +167,7 @@ boolean R_FindBottomTop(SideDefSection section, int lineFlags,

if(matOffset)
{
V2f_Copy(matOffset, suf->visMaterialOrigin());
V2f_Set(matOffset, suf->visMaterialOrigin().x, suf->visMaterialOrigin().y);
if(bfloor->visHeight() > fceil->visHeight())
{
matOffset[1] -= (raiseToBackFloor? t : fceil->visHeight()) - bfloor->visHeight();
Expand Down Expand Up @@ -531,8 +531,7 @@ static void initAllMapPlaneHeights(GameMap &map)

static inline void initSurfaceMaterialOrigin(Surface &suf)
{
suf._visOffset[VX] = suf._oldOffset[0][VX] = suf._oldOffset[1][VX] = suf._offset[VX];
suf._visOffset[VY] = suf._oldOffset[0][VY] = suf._oldOffset[1][VY] = suf._offset[VY];
suf._visMaterialOrigin = suf._oldMaterialOrigin[0] = suf._oldMaterialOrigin[1] = suf._materialOrigin;
}

static void initAllMapSurfaceMaterialOrigins(GameMap &map)
Expand Down

0 comments on commit c6c15e3

Please sign in to comment.