15 #include <vsg/maths/common.h>
21 constexpr t_mat4<T> rotate(
const t_quat<T>& q)
37 return t_mat4<T>(one - two * (qyy + qzz), two * (qxy + qwz), two * (qxz - qwy), zero,
38 two * (qxy - qwz), one - two * (qxx + qzz), two * (qyz + qwx), zero,
39 two * (qxz + qwy), two * (qyz - qwx), one - two * (qxx + qyy), zero,
40 zero, zero, zero, 1.0);
45 t_mat4<T> rotate(T angle_radians, T x, T y, T z)
47 const T c = std::cos(angle_radians);
48 const T s = std::sin(angle_radians);
49 const T one_minus_c = 1 - c;
50 return t_mat4<T>(x * x * one_minus_c + c, y * x * one_minus_c + z * s, x * z * one_minus_c - y * s, 0,
51 x * y * one_minus_c - z * s, y * y * one_minus_c + c, y * z * one_minus_c + x * s, 0,
52 x * z * one_minus_c + y * s, y * z * one_minus_c - x * s, z * z * one_minus_c + c, 0,
58 t_mat4<T> rotate(T angle_radians,
const t_vec3<T>& v)
60 return rotate(angle_radians, v.value[0], v.value[1], v.value[2]);
65 constexpr t_mat4<T> translate(T x, T y, T z)
67 return t_mat4<T>(1, 0, 0, 0,
75 constexpr t_mat4<T> translate(
const t_vec3<T>& v)
77 return translate(v.value[0], v.value[1], v.value[2]);
82 constexpr t_mat4<T> scale(T s)
84 return t_mat4<T>(s, 0, 0, 0,
92 constexpr t_mat4<T> scale(T sx, T sy, T sz)
94 return t_mat4<T>(sx, 0, 0, 0,
102 constexpr t_mat4<T> scale(
const t_vec3<T>& v)
104 return scale(v.value[0], v.value[1], v.value[2]);
109 constexpr t_mat3<T> transpose(
const t_mat3<T>& m)
111 return t_mat3<T>(m[0][0], m[1][0], m[2][0],
112 m[0][1], m[1][1], m[2][1],
113 m[0][2], m[1][2], m[2][2]);
118 constexpr t_mat4<T> transpose(
const t_mat4<T>& m)
120 return t_mat4<T>(m[0][0], m[1][0], m[2][0], m[3][0],
121 m[0][1], m[1][1], m[2][1], m[3][1],
122 m[0][2], m[1][2], m[2][2], m[3][2],
123 m[0][3], m[1][3], m[2][3], m[3][3]);
132 constexpr t_mat4<T> perspective(T fovy_radians, T aspectRatio, T zNear, T zFar)
134 T f =
static_cast<T
>(1.0 / std::tan(fovy_radians * 0.5));
135 T r =
static_cast<T
>(1.0 / (zFar - zNear));
136 return t_mat4<T>(f / aspectRatio, 0, 0, 0,
139 0, 0, (zFar * zNear) * r, 0);
144 constexpr t_mat4<T> perspective(T left, T right, T bottom, T top, T zNear, T zFar)
146 return t_mat4<T>(2.0 * zNear / (right - left), 0.0, 0.0, 0.0,
147 0.0, 2.0 * zNear / (bottom - top), 0.0, 0.0,
148 (right + left) / (right - left), (bottom + top) / (bottom - top), zNear / (zFar - zNear), -1.0,
149 0.0, 0.0, zNear * zFar / (zFar - zNear), 0.0);
154 constexpr t_mat4<T> orthographic(T left, T right, T bottom, T top, T zNear, T zFar)
156 return t_mat4<T>(2.0 / (right - left), 0.0, 0.0, 0.0,
157 0.0, 2.0 / (bottom - top), 0.0, 0.0,
158 0.0, 0.0, 1.0 / (zFar - zNear), 0.0,
159 -(right + left) / (right - left), -(bottom + top) / (bottom - top), zFar / (zFar - zNear), 1.0);
163 constexpr t_mat4<T> lookAt(
const t_vec3<T>& eye,
const t_vec3<T>& center,
const t_vec3<T>& up)
165 using vec_type = t_vec3<T>;
167 vec_type forward = normalize(center - eye);
168 vec_type up_normal = normalize(up);
169 vec_type side = normalize(cross(forward, up_normal));
170 vec_type u = normalize(cross(side, forward));
172 return t_mat4<T>(side[0], u[0], -forward[0], 0,
173 side[1], u[1], -forward[1], 0,
174 side[2], u[2], -forward[2], 0,
176 vsg::translate(-eye.x, -eye.y, -eye.z);
180 enum class CoordinateConvention
190 extern VSG_DECLSPEC
bool transform(CoordinateConvention source, CoordinateConvention destination, dmat4& matrix);
193 extern VSG_DECLSPEC mat3 inverse_3x3(
const mat4& m);
196 extern VSG_DECLSPEC dmat3 inverse_3x3(
const dmat4& m);
199 extern VSG_DECLSPEC mat4 inverse_4x3(
const mat4& m);
202 extern VSG_DECLSPEC dmat4 inverse_4x3(
const dmat4& m);
205 extern VSG_DECLSPEC mat4 inverse_4x4(
const mat4& m);
208 extern VSG_DECLSPEC dmat4 inverse_4x4(
const dmat4& m);
211 extern VSG_DECLSPEC mat4 inverse(
const mat4& m);
214 extern VSG_DECLSPEC dmat4 inverse(
const dmat4& m);
217 extern VSG_DECLSPEC
float determinant(
const mat4& m);
220 extern VSG_DECLSPEC
double determinant(
const dmat4& m);
225 extern VSG_DECLSPEC
bool decompose(
const mat4& m, vec3& translation, quat& rotation, vec3& scale);
230 extern VSG_DECLSPEC
bool decompose(
const dmat4& m, dvec3& translation, dquat& rotation, dvec3& scale);
233 extern VSG_DECLSPEC sphere computeFrustumBound(
const mat4& m);
236 extern VSG_DECLSPEC dsphere computeFrustumBound(
const dmat4& m);
244 void apply(
const Transform& transform)
override;
246 void apply(
const Camera& camera)
override;
251 dmat4 computeTransform(
const T& nodePath)
253 return visit<ComputeTransform>(nodePath).matrix;
Definition: ConstVisitor.h:147