mirror of
https://github.com/barkeser2002/flower.git
synced 2026-09-25 20:56:34 +03:00
104 lines
2.8 KiB
C++
104 lines
2.8 KiB
C++
#pragma once
|
|
|
|
// glm math.
|
|
// 0. glm force compute on radians.
|
|
// 1. glm vulkan depth force 0 to 1.
|
|
// 2. glm enable experimental.
|
|
#define GLM_FORCE_RADIANS
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
#define GLM_ENABLE_EXPERIMENTAL
|
|
|
|
// Common glm headers
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtx/hash.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/matrix_inverse.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <glm/gtx/euler_angles.hpp>
|
|
#include <glm/gtx/quaternion.hpp>
|
|
#include <glm/gtx/matrix_decompose.hpp>
|
|
|
|
namespace engine
|
|
{
|
|
// namespace alias to ensure all glm header under this file's macro control.
|
|
namespace math = glm;
|
|
|
|
// From https://github.com/TheCherno/Hazel/blob/master/Hazel/src/Hazel/Math/Math.cpp
|
|
inline bool decomposeTransform(const math::mat4& transform, math::vec3& translation, math::vec3& rotation, math::vec3& scale)
|
|
{
|
|
using namespace math;
|
|
using T = float;
|
|
|
|
mat4 LocalMatrix(transform);
|
|
|
|
// Normalize the matrix.
|
|
if (epsilonEqual(LocalMatrix[3][3], static_cast<float>(0), epsilon<T>()))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// First, isolate perspective. This is the messiest.
|
|
if (
|
|
epsilonNotEqual(LocalMatrix[0][3], static_cast<T>(0), epsilon<T>()) ||
|
|
epsilonNotEqual(LocalMatrix[1][3], static_cast<T>(0), epsilon<T>()) ||
|
|
epsilonNotEqual(LocalMatrix[2][3], static_cast<T>(0), epsilon<T>()))
|
|
{
|
|
// Clear the perspective partition
|
|
LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0);
|
|
LocalMatrix[3][3] = static_cast<T>(1);
|
|
}
|
|
|
|
// Next take care of translation (easy).
|
|
translation = vec3(LocalMatrix[3]);
|
|
LocalMatrix[3] = vec4(0, 0, 0, LocalMatrix[3].w);
|
|
|
|
vec3 Row[3]{};//, Pdum3;
|
|
|
|
// Now get scale and shear.
|
|
for (length_t i = 0; i < 3; ++i)
|
|
{
|
|
for (length_t j = 0; j < 3; ++j)
|
|
{
|
|
Row[i][j] = LocalMatrix[i][j];
|
|
}
|
|
}
|
|
|
|
// Compute X scale factor and normalize first row.
|
|
scale.x = length(Row[0]);
|
|
Row[0] = detail::scale(Row[0], static_cast<T>(1));
|
|
scale.y = length(Row[1]);
|
|
Row[1] = detail::scale(Row[1], static_cast<T>(1));
|
|
scale.z = length(Row[2]);
|
|
Row[2] = detail::scale(Row[2], static_cast<T>(1));
|
|
|
|
// At this point, the matrix (in rows[]) is orthonormal.
|
|
// Check for a coordinate system flip. If the determinant
|
|
// is -1, then negate the matrix and the scaling factors.
|
|
#if 0
|
|
Pdum3 = cross(Row[1], Row[2]); // v3Cross(row[1], row[2], Pdum3);
|
|
if (dot(Row[0], Pdum3) < 0)
|
|
{
|
|
for (length_t i = 0; i < 3; i++)
|
|
{
|
|
scale[i] *= static_cast<T>(-1);
|
|
Row[i] *= static_cast<T>(-1);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
rotation.y = asin(-Row[0][2]);
|
|
if (cos(rotation.y) != 0.f)
|
|
{
|
|
rotation.x = atan2(Row[1][2], Row[2][2]);
|
|
rotation.z = atan2(Row[0][1], Row[0][0]);
|
|
}
|
|
else
|
|
{
|
|
rotation.x = atan2(-Row[2][0], Row[1][1]);
|
|
rotation.z = 0;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |