diff options
| author | Mel <einebeere@gmail.com> | 2022-10-06 02:48:43 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-06 02:48:43 +0200 |
| commit | fdbfa8e36f85eee051fc562f1a8588970257a20f (patch) | |
| tree | e861a56c50b1e8532bd71a21064d76d0c148ed3a | |
| parent | 731846a0c654b39e23c26f611470e401df404c9d (diff) | |
| download | meowcraft-fdbfa8e36f85eee051fc562f1a8588970257a20f.tar.zst meowcraft-fdbfa8e36f85eee051fc562f1a8588970257a20f.zip | |
Rotating camera with mouse
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/Camera.cpp | 6 | ||||
| -rw-r--r-- | src/Math/MVP.cpp | 2 | ||||
| -rw-r--r-- | src/Math/Math.hpp | 3 | ||||
| -rw-r--r-- | src/Math/Matrix.hpp | 7 | ||||
| -rw-r--r-- | src/Math/Rotation.hpp | 16 | ||||
| -rw-r--r-- | src/Math/Trig.hpp | 17 | ||||
| -rw-r--r-- | src/Mouse.cpp | 25 | ||||
| -rw-r--r-- | src/Mouse.hpp | 19 | ||||
| -rw-r--r-- | src/Window.cpp | 1 | ||||
| -rw-r--r-- | src/main.cpp | 20 |
11 files changed, 102 insertions, 16 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 827a239..b2f0f90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ find_package(glfw3 3.3 REQUIRED) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) -add_executable(meowcraft src/main.cpp src/Window.cpp src/Window.hpp src/Mesh.cpp src/Mesh.hpp src/Math/Vector.hpp src/Math/Math.hpp src/Binder.cpp src/Binder.hpp src/Shader/ShaderSources.hpp src/Shader/Shader.cpp src/Shader/Shader.hpp src/Shader/ShaderProgram.cpp src/Shader/ShaderProgram.hpp src/Math/Matrix.hpp src/Math/MVP.cpp src/Math/MVP.hpp src/Camera.cpp src/Camera.hpp src/Math/Rotation.hpp src/Shader/Uniform.cpp src/Shader/Uniform.hpp) +add_executable(meowcraft src/main.cpp src/Window.cpp src/Window.hpp src/Mesh.cpp src/Mesh.hpp src/Math/Vector.hpp src/Math/Math.hpp src/Binder.cpp src/Binder.hpp src/Shader/ShaderSources.hpp src/Shader/Shader.cpp src/Shader/Shader.hpp src/Shader/ShaderProgram.cpp src/Shader/ShaderProgram.hpp src/Math/Matrix.hpp src/Math/MVP.cpp src/Math/MVP.hpp src/Camera.cpp src/Camera.hpp src/Math/Rotation.hpp src/Shader/Uniform.cpp src/Shader/Uniform.hpp src/Mouse.cpp src/Mouse.hpp src/Math/Trig.hpp) target_link_libraries(meowcraft glfw GLEW::GLEW OpenGL) function(make_includable input_file output_file) diff --git a/src/Camera.cpp b/src/Camera.cpp index 59d677c..3a9d7cd 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -31,6 +31,12 @@ void Camera::set_angles(Rotation angles) { void Camera::rotate(Rotation by) { m_angles = m_angles + by; + + if (m_angles.pitch() > 89.0f) { + m_angles.pitch() = 89.0f; + } else if (m_angles.pitch() < -89.0f) { + m_angles.pitch() = -89.0f; + } } } diff --git a/src/Math/MVP.cpp b/src/Math/MVP.cpp index 146b208..de9f2ee 100644 --- a/src/Math/MVP.cpp +++ b/src/Math/MVP.cpp @@ -19,7 +19,7 @@ Matrix<4, 4> view(Vector<3> position, Rotation angles) { } Matrix<4, 4> projection(float aspect, float fov, float near, float far) { - auto fov_radians = (fov * M_PI) / 180.0f; + auto fov_radians = Math::radians(fov); float x_scale = 1.0f / (tan(fov_radians / 2.0f) * aspect); float y_scale = 1.0f / tan(fov_radians / 2.0f); diff --git a/src/Math/Math.hpp b/src/Math/Math.hpp index b8b61a1..94840b1 100644 --- a/src/Math/Math.hpp +++ b/src/Math/Math.hpp @@ -2,4 +2,5 @@ #include "Vector.hpp" #include "Matrix.hpp" -#include "Rotation.hpp" \ No newline at end of file +#include "Rotation.hpp" +#include "Trig.hpp" \ No newline at end of file diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp index d77760a..545b6a5 100644 --- a/src/Math/Matrix.hpp +++ b/src/Math/Matrix.hpp @@ -4,6 +4,7 @@ #include <cstddef> #include <iostream> #include "Rotation.hpp" +#include "Trig.hpp" template <size_t R, size_t C, typename T = float> struct Matrix { @@ -39,8 +40,10 @@ public: } static Matrix<4, 4> rotation(Rotation angles) { - auto c = angles.vector.map([](auto a) { return cos(a); }); - auto s = angles.vector.map([](auto a) { return sin(a); }); + auto radians = angles.vector.map([](auto a) { return Math::radians(a); }); + + auto c = radians.map([](auto a) { return cos(a); }); + auto s = radians.map([](auto a) { return sin(a); }); Matrix<4, 4> rotation_x{ 1.0f, 0.0f, 0.0f, 0.0f, diff --git a/src/Math/Rotation.hpp b/src/Math/Rotation.hpp index 73a8219..0c5c606 100644 --- a/src/Math/Rotation.hpp +++ b/src/Math/Rotation.hpp @@ -11,8 +11,8 @@ public: Rotation(float angles[3]) : Rotation(angles[0], angles[1], angles[2]) {}; - Rotation(float x, float y, float z) { - vector = wrap({ x, y, z }); + Rotation(float pitch, float yaw, float roll) { + vector = wrap({pitch, yaw, roll }); }; Rotation operator+(Rotation other) const { @@ -27,5 +27,17 @@ public: return v.map([](auto a) { return fmod(a, 360.0f); }); } + float& pitch() { + return vector[0]; + } + + float& yaw() { + return vector[1]; + } + + float& roll() { + return vector[2]; + } + Vector<3> vector; }; \ No newline at end of file diff --git a/src/Math/Trig.hpp b/src/Math/Trig.hpp new file mode 100644 index 0000000..c64e2f2 --- /dev/null +++ b/src/Math/Trig.hpp @@ -0,0 +1,17 @@ +#pragma once + +namespace Math { + +constexpr double PI = 3.14159265358979323846; + +template<typename T> +T radians(T degrees) { + return (degrees * PI) / 180.0f; +} + +template<typename T> +T degrees(T radians) { + return radians * 180.0f / PI; +} + +} \ No newline at end of file diff --git a/src/Mouse.cpp b/src/Mouse.cpp new file mode 100644 index 0000000..4715362 --- /dev/null +++ b/src/Mouse.cpp @@ -0,0 +1,25 @@ +#include "Mouse.hpp" +#include "Window.hpp" + +namespace MC { + +Vector<2> Mouse::update(Window& window) { + double x, y; + glfwGetCursorPos(window.get(), &x, &y); + + if (m_first_event) { + m_last_x = x; + m_last_y = y; + + m_first_event = false; + } + + Vector<2> movement{static_cast<float>(x) - m_last_x, static_cast<float>(y) - m_last_y}; + + m_last_x = x; + m_last_y = y; + + return movement; +} + +} \ No newline at end of file diff --git a/src/Mouse.hpp b/src/Mouse.hpp new file mode 100644 index 0000000..c546a7e --- /dev/null +++ b/src/Mouse.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include <cstdint> +#include "Math/Vector.hpp" +#include "Window.hpp" + +namespace MC { + +class Mouse { +public: + Mouse() = default; + + Vector<2> update(Window &window); +private: + bool m_first_event = true; + float m_last_x, m_last_y = 0.0f; +}; + +} \ No newline at end of file diff --git a/src/Window.cpp b/src/Window.cpp index 413cd47..2f1c2c0 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -16,6 +16,7 @@ Window::Window(const char *title, uint32_t width, uint32_t height) { } glfwMakeContextCurrent(m_window); + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); } Window::~Window() { diff --git a/src/main.cpp b/src/main.cpp index 63d371a..3da3d90 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "Binder.hpp" #include "Math/MVP.hpp" #include "Shader/ShaderProgram.hpp" +#include "Mouse.hpp" #define APP_NAME "Meowcraft" @@ -21,7 +22,7 @@ void run(); void render(MC::BindableMesh&); -void process_input(MC::Window&, MC::Camera&); +void process_input(MC::Window&, MC::Mouse&, MC::Camera&); void setup_gl(); void fix_macos_render(MC::Window&); @@ -42,6 +43,7 @@ int main() { void run() { MC::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT); + MC::Mouse mouse{}; setup_gl(); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); @@ -92,7 +94,7 @@ void run() { fix_macos_render(window); #endif - process_input(window, camera); + process_input(window, mouse, camera); program.bind(); @@ -117,24 +119,24 @@ void render(MC::BindableMesh& mesh) { mesh.unbind(); } -void process_input(MC::Window& window, MC::Camera& camera) { +void process_input(MC::Window& window, MC::Mouse& mouse, MC::Camera& camera) { if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) { window.close(); } + auto r = mouse.update(window); + auto key = [&](int key) -> float { return window.key(key, GLFW_PRESS); }; float x = key(GLFW_KEY_D) - key(GLFW_KEY_A); float y = key(GLFW_KEY_SPACE) - key(GLFW_KEY_LEFT_SHIFT); float z = key(GLFW_KEY_S) - key(GLFW_KEY_W); - float rx = key(GLFW_KEY_DOWN) - key(GLFW_KEY_UP); - float ry = key(GLFW_KEY_RIGHT) - key(GLFW_KEY_LEFT); - - auto speed = 0.05f; + auto move_speed = 0.05f; + auto rotation_speed = 0.1f; - camera.move_relative({x * speed, y * speed, z * speed}); - camera.rotate({rx * speed, ry * speed, 0.0f}); + camera.move_relative({x * move_speed, y * move_speed, z * move_speed}); + camera.rotate({r.y() * rotation_speed, r.x() * rotation_speed, 0.0f}); } void setup_gl() { |
