From 99715470eefa6d8192ab44f7355187848070031a Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 30 Apr 2024 02:47:42 -0400 Subject: [PATCH] 2d fps camera --- CMakeLists.txt | 2 +- include/blt/gfx/renderer/2d_textured.vert | 2 +- include/blt/gfx/renderer/camera.h | 35 ++++++++- include/blt/gfx/state.h | 15 +++- src/blt/gfx/renderer/camera.cpp | 96 +++++++++++++++++++---- src/blt/gfx/state.cpp | 4 +- 6 files changed, 133 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1552861..1b65069 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -set(BLT_GRAPHICS_VERSION 0.11.7) +set(BLT_GRAPHICS_VERSION 0.12.0) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) diff --git a/include/blt/gfx/renderer/2d_textured.vert b/include/blt/gfx/renderer/2d_textured.vert index 786d0ab..7f8d14e 100644 --- a/include/blt/gfx/renderer/2d_textured.vert +++ b/include/blt/gfx/renderer/2d_textured.vert @@ -23,7 +23,7 @@ uniform mat4 model; uniform float z_index; void main() { - gl_Position = ortho * model * vec4(vertex.xy, z_index, 1.0); + gl_Position = ovm * model * vec4(vertex.xy, z_index, 1.0); pos = vertex.xy; uv = uv_in; } diff --git a/include/blt/gfx/renderer/camera.h b/include/blt/gfx/renderer/camera.h index f7018e2..ac8f3b5 100644 --- a/include/blt/gfx/renderer/camera.h +++ b/include/blt/gfx/renderer/camera.h @@ -23,13 +23,46 @@ namespace blt::gfx { + class lockable_camera + { + public: + static void handle_lock(); + }; class first_person_camera { private: blt::vec3 position_; blt::vec3 rotation_; - blt::vec3 speed_ {25, 25, 25}; + blt::vec3 speed_{25, 25, 25}; + public: + void update(); + + inline void speed(const blt::vec3& speed) + { + speed_ = speed; + } + + [[nodiscard]] inline const vec3& speed() const + { + return speed_; + } + + [[nodiscard]] inline const vec3& position() const + { + return position_; + } + + void update_view(matrix_state_manager& manager); + }; + + class first_person_camera_2d + { + private: + blt::vec3 position_; + blt::vec3 rotation_; + blt::vec3 speed_{165, 165, 165}; + blt::vec3 world_scale_{1,1,1}; public: void update(); diff --git a/include/blt/gfx/state.h b/include/blt/gfx/state.h index 1ef9c4e..c4b012c 100644 --- a/include/blt/gfx/state.h +++ b/include/blt/gfx/state.h @@ -43,6 +43,8 @@ namespace blt::gfx blt::mat4x4 view; blt::mat4x4 pvm; blt::mat4x4 ovm; + blt::mat4x4 view_2d; + blt::vec3 scale_2d; std::int32_t last_width = 0, last_height = 0; uniform_buffer* global_matrix_ubo = nullptr; public: @@ -69,19 +71,26 @@ namespace blt::gfx template void setView(U&& mat) { - view = mat; + view = (mat); + } + + template + void setView2D(U&& mat, G&& scale) + { + view_2d = (mat); + scale_2d = (scale); } template void setPerspective(U&& mat) { - perspective = mat; + perspective = (mat); } template void setOrtho(U&& mat) { - ortho = mat; + ortho = (mat); } void update_perspectives(std::int32_t width, std::int32_t height, float fov = 90, float near = 0.1f, float far = 500.0f, diff --git a/src/blt/gfx/renderer/camera.cpp b/src/blt/gfx/renderer/camera.cpp index 11b93b8..2289aa4 100644 --- a/src/blt/gfx/renderer/camera.cpp +++ b/src/blt/gfx/renderer/camera.cpp @@ -17,28 +17,17 @@ */ #include #include +#include void blt::gfx::first_person_camera::update() { + lockable_camera::handle_lock(); + using namespace blt::gfx; vec3 move_at; float speed_multi = 1; - int locking_key = GLFW_KEY_ESCAPE; - -#ifdef __EMSCRIPTEN__ - locking_key = GLFW_KEY_F1; -#endif - - if (isKeyPressed(locking_key) && keyPressedLastFrame()) - { - if (isCursorLocked()) - unlockCursor(); - else - lockCursor(); - } - if (isKeyPressed(GLFW_KEY_LEFT_CONTROL)) speed_multi = 10; if (isKeyPressed(GLFW_KEY_LEFT_ALT)) @@ -117,3 +106,82 @@ void blt::gfx::first_person_camera::update_view(blt::gfx::matrix_state_manager& view.translate(-position_); manager.setView(view); } + +float easeInOutCubic(float x) +{ + return static_cast(x < 0.5 ? 4 * x * x * x : (x < 0.7 ? 1 - std::pow(-2 * x + 2, 3) / 2 : x + 0.19)); +} + +float easeInOutSine(float x) +{ + return static_cast(-(std::cos(blt::PI * x) - 1) / 2); +} + +void blt::gfx::first_person_camera_2d::update() +{ + lockable_camera::handle_lock(); + + float speed_multi = 1; + + if (isKeyPressed(GLFW_KEY_LEFT_SHIFT)) + speed_multi = 10; + if (isKeyPressed(GLFW_KEY_LEFT_CONTROL)) + speed_multi = 0.5; + + vec3 direction; + + if (isKeyPressed(GLFW_KEY_W)) + direction[1] = 1; + else if (isKeyPressed(GLFW_KEY_S)) + direction[1] = -1; + else + direction[1] = 0; + + if (isKeyPressed(GLFW_KEY_A)) + direction[0] = -1; + else if (isKeyPressed(GLFW_KEY_D)) + direction[0] = 1; + else + direction[0] = 0; + + float scaling_const = easeInOutCubic(world_scale_[0]); + + if (isKeyPressed(GLFW_KEY_Q)) + world_scale_[0] += scaling_const * static_cast(getFrameDeltaSeconds()); + else if (isKeyPressed(GLFW_KEY_E)) + world_scale_[0] -= scaling_const * static_cast(getFrameDeltaSeconds()); + + if (world_scale_[0] <= 0.1f) + world_scale_[0] = 0.1f; + + world_scale_[1] = world_scale_[0]; + + position_ = position_ + (direction * speed_ * speed_multi * static_cast(getFrameDeltaSeconds())); +} + +void blt::gfx::first_person_camera_2d::update_view(blt::gfx::matrix_state_manager& manager) +{ + blt::mat4x4 view; + view.rotateX(toRadians(rotation_.x())); + view.rotateY(toRadians(rotation_.y())); + view.rotateZ(toRadians(rotation_.z())); + view.translate(-position_); + manager.setView2D(view, world_scale_); +} + +void blt::gfx::lockable_camera::handle_lock() +{ + int locking_key = GLFW_KEY_ESCAPE; + +#ifdef __EMSCRIPTEN__ + locking_key = GLFW_KEY_F1; +#endif + + if (isKeyPressed(locking_key) && keyPressedLastFrame()) + { + if (isCursorLocked()) + unlockCursor(); + else + lockCursor(); + } +} diff --git a/src/blt/gfx/state.cpp b/src/blt/gfx/state.cpp index b26291e..0f03a26 100644 --- a/src/blt/gfx/state.cpp +++ b/src/blt/gfx/state.cpp @@ -22,7 +22,9 @@ void blt::gfx::matrix_state_manager::update() { pvm = perspective * view; - ovm = ortho * view; + blt::mat4x4 scaled; + scaled.scale(scale_2d); + ovm = scaled * ortho * view_2d; BLT_ASSERT(global_matrix_ubo != nullptr && "You forgot to call create_internals(). Make sure you call cleanup() as well!"); global_matrix_ubo->upload((void*)perspective.ptr(), sizeof(blt::mat4x4), 0); global_matrix_ubo->upload((void*)ortho.ptr(), sizeof(blt::mat4x4), sizeof(blt::mat4x4));