Lugdunum  0.1.0
Transform.inl
Go to the documentation of this file.
1 template <typename T>
2 inline Matrix<4, 4, T> translate(const Vector<3, T>& direction) {
3  Matrix<4, 4, T> matrix = Matrix<4, 4, T>::identity();
4 
5  matrix(0, 3) = direction(0);
6  matrix(1, 3) = direction(1);
7  matrix(2, 3) = direction(2);
8 
9  return matrix;
10 }
11 
12 template <typename T>
13 inline Matrix<4, 4, T> rotate(T angle, const Vector<3, T>& a) {
14  T const c = ::lug::Math::Geometry::cos(angle);
15  T const s = ::lug::Math::Geometry::sin(angle);
16 
17  Vector<3, T> axis(normalize(a));
18  Vector<3, T> tmp((T(1) - c) * axis);
19 
20  return Matrix<4, 4, T> {
21  c + tmp.x() * axis.x(),
22  tmp.x() * axis.y() + s * axis.z(),
23  tmp.x() * axis.z() - s * axis.y(),
24  0,
25 
26  tmp.y() * axis.x() - s * axis.z(),
27  c + tmp.y() * axis.y(),
28  tmp.y() * axis.z() + s * axis.x(),
29  0,
30 
31  tmp.z() * axis.x() + s * axis.y(),
32  tmp.z() * axis.y() - s * axis.x(),
33  c + tmp.z() * axis.z(),
34  0,
35 
36  0, 0, 0, 1
37  };
38 }
39 
40 template <typename T>
41 inline Matrix<4, 4, T> scale(const Vector<3, T>& factors) {
42  Matrix<4, 4, T> matrix = Matrix<4, 4, T>::identity();
43 
44  matrix(0, 0) = factors(0);
45  matrix(1, 1) = factors(1);
46  matrix(2, 2) = factors(2);
47 
48  return matrix;
49 }
50 
51 template <typename T>
52 inline Matrix<4, 4, T> lookAt(const Vector<3, T>& eye, const Vector<3, T>& center, const Vector<3, T>& up) {
53  const Vector<3, T> direction(normalize(static_cast<Vector<3, T>>(eye - center)));
54  const Vector<3, T> right(normalize(cross(up, direction)));
55  const Vector<3, T> newUp(cross(direction, right));
56 
57  return Matrix<4, 4, T> {
58  right.x(),
59  right.y(),
60  right.z(),
61  -dot(right, eye),
62 
63  newUp.x(),
64  newUp.y(),
65  newUp.z(),
66  -dot(newUp, eye),
67 
68  direction.x(),
69  direction.y(),
70  direction.z(),
71  -dot(direction, eye),
72 
73  0,
74  0,
75  0,
76  1,
77  };
78 }
79 
80 template <typename T>
81 inline Matrix<4, 4, T> ortho(T left, T right, T bottom, T top, T zNear, T zFar) {
82  Matrix<4, 4, T> matrix = Matrix<4, 4, T>::identity();
83 
84  matrix(0, 0) = T(2) / (right - left);
85  matrix(0, 3) = -(right + left) / (right - left);
86  matrix(1, 1) = T(2) / (top - bottom);
87  matrix(1, 3) = -(top + bottom) / (top - bottom);
88  matrix(2, 2) = -T(1) / (zFar - zNear);
89  matrix(2, 3) = -zNear / (zFar - zNear);
90 
91  return matrix;
92 }
93 
94 template <typename T>
95 inline Matrix<4, 4, T> perspective(T fovy, T aspect, T zNear, T zFar) {
96  const T tanHalfFovy = ::lug::Math::Geometry::tan(fovy / T(2));
97 
98  Matrix<4, 4, T> matrix(0);
99 
100  matrix(0, 0) = T(1) / (aspect * tanHalfFovy);
101  matrix(1, 1) = T(1) / (tanHalfFovy);
102  matrix(2, 2) = zFar / (zNear - zFar);
103  matrix(2, 3) = -(zFar * zNear) / (zFar - zNear);
104  matrix(3, 2) = -T(1);
105 
106  return matrix;
107 }
T sin(T radians)
Matrix< 4, 4, T > scale(const Vector< 3, T > &factors)
Definition: Transform.inl:41
Matrix< 4, 4, T > translate(const Vector< 3, T > &direction)
Definition: Transform.inl:2
constexpr Vector< 3, T > cross(const Vector< 3, T > &lhs, const Vector< 3, T > &rhs)
Definition: Vector.inl:72
T cos(T radians)
Matrix< 4, 4, T > lookAt(const Vector< 3, T > &eye, const Vector< 3, T > &center, const Vector< 3, T > &up)
Definition: Transform.inl:52
T tan(T radians)
Matrix< 4, 4, T > rotate(T angle, const Vector< 3, T > &a)
Definition: Transform.inl:13
Quaternion< T > normalize(const Quaternion< T > &lhs)
Matrix< 4, 4, T > perspective(T fovy, T aspect, T zNear, T zFar)
Definition: Transform.inl:95
T dot(const Quaternion< T > &lhs, const Quaternion< T > &rhs)
Matrix< 4, 4, T > ortho(T left, T right, T bottom, T top, T zNear, T zFar)
Definition: Transform.inl:81