Berserk
MathUtils3d.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_UTILS3d_H
29 #define BERSERK_UTILS3d_H
30 
31 #include <core/math/TMatMxN.hpp>
32 
34 
44 class MathUtils3d {
45 public:
46  static TVecN<float, 3> Multiply(const Mat4x4f &m, const TVecN<float, 3> &v) {
48  t[0] = v[0];
49  t[1] = v[1];
50  t[2] = v[2];
51  t[3] = 1.0f;
52 
53  return TVecN<float, 3>(m * t);
54  }
55 
58  return {1, 0, 0, 0,
59  0, 1, 0, 0,
60  0, 0, 1, 0,
61  0, 0, 0, 1};
62  }
63 
65  static void SetScale(Mat4x4f &m, const Vec3f &scale) {
66  m.values[0] = scale[0];
67  m.values[5] = scale[1];
68  m.values[10] = scale[2];
69  }
70 
71  static Mat4x4f Scale(const Vec3f &scale) {
72  return {scale[0], 0, 0, 0,
73  0, scale[1], 0, 0,
74  0, 0, scale[2], 0,
75  0, 0, 0, 1};
76  }
77 
79  static void SetTranslation(Mat4x4f &m, const Vec3f &t) {
80  m.values[3] = t.values[0];
81  m.values[7] = t.values[1];
82  m.values[11] = t.values[2];
83  }
84 
86  static Mat4x4f Translate(const Vec3f &t) {
87  return {1, 0, 0, t.values[0],
88  0, 1, 0, t.values[1],
89  0, 0, 1, t.values[2],
90  0, 0, 0, 1};
91  }
92 
94  static Mat4x4f RotateX(float angleRad) {
95  auto s = MathUtils::Sin(angleRad);
96  auto c = MathUtils::Cos(angleRad);
97 
98  return {1, 0, 0, 0,
99  0, c, -s, 0,
100  0, s, c, 0,
101  0, 0, 0, 1};
102  }
103 
105  static Mat4x4f RotateY(float angleRad) {
106  auto s = MathUtils::Sin(angleRad);
107  auto c = MathUtils::Cos(angleRad);
108 
109  return {c, 0, s, 0,
110  0, 1, 0, 0,
111  -s, 0, c, 0,
112  0, 0, 0, 1};
113  }
114 
116  static Mat4x4f RotateZ(float angleRad) {
117  auto s = MathUtils::Sin(angleRad);
118  auto c = MathUtils::Cos(angleRad);
119 
120  return {c, -s, 0, 0,
121  s, c, 0, 0,
122  0, 0, 1, 0,
123  0, 0, 0, 1};
124  }
125 
127  static Mat4x4f Rotate(const Vec3f &axis, float angleRad) {
128  auto Ax = axis.Normalized();
129  auto s = MathUtils::Sin(angleRad);
130  auto c = MathUtils::Cos(angleRad);
131  auto oneMinC = 1 - c;
132 
133  return {
134  // 0 row
135  c + Ax[0] * Ax[0] * oneMinC,
136  Ax[0] * Ax[1] * oneMinC - Ax[2] * s,
137  Ax[0] * Ax[2] * oneMinC + Ax[1] * s,
138  0,
139 
140  // 1 row
141  Ax[1] * Ax[0] * oneMinC + Ax[2] * s,
142  c + Ax[1] * Ax[1] * oneMinC,
143  Ax[1] * Ax[2] * oneMinC - Ax[0] * s,
144  0,
145 
146  // 2 row
147  Ax[2] * Ax[0] * oneMinC - Ax[1] * s,
148  Ax[2] * Ax[1] * oneMinC + Ax[0] * s,
149  c + Ax[2] * Ax[2] * oneMinC,
150  0,
151 
152  // 3 row
153  0, 0, 0, 1};
154  }
155 
172  static Mat4x4f LookAt(const Vec3f &eye, const Vec3f &direction, const Vec3f &up) {
173  auto Z = (-direction).Normalized();
174  auto X = Vec3f::Cross(up, Z).Normalized();
175  auto Y = Vec3f::Cross(Z, X);
176 
177  return {X[0], X[1], X[2], -Vec3f::Dot(X, eye),
178  Y[0], Y[1], Y[2], -Vec3f::Dot(Y, eye),
179  Z[0], Z[1], Z[2], -Vec3f::Dot(Z, eye),
180  0.0f, 0.0f, 0.0f, 1.0f};
181  }
182 
199  static Mat4x4f Perspective(float fov, float aspect, float near, float far) {
200  float ctg_angle = 1.0f / MathUtils::Tan(fov / 2.0f);
201 
202  return {ctg_angle / aspect, 0.0f, 0.0f, 0.0f,
203  0.0f, ctg_angle, 0.0f, 0.0f,
204  0.0f, 0.0f, (far + near) / (near - far), (2 * far * near) / (near - far),
205  0.0f, 0.0f, -1.0f, 0.0f};
206  }
207 
219  static Mat4x4f Orthographic(float left, float right, float bottom, float top, float near, float far) {
220  return {2.0f / (right - left), 0.0f, 0.0f, (right + left) / (left - right),
221  0.0f, 2.0f / (top - bottom), 0.0f, (top + bottom) / (bottom - top),
222  0.0f, 0.0f, 2.0f / (far - near), (far + near) / (near - far),
223  0.0f, 0.0f, 0.0f, 1.0f};
224  }
225 };
226 
232 
233 #endif//BERSERK_UTILS3d_H
#define BRK_NS_END
Definition: Config.hpp:48
3d space math utils
Definition: MathUtils3d.hpp:44
static Mat4x4f RotateY(float angleRad)
Clockwise around axis rotation.
Definition: MathUtils3d.hpp:105
static Mat4x4f Perspective(float fov, float aspect, float near, float far)
Perspective projection.
Definition: MathUtils3d.hpp:199
static Mat4x4f RotateX(float angleRad)
Clockwise around axis rotation.
Definition: MathUtils3d.hpp:94
static void SetTranslation(Mat4x4f &m, const Vec3f &t)
Set translation column of matrix m to vec t.
Definition: MathUtils3d.hpp:79
static Mat4x4f Orthographic(float left, float right, float bottom, float top, float near, float far)
Orthographic projection.
Definition: MathUtils3d.hpp:219
static Mat4x4f IdentityMatrix()
Identity matrix e.
Definition: MathUtils3d.hpp:57
static Mat4x4f Scale(const Vec3f &scale)
Definition: MathUtils3d.hpp:71
static Mat4x4f LookAt(const Vec3f &eye, const Vec3f &direction, const Vec3f &up)
Look at view matrix for camera (in OpenGL style)
Definition: MathUtils3d.hpp:172
static void SetScale(Mat4x4f &m, const Vec3f &scale)
Set scale elements.
Definition: MathUtils3d.hpp:65
static Mat4x4f Rotate(const Vec3f &axis, float angleRad)
Clockwise rotation around an arbitrary axis.
Definition: MathUtils3d.hpp:127
static Mat4x4f Translate(const Vec3f &t)
Translation in the direction of vec t.
Definition: MathUtils3d.hpp:86
static Mat4x4f RotateZ(float angleRad)
Clockwise around axis rotation.
Definition: MathUtils3d.hpp:116
static TVecN< float, 3 > Multiply(const Mat4x4f &m, const TVecN< float, 3 > &v)
Definition: MathUtils3d.hpp:46
static float Tan(float a)
Definition: MathUtils.hpp:94
static float Cos(float a)
Definition: MathUtils.hpp:91
static float Sin(float a)
Definition: MathUtils.hpp:88
T values[M *N]
Definition: TMatMxN.hpp:377
static TVecN Cross(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:375
TVecN Normalized() const
Definition: TVecN.hpp:520
T values[N]
Definition: TVecN.hpp:595
static float Dot(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:354
Definition: GLDevice.cpp:46