Files
CDAG/Research/core/MathHelper.h

40 lines
1.2 KiB
C++

#pragma once
#include "Defines.h"
#include "../inc/glm/glm.hpp"
class MathHelper
{
public:
// Given a time (t between 0 and 1) and two values, linearly interpolates between the values
template<typename T>
static T lerp(float t, T a, T b)
{
return ((T(1) - t) * a) + (b * t);
}
// Spherical linear interpolation of two vectors (start and end)
static glm::vec3 slerp(float t, glm::vec3 a, glm::vec3 b)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = glm::dot(a, b);
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
glm::clamp(dot, -1.0f, 1.0f);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = glm::acos(dot) * t;
glm::vec3 relativeVec = glm::normalize(b - a * dot); // Orthonormal basis
// The final result.
return ((a * glm::cos(theta)) + (relativeVec * glm::sin(theta)));
}
template<typename T>
inline static T DivideRoundUp(T numerator, T denominator)
{
return (numerator / denominator) + (numerator % denominator == 0 ? 0 : 1);
}
};