21 using Mat4 = MatQ<T, 4>;
24 Mat4<T> translationMatrix(Vec3<T> translation) {
25 Mat4<T> translationMatrix;
26 Vec4<T> column = Vec4<T>(translation.x, translation.y, translation.z,
static_cast<T
>(1));
27 translationMatrix.setToIdentity().setColumn(3, column);
28 return translationMatrix;
32 Mat4<T> scalingMatrix(Vec3<T> scaling) {
33 Vec4<T> diagonal = Vec4<T>(scaling.x, scaling.y, scaling.z,
static_cast<T
>(1));
34 Mat4<T> scalingMatrix;
35 scalingMatrix.setToZero();
36 scalingMatrix.setDiagonal(diagonal);
41 Mat4<T> rotationMatrix(Quaternion<T> rotation) {
42 Mat4<T> rotationMatrix;
43 rotationMatrix.setToIdentity();
45 T _2qx2 = 2 * rotation.x * rotation.x;
46 T _2qy2 = 2 * rotation.y * rotation.y;
47 T _2qz2 = 2 * rotation.z * rotation.z;
49 T _2qxqy = 2 * rotation.x * rotation.y;
50 T _2qxqz = 2 * rotation.x * rotation.z;
51 T _2qxqw = 2 * rotation.x * rotation.w;
52 T _2qyqz = 2 * rotation.y * rotation.z;
53 T _2qyqw = 2 * rotation.y * rotation.w;
54 T _2qzqw = 2 * rotation.z * rotation.w;
56 rotationMatrix.data[0][0] -= _2qy2 + _2qz2;
57 rotationMatrix.data[0][1] = _2qxqy - _2qzqw;
58 rotationMatrix.data[0][2] = _2qxqz + _2qyqw;
60 rotationMatrix.data[1][0] = _2qxqy + _2qzqw;
61 rotationMatrix.data[1][1] -= _2qx2 + _2qz2;
62 rotationMatrix.data[1][2] = _2qyqz - _2qxqw;
64 rotationMatrix.data[2][0] = _2qxqz - _2qyqw;
65 rotationMatrix.data[2][1] = _2qyqz + _2qxqw;
66 rotationMatrix.data[2][2] -= _2qx2 + _2qy2;
68 return rotationMatrix;
72 Mat4<T> viewMatrix(Vec3<T> eye, Vec3<T> direction, Vec3<T> up) {
73 Vec3<T> z = -direction.normalized();
74 Vec3<T> x = cross(direction, up).normalized();
75 Vec3<T> y = cross(z, x);
77 Mat4<T> basisChangeMatrix;
78 basisChangeMatrix.setToIdentity();
79 basisChangeMatrix.setRow(0, Vec4<T>(x.x, x.y, x.z, 0.f));
80 basisChangeMatrix.setRow(1, Vec4<T>(y.x, y.y, y.z, 0.f));
81 basisChangeMatrix.setRow(2, Vec4<T>(z.x, z.y, z.z, 0.f));
83 return basisChangeMatrix * translationMatrix(-eye);
87 Mat4<T> projectionMatrix(T fovy, T aspect, T near, T far) {
88 Mat4<T> projectionMatrix;
89 projectionMatrix.setToZero();
91 T d =
static_cast<T
>(std::tan(M_PI_2 - fovy / 2));
92 T _1_n_f = 1 / (near - far);
94 projectionMatrix.data[0][0] = d / aspect;
95 projectionMatrix.data[1][1] = d;
96 projectionMatrix.data[2][2] = (near + far) * _1_n_f;
97 projectionMatrix.data[2][3] = 2 * near * far * _1_n_f;
98 projectionMatrix.data[3][2] = -1;
100 return projectionMatrix;
This file will define Vector and various vector functions.
This file is part of the Lumina Graphics Framework.
This file is part of the Lumina Graphics Framework.