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

@@ -47,7 +47,14 @@
CGLM_INLINE void glm_vec4_maxv(vec4 a, vec4 b, vec4 dest);
CGLM_INLINE void glm_vec4_minv(vec4 a, vec4 b, vec4 dest);
CGLM_INLINE void glm_vec4_clamp(vec4 v, float minVal, float maxVal);
CGLM_INLINE void glm_vec4_lerp(vec4 from, vec4 to, float t, vec4 dest)
CGLM_INLINE void glm_vec4_lerp(vec4 from, vec4 to, float t, vec4 dest);
CGLM_INLINE void glm_vec4_lerpc(vec4 from, vec4 to, float t, vec4 dest);
CGLM_INLINE void glm_vec4_step_uni(float edge, vec4 x, vec4 dest);
CGLM_INLINE void glm_vec4_step(vec4 edge, vec4 x, vec4 dest);
CGLM_INLINE void glm_vec4_smoothstep_uni(float edge0, float edge1, vec4 x, vec4 dest);
CGLM_INLINE void glm_vec4_smoothstep(vec4 edge0, vec4 edge1, vec4 x, vec4 dest);
CGLM_INLINE void glm_vec4_smoothinterp(vec4 from, vec4 to, float t, vec4 dest);
CGLM_INLINE void glm_vec4_smoothinterpc(vec4 from, vec4 to, float t, vec4 dest);
CGLM_INLINE void glm_vec4_swizzle(vec4 v, int mask, vec4 dest);
DEPRECATED:
@@ -800,13 +807,13 @@ glm_vec4_clamp(vec4 v, float minVal, float maxVal) {
}
/*!
* @brief linear interpolation between two vector
* @brief linear interpolation between two vectors
*
* 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) clamped between 0 and 1
* @param[in] t interpolant (amount)
* @param[out] dest destination
*/
CGLM_INLINE
@@ -815,12 +822,164 @@ glm_vec4_lerp(vec4 from, vec4 to, float t, vec4 dest) {
vec4 s, v;
/* from + s * (to - from) */
glm_vec4_broadcast(glm_clamp_zo(t), s);
glm_vec4_broadcast(t, s);
glm_vec4_sub(to, from, v);
glm_vec4_mul(s, v, v);
glm_vec4_add(from, v, dest);
}
/*!
* @brief linear interpolation between two vectors (clamped)
*
* 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
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_lerpc(vec4 from, vec4 to, float t, vec4 dest) {
glm_vec4_lerp(from, to, glm_clamp_zo(t), dest);
}
/*!
* @brief linear interpolation between two vectors
*
* formula: from + t * (to - from)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount)
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_mix(vec4 from, vec4 to, float t, vec4 dest) {
glm_vec4_lerp(from, to, t, dest);
}
/*!
* @brief linear interpolation between two vectors (clamped)
*
* 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
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_mixc(vec4 from, vec4 to, float t, vec4 dest) {
glm_vec4_lerpc(from, to, t, dest);
}
/*!
* @brief threshold function (unidimensional)
*
* @param[in] edge threshold
* @param[in] x value to test against threshold
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_step_uni(float edge, vec4 x, vec4 dest) {
dest[0] = glm_step(edge, x[0]);
dest[1] = glm_step(edge, x[1]);
dest[2] = glm_step(edge, x[2]);
dest[3] = glm_step(edge, x[3]);
}
/*!
* @brief threshold function
*
* @param[in] edge threshold
* @param[in] x value to test against threshold
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_step(vec4 edge, vec4 x, vec4 dest) {
dest[0] = glm_step(edge[0], x[0]);
dest[1] = glm_step(edge[1], x[1]);
dest[2] = glm_step(edge[2], x[2]);
dest[3] = glm_step(edge[3], x[3]);
}
/*!
* @brief threshold function with a smooth transition (unidimensional)
*
* @param[in] edge0 low threshold
* @param[in] edge1 high threshold
* @param[in] x value to test against threshold
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_smoothstep_uni(float edge0, float edge1, vec4 x, vec4 dest) {
dest[0] = glm_smoothstep(edge0, edge1, x[0]);
dest[1] = glm_smoothstep(edge0, edge1, x[1]);
dest[2] = glm_smoothstep(edge0, edge1, x[2]);
dest[3] = glm_smoothstep(edge0, edge1, x[3]);
}
/*!
* @brief threshold function with a smooth transition
*
* @param[in] edge0 low threshold
* @param[in] edge1 high threshold
* @param[in] x value to test against threshold
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_smoothstep(vec4 edge0, vec4 edge1, vec4 x, vec4 dest) {
dest[0] = glm_smoothstep(edge0[0], edge1[0], x[0]);
dest[1] = glm_smoothstep(edge0[1], edge1[1], x[1]);
dest[2] = glm_smoothstep(edge0[2], edge1[2], x[2]);
dest[3] = glm_smoothstep(edge0[3], edge1[3], x[3]);
}
/*!
* @brief smooth Hermite interpolation between two vectors
*
* formula: t^2 * (3 - 2*t)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount)
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_smoothinterp(vec4 from, vec4 to, float t, vec4 dest) {
vec4 s, v;
/* from + smoothstep * (to - from) */
glm_vec4_broadcast(glm_smooth(t), s);
glm_vec4_sub(to, from, v);
glm_vec4_mul(s, v, v);
glm_vec4_add(from, v, dest);
}
/*!
* @brief smooth Hermite interpolation between two vectors (clamped)
*
* formula: t^2 * (3 - 2*t)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount) clamped between 0 and 1
* @param[out] dest destination
*/
CGLM_INLINE
void
glm_vec4_smoothinterpc(vec4 from, vec4 to, float t, vec4 dest) {
glm_vec4_smoothinterp(from, to, glm_clamp_zo(t), dest);
}
/*!
* @brief helper to fill vec4 as [S^3, S^2, S, 1]
*