Skip to content

Commit

Permalink
Added some interp funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
bstormweb committed Feb 10, 2014
1 parent f1116de commit e7f2b36
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/luddite/common/useful.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ float sgn( float n )
return (n<0)?-1.0:1.0;
}

float clamp( float x, float a, float b)
{
if (x < a) return a;
else if (x > b) return b;
else return x;
}

float saturate( float x )
{
if (x < 0.0) return 0.0;
else if (x > 1.0) return 1.0;
else return x;
}

float smoothstep(float edge0, float edge1, float x)
{
// Scale, bias and saturate x to 0..1 range
x = saturate((x - edge0)/(edge1 - edge0));
// Evaluate polynomial
return x*x*(3 - 2*x);
}

float smootherstep(float edge0, float edge1, float x)
{
// Scale, and clamp x to 0..1 range
x = clamp((x - edge0)/(edge1 - edge0), 0.0, 1.0);
// Evaluate polynomial
return x*x*x*(x*(x*6 - 15) + 10);
}

// ===========================================================================
// Error Checkings
Expand Down
7 changes: 7 additions & 0 deletions src/luddite/common/useful.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ float randNormal( float mean, float stddev );
// general mathy stuff
// ==============================
float sgn( float n );
float clamp( float x, float a, float b); // clamp x to range [a..b]
float saturate( float x ); // clamp x to [0..1]
float smoothstep(float edge0, float edge1, float x);
float smootherstep(float edge0, float edge1, float x);




#ifdef WIN32
#define M_PI (3.1415926535897932384626433832795)
Expand Down

0 comments on commit e7f2b36

Please sign in to comment.