summary refs log tree commit diff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-04 01:18:19 +0200
committerMel <einebeere@gmail.com>2022-10-04 01:18:19 +0200
commit0631fb666d2a28a6eb9b8d1578675699b41a5de6 (patch)
tree1db9b88d9c11074864f6959d32960eb6c54d3c4b /src/main.cpp
parent75f3941579c756655fc7d4d29e7b92b6eae436b7 (diff)
downloadmeowcraft-0631fb666d2a28a6eb9b8d1578675699b41a5de6.tar.zst
meowcraft-0631fb666d2a28a6eb9b8d1578675699b41a5de6.zip
Cube Rendering
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp76
1 files changed, 69 insertions, 7 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 0893849..16eda51 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,19 +1,27 @@
 #include <iostream>
 #include <GL/glew.h>
 #include <GLFW/glfw3.h>
+#include <cmath>
+#include <cstdint>
 
 #include "Window.hpp"
 #include "Mesh.hpp"
+#include "Camera.hpp"
 #include "Binder.hpp"
+#include "Math/MVP.hpp"
 #include "Shader/ShaderProgram.hpp"
 
 #define APP_NAME "Meowcraft"
 
 #define WINDOW_WIDTH 1000
 #define WINDOW_HEIGHT 800
+#define ASPECT static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT
+
+#define FOV 45
 
 void run();
 void render(MC::BindableMesh&);
+void process_input(MC::Window&, MC::Camera&);
 void setup_gl();
 void fix_macos_render(MC::Window&);
 
@@ -37,18 +45,45 @@ void run() {
     setup_gl();
 
     glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
+    window.on_size_change([](GLFWwindow* window, int w, int h) {
+        glViewport(0, 0, w, h);
+    });
 
     MC::Mesh shape({
-        {-0.5f, -0.5f, 0.0f}, {0.5f, -0.5f, 0.0f},
-        {-0.5f, 0.5f, 0.0f},  {0.5f, 0.5f, 0.0f},
-    }, {0, 1, 2, 3, 2, 1});
+       {-0.5f, -0.5f,  0.5f},
+       {0.5f, -0.5f,  0.5f},
+       {0.5f,  0.5f,  0.5f},
+       {-0.5f,  0.5f,  0.5f},
+
+       {-0.5f, -0.5f, -0.5f},
+       {0.5f, -0.5f, -0.5f},
+       {0.5f,  0.5f, -0.5f},
+       {-0.5f,  0.5f, -0.5f}
+    }, {
+        0, 1, 2, 2, 3, 0,
+        1, 5, 6, 6, 2, 1,
+        7, 6, 5, 5, 4, 7,
+        4, 0, 3, 3, 7, 4,
+        4, 5, 1, 1, 0, 4,
+        3, 2, 6, 6, 7, 3
+    });
 
     auto mesh = MC::Binder::load(shape);
 
-    auto mesh = MC::Binder::load(quad);
+    MC::Camera camera{};
+    camera.set_position({0.0f, 0.0f, -3.0f});
 
     MC::ShaderProgram program(MC::Shader::create_fragment(), MC::Shader::create_vertex());
+
+    auto model_uniform = program.uniform("model_matrix");
+    auto view_uniform = program.uniform("view_matrix");
+    auto projection_uniform = program.uniform("projection_matrix");
+
     program.bind();
+    auto projection = Math::MVP::projection(ASPECT, FOV, 0.1f, 100.0f);
+    projection_uniform.set(projection);
+
+    uint64_t time = 0;
 
     while (!window.should_close()) {
         window.start_frame();
@@ -57,11 +92,19 @@ void run() {
         fix_macos_render(window);
 #endif
 
-        if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) {
-            window.close();
-        }
+        process_input(window, camera);
+
+        program.bind();
+
+        auto angle = fmod(time / 10.0f, 360.0f);
+        auto model = Math::MVP::model({0.0f, 0.0f, 0.0f}, {angle, angle, 0.0f});
+        model_uniform.set(model);
+
+        auto view = Math::MVP::view(camera.position(), camera.angles());
+        view_uniform.set(view);
 
         render(mesh);
+        time++;
     }
 }
 
@@ -74,6 +117,25 @@ void render(MC::BindableMesh& mesh) {
     mesh.unbind();
 }
 
+void process_input(MC::Window& window, MC::Camera& camera) {
+    if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) {
+        window.close();
+    }
+
+    auto key = [&](int key) -> float { return window.key(key, GLFW_PRESS); };
+
+    float forward = key(GLFW_KEY_W) - key(GLFW_KEY_S);
+    float side = key(GLFW_KEY_D) - key(GLFW_KEY_A);
+
+    float vertical = key(GLFW_KEY_UP) - key(GLFW_KEY_DOWN);
+    float horizontal = key(GLFW_KEY_LEFT) - key(GLFW_KEY_RIGHT);
+
+    auto speed = 0.05f;
+
+    camera.move({side * speed, forward * speed, 0.0f});
+    camera.rotate({vertical * speed, horizontal * speed, 0.0f});
+}
+
 void setup_gl() {
     GLenum error;
     if ((error = glewInit()) != GLEW_OK) {