From 564324f5d21e5d6a93469df503f28061b1bb73bd Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Sun, 10 Jun 2018 10:29:02 +0300 Subject: [PATCH 1/4] easing functions --- include/cglm/call.h | 1 + include/cglm/call/ease.h | 140 +++++++++++++++++ include/cglm/cglm.h | 1 + include/cglm/ease.h | 318 +++++++++++++++++++++++++++++++++++++++ src/ease.c | 195 ++++++++++++++++++++++++ 5 files changed, 655 insertions(+) create mode 100644 include/cglm/call/ease.h create mode 100644 include/cglm/ease.h create mode 100644 src/ease.c diff --git a/include/cglm/call.h b/include/cglm/call.h index 77148ac..b7fa6e1 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -26,6 +26,7 @@ extern "C" { #include "call/io.h" #include "call/project.h" #include "call/sphere.h" +#include "call/ease.h" #ifdef __cplusplus } diff --git a/include/cglm/call/ease.h b/include/cglm/call/ease.h new file mode 100644 index 0000000..9f1757e --- /dev/null +++ b/include/cglm/call/ease.h @@ -0,0 +1,140 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglmc_ease_h +#define cglmc_ease_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +float +glmc_ease_linear(float t); + +CGLM_EXPORT +float +glmc_ease_sine_in(float t); + +CGLM_EXPORT +float +glmc_ease_sine_out(float t); + +CGLM_EXPORT +float +glmc_ease_sine_inout(float t); + +CGLM_EXPORT +float +glmc_ease_quad_in(float t); + +CGLM_EXPORT +float +glmc_ease_quad_out(float t); + +CGLM_EXPORT +float +glmc_ease_quad_inout(float t); + +CGLM_EXPORT +float +glmc_ease_cubic_in(float t); + +CGLM_EXPORT +float +glmc_ease_cubic_out(float t); + +CGLM_EXPORT +float +glmc_ease_cubic_inout(float t); + +CGLM_EXPORT +float +glmc_ease_quart_in(float t); + +CGLM_EXPORT +float +glmc_ease_quart_out(float t); + +CGLM_EXPORT +float +glmc_ease_quart_inout(float t); + +CGLM_EXPORT +float +glmc_ease_quint_in(float t); + +CGLM_EXPORT +float +glmc_ease_quint_out(float t); + +CGLM_EXPORT +float +glmc_ease_quint_inout(float t); + +CGLM_EXPORT +float +glmc_ease_exp_in(float t); + +CGLM_EXPORT +float +glmc_ease_exp_out(float t); + +CGLM_EXPORT +float +glmc_ease_exp_inout(float t); + +CGLM_EXPORT +float +glmc_ease_circ_in(float t); + +CGLM_EXPORT +float +glmc_ease_circ_out(float t); + +CGLM_EXPORT +float +glmc_ease_circ_inout(float t); + +CGLM_EXPORT +float +glmc_ease_back_in(float t); + +CGLM_EXPORT +float +glmc_ease_back_out(float t); + +CGLM_EXPORT +float +glmc_ease_back_inout(float t); + +CGLM_EXPORT +float +glmc_ease_elast_in(float t); + +CGLM_EXPORT +float +glmc_ease_elast_out(float t); + +CGLM_EXPORT +float +glmc_ease_elast_inout(float t); + +CGLM_EXPORT +float +glmc_ease_bounce_out(float t); + +CGLM_EXPORT +float +glmc_ease_bounce_in(float t); + +CGLM_EXPORT +float +glmc_ease_bounce_inout(float t); + +#endif /* cglmc_ease_h */ diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index 720ae08..8b37162 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -25,5 +25,6 @@ #include "io.h" #include "project.h" #include "sphere.h" +#include "ease.h" #endif /* cglm_h */ diff --git a/include/cglm/ease.h b/include/cglm/ease.h new file mode 100644 index 0000000..fca9691 --- /dev/null +++ b/include/cglm/ease.h @@ -0,0 +1,318 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_ease_h +#define cglm_ease_h + +#include "common.h" + +CGLM_INLINE +float +glm_ease_linear(float t) { + return t; +} + +CGLM_INLINE +float +glm_ease_sine_in(float t) { + return sinf((t - 1.0f) * CGLM_PI_2) + 1.0f; +} + +CGLM_INLINE +float +glm_ease_sine_out(float t) { + return sinf(t * CGLM_PI_2); +} + +CGLM_INLINE +float +glm_ease_sine_inout(float t) { + return 0.5f * (1.0f - cosf(t * CGLM_PI)); +} + +CGLM_INLINE +float +glm_ease_quad_in(float t) { + return t * t; +} + +CGLM_INLINE +float +glm_ease_quad_out(float t) { + return -(t * (t - 2.0f)); +} + +CGLM_INLINE +float +glm_ease_quad_inout(float t) { + float tt; + + tt = t * t; + if (t < 0.5f) + return 2.0f * tt; + + return (-2.0f * tt) + (4.0f * t) - 1.0f; +} + +CGLM_INLINE +float +glm_ease_cubic_in(float t) { + return t * t * t; +} + +CGLM_INLINE +float +glm_ease_cubic_out(float t) { + float f; + f = t - 1.0f; + return f * f * f + 1.0f; +} + +CGLM_INLINE +float +glm_ease_cubic_inout(float t) { + float f; + + if (t < 0.5f) + return 4.0f * t * t * t; + + f = 2.0f * t - 2.0f; + + return 0.5f * f * f * f + 1.0f; +} + +CGLM_INLINE +float +glm_ease_quart_in(float t) { + float f; + f = t * t; + return f * f; +} + +CGLM_INLINE +float +glm_ease_quart_out(float t) { + float f; + + f = t - 1.0f; + + return f * f * f * (1.0f - t) + 1.0f; +} + +CGLM_INLINE +float +glm_ease_quart_inout(float t) { + float f, g; + + if (t < 0.5f) { + f = t * t; + return 8.0f * f * f; + } + + f = t - 1.0f; + g = f * f; + + return -8.0f * g * g + 1.0f; +} + +CGLM_INLINE +float +glm_ease_quint_in(float t) { + float f; + f = t * t; + return f * f * t; +} + +CGLM_INLINE +float +glm_ease_quint_out(float t) { + float f, g; + + f = t - 1.0f; + g = f * f; + + return g * g * f + 1.0f; +} + +CGLM_INLINE +float +glm_ease_quint_inout(float t) { + float f, g; + + if (t < 0.5f) { + f = t * t; + return 16.0f * f * f * f; + } + + f = 2.0f * t - 2.0f; + g = f * f; + + return 0.5f * g * g * f + 1.0f; +} + +CGLM_INLINE +float +glm_ease_exp_in(float t) { + float c; + + if (t <= 0.0f) + return t; + + c = t - 1.0f; + + return powf(2.0f, c * 10.0f); +} + +CGLM_INLINE +float +glm_ease_exp_out(float t) { + if (t >= 1.0f) + return t; + + return 1.0f - powf(2.0f, -10.0f * t); +} + +CGLM_INLINE +float +glm_ease_exp_inout(float t) { + if (t < 0.5f) + return 0.5f * powf(2.0f, (20.0f * t) - 10.0f); + + return -0.5f * powf(2.0f, (-20.0f * t) + 10.0f) + 1.0f; +} + +CGLM_INLINE +float +glm_ease_circ_in(float t) { + return 1.0f - sqrtf(1.0f - (t * t)); +} + +CGLM_INLINE +float +glm_ease_circ_out(float t) { + return sqrtf((2.0f - t) * t); +} + +CGLM_INLINE +float +glm_ease_circ_inout(float t) { + if (t < 0.5f) + return 0.5f * (1.0f - sqrtf(1.0f - 4.0f * (t * t))); + + return 0.5f * (sqrtf(-((2.0f * t) - 3.0f) * ((2.0f * t) - 1.0f)) + 1.0f); +} + +CGLM_INLINE +float +glm_ease_back_in(float t) { + float o, z; + + o = 1.70158f; + z = ((o + 1.0f) * t) - o; + + return t * t * z; +} + +CGLM_INLINE +float +glm_ease_back_out(float t) { + float o, z, n; + + o = 1.70158f; + n = t - 1.0f; + z = (o + 1.0f) * n + o; + + return n * n * z + 1.0f; +} + +CGLM_INLINE +float +glm_ease_back_inout(float t) { + float o, z, n, m, s, x; + + o = 1.70158f; + s = o * 1.525f; + x = 0.5; + n = t / 0.5f; + + if (n < 1) { + z = ((s + 1) * n) - s; + m = n * n * z; + return x * m; + } + + n -= 2; + z = ((s + 1) * n) + s; + m = (n * n * z) + 2; + + return x * m; +} + +CGLM_INLINE +float +glm_ease_elast_in(float t) { + return sinf(13.0f * CGLM_PI_2 * t) * powf(2.0f, 10.0f * (t - 1.0f)); +} + +CGLM_INLINE +float +glm_ease_elast_out(float t) { + return sinf(-13.0f * CGLM_PI_2 * (t + 1.0f)) * powf(2.0f, -10.0f * t) + 1.0f; +} + +CGLM_INLINE +float +glm_ease_elast_inout(float t) { + float a; + + a = 2.0f * t; + + if (t < 0.5f) + return 0.5f * sinf(13.0f * CGLM_PI_2 * (a)) + * powf(2.0f, 10.0f * ((a) - 1.0f)); + + return 0.5f * (sinf(-13.0f * CGLM_PI_2 * ((a - 1.0f) + 1.0f)) + * powf(2.0f, -10.0f * (a - 1.0f)) + 2.0f); +} + +CGLM_INLINE +float +glm_ease_bounce_out(float t) { + float tt; + + tt = t * t; + + if (t < (4.0f / 11.0f)) + return (121.0f * tt) / 16.0f; + + if (t < 8.0f / 11.0f) + return ((363.0f / 40.0f) * tt) - ((99.0f / 10.0f) * t) + (17.0f / 5.0f); + + if (t < (9.0f / 10.0f)) + return (4356.0f / 361.0f) * tt + - (35442.0f / 1805.0f) * t + + (16061.0f / 1805.0f); + + return ((54.0f / 5.0f) * tt) - ((513.0f / 25.0f) * t) + (268.0f / 25.0f); +} + +CGLM_INLINE +float +glm_ease_bounce_in(float t) { + return 1.0f - glm_ease_bounce_out(1.0f - t); +} + +CGLM_INLINE +float +glm_ease_bounce_inout(float t) { + if (t < 0.5f) + return 0.5f * (1.0f - glm_ease_bounce_out(t * 2.0f)); + + return 0.5f * glm_ease_bounce_out(t * 2.0f - 1.0f) + 0.5f; +} + +#endif /* cglm_ease_h */ diff --git a/src/ease.c b/src/ease.c new file mode 100644 index 0000000..702dfce --- /dev/null +++ b/src/ease.c @@ -0,0 +1,195 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "../include/cglm/cglm.h" +#include "../include/cglm/call.h" + +CGLM_EXPORT +float +glmc_ease_linear(float t) { + return glm_ease_linear(t); +} + +CGLM_EXPORT +float +glmc_ease_sine_in(float t) { + return glm_ease_sine_in(t); +} + +CGLM_EXPORT +float +glmc_ease_sine_out(float t) { + return glm_ease_sine_out(t); +} + +CGLM_EXPORT +float +glmc_ease_sine_inout(float t) { + return glm_ease_sine_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_quad_in(float t) { + return glm_ease_quad_in(t); +} + +CGLM_EXPORT +float +glmc_ease_quad_out(float t) { + return glm_ease_quad_out(t); +} + +CGLM_EXPORT +float +glmc_ease_quad_inout(float t) { + return glm_ease_quad_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_cubic_in(float t) { + return glm_ease_cubic_in(t); +} + +CGLM_EXPORT +float +glmc_ease_cubic_out(float t) { + return glm_ease_cubic_out(t); +} + +CGLM_EXPORT +float +glmc_ease_cubic_inout(float t) { + return glm_ease_cubic_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_quart_in(float t) { + return glm_ease_quart_in(t); +} + +CGLM_EXPORT +float +glmc_ease_quart_out(float t) { + return glm_ease_quart_out(t); +} + +CGLM_EXPORT +float +glmc_ease_quart_inout(float t) { + return glm_ease_quart_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_quint_in(float t) { + return glm_ease_quint_in(t); +} + +CGLM_EXPORT +float +glmc_ease_quint_out(float t) { + return glm_ease_quint_out(t); +} + +CGLM_EXPORT +float +glmc_ease_quint_inout(float t) { + return glm_ease_quint_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_exp_in(float t) { + return glm_ease_exp_in(t); +} + +CGLM_EXPORT +float +glmc_ease_exp_out(float t) { + return glm_ease_exp_out(t); +} + +CGLM_EXPORT +float +glmc_ease_exp_inout(float t) { + return glm_ease_exp_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_circ_in(float t) { + return glm_ease_circ_in(t); +} + +CGLM_EXPORT +float +glmc_ease_circ_out(float t) { + return glm_ease_circ_out(t); +} + +CGLM_EXPORT +float +glmc_ease_circ_inout(float t) { + return glm_ease_circ_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_back_in(float t) { + return glm_ease_back_in(t); +} + +CGLM_EXPORT +float +glmc_ease_back_out(float t) { + return glm_ease_back_out(t); +} + +CGLM_EXPORT +float +glmc_ease_back_inout(float t) { + return glm_ease_back_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_elast_in(float t) { + return glm_ease_elast_in(t); +} + +CGLM_EXPORT +float +glmc_ease_elast_out(float t) { + return glm_ease_elast_out(t); +} + +CGLM_EXPORT +float +glmc_ease_elast_inout(float t) { + return glm_ease_elast_inout(t); +} + +CGLM_EXPORT +float +glmc_ease_bounce_out(float t) { + return glm_ease_bounce_out(t); +} + +CGLM_EXPORT +float +glmc_ease_bounce_in(float t) { + return glm_ease_bounce_in(t); +} + +CGLM_EXPORT +float +glmc_ease_bounce_inout(float t) { + return glm_ease_bounce_inout(t); +} From 02f6c67393355b6e76d944e241da27771ccc6341 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Fri, 15 Jun 2018 08:55:59 +0300 Subject: [PATCH 2/4] improve easing funcs --- include/cglm/ease.h | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/include/cglm/ease.h b/include/cglm/ease.h index fca9691..30ecf9b 100644 --- a/include/cglm/ease.h +++ b/include/cglm/ease.h @@ -145,7 +145,7 @@ glm_ease_quint_inout(float t) { if (t < 0.5f) { f = t * t; - return 16.0f * f * f * f; + return 16.0f * f * f * t; } f = 2.0f * t - 2.0f; @@ -157,20 +157,16 @@ glm_ease_quint_inout(float t) { CGLM_INLINE float glm_ease_exp_in(float t) { - float c; - - if (t <= 0.0f) + if (t == 0.0f) return t; - c = t - 1.0f; - - return powf(2.0f, c * 10.0f); + return powf(2.0f, 10.0f * (t - 1.0f)); } CGLM_INLINE float glm_ease_exp_out(float t) { - if (t >= 1.0f) + if (t == 1.0f) return t; return 1.0f - powf(2.0f, -10.0f * t); @@ -179,6 +175,9 @@ glm_ease_exp_out(float t) { CGLM_INLINE float glm_ease_exp_inout(float t) { + if (t == 0.0f || t == 1.0f) + return t; + if (t < 0.5f) return 0.5f * powf(2.0f, (20.0f * t) - 10.0f); @@ -239,14 +238,14 @@ glm_ease_back_inout(float t) { x = 0.5; n = t / 0.5f; - if (n < 1) { - z = ((s + 1) * n) - s; + if (n < 1.0f) { + z = (s + 1) * n - s; m = n * n * z; return x * m; } - n -= 2; - z = ((s + 1) * n) + s; + n -= 2.0f; + z = (s + 1.0f) * n + s; m = (n * n * z) + 2; return x * m; @@ -272,10 +271,10 @@ glm_ease_elast_inout(float t) { a = 2.0f * t; if (t < 0.5f) - return 0.5f * sinf(13.0f * CGLM_PI_2 * (a)) - * powf(2.0f, 10.0f * ((a) - 1.0f)); + return 0.5f * sinf(13.0f * CGLM_PI_2 * a) + * powf(2.0f, 10.0f * (a - 1.0f)); - return 0.5f * (sinf(-13.0f * CGLM_PI_2 * ((a - 1.0f) + 1.0f)) + return 0.5f * (sinf(-13.0f * CGLM_PI_2 * a) * powf(2.0f, -10.0f * (a - 1.0f)) + 2.0f); } From 669777eb37eaffd6079ba06893b87ccf1a352183 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Mon, 18 Jun 2018 17:55:25 +0300 Subject: [PATCH 3/4] additional utils --- docs/source/util.rst | 35 +++++++++++++++++++++++++++++++++++ include/cglm/util.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/docs/source/util.rst b/docs/source/util.rst index f8dbac4..846934f 100644 --- a/docs/source/util.rst +++ b/docs/source/util.rst @@ -136,3 +136,38 @@ Functions documentation Returns: interpolated value + +.. c:function:: bool glm_eq(float a, float b) + + check if two float equal with using EPSILON + + Parameters: + | *[in]* **a** a + | *[in]* **b** b + + Returns: + true if a and b equals + +.. c:function:: float glm_percent(float from, float to, float current) + + percentage of current value between start and end value + + Parameters: + | *[in]* **from** from value + | *[in]* **to** to value + | *[in]* **current** value between from and to values + + Returns: + clamped normalized percent (0-100 in 0-1) + +.. c:function:: float glm_percentc(float from, float to, float current) + + clamped percentage of current value between start and end value + + Parameters: + | *[in]* **from** from value + | *[in]* **to** to value + | *[in]* **current** value between from and to values + + Returns: + clamped normalized percent (0-100 in 0-1) diff --git a/include/cglm/util.h b/include/cglm/util.h index af7514c..7a7b004 100644 --- a/include/cglm/util.h +++ b/include/cglm/util.h @@ -19,6 +19,7 @@ #define cglm_util_h #include "common.h" +#include /*! * @brief get sign of 32 bit integer as +1, -1, 0 @@ -157,4 +158,44 @@ glm_lerp(float from, float to, float t) { return from + glm_clamp(t, 0.0f, 1.0f) * (to - from); } +/*! + * @brief check if two float equal with using EPSILON + * + * @param[in] a a + * @param[in] b b + */ +CGLM_INLINE +bool +glm_eq(float a, float b) { + return fabsf(a - b) <= FLT_EPSILON; +} + +/*! + * @brief percentage of current value between start and end value + * + * maybe fraction could be alternative name. + * + * @param[in] from from value + * @param[in] to to value + * @param[in] t current value + */ +CGLM_INLINE +float +glm_percent(float from, float to, float current) { + return (current - from) / (to - from); +} + +/*! + * @brief clamped percentage of current value between start and end value + * + * @param[in] from from value + * @param[in] to to value + * @param[in] t current value + */ +CGLM_INLINE +float +glm_percentc(float from, float to, float current) { + return glm_clamp(glm_percent(from, to, current), 0.0f, 1.0f); +} + #endif /* cglm_util_h */ From 3adeac06f88a2cf3c1465c4ddb074ca59f78fc8a Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Thu, 21 Jun 2018 10:07:51 +0300 Subject: [PATCH 4/4] update build files --- makefile.am | 9 ++++++--- win/cglm.vcxproj | 3 +++ win/cglm.vcxproj.filters | 9 +++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/makefile.am b/makefile.am index 7ee0904..2e9336c 100644 --- a/makefile.am +++ b/makefile.am @@ -56,7 +56,8 @@ cglm_HEADERS = include/cglm/version.h \ include/cglm/box.h \ include/cglm/color.h \ include/cglm/project.h \ - include/cglm/sphere.h + include/cglm/sphere.h \ + include/cglm/ease.h cglm_calldir=$(includedir)/cglm/call cglm_call_HEADERS = include/cglm/call/mat4.h \ @@ -72,7 +73,8 @@ cglm_call_HEADERS = include/cglm/call/mat4.h \ include/cglm/call/frustum.h \ include/cglm/call/box.h \ include/cglm/call/project.h \ - include/cglm/call/sphere.h + include/cglm/call/sphere.h \ + include/cglm/call/ease.h cglm_simddir=$(includedir)/cglm/simd cglm_simd_HEADERS = include/cglm/simd/intrin.h @@ -104,7 +106,8 @@ libcglm_la_SOURCES=\ src/frustum.c \ src/box.c \ src/project.c \ - src/sphere.c + src/sphere.c \ + src/ease.c test_tests_SOURCES=\ test/src/test_common.c \ diff --git a/win/cglm.vcxproj b/win/cglm.vcxproj index 556a24e..5678688 100644 --- a/win/cglm.vcxproj +++ b/win/cglm.vcxproj @@ -23,6 +23,7 @@ + @@ -43,6 +44,7 @@ + @@ -58,6 +60,7 @@ + diff --git a/win/cglm.vcxproj.filters b/win/cglm.vcxproj.filters index cf7634b..5e65853 100644 --- a/win/cglm.vcxproj.filters +++ b/win/cglm.vcxproj.filters @@ -81,6 +81,9 @@ src + + src + @@ -224,5 +227,11 @@ include\cglm + + include\cglm\call + + + include\cglm + \ No newline at end of file