aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp257
1 files changed, 257 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..836418c
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,257 @@
+#include <iostream>
+#include <ncurses.h>
+#include <cmath>
+#include "chfb.h"
+#include "obj3d.h"
+#include "math_vector.h"
+#include "vertex.h"
+#include "vertex_data.h"
+#include "tri_vertex.h"
+
+#define MKEY_Z 122
+#define MKEY_Q 113
+#define MKEY_S 115
+#define MKEY_D 100
+
+#define MKEY_ESC 27
+
+#define FPS 60
+
+#define PI 3.1415926535f
+
+int main() {
+ // init
+ setlocale(LC_ALL, "");
+ initscr();
+ cbreak();
+ noecho();
+ intrflush(stdscr, FALSE);
+ keypad(stdscr, TRUE);
+ set_escdelay(0);
+ curs_set(0);
+
+ int w, h;
+ getmaxyx(stdscr, h, w);
+ CharacterFrameBuffer cfb{static_cast<unsigned int>(w), static_cast<unsigned int>(h)};
+
+ MathVector3 a{0.f, 0.f, 0.f};
+ float dist = 4.f;
+ while (1) {
+ //timeout(1000 / FPS);
+ timeout(10);
+ int c = getch();
+
+ if (c == MKEY_ESC) break;
+ switch (c) {
+ case KEY_UP:
+ // a.x += 0.1f;
+ dist += .1f;
+ break;
+ case KEY_DOWN:
+ // a.x -= 0.1f;
+ dist -= .1f;
+ break;
+ case KEY_LEFT:
+ // a.y += 0.1f;
+ break;
+ case KEY_RIGHT:
+ // a.y -= 0.1f;
+ break;
+ case MKEY_Q:
+ // a.z += 0.1f;
+ break;
+ case MKEY_D:
+ // a.z -= 0.1f;
+ break;
+ }
+
+ a.x += .0050f;
+ a.y += .0065f;
+ a.z += .0080f;
+
+ getmaxyx(stdscr, h, w);
+ cfb.resize(static_cast<unsigned int>(w), static_cast<unsigned int>(h));
+
+ 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];
+
+ std::array<Object3D, 2> objs{{
+ {
+ {
+ {
+ rad * (-e_x + -e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + -e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (-e_x + +e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + +e_y + -e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (-e_x + -e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + -e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (-e_x + +e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + +e_y + +e_z - .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ }
+ },
+ {
+ // face 1
+ { 0, 2, 3 },
+ { 0, 3, 1 },
+
+ // face 2
+ { 0, 4, 6 },
+ { 0, 6, 2 },
+
+ // face 3
+ { 0, 1, 5 },
+ { 0, 5, 4 },
+
+ // face 4
+ { 7, 6, 4 },
+ { 7, 4, 5 },
+
+ // face 5
+ { 7, 3, 2 },
+ { 7, 2, 6 },
+
+ // face 6
+ { 7, 5, 1 },
+ { 7, 1, 3 },
+ }
+ },
+ {
+ {
+ {
+ rad * (-e_x + -e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + -e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (-e_x + +e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + +e_y + -e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (-e_x + -e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + -e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (-e_x + +e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ },
+ {
+ rad * (+e_x + +e_y + +e_z + .5f * (e_x + e_y + e_z)) - dist * rad * MathVector3{0.f, 0.f, 1.f},
+ {}
+ }
+ },
+ {
+ // face 1
+ { 0, 2, 3 },
+ { 0, 3, 1 },
+
+ // face 2
+ { 0, 4, 6 },
+ { 0, 6, 2 },
+
+ // face 3
+ { 0, 1, 5 },
+ { 0, 5, 4 },
+
+ // face 4
+ { 7, 6, 4 },
+ { 7, 4, 5 },
+
+ // face 5
+ { 7, 3, 2 },
+ { 7, 2, 6 },
+
+ // face 6
+ { 7, 5, 1 },
+ { 7, 1, 3 },
+ }
+ }
+ }};
+ cfb.clear();
+ float min_z = 2.f, max_z = 50.f;
+ float fac_for_aspect_ratio = 2.f * static_cast<float>(cfb.height()) / static_cast<float>(cfb.width());
+ 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;
+
+ cfb.draw_triangle(t);
+ }
+ }
+ mvaddnstr(0, 0, cfb.chars(), cfb.width() * cfb.height());
+ }
+
+ // terminate
+ endwin();
+ return EXIT_SUCCESS;
+}