AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
viscous_fluid.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <glm/glm.hpp>
   15
   16namespace aui::animation_curves {
   17    class ViscousFluid {
   18    public:
   19        float operator()(float input);
   20
   21    private:
   22        // controls the viscous fluid effect (how much of it).
   23        constexpr static float VISCOUS_FLUID_SCALE = 8.0f;
   24
   25        constexpr static float viscousFluid(float x) {
   26            x *= VISCOUS_FLUID_SCALE;
   27            if (x < 1.0f) {
   28                x -= (1.0f - (float)glm::exp(-x));
   29            } else {
   30                float start = 0.36787944117f;   // 1/e == exp(-1)
   31                x = 1.0f - (float)glm::exp(1.0f - x);
   32                x = start + x * (1.0f - start);
   33            }
   34            return x;
   35        }
   36    };
   37
   38    inline float ViscousFluid::operator()(float input) {
   39
   40        // must be set to 1.0 (used in viscousFluid())
   41        const float VISCOUS_FLUID_NORMALIZE = 1.0f / viscousFluid(1.0f);
   42
   43        // account for very small floating-point error
   44        const float VISCOUS_FLUID_OFFSET = 1.0f - VISCOUS_FLUID_NORMALIZE * viscousFluid(1.0f);
   45
   46        float interpolated = VISCOUS_FLUID_NORMALIZE * viscousFluid(input);
   47        if (interpolated > 0) {
   48            return interpolated + VISCOUS_FLUID_OFFSET;
   49        }
   50        return interpolated;
   51    }
   52}
Definition viscous_fluid.h:17