All Classes Files Functions Variables Typedefs Pages
Transformation.hpp
1 #pragma once
2 
11 #include "VectorCore.hpp"
12 #include "Quaternion.hpp"
13 #include "MatrixCore.hpp"
14 
15 #include <cmath>
16 
17 namespace lumina {
18 
19 // We are mostly dealing with 4x4 matrices here.
20 template <typename T>
21 using Mat4 = MatQ<T, 4>;
22 
23 template <typename T>
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;
29 }
30 
31 template <typename T>
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);
37  return scalingMatrix;
38 }
39 
40 template <typename T>
41 Mat4<T> rotationMatrix(Quaternion<T> rotation) {
42  Mat4<T> rotationMatrix;
43  rotationMatrix.setToIdentity();
44 
45  T _2qx2 = 2 * rotation.x * rotation.x;
46  T _2qy2 = 2 * rotation.y * rotation.y;
47  T _2qz2 = 2 * rotation.z * rotation.z;
48 
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;
55 
56  rotationMatrix.data[0][0] -= _2qy2 + _2qz2;
57  rotationMatrix.data[0][1] = _2qxqy - _2qzqw;
58  rotationMatrix.data[0][2] = _2qxqz + _2qyqw;
59 
60  rotationMatrix.data[1][0] = _2qxqy + _2qzqw;
61  rotationMatrix.data[1][1] -= _2qx2 + _2qz2;
62  rotationMatrix.data[1][2] = _2qyqz - _2qxqw;
63 
64  rotationMatrix.data[2][0] = _2qxqz - _2qyqw;
65  rotationMatrix.data[2][1] = _2qyqz + _2qxqw;
66  rotationMatrix.data[2][2] -= _2qx2 + _2qy2;
67 
68  return rotationMatrix;
69 }
70 
71 template <typename T>
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);
76 
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));
82 
83  return basisChangeMatrix * translationMatrix(-eye);
84 }
85 
86 template <typename T>
87 Mat4<T> projectionMatrix(T fovy, T aspect, T near, T far) {
88  Mat4<T> projectionMatrix;
89  projectionMatrix.setToZero();
90  // cot(x) = tan(pi/2 - x)
91  T d = static_cast<T>(std::tan(M_PI_2 - fovy / 2));
92  T _1_n_f = 1 / (near - far);
93 
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;
99 
100  return projectionMatrix;
101 }
102 
103 }
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.