From a8b2bc2d010480544c3162daa7384a252946bda1 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 14 May 2024 21:57:51 -0400 Subject: [PATCH] easing functions --- CMakeLists.txt | 2 +- include/blt/math/interpolation.h | 109 +++++++++++++++++++++++++++++++ include/blt/math/vectors.h | 6 -- 3 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 include/blt/math/interpolation.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 62fa748..2707ed0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.17.9) +set(BLT_VERSION 0.17.10) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/interpolation.h b/include/blt/math/interpolation.h new file mode 100644 index 0000000..1dfeb3b --- /dev/null +++ b/include/blt/math/interpolation.h @@ -0,0 +1,109 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_INTERPOLATION_H +#define BLT_INTERPOLATION_H + +#include "vectors.h" + +namespace blt +{ + inline constexpr color4 linear_interpolate(const color4& in, const color4& desired, float factor) + { + auto diff = desired - in; + return in + (diff * factor); + } + + class easing_function + { + public: + easing_function() = default; + + virtual color4 apply(const color4& start, const color4& end) = 0; + + void progress(float progress) + { + total_progress += progress; + } + + void reset() + { + total_progress = 0; + } + + protected: + float x() + { + return total_progress; + } + + private: + float total_progress = 0; + }; + + class quad_easing : public easing_function + { + public: + color4 apply(const color4& start, const color4& end) final + { + if (x() >= 1) + return end; + auto diff = end - start; + return start + (diff * (x() * x())); + } + }; + + class cubic_easing : public easing_function + { + public: + color4 apply(const color4& start, const color4& end) final + { + if (x() >= 1) + return end; + auto diff = end - start; + return start + (diff * (x() * x() * x())); + } + }; + + class quart_easing : public easing_function + { + public: + color4 apply(const color4& start, const color4& end) final + { + if (x() >= 1) + return end; + auto diff = end - start; + return start + (diff * (x() * x() * x() * x())); + } + }; + + class quint_easing : public easing_function + { + public: + color4 apply(const color4& start, const color4& end) final + { + if (x() >= 1) + return end; + auto diff = end - start; + return start + (diff * (x() * x() * x() * x() * x())); + } + }; + +} + +#endif //BLT_INTERPOLATION_H diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index d83d397..ade8c32 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -433,12 +433,6 @@ namespace blt using color4 = vec4; using color3 = vec3; - inline constexpr color4 interpolate(const color4& in, const color4& desired, float factor) - { - auto diff = desired - in; - return in + (diff * factor); - } - inline constexpr color4 make_color(float r, float g, float b) { return color4{r, g, b, 1.0f};