glm_lerpc(), glm_step(), glm_smoothstep(), glm_smoothinterp() (#98)

* lerp, step, smoothstep

* glm_lerp() and friends are no longer clamped, use glm_lerpc() and friends
* mix() function as wrapper of lerp()
* no there are clamp and raw version of lerp functions
This commit is contained in:
Luigi Castelli
2019-08-25 21:17:36 +02:00
committed by Recep Aslantas
parent 43b36f1dc1
commit 4639f3184a
13 changed files with 978 additions and 23 deletions

View File

@@ -8,11 +8,26 @@
/*
Functions:
CGLM_INLINE int glm_sign(int val);
CGLM_INLINE float glm_signf(float val);
CGLM_INLINE float glm_rad(float deg);
CGLM_INLINE float glm_deg(float rad);
CGLM_INLINE void glm_make_rad(float *deg);
CGLM_INLINE void glm_make_deg(float *rad);
CGLM_INLINE float glm_pow2(float x);
CGLM_INLINE float glm_min(float a, float b);
CGLM_INLINE float glm_max(float a, float b);
CGLM_INLINE float glm_clamp(float val, float minVal, float maxVal);
CGLM_INLINE float glm_clamp_zo(float val, float minVal, float maxVal);
CGLM_INLINE float glm_lerp(float from, float to, float t);
CGLM_INLINE float glm_lerpc(float from, float to, float t);
CGLM_INLINE float glm_step(float edge, float x);
CGLM_INLINE float glm_smooth(float t);
CGLM_INLINE float glm_smoothstep(float edge0, float edge1, float x);
CGLM_INLINE float glm_smoothinterp(float from, float to, float t);
CGLM_INLINE float glm_smoothinterpc(float from, float to, float t);
CGLM_INLINE bool glm_eq(float a, float b);
CGLM_INLINE float glm_percent(float from, float to, float current);
CGLM_INLINE float glm_percentc(float from, float to, float current);
*/
#ifndef cglm_util_h
@@ -157,9 +172,103 @@ glm_clamp_zo(float val) {
}
/*!
* @brief linear interpolation between two number
* @brief linear interpolation between two numbers
*
* formula: from + s * (to - from)
* formula: from + t * (to - from)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount)
*/
CGLM_INLINE
float
glm_lerp(float from, float to, float t) {
return from + t * (to - from);
}
/*!
* @brief clamped linear interpolation between two numbers
*
* formula: from + t * (to - from)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount) clamped between 0 and 1
*/
CGLM_INLINE
float
glm_lerpc(float from, float to, float t) {
return glm_lerp(from, to, glm_clamp_zo(t));
}
/*!
* @brief threshold function
*
* @param[in] edge threshold
* @param[in] x value to test against threshold
* @return returns 0.0 if x < edge, else 1.0
*/
CGLM_INLINE
float
glm_step(float edge, float x) {
/* branching - no type conversion */
return (x < edge) ? 0.0f : 1.0f;
/*
* An alternative implementation without branching
* but with type conversion could be:
* return !(x < edge);
*/
}
/*!
* @brief smooth Hermite interpolation
*
* formula: t^2 * (3-2t)
*
* @param[in] t interpolant (amount)
*/
CGLM_INLINE
float
glm_smooth(float t) {
return t * t * (3.0f - 2.0f * t);
}
/*!
* @brief threshold function with a smooth transition (according to OpenCL specs)
*
* formula: t^2 * (3-2t)
*
* @param[in] edge0 low threshold
* @param[in] edge1 high threshold
* @param[in] x interpolant (amount)
*/
CGLM_INLINE
float
glm_smoothstep(float edge0, float edge1, float x) {
float t;
t = glm_clamp_zo((x - edge0) / (edge1 - edge0));
return glm_smooth(t);
}
/*!
* @brief smoothstep interpolation between two numbers
*
* formula: from + smoothstep(t) * (to - from)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount)
*/
CGLM_INLINE
float
glm_smoothinterp(float from, float to, float t) {
return from + glm_smooth(t) * (to - from);
}
/*!
* @brief clamped smoothstep interpolation between two numbers
*
* formula: from + smoothstep(t) * (to - from)
*
* @param[in] from from value
* @param[in] to to value
@@ -167,8 +276,8 @@ glm_clamp_zo(float val) {
*/
CGLM_INLINE
float
glm_lerp(float from, float to, float t) {
return from + glm_clamp_zo(t) * (to - from);
glm_smoothinterpc(float from, float to, float t) {
return glm_smoothinterp(from, to, glm_clamp_zo(t));
}
/*!