diff --git a/src/graphics/camera.cpp b/src/graphics/camera.cpp new file mode 100644 index 0000000..473e200 --- /dev/null +++ b/src/graphics/camera.cpp @@ -0,0 +1,93 @@ + +#include +#include + +#include "camera.hpp" +#include "keyboard.hpp" + +#include +#include +#include + +using namespace sim::graphics; + +static double yaw = 0, pitch = 0; +static double x = 0, y = 0, z = 0; + +void camera::rotate(double y, double p) +{ + yaw += y * 0.1; + pitch += p * 0.1; + + if(pitch < -90) pitch = -90; + if(pitch > 90) pitch = 90; +} + +void camera::move(double xoff, double yoff, double zoff) +{ + x += xoff; + y += yoff; + z += zoff; +} + +void camera::update() +{ + double xoff = 0, yoff = 0, zoff = 0; + + glm::vec<3, double> off(0, 0, 0); + + if(keyboard::is_pressed(GLFW_KEY_SPACE)) + { + off.y -= 1; + } + + if(keyboard::is_pressed(GLFW_KEY_LEFT_SHIFT)) + { + off.y += 1; + } + + if(keyboard::is_pressed(GLFW_KEY_W)) + { + off.z += 1; + } + + if(keyboard::is_pressed(GLFW_KEY_S)) + { + off.z -= 1; + } + + if(keyboard::is_pressed(GLFW_KEY_A)) + { + off.x += 1; + } + + if(keyboard::is_pressed(GLFW_KEY_D)) + { + off.x -= 1; + } + + double angle = -glm::radians(yaw); + + glm::mat<2, 2, double> mat = { + std::cos(angle), std::sin(angle), + -std::sin(angle), std::cos(angle) + }; + + glm::vec<2, double> rotated = glm::vec<2, double>(off.x, off.z) * mat; + + y += off.y * 0.1; + x += rotated.x * 0.1; + z += rotated.y * 0.1; +} + +glm::mat4 camera::get() +{ + glm::mat4 mat(1); + + mat = glm::rotate(mat, (float)glm::radians(pitch), glm::vec3(1, 0, 0)); + mat = glm::rotate(mat, (float)glm::radians(yaw), glm::vec3(0, 1, 0)); + mat = glm::translate(mat, glm::vec3(x, y, z)); + + return mat; +} + diff --git a/src/graphics/camera.hpp b/src/graphics/camera.hpp new file mode 100644 index 0000000..1f81ccb --- /dev/null +++ b/src/graphics/camera.hpp @@ -0,0 +1,15 @@ + +#pragma once + +#include + +namespace sim::graphics::camera +{ + +glm::mat4 get(); +void rotate(double pitch, double yaw); +void move(double x, double y, double z); +void update(); + +}; + diff --git a/src/graphics/keyboard.cpp b/src/graphics/keyboard.cpp index efadd6c..aa77087 100644 --- a/src/graphics/keyboard.cpp +++ b/src/graphics/keyboard.cpp @@ -2,18 +2,48 @@ #include #include +#include + #include "keyboard.hpp" #include "window.hpp" #include "resize.hpp" +#include "camera.hpp" using namespace sim::graphics; +static std::unordered_map pressed; + static void cb_keypress(GLFWwindow* win, int key, int sc, int action, int mods) { if(key == GLFW_KEY_F11 && action == GLFW_RELEASE) { resize::toggle_fullscreen(); } + + if(action == GLFW_PRESS) + { + pressed[key] = true; + } + + if(action == GLFW_RELEASE) + { + pressed[key] = false; + } +} + +bool keyboard::is_pressed(int key) +{ + auto it = pressed.find(key); + + if(it == pressed.end()) + { + return false; + } + + else + { + return it->second; + } } void keyboard::init() diff --git a/src/graphics/keyboard.hpp b/src/graphics/keyboard.hpp index 6ad89db..9762383 100644 --- a/src/graphics/keyboard.hpp +++ b/src/graphics/keyboard.hpp @@ -5,6 +5,7 @@ namespace sim::graphics::keyboard { void init(); +bool is_pressed(int key); }; diff --git a/src/graphics/mouse.cpp b/src/graphics/mouse.cpp new file mode 100644 index 0000000..05d10fc --- /dev/null +++ b/src/graphics/mouse.cpp @@ -0,0 +1,34 @@ + +#include +#include + +#include "mouse.hpp" +#include "window.hpp" +#include "camera.hpp" + +using namespace sim::graphics; + +static double xpos = 0, ypos = 0; + +static void cb_cursor_pos(GLFWwindow* win, double x, double y) +{ + camera::rotate(x - xpos, y - ypos); + + xpos = x; + ypos = y; +} + +void mouse::get(double& x, double& y) +{ + x = xpos; + y = ypos; +} + +void mouse::init() +{ + GLFWwindow* win = window::get_window(); + glfwSetCursorPosCallback(win, cb_cursor_pos); + glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + glfwSetCursorPos(win, 0, 0); +} + diff --git a/src/graphics/mouse.hpp b/src/graphics/mouse.hpp new file mode 100644 index 0000000..20125d9 --- /dev/null +++ b/src/graphics/mouse.hpp @@ -0,0 +1,11 @@ + +#pragma once + +namespace sim::graphics::mouse +{ + +void init(); +void get(double& x, double& y); + +}; + diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index 71c31e4..fe0447d 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -11,6 +11,8 @@ #include "model.hpp" #include "arrays.hpp" #include "keyboard.hpp" +#include "mouse.hpp" +#include "camera.hpp" #include "resize.hpp" #include "window.hpp" #include "shader.hpp" @@ -58,8 +60,10 @@ void window::create() glDebugMessageCallback(cb_debug_message, nullptr); keyboard::init(); + mouse::init(); resize::init(); font::init(); + shader::init_program(); Model.alloc(); @@ -80,10 +84,14 @@ void window::loop() 0, 0, 0, 0 }; - glm::mat4 mat_model = glm::mat4(1.0f); + camera::update(); + + glm::mat4 mat_model = camera::get(); + + double mouse_x, mouse_y; + mouse::get(mouse_x, mouse_y); mat_model = glm::translate(mat_model, glm::vec3(0.0f, 0.0f, -5.0f)); -// mat_model = glm::scale(mat_model, glm::vec3(1.0f) * 0.05f); mat_model = glm::rotate(mat_model, float(M_PI * 0.125), glm::vec3(1, 1, 1)); glm::mat4 mat_projection = glm::perspective(float(M_PI * 0.25), (float)resize::get_aspect(), 0.1f, 100.f);