aboutsummaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2023-11-26 06:49:51 +0100
committervimene <vincent.menegaux@gmail.com>2023-11-26 06:49:51 +0100
commitf63febed2a769d0c55192e192a20b8e39f162932 (patch)
treef12fef4643e03e93363f902e35d06ef92dd8350d /src/math
parent97f4e5c80483255912b177e6a2557344da499a3e (diff)
downloadengine-f63febed2a769d0c55192e192a20b8e39f162932.tar.gz
improved matrices, cube building and command parsing
Diffstat (limited to 'src/math')
-rw-r--r--src/math/mat4.cpp39
-rw-r--r--src/math/mat4.h6
2 files changed, 45 insertions, 0 deletions
diff --git a/src/math/mat4.cpp b/src/math/mat4.cpp
index 0342fdf..ece42d8 100644
--- a/src/math/mat4.cpp
+++ b/src/math/mat4.cpp
@@ -12,6 +12,23 @@ Mat4 Mat4::idty() {
};
}
+Mat4 Mat4::translate(MathVector3 v) {
+ return {
+ 1.f, 0.f, 0.f, v.x,
+ 0.f, 1.f, 0.f, v.y,
+ 0.f, 0.f, 1.f, v.z,
+ 0.f, 0.f, 0.f, 1.f,
+ };
+}
+Mat4 Mat4::scale(float fac) {
+ return {
+ fac, 0.f, 0.f, 0.f,
+ 0.f, fac, 0.f, 0.f,
+ 0.f, 0.f, fac, 0.f,
+ 0.f, 0.f, 0.f, 1.f,
+ };
+}
+
Mat4 Mat4::rot_x(float a) {
float c = std::cos(a);
float s = std::sin(a);
@@ -45,6 +62,19 @@ Mat4 Mat4::rot_z(float a) {
};
}
+Mat4 Mat4::projection(float aspect_ratio, float min_z, float max_z) {
+ return {{
+ aspect_ratio, 0.f, 0.f, 0.f,
+ 0.f, -1.f, 0.f, 0.f,
+ 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z),
+ 0.f, 0.f, -1.f, 0.f,
+ }};
+}
+
+Mat4 Mat4::operator+() {
+ return *this;
+}
+
Mat4 Mat4::operator-() {
return {
-values[ 0], -values[ 1], -values[ 2], -values[ 3],
@@ -96,3 +126,12 @@ std::array<MathVector4, 4> Mat4::to_vecs() {
{ values[ 3], values[ 7], values[11], values[15] },
}};
}
+
+Mat4 operator*(float fac, Mat4 m) {
+ return {
+ fac * m.values[ 0], fac * m.values[ 1], fac * m.values[ 2], fac * m.values[ 3],
+ fac * m.values[ 4], fac * m.values[ 5], fac * m.values[ 6], fac * m.values[ 7],
+ fac * m.values[ 8], fac * m.values[ 9], fac * m.values[10], fac * m.values[11],
+ fac * m.values[12], fac * m.values[13], fac * m.values[14], fac * m.values[15],
+ };
+}
diff --git a/src/math/mat4.h b/src/math/mat4.h
index d81da63..6df9d49 100644
--- a/src/math/mat4.h
+++ b/src/math/mat4.h
@@ -7,12 +7,16 @@
class Mat4 {
public:
static Mat4 idty();
+ static Mat4 translate(MathVector3 v);
+ static Mat4 scale(float fac);
static Mat4 rot_x(float a);
static Mat4 rot_y(float a);
static Mat4 rot_z(float a);
+ static Mat4 projection(float aspect_ratio, float min_z, float max_z);
std::array<float, 16> values;
+ Mat4 operator+();
Mat4 operator-();
Mat4 operator+(Mat4 m);
Mat4 operator-(Mat4 m);
@@ -21,4 +25,6 @@ class Mat4 {
std::array<MathVector4, 4> to_vecs();
};
+Mat4 operator*(float fac, Mat4 m);
+
#endif // MATH_MAT4_H