27#ifndef GLM_FORCE_RADIANS
28 #define GLM_FORCE_RADIANS
30#ifndef GLM_FORCE_DEPTH_ZERO_TO_ONE
31 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
33#ifndef GLM_ENABLE_EXPERIMENTAL
34 #define GLM_ENABLE_EXPERIMENTAL
42#include <glm/gtc/matrix_transform.hpp>
43#include <glm/gtc/type_ptr.hpp>
44#include <glm/gtc/quaternion.hpp>
45#include <glm/gtx/quaternion.hpp>
46#include <glm/gtx/euler_angles.hpp>
67constexpr float PI = 3.14159265358979323846f;
142inline T
lerp(
const T& a,
const T& b,
float t) {
143 return a + t * (b - a);
155inline T
clamp(
const T& value,
const T& min,
const T& max) {
156 return glm::clamp(value, min, max);
171 return std::abs(a - b) < epsilon;
201inline glm::mat4
ortho(
float left,
float right,
float bottom,
float top,
float nearPlane,
float farPlane) {
205 return glm::orthoRH_NO(left, right, bottom, top, nearPlane, farPlane);
216inline glm::mat4
perspective(
float fov,
float aspect,
float nearPlane,
float farPlane) {
218 return glm::perspectiveRH_NO(fov, aspect, nearPlane, farPlane);
228inline glm::mat4
lookAt(
const glm::vec3& eye,
const glm::vec3& center,
const glm::vec3& up) {
229 return glm::lookAt(eye, center, up);
248inline void decompose(
const glm::mat4& matrix, glm::vec3& position, glm::vec3& rotation, glm::vec3& scale) {
250 position = glm::vec3(matrix[3]);
253 scale.x = glm::length(glm::vec3(matrix[0]));
254 scale.y = glm::length(glm::vec3(matrix[1]));
255 scale.z = glm::length(glm::vec3(matrix[2]));
264 glm::vec3(matrix[0]) / sx,
265 glm::vec3(matrix[1]) / sy,
266 glm::vec3(matrix[2]) / sz
269 rotation.x = std::atan2(rotMatrix[2][1], rotMatrix[2][2]);
270 rotation.y = std::atan2(-rotMatrix[2][0],
271 std::sqrt(rotMatrix[2][1] * rotMatrix[2][1] + rotMatrix[2][2] * rotMatrix[2][2]));
272 rotation.z = std::atan2(rotMatrix[1][0], rotMatrix[0][0]);
288inline void decompose(
const glm::mat4& matrix, glm::vec3& position, glm::quat& rotation, glm::vec3& scale) {
290 position = glm::vec3(matrix[3]);
293 scale.x = glm::length(glm::vec3(matrix[0]));
294 scale.y = glm::length(glm::vec3(matrix[1]));
295 scale.z = glm::length(glm::vec3(matrix[2]));
299 if (matrix[0].z == 0.0f && matrix[1].z == 0.0f && matrix[2].x == 0.0f && matrix[2].y == 0.0f) {
300 glm::vec2 direction{1.0f, 0.0f};
302 direction = glm::vec2(matrix[0]) / scale.x;
303 if (matrix[0].x * matrix[1].y - matrix[0].y * matrix[1].x < 0.0f) scale.y = -scale.y;
304 }
else if (scale.y >
EPSILON) {
305 direction = glm::vec2(matrix[1].y, -matrix[1].x) / scale.y;
307 scale.z = matrix[2].z;
308 rotation = glm::quat_cast(glm::mat3(glm::vec3(direction, 0.0f),
309 glm::vec3(-direction.y, direction.x, 0.0f),
310 glm::vec3(0.0f, 0.0f, 1.0f)));
316 if (glm::determinant(glm::mat3(matrix)) < 0.0f) scale.x = -scale.x;
319 const float sx = std::abs(scale.x) >
EPSILON ? scale.x : std::copysign(
EPSILON, scale.x);
324 glm::vec3(matrix[0]) / sx,
325 glm::vec3(matrix[1]) / sy,
326 glm::vec3(matrix[2]) / sz
329 rotation = glm::quat_cast(rotMatrix);
346 return glm::quat(euler);
359 return glm::eulerAngles(quat);
377 return toDegrees(glm::eulerAngles(quat));
395inline glm::mat4
compose(
const glm::vec3& position,
const glm::quat& rotation,
const glm::vec3& scale) {
396 glm::mat4 result = glm::mat4(1.0f);
397 result = glm::translate(result, position);
398 result *= glm::mat4_cast(rotation);
399 result = glm::scale(result, scale);
410inline glm::mat4
compose(
const glm::vec3& position,
const glm::vec3& rotation,
const glm::vec3& scale) {
411 glm::mat4 result = glm::mat4(1.0f);
412 result = glm::translate(result, position);
413 result = glm::rotate(result, rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
414 result = glm::rotate(result, rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
415 result = glm::rotate(result, rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
416 result = glm::scale(result, scale);
Math constants and utility functions.
glm::vec3 quatToEuler(const glm::quat &quat)
Converts quaternion to Euler angles (radians).
glm::mat4 lookAt(const glm::vec3 &eye, const glm::vec3 ¢er, const glm::vec3 &up)
Creates a look-at view matrix.
T lerp(const T &a, const T &b, float t)
Linear interpolation between two values.
float toRadians(float degrees)
Converts degrees to radians.
T clamp(const T &value, const T &min, const T &max)
Clamps a value between min and max.
constexpr float PI
Pi constant.
bool approxEqual(float a, float b, float epsilon=EPSILON)
Checks if two floats are approximately equal.
glm::quat eulerDegreesToQuat(const glm::vec3 °rees)
Converts Euler angles (degrees) to quaternion.
constexpr float RAD_TO_DEG
Radians to degrees conversion factor.
constexpr float TWO_PI
Two times pi (full circle in radians).
glm::mat4 ortho(float left, float right, float bottom, float top, float nearPlane, float farPlane)
Creates an orthographic projection matrix.
glm::mat4 compose(const glm::vec3 &position, const glm::quat &rotation, const glm::vec3 &scale)
Constructs a transformation matrix from TRS components.
void decompose(const glm::mat4 &matrix, glm::vec3 &position, glm::vec3 &rotation, glm::vec3 &scale)
Decomposes a transformation matrix into components (Euler angles).
float toDegrees(float radians)
Converts radians to degrees.
constexpr float HALF_PI
Half pi (quarter circle in radians).
glm::mat4 perspective(float fov, float aspect, float nearPlane, float farPlane)
Creates a perspective projection matrix.
constexpr float EPSILON
Small epsilon for floating-point comparisons.
glm::vec3 quatToEulerDegrees(const glm::quat &quat)
Converts quaternion to Euler angles (degrees).
constexpr float DEG_TO_RAD
Degrees to radians conversion factor.
glm::quat eulerToQuat(const glm::vec3 &euler)
Converts Euler angles (radians) to quaternion.