Browse Source

Implementation of the linear interpolation & smooth step functions, and convert SceneWindow movement to use them.

Paul Jan 12 years ago
parent
commit
ae757c914e

+ 2 - 29
engine/source/2d/gui/SceneWindow.cc

@@ -576,42 +576,15 @@ F32 SceneWindow::interpolate( F32 from, F32 to, F32 delta )
 {
     // Linear.
     if ( mCameraInterpolationMode == LINEAR )
-        return linearInterpolate( from, to, delta );
+        return mLerp( from, to, delta );
     // Sigmoid.
     else if ( mCameraInterpolationMode == SIGMOID )
-        return sigmoidInterpolate( from, to, delta );
+        return mSmoothStep( from, to, delta );
     // Hmmm...
     else
         return from;
 }
 
-//-----------------------------------------------------------------------------
-
-F32 SceneWindow::linearInterpolate( F32 from, F32 to, F32 delta )
-{
-    // Clamp if we're over/under time.
-    if ( delta <= 0.0f )
-        return from;
-    else if ( delta >= 1.0f )
-        return to;
-
-    // Calculate resultant interpolation.
-    return ( from * ( 1.0f - delta ) ) + ( to * delta );
-}
-
-//-----------------------------------------------------------------------------
-
-F32 SceneWindow::sigmoidInterpolate( F32 from, F32 to, F32 delta )
-{
-    // Range Expand/Clamp Delta to (-1 -> +1).
-    delta = mClampF( (delta - 0.5f) * 2.0f, -1.0f, 1.0f );
-
-    // Calculate interpolator value using sigmoid function.
-    F32 sigmoid = mClampF ( 1.0f / (1.0f + mPow(2.718282f, -15.0f * delta)), 0.0f, 1.0f );
-
-    // Calculate resultant interpolation.
-    return ( from * ( 1.0f - sigmoid ) ) + ( to * sigmoid );
-}
 
 //-----------------------------------------------------------------------------
 

+ 0 - 2
engine/source/2d/gui/SceneWindow.h

@@ -270,8 +270,6 @@ public:
     void completeCameraMove( void );
     void undoCameraMove( const F32 interpolationTime );
     F32 interpolate( F32 from, F32 to, F32 delta );
-    F32 linearInterpolate( F32 from, F32 to, F32 delta );
-    F32 sigmoidInterpolate( F32 from, F32 to, F32 delta );
     void updateCamera( void );
 
     inline Vector2 getCameraRenderPosition( void )                      { calculateCameraView( &mCameraCurrent ); return mCameraCurrent.mDestinationArea.centre(); }

+ 15 - 0
engine/source/math/mMathFn.h

@@ -365,6 +365,21 @@ inline U32 mMulDiv(S32 a, S32 b, U32 c)
    return m_mulDivU32(a, b, c);
 }
 
+/// Template function for doing a linear interpolation between any two
+/// types which implement operators for scalar multiply and addition.
+template <typename T>
+inline T mLerp( const T &v1, const T &v2, F32 factor )
+{
+   factor = mClampF( factor, 0.0f, 1.0f);
+   return ( v1 * ( 1.0f - factor ) ) + ( v2 * factor );
+}
+
+template <typename T>
+inline T mSmoothStep( const T &v1, const T &v2, F32 factor)
+{
+   factor = mClampF( factor, 0.0f, 1.0f);
+   return mLerp(v1, v2, (factor*factor*(3.0f-2.0f*factor)));
+}
 
 inline F32 mSin(const F32 angle)
 {