2
3
4
5
10#define GLM_ENABLE_EXPERIMENTAL
11#include <glm/gtx/matrix_decompose.hpp>
16 const glm::mat4& transform,
17 glm::vec3& translation,
27 mat4 LocalMatrix(transform);
30 if (epsilonEqual(LocalMatrix[3][3],
static_cast<
float>(0), epsilon<T>()))
37 epsilonNotEqual(LocalMatrix[0][3],
static_cast<T>(0), epsilon<T>()) ||
38 epsilonNotEqual(LocalMatrix[1][3],
static_cast<T>(0), epsilon<T>()) ||
39 epsilonNotEqual(LocalMatrix[2][3],
static_cast<T>(0), epsilon<T>()))
42 LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] =
static_cast<T>(0);
43 LocalMatrix[3][3] =
static_cast<T>(1);
47 translation = vec3(LocalMatrix[3]);
48 LocalMatrix[3] = vec4(0, 0, 0, LocalMatrix[3].w);
53 for (length_t i = 0; i < 3; ++i)
55 for (length_t j = 0; j < 3; ++j)
57 Row[i][j] = LocalMatrix[i][j];
62 scale.x = length(Row[0]);
63 Row[0] = detail::scale(Row[0],
static_cast<T>(1));
64 scale.y = length(Row[1]);
65 Row[1] = detail::scale(Row[1],
static_cast<T>(1));
66 scale.z = length(Row[2]);
67 Row[2] = detail::scale(Row[2],
static_cast<T>(1));
74 Pdum3 = cross(Row[1], Row[2]);
75 if (dot(Row[0], Pdum3) < 0)
77 for (length_t i = 0; i < 3; i++)
79 scale[i] *=
static_cast<T>(-1);
80 Row[i] *=
static_cast<T>(-1);
85 rotation.y = asin(-Row[0][2]);
86 if (cos(rotation.y) != 0.0f)
88 rotation.x = atan2(Row[1][2], Row[2][2]);
89 rotation.z = atan2(Row[0][1], Row[0][0]);
93 rotation.x = atan2(-Row[2][0], Row[1][1]);
102 const float tanHalfFovy = tan(glm::radians(fov) / 2.0f);
104 glm::mat4 mat = glm::mat4{ 0.0f };
105 mat[0][0] = 1.0f / (aspectRatio * tanHalfFovy);
106 mat[1][1] = 1.0f / tanHalfFovy;
107 mat[2][2] = farPlane / (farPlane - nearPlane);
109 mat[3][2] = -(farPlane * nearPlane) / (farPlane - nearPlane);
116 glm::mat4 mat = glm::mat4{ 1.0f };
117 mat[0][0] = 2.0f / (right - left);
118 mat[1][1] = 2.0f / (bottom - top);
119 mat[2][2] = 1.0f / (farPlane - nearPlane);
120 mat[3][0] = -(right + left) / (right - left);
121 mat[3][1] = -(bottom + top) / (bottom - top);
122 mat[3][2] = -nearPlane / (farPlane - nearPlane);
129 const float tanHalfFovy = tan(glm::radians(fov) / 2.0f);
131 glm::mat4 mat = glm::mat4{ 0.0f };
132 mat[0][0] = 1.0f / (aspectRatio * tanHalfFovy);
133 mat[1][1] = 1.0f / tanHalfFovy;
136 mat[3][2] = nearPlane;
glm::mat4 OtrhographicMatrix(float left, float right, float top, float bottom, float nearPlane, float farPlane)
Calculate Otrhographic Matrix.
glm::mat4 PerspectiveMatrixInverseZ(float fov, float nearPlane, float aspectRatio)
Calculate Perspective Matrix(reverse z version).
glm::mat4 PerspectiveMatrix(float fov, float nearPlane, float farPlane, float aspectRatio)
Calculate Perspective Matrix.
bool DecomposeTransform(const glm::mat4 &transform, glm::vec3 &outTranslation, glm::vec3 &outRotation, glm::vec3 &outScale)
Decompose matrix to split SRT transform.