From 29583345a087824e5b09510d1c7330090b78a111 Mon Sep 17 00:00:00 2001 From: danij Date: Mon, 2 Dec 2013 20:30:19 +0000 Subject: [PATCH] libdeng2: Added template de::lerp() --- doomsday/client/src/render/rend_model.cpp | 28 ++++++++--------------- doomsday/libdeng2/include/de/math.h | 13 +++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/doomsday/client/src/render/rend_model.cpp b/doomsday/client/src/render/rend_model.cpp index e0be0f8c97..f08bac358a 100644 --- a/doomsday/client/src/render/rend_model.cpp +++ b/doomsday/client/src/render/rend_model.cpp @@ -498,14 +498,6 @@ static void Mod_RenderPrimitives(rendcmd_t mode, Model::Primitives const &primit } } -/** - * Linear interpolation between two values. - */ -static inline float Mod_Lerp(float start, float end, float pos) -{ - return end * pos + start * (1 - pos); -} - /** * Interpolate linearly between two sets of vertices. */ @@ -513,7 +505,7 @@ static void Mod_LerpVertices(float inter, int count, ModelFrame const &from, ModelFrame const &to, Vector3f *posOut, Vector3f *normOut) { DENG2_ASSERT(&from.model == &to.model); // sanity check. - DENG2_ASSERT(&activeLod->model == &from.model); // sanity check. + DENG2_ASSERT(!activeLod || &activeLod->model == &from.model); // sanity check. DENG2_ASSERT(from.vertices.count() == to.vertices.count()); // sanity check. ModelFrame::VertexBuf::const_iterator startIt = from.vertices.begin(); @@ -532,14 +524,12 @@ static void Mod_LerpVertices(float inter, int count, ModelFrame const &from, } else { - float const invInter = 1 - inter; - for(int i = 0; i < count; ++i, startIt++, endIt++, posOut++, normOut++) { if(!activeLod || activeLod->hasVertex(i)) { - *posOut = startIt->pos * invInter + endIt->pos * inter; - *normOut = startIt->norm * invInter + endIt->norm * inter; + *posOut = de::lerp(startIt->pos, endIt->pos, inter); + *normOut = de::lerp(startIt->norm, endIt->norm, inter); } } } @@ -876,11 +866,11 @@ static void Mod_RenderSubModel(uint number, rendmodelparams_t const *parm) // Model space => World space glTranslatef(parm->origin[VX] + parm->srvo[VX] + - Mod_Lerp(mf->offset.x, mfNext->offset.x, inter), + de::lerp(mf->offset.x, mfNext->offset.x, inter), parm->origin[VZ] + parm->srvo[VZ] + - Mod_Lerp(mf->offset.y, mfNext->offset.y, inter), + de::lerp(mf->offset.y, mfNext->offset.y, inter), parm->origin[VY] + parm->srvo[VY] + zSign * - Mod_Lerp(mf->offset.z, mfNext->offset.z, inter)); + de::lerp(mf->offset.z, mfNext->offset.z, inter)); if(parm->extraYawAngle || parm->extraPitchAngle) { @@ -898,9 +888,9 @@ static void Mod_RenderSubModel(uint number, rendmodelparams_t const *parm) 0, 0, 1); // Scaling and model space offset. - glScalef(Mod_Lerp(mf->scale.x, mfNext->scale.x, inter), - Mod_Lerp(mf->scale.y, mfNext->scale.y, inter), - Mod_Lerp(mf->scale.z, mfNext->scale.z, inter)); + glScalef(de::lerp(mf->scale.x, mfNext->scale.x, inter), + de::lerp(mf->scale.y, mfNext->scale.y, inter), + de::lerp(mf->scale.z, mfNext->scale.z, inter)); if(parm->extraScale) { // Particle models have an extra scale. diff --git a/doomsday/libdeng2/include/de/math.h b/doomsday/libdeng2/include/de/math.h index 73c655e046..72997d173e 100644 --- a/doomsday/libdeng2/include/de/math.h +++ b/doomsday/libdeng2/include/de/math.h @@ -152,6 +152,19 @@ inline dint cmp(Type const &a, Type const &b) { return 0; } +/** + * Linear interpolation between two values. + * + * @param start Value to interpolate from. + * @param end Value to interpolate to. + * @param pos Normalized interpolation point [0..1]. + */ +template +static Type lerp(Type start, Type end, float pos) +{ + return end * pos + start * (1 - pos); +} + } // namespace de #endif /* LIBDENG2_MATH_H */