SpiecsEngine
 
Loading...
Searching...
No Matches
Math.cpp
Go to the documentation of this file.
1/**
2* @file Math.h.
3* @brief The Math Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
8#include "Math.h"
9
10#define GLM_ENABLE_EXPERIMENTAL
11#include <glm/gtx/matrix_decompose.hpp>
12
13namespace Spices {
14
16 const glm::mat4& transform,
17 glm::vec3& translation,
18 glm::vec3& rotation,
19 glm::vec3& scale
20 )
21 {
22 // From glm::decompose in matrix_decompose.ini
23
24 using namespace glm;
25 using T = float;
26
27 mat4 LocalMatrix(transform);
28
29 // Normalize the matrix;
30 if (epsilonEqual(LocalMatrix[3][3], static_cast<float>(0), epsilon<T>()))
31 {
32 return false;
33 }
34
35 // First, isolate perspective. This is the messiest.
36 if (
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>()))
40 {
41 // Clear the perspective partition
42 LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0);
43 LocalMatrix[3][3] = static_cast<T>(1);
44 }
45
46 // Next take care of translation (easy).
47 translation = vec3(LocalMatrix[3]);
48 LocalMatrix[3] = vec4(0, 0, 0, LocalMatrix[3].w);
49
50 vec3 Row[3], Pdum3;
51
52 // Now get scale and shear
53 for (length_t i = 0; i < 3; ++i)
54 {
55 for (length_t j = 0; j < 3; ++j)
56 {
57 Row[i][j] = LocalMatrix[i][j];
58 }
59 }
60
61 // Compute X scale factor and normalize first row.
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));
68
69 // At this point, the matrix (in rows[]) is orthonormal.
70 // Check for a coordinate system flip. If the determinant.
71 // is -1, then negate the matrix and the scaling factors.
72
73#if 0
74 Pdum3 = cross(Row[1], Row[2]); // v3Cross(row[1], row[2], Pdum3);
75 if (dot(Row[0], Pdum3) < 0)
76 {
77 for (length_t i = 0; i < 3; i++)
78 {
79 scale[i] *= static_cast<T>(-1);
80 Row[i] *= static_cast<T>(-1);
81 }
82 }
83#endif
84
85 rotation.y = asin(-Row[0][2]);
86 if (cos(rotation.y) != 0.0f)
87 {
88 rotation.x = atan2(Row[1][2], Row[2][2]);
89 rotation.z = atan2(Row[0][1], Row[0][0]);
90 }
91 else
92 {
93 rotation.x = atan2(-Row[2][0], Row[1][1]);
94 rotation.z = 0;
95 }
96
97 return false;
98 }
99
101 {
102 const float tanHalfFovy = tan(glm::radians(fov) / 2.0f);
103
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);
108 mat[2][3] = 1.0f;
109 mat[3][2] = -(farPlane * nearPlane) / (farPlane - nearPlane);
110
111 return mat;
112 }
113
114 glm::mat4 OtrhographicMatrix(float left, float right, float top, float bottom, float nearPlane, float farPlane)
115 {
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);
123
124 return mat;
125 }
126
128 {
129 const float tanHalfFovy = tan(glm::radians(fov) / 2.0f);
130
131 glm::mat4 mat = glm::mat4{ 0.0f };
132 mat[0][0] = 1.0f / (aspectRatio * tanHalfFovy);
133 mat[1][1] = 1.0f / tanHalfFovy;
134 mat[2][2] = 0.0f;
135 mat[2][3] = 1.0f;
136 mat[3][2] = nearPlane;
137
138 return mat;
139 }
140}
glm::mat4 OtrhographicMatrix(float left, float right, float top, float bottom, float nearPlane, float farPlane)
Calculate Otrhographic Matrix.
Definition Math.cpp:114
glm::mat4 PerspectiveMatrixInverseZ(float fov, float nearPlane, float aspectRatio)
Calculate Perspective Matrix(reverse z version).
Definition Math.cpp:127
glm::mat4 PerspectiveMatrix(float fov, float nearPlane, float farPlane, float aspectRatio)
Calculate Perspective Matrix.
Definition Math.cpp:100
bool DecomposeTransform(const glm::mat4 &transform, glm::vec3 &outTranslation, glm::vec3 &outRotation, glm::vec3 &outScale)
Decompose matrix to split SRT transform.
Definition Math.cpp:15