vec: lerp for vec3 and vec4

This commit is contained in:
Recep Aslantas
2018-04-10 11:44:16 +03:00
parent 1fb82a1922
commit 416e2f4452
3 changed files with 59 additions and 0 deletions

View File

@@ -143,4 +143,19 @@ glm_clamp(float val, float minVal, float maxVal) {
return glm_min(glm_max(val, minVal), maxVal);
}
/*!
* @brief linear interpolation between two number
*
* formula: from + s * (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_lerp(float from, float to, float t) {
return from + glm_clamp(t, 0.0f, 1.0f) * (to - from);
}
#endif /* cglm_util_h */

View File

@@ -506,6 +506,28 @@ glm_vec_clamp(vec3 v, float minVal, float maxVal) {
v[2] = glm_clamp(v[2], minVal, maxVal);
}
/*!
* @brief linear interpolation between two vector
*
* formula: from + s * (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_vec_lerp(vec3 from, vec3 to, float t, vec3 dest) {
vec3 s, v;
/* from + s * (to - from) */
glm_vec_broadcast(glm_clamp(t, 0.0f, 1.0f), s);
glm_vec_sub(to, from, v);
glm_vec_mulv(s, v, v);
glm_vec_add(from, v, dest);
}
/*!
* @brief vec3 cross product
*

View File

@@ -417,4 +417,26 @@ glm_vec4_clamp(vec4 v, float minVal, float maxVal) {
v[3] = glm_clamp(v[3], minVal, maxVal);
}
/*!
* @brief linear interpolation between two vector
*
* formula: from + s * (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_lerp(vec4 from, vec4 to, float t, vec4 dest) {
vec4 s, v;
/* from + s * (to - from) */
glm_vec4_broadcast(glm_clamp(t, 0.0f, 1.0f), s);
glm_vec4_sub(to, from, v);
glm_vec4_mulv(s, v, v);
glm_vec4_add(from, v, dest);
}
#endif /* cglm_vec4_h */