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 | |
parent | da56b98fd91fc94dd2a17936f362c0c9873f1d19 (diff) | |
download | engine-97f4e5c80483255912b177e6a2557344da499a3e.tar.gz |
added matrices
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | src/engine.cpp | 53 | ||||
-rw-r--r-- | src/math/mat4.cpp | 98 | ||||
-rw-r--r-- | src/math/mat4.h | 24 |
4 files changed, 137 insertions, 39 deletions
diff --git a/Makefile.am b/Makefile.am index fae27fd..2a81b4c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,6 +6,7 @@ bin_PROGRAMS = engine engine_SOURCES = src/engine.cpp \ src/fb/chfb.h src/fb/chfb.cpp src/fb/pixfb.h src/fb/pixfb.cpp \ src/math/math_vector.h src/math/math_vector.cpp \ + src/math/mat4.h src/math/mat4.cpp \ src/o3d/obj3d.h src/o3d/obj3d.cpp src/o3d/tri_vertex.h \ src/o3d/tri_vertex.cpp src/o3d/vertex.h src/o3d/vertex.cpp \ src/o3d/vertex_data.h src/o3d/vertex_data.cpp diff --git a/src/engine.cpp b/src/engine.cpp index eb3b156..4740046 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -22,6 +22,7 @@ #include "o3d/vertex_data.h" #include "o3d/tri_vertex.h" #include "math/math_vector.h" +#include "math/mat4.h" #define FPS 60 @@ -55,32 +56,10 @@ void scene_main(FB& fb, std::function<bool()> update_frame) { a.z += .0080f; float rad = 5.f; - MathVector3 ca{std::cos(a.x), std::cos(a.y), std::cos(a.z)}; - MathVector3 sa{std::sin(a.x), std::sin(a.y), std::sin(a.z)}; - - std::array<MathVector3, 3> rot_x = {{ - { 1.f, 0.f, 0.f }, - { 0.f, ca.x, sa.x }, - { 0.f, -sa.x, ca.x }, - }}; - std::array<MathVector3, 3> rot_y = {{ - { ca.y, 0.f, -sa.y }, - { 0.f, 1.f, 0.f }, - { sa.y, 0.f, ca.y }, - }}; - std::array<MathVector3, 3> rot_z = {{ - { ca.z, sa.z, 0.f }, - { -sa.z, ca.z, 0.f }, - { 0.f, 0.f, 1.f }, - }}; - - auto [e_x, e_y, e_z] = rot_x; - e_x = e_x.x * rot_y[0] + e_x.y * rot_y[1] + e_x.z * rot_y[2]; - e_y = e_y.x * rot_y[0] + e_y.y * rot_y[1] + e_y.z * rot_y[2]; - e_z = e_z.x * rot_y[0] + e_z.y * rot_y[1] + e_z.z * rot_y[2]; - e_x = e_x.x * rot_z[0] + e_x.y * rot_z[1] + e_x.z * rot_z[2]; - e_y = e_y.x * rot_z[0] + e_y.y * rot_z[1] + e_y.z * rot_z[2]; - e_z = e_z.x * rot_z[0] + e_z.y * rot_z[1] + e_z.z * rot_z[2]; + auto [e4_x, e4_y, e4_z, e4_w] = (Mat4::rot_x(a.x) * Mat4::rot_y(a.y) * Mat4::rot_z(a.z)).to_vecs(); + auto e_x = e4_x.xyz(); + auto e_y = e4_y.xyz(); + auto e_z = e4_z.xyz(); std::array<Object3D, 2> objs{{ { @@ -209,23 +188,19 @@ void scene_main(FB& fb, std::function<bool()> update_frame) { fb.clear(); float min_z = 2.f, max_z = 50.f; float fac_for_aspect_ratio = static_cast<float>(fb.height()) / static_cast<float>(fb.width()); + Mat4 mat{{ + fac_for_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, + }}; for (auto obj : objs) { for (auto triangle : obj) { TriangleVertex4 t{triangle}; - // should be multiplied by a matrix, temporary replacement - t.vertex1.point.x *= fac_for_aspect_ratio; - t.vertex1.point.y = -t.vertex1.point.y; - t.vertex1.point.w = -t.vertex1.point.z; - t.vertex1.point.z = 2.f * (-t.vertex1.point.z - min_z) / (max_z - min_z) - 1.f; - t.vertex2.point.x *= fac_for_aspect_ratio; - t.vertex2.point.y = -t.vertex2.point.y; - t.vertex2.point.w = -t.vertex2.point.z; - t.vertex2.point.z = 2.f * (-t.vertex2.point.z - min_z) / (max_z - min_z) - 1.f; - t.vertex3.point.x *= fac_for_aspect_ratio; - t.vertex3.point.y = -t.vertex3.point.y; - t.vertex3.point.w = -t.vertex3.point.z; - t.vertex3.point.z = 2.f * (-t.vertex3.point.z - min_z) / (max_z - min_z) - 1.f; + t.vertex1.point = mat * t.vertex1.point; + t.vertex2.point = mat * t.vertex2.point; + t.vertex3.point = mat * t.vertex3.point; fb.draw_triangle(t); } 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 |