diff options
author | vimene <vincent.menegaux@gmail.com> | 2023-11-26 02:08:10 +0100 |
---|---|---|
committer | vimene <vincent.menegaux@gmail.com> | 2023-11-26 02:08:10 +0100 |
commit | 97f4e5c80483255912b177e6a2557344da499a3e (patch) | |
tree | c75b709785ed6425e7a755eab859ed02e215c936 /src/math | |
parent | da56b98fd91fc94dd2a17936f362c0c9873f1d19 (diff) | |
download | engine-97f4e5c80483255912b177e6a2557344da499a3e.tar.gz |
added matrices
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/mat4.cpp | 98 | ||||
-rw-r--r-- | src/math/mat4.h | 24 |
2 files changed, 122 insertions, 0 deletions
diff --git a/src/math/mat4.cpp b/src/math/mat4.cpp new file mode 100644 index 0000000..0342fdf --- /dev/null +++ b/src/math/mat4.cpp @@ -0,0 +1,98 @@ +#include "math/mat4.h" +#include <array> +#include <cmath> +#include "math/math_vector.h" + +Mat4 Mat4::idty() { + return { + 1.f, 0.f, 0.f, 0.f, + 0.f, 1.f, 0.f, 0.f, + 0.f, 0.f, 1.f, 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); + return { + 1.f, 0.f, 0.f, 0.f, + 0.f, c, -s, 0.f, + 0.f, s, c, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; +} + +Mat4 Mat4::rot_y(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + c, 0.f, s, 0.f, + 0.f, 1.f, 0.f, 0.f, + -s, 0.f, c, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; +} + +Mat4 Mat4::rot_z(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + c, -s, 0.f, 0.f, + s, c, 0.f, 0.f, + 0.f, 0.f, 1.f, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; +} + +Mat4 Mat4::operator-() { + return { + -values[ 0], -values[ 1], -values[ 2], -values[ 3], + -values[ 4], -values[ 5], -values[ 6], -values[ 7], + -values[ 8], -values[ 9], -values[10], -values[11], + -values[12], -values[13], -values[14], -values[15], + }; +} + +Mat4 Mat4::operator+(Mat4 m) { + return { + values[ 0] + m.values[ 0], values[ 1] + m.values[ 1], values[ 2] + m.values[ 2], values[ 3] + m.values[ 3], + values[ 4] + m.values[ 4], values[ 5] + m.values[ 5], values[ 6] + m.values[ 6], values[ 7] + m.values[ 7], + values[ 8] + m.values[ 8], values[ 9] + m.values[ 9], values[10] + m.values[10], values[11] + m.values[11], + values[12] + m.values[12], values[13] + m.values[13], values[14] + m.values[14], values[15] + m.values[15], + }; +} + +Mat4 Mat4::operator-(Mat4 m) { + return *this + (-m); +} + +Mat4 Mat4::operator*(Mat4 m) { + Mat4 ret; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + ret.values[i * 4 + j] = 0.f; + for (int k = 0; k < 4; k++) + ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j]; + } + } + return ret; +} + +MathVector4 Mat4::operator*(MathVector4 v) { + return { + values[ 0] * v.x + values[ 1] * v.y + values[ 2] * v.z + values[ 3] * v.w, + values[ 4] * v.x + values[ 5] * v.y + values[ 6] * v.z + values[ 7] * v.w, + values[ 8] * v.x + values[ 9] * v.y + values[10] * v.z + values[11] * v.w, + values[12] * v.x + values[13] * v.y + values[14] * v.z + values[15] * v.w, + }; +} + +std::array<MathVector4, 4> Mat4::to_vecs() { + return {{ + { values[ 0], values[ 4], values[ 8], values[12] }, + { values[ 1], values[ 5], values[ 9], values[13] }, + { values[ 2], values[ 6], values[10], values[14] }, + { values[ 3], values[ 7], values[11], values[15] }, + }}; +} diff --git a/src/math/mat4.h b/src/math/mat4.h new file mode 100644 index 0000000..d81da63 --- /dev/null +++ b/src/math/mat4.h @@ -0,0 +1,24 @@ +#ifndef MATH_MAT4_H +#define MATH_MAT4_H + +#include <array> +#include "math/math_vector.h" + +class Mat4 { + public: + static Mat4 idty(); + static Mat4 rot_x(float a); + static Mat4 rot_y(float a); + static Mat4 rot_z(float a); + + std::array<float, 16> values; + + Mat4 operator-(); + Mat4 operator+(Mat4 m); + Mat4 operator-(Mat4 m); + Mat4 operator*(Mat4 m); + MathVector4 operator*(MathVector4 v); + std::array<MathVector4, 4> to_vecs(); +}; + +#endif // MATH_MAT4_H |