aboutsummaryrefslogtreecommitdiff
path: root/src/engine.cpp
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2023-11-26 02:08:10 +0100
committervimene <vincent.menegaux@gmail.com>2023-11-26 02:08:10 +0100
commit97f4e5c80483255912b177e6a2557344da499a3e (patch)
treec75b709785ed6425e7a755eab859ed02e215c936 /src/engine.cpp
parentda56b98fd91fc94dd2a17936f362c0c9873f1d19 (diff)
downloadengine-97f4e5c80483255912b177e6a2557344da499a3e.tar.gz
added matrices
Diffstat (limited to 'src/engine.cpp')
-rw-r--r--src/engine.cpp53
1 files changed, 14 insertions, 39 deletions
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);
}