From 73f06b74db74180cb40e47e6964ded7b5b991565 Mon Sep 17 00:00:00 2001 From: edvin Date: Wed, 25 Sep 2024 15:10:02 +0200 Subject: [PATCH 01/11] Renderer: Added camera boundaries to CameraManager and clamping to the camera movement. --- libopenage/renderer/camera/camera.cpp | 30 ++++++++++++++++--- libopenage/renderer/camera/camera.h | 9 ++++-- libopenage/renderer/demo/demo_5.cpp | 10 ++++--- libopenage/renderer/demo/demo_6.cpp | 10 ++++--- libopenage/renderer/demo/demo_6.h | 4 +++ libopenage/renderer/stages/camera/manager.cpp | 14 +++++---- libopenage/renderer/stages/camera/manager.h | 12 ++++++++ 7 files changed, 70 insertions(+), 19 deletions(-) diff --git a/libopenage/renderer/camera/camera.cpp b/libopenage/renderer/camera/camera.cpp index f70efbee64..c2aa74cbe4 100644 --- a/libopenage/renderer/camera/camera.cpp +++ b/libopenage/renderer/camera/camera.cpp @@ -102,15 +102,37 @@ void Camera::look_at_coord(coord::scene3 coord_pos) { this->look_at_scene(scene_pos); } -void Camera::move_to(Eigen::Vector3f scene_pos) { - // TODO: Check and set bounds for where the camera can go and check them here +void Camera::move_to(Eigen::Vector3f scene_pos, + std::optional> x_bounds, + std::optional> z_bounds) { + // Calculate look at coordinates using same method as in look_at_scene(..) + auto y_delta = this->scene_pos[1] - scene_pos[1]; + auto xz_distance = y_delta * std::numbers::sqrt3; + + float side_length = xz_distance / std::numbers::sqrt2; + scene_pos[0] += side_length; + scene_pos[2] += side_length; + + // Clamp the x coordinate if x_bounds are provided + if (x_bounds.has_value()) { + scene_pos[0] = std::clamp(scene_pos[0], x_bounds->first, x_bounds->second); + } + + // Clamp the z coordinate if z_bounds are provided + if (z_bounds.has_value()) { + scene_pos[2] = std::clamp(scene_pos[2], z_bounds->first, z_bounds->second); + } this->scene_pos = scene_pos; this->moved = true; } -void Camera::move_rel(Eigen::Vector3f direction, float delta) { - this->move_to(this->scene_pos + (direction * delta)); +void Camera::move_rel(Eigen::Vector3f direction, + std::pair x_bounds, + std::pair z_bounds, + float delta) { + + this->move_to(this->scene_pos + (direction * delta), x_bounds, z_bounds); } void Camera::set_zoom(float zoom) { diff --git a/libopenage/renderer/camera/camera.h b/libopenage/renderer/camera/camera.h index eccb5b40e6..629b04dbe3 100644 --- a/libopenage/renderer/camera/camera.h +++ b/libopenage/renderer/camera/camera.h @@ -15,6 +15,7 @@ #include "renderer/camera/definitions.h" #include "renderer/camera/frustum_2d.h" #include "renderer/camera/frustum_3d.h" +#include namespace openage::renderer { @@ -84,7 +85,9 @@ class Camera { * * @param scene_pos New 3D position of the camera in the scene. */ - void move_to(Eigen::Vector3f scene_pos); + void move_to(Eigen::Vector3f scene_pos, std::optional> x_bounds = std::nullopt, + std::optional> z_bounds = std::nullopt); + /** * Move the camera position in the direction of a given vector. @@ -94,7 +97,9 @@ class Camera { * value is multiplied with the directional vector before its applied to * the positional vector. */ - void move_rel(Eigen::Vector3f direction, float delta = 1.0f); + void move_rel(Eigen::Vector3f direction, std::pair x_bounds, + std::pair z_bounds, + float delta = 1.0f); /** * Set the zoom level of the camera. Values smaller than 1.0f let the diff --git a/libopenage/renderer/demo/demo_5.cpp b/libopenage/renderer/demo/demo_5.cpp index 85752db18c..465e61921a 100644 --- a/libopenage/renderer/demo/demo_5.cpp +++ b/libopenage/renderer/demo/demo_5.cpp @@ -17,6 +17,7 @@ #include "renderer/shader_program.h" #include "renderer/uniform_buffer.h" #include "renderer/uniform_input.h" +#include "renderer/stages/camera/manager.h" namespace openage::renderer::tests { @@ -38,6 +39,7 @@ void renderer_demo_5(const util::Path &path) { camera->resize(w, h); }); + camera::CameraManager camera_manager(camera); /* Display the subtextures using the meta information */ log::log(INFO << "Loading shaders..."); @@ -142,7 +144,7 @@ void renderer_demo_5(const util::Path &path) { switch (key) { case Qt::Key_W: { // forward - camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), 0.5f); + camera_manager.move_frame(camera::MoveDirection::FORWARD, 0.5f); cam_update = true; log::log(INFO << "Camera moved forward."); @@ -150,13 +152,13 @@ void renderer_demo_5(const util::Path &path) { case Qt::Key_A: { // left // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), 0.25f); + camera_manager.move_frame(camera::MoveDirection::LEFT, 0.25f); cam_update = true; log::log(INFO << "Camera moved left."); } break; case Qt::Key_S: { // back - camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), 0.5f); + camera_manager.move_frame(camera::MoveDirection::BACKWARD, 0.5f); cam_update = true; log::log(INFO << "Camera moved back."); @@ -164,7 +166,7 @@ void renderer_demo_5(const util::Path &path) { case Qt::Key_D: { // right // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), 0.25f); + camera_manager.move_frame(camera::MoveDirection::RIGHT, 0.25f); cam_update = true; log::log(INFO << "Camera moved right."); diff --git a/libopenage/renderer/demo/demo_6.cpp b/libopenage/renderer/demo/demo_6.cpp index 9844503e64..96f894d69d 100644 --- a/libopenage/renderer/demo/demo_6.cpp +++ b/libopenage/renderer/demo/demo_6.cpp @@ -23,6 +23,7 @@ #include "renderer/shader_program.h" #include "renderer/texture.h" #include "renderer/uniform_buffer.h" +#include "renderer/stages/camera/manager.h" #include "time/clock.h" #include "util/path.h" #include "util/vector.h" @@ -52,16 +53,16 @@ void renderer_demo_6(const util::Path &path) { // move_frame moves the camera in the specified direction in the next drawn frame switch (key) { case Qt::Key_W: { // forward - render_mgr.camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), 0.2f); + render_mgr.camera_manager->move_frame(camera::MoveDirection::FORWARD, 0.2f); } break; case Qt::Key_A: { // left - render_mgr.camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), 0.1f); + render_mgr.camera_manager->move_frame(camera::MoveDirection::LEFT, 0.1f); } break; case Qt::Key_S: { // back - render_mgr.camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), 0.2f); + render_mgr.camera_manager->move_frame(camera::MoveDirection::BACKWARD, 0.2f); } break; case Qt::Key_D: { // right - render_mgr.camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), 0.1f); + render_mgr.camera_manager->move_frame(camera::MoveDirection::RIGHT, 0.1f); } break; default: break; @@ -438,6 +439,7 @@ void RenderManagerDemo6::create_camera() { 1.0f / static_cast(viewport_size[1])}; camera_unifs->update("inv_viewport_size", viewport_size_vec); this->camera->get_uniform_buffer()->update_uniforms(camera_unifs); + this->camera_manager = std::make_shared(camera); } void RenderManagerDemo6::create_render_passes() { diff --git a/libopenage/renderer/demo/demo_6.h b/libopenage/renderer/demo/demo_6.h index f86728819b..279276122a 100644 --- a/libopenage/renderer/demo/demo_6.h +++ b/libopenage/renderer/demo/demo_6.h @@ -19,6 +19,7 @@ class Texture2d; namespace camera { class Camera; +class CameraManager; } namespace gui { @@ -72,6 +73,9 @@ class RenderManagerDemo6 { /// Camera std::shared_ptr camera; + /// Camera manager + std::shared_ptr camera_manager; + /// Render passes std::shared_ptr obj_2d_pass; std::shared_ptr obj_3d_pass; diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 39eddd2b52..4d96d36af9 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -3,6 +3,7 @@ #include "manager.h" #include +#include #include "renderer/camera/camera.h" #include "renderer/uniform_buffer.h" @@ -21,6 +22,9 @@ CameraManager::CameraManager(const std::shared_ptr &ca camera->get_view_matrix(), "proj", camera->get_projection_matrix()); + + x_bounds = std::make_pair(XMIN, XMAX); + z_bounds = std::make_pair(ZMIN, ZMAX); } void CameraManager::update() { @@ -33,18 +37,18 @@ void CameraManager::move_frame(MoveDirection direction, float speed) { case MoveDirection::LEFT: // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), speed / 2); + this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), this->x_bounds, this->z_bounds, speed / 2); break; case MoveDirection::RIGHT: // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), speed / 2); + this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), this->x_bounds, this->z_bounds, speed / 2); break; case MoveDirection::FORWARD: - this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), speed); + this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), this->x_bounds, this->z_bounds, speed); break; case MoveDirection::BACKWARD: - this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), speed); + this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), this->x_bounds, this->z_bounds, speed); break; default: @@ -83,7 +87,7 @@ void CameraManager::update_motion() { move_dir += Eigen::Vector3f(1.0f, 0.0f, 1.0f); } - this->camera->move_rel(move_dir, this->move_motion_speed); + this->camera->move_rel(move_dir, this->x_bounds, this->z_bounds, this->move_motion_speed); } if (this->zoom_motion_direction != static_cast(ZoomDirection::NONE)) { diff --git a/libopenage/renderer/stages/camera/manager.h b/libopenage/renderer/stages/camera/manager.h index 13d876b25f..73bb06a506 100644 --- a/libopenage/renderer/stages/camera/manager.h +++ b/libopenage/renderer/stages/camera/manager.h @@ -3,6 +3,7 @@ #pragma once #include +#include namespace openage::renderer { @@ -143,6 +144,17 @@ class CameraManager { * Uniform buffer input for the camera. */ std::shared_ptr uniforms; + + /** + * x and z bounds for the camera. + */ + std::pair x_bounds, z_bounds; + + /** + * Constant values for the camera bounds. + * TODO: Make boundaries dynamic based on map size. + */ + const float XMIN = 12.25f, XMAX = 32.25f, ZMIN = -8.25f, ZMAX = 12.25f; }; } // namespace camera From 85918c944f40456f9365510515b45dd14774e848 Mon Sep 17 00:00:00 2001 From: edvin Date: Fri, 18 Oct 2024 14:27:37 +0200 Subject: [PATCH 02/11] Refactoring and documentation. --- libopenage/renderer/camera/camera.cpp | 105 ++++++++---------- libopenage/renderer/camera/camera.h | 38 ++++++- libopenage/renderer/camera/definitions.h | 6 + libopenage/renderer/demo/demo_5.cpp | 10 +- libopenage/renderer/demo/demo_6.cpp | 10 +- libopenage/renderer/stages/camera/manager.cpp | 17 ++- libopenage/renderer/stages/camera/manager.h | 11 +- 7 files changed, 104 insertions(+), 93 deletions(-) diff --git a/libopenage/renderer/camera/camera.cpp b/libopenage/renderer/camera/camera.cpp index c2aa74cbe4..2ea7adc4c9 100644 --- a/libopenage/renderer/camera/camera.cpp +++ b/libopenage/renderer/camera/camera.cpp @@ -61,38 +61,7 @@ Camera::Camera(const std::shared_ptr &renderer, } void Camera::look_at_scene(Eigen::Vector3f scene_pos) { - if (scene_pos[1] > this->scene_pos[1]) { - // TODO: camera can't look at a position that's - // higher than it's own position - } - - // TODO: Although the below method should be faster, calculating and adding the direction - // vector from scene_pos to new_pos may be easier to understand - // i.e. new_pos = scene_pos + b/sin(30) * direction_vec - - // due to the fixed angle, the centered scene position - // and the new camera position form a right triangle. - // - // c - + new camera pos - // - |b - // center +------+ - // a - // - // we can calculate the new camera position via the offset a - // using the angle and length of side b. - auto y_delta = this->scene_pos[1] - scene_pos[1]; // b (vertical distance) - auto xz_distance = y_delta * std::numbers::sqrt3; // a (horizontal distance); a = b * (cos(30°) / sin(30°)) - - // get x and z offsets - // the camera is pointed diagonally to the negative x and z axis - // a is the length of the diagonal from camera.xz to scene_pos.xz - // so the x and z offest are sides of a square with the same diagonal - auto side_length = xz_distance / std::numbers::sqrt2; - auto new_pos = Eigen::Vector3f( - scene_pos[0] + side_length, - this->scene_pos[1], // height unchanged - scene_pos[2] + side_length); - + auto new_pos = calc_look_at(scene_pos); this->move_to(new_pos); } @@ -102,37 +71,22 @@ void Camera::look_at_coord(coord::scene3 coord_pos) { this->look_at_scene(scene_pos); } -void Camera::move_to(Eigen::Vector3f scene_pos, - std::optional> x_bounds, - std::optional> z_bounds) { - // Calculate look at coordinates using same method as in look_at_scene(..) - auto y_delta = this->scene_pos[1] - scene_pos[1]; - auto xz_distance = y_delta * std::numbers::sqrt3; - - float side_length = xz_distance / std::numbers::sqrt2; - scene_pos[0] += side_length; - scene_pos[2] += side_length; - - // Clamp the x coordinate if x_bounds are provided - if (x_bounds.has_value()) { - scene_pos[0] = std::clamp(scene_pos[0], x_bounds->first, x_bounds->second); - } - - // Clamp the z coordinate if z_bounds are provided - if (z_bounds.has_value()) { - scene_pos[2] = std::clamp(scene_pos[2], z_bounds->first, z_bounds->second); - } - +void Camera::move_to(Eigen::Vector3f scene_pos) { this->scene_pos = scene_pos; this->moved = true; } -void Camera::move_rel(Eigen::Vector3f direction, - std::pair x_bounds, - std::pair z_bounds, - float delta) { +void Camera::move_rel(Eigen::Vector3f direction, float delta) { + this->move_to(this->scene_pos + (direction * delta)); +} + +void Camera::move_rel(Eigen::Vector3f direction, float delta, struct CameraBoundaries camera_boundaries) { + auto new_pos = calc_look_at(this->scene_pos + (direction * delta)); + + new_pos[0] = std::clamp(new_pos[0], camera_boundaries.x_min, camera_boundaries.x_max); + new_pos[2] = std::clamp(new_pos[2], camera_boundaries.z_min, camera_boundaries.z_max); - this->move_to(this->scene_pos + (direction * delta), x_bounds, z_bounds); + this->move_to(new_pos); } void Camera::set_zoom(float zoom) { @@ -314,8 +268,43 @@ void Camera::init_uniform_buffer(const std::shared_ptr &renderer) { this->uniform_buffer = renderer->add_uniform_buffer(ubo_info); } +Eigen::Vector3f Camera::calc_look_at(Eigen::Vector3f target) { + if (target[1] > this->scene_pos[1]) { + // TODO: camera can't look at a position that's + // higher than it's own position + } + + // TODO: Although the below method should be faster, calculating and adding the direction + // vector from scene_pos to new_pos may be easier to understand + // i.e. new_pos = scene_pos + b/sin(30) * direction_vec + + // due to the fixed angle, the centered scene position + // and the new camera position form a right triangle. + // + // c - + new camera pos + // - |b + // center +------+ + // a + // + // we can calculate the new camera position via the offset a + // using the angle and length of side b. + auto y_delta = this->scene_pos[1] - target[1]; // b (vertical distance) + auto xz_distance = y_delta * std::numbers::sqrt3; // a (horizontal distance); a = b * (cos(30°) / sin(30°)) + + // get x and z offsets + // the camera is pointed diagonally to the negative x and z axis + // a is the length of the diagonal from camera.xz to scene_pos.xz + // so the x and z offest are sides of a square with the same diagonal + auto side_length = xz_distance / std::numbers::sqrt2; + return Eigen::Vector3f( + target[0] + side_length, + this->scene_pos[1], // height unchanged + target[2] + side_length); +} + inline float Camera::get_real_zoom_factor() const { return 0.5f * this->default_zoom_ratio * this->zoom; } + } // namespace openage::renderer::camera diff --git a/libopenage/renderer/camera/camera.h b/libopenage/renderer/camera/camera.h index 629b04dbe3..1ba0e3bdfb 100644 --- a/libopenage/renderer/camera/camera.h +++ b/libopenage/renderer/camera/camera.h @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include @@ -15,7 +17,6 @@ #include "renderer/camera/definitions.h" #include "renderer/camera/frustum_2d.h" #include "renderer/camera/frustum_3d.h" -#include namespace openage::renderer { @@ -24,6 +25,13 @@ class UniformBuffer; namespace camera { +/** + * Defines constant boundaries for the camera's view in the X and Z axes. + */ +struct CameraBoundaries { + const float x_min, x_max, z_min, z_max; +}; + /** * Camera for selecting what part of the ingame world is displayed. * @@ -85,8 +93,7 @@ class Camera { * * @param scene_pos New 3D position of the camera in the scene. */ - void move_to(Eigen::Vector3f scene_pos, std::optional> x_bounds = std::nullopt, - std::optional> z_bounds = std::nullopt); + void move_to(Eigen::Vector3f scene_pos); /** @@ -97,9 +104,20 @@ class Camera { * value is multiplied with the directional vector before its applied to * the positional vector. */ - void move_rel(Eigen::Vector3f direction, std::pair x_bounds, - std::pair z_bounds, - float delta = 1.0f); + void move_rel(Eigen::Vector3f direction, float delta = 1.0f); + + + /** + * Move the camera position in the direction of a given vector taking the + * camera boundaries into account. + * + * @param direction Direction vector. Added to the current position. + * @param delta Delta for controlling the amount by which the camera is moved. The + * value is multiplied with the directional vector before its applied to + * the positional vector. + * @param camera_boundaries X and Z boundaries for the camera in the scene. + */ + void move_rel(Eigen::Vector3f direction, float delta, struct CameraBoundaries camera_boundaries); /** * Set the zoom level of the camera. Values smaller than 1.0f let the @@ -205,6 +223,7 @@ class Camera { */ const Frustum3d get_frustum_3d() const; + private: /** * Create the uniform buffer for the camera. @@ -213,6 +232,13 @@ class Camera { */ void init_uniform_buffer(const std::shared_ptr &renderer); + /** + * Calculates the camera's position needed to center its view on the given target. + * + * @param target The target position in the 3D scene the camera should focus on. + */ + Eigen::Vector3f calc_look_at(Eigen::Vector3f target); + /** * Get the zoom factor applied to the camera projection. * diff --git a/libopenage/renderer/camera/definitions.h b/libopenage/renderer/camera/definitions.h index 9586239a2f..1a35c7fe5e 100644 --- a/libopenage/renderer/camera/definitions.h +++ b/libopenage/renderer/camera/definitions.h @@ -58,4 +58,10 @@ static constexpr float DEFAULT_MAX_ZOOM_OUT = 64.0f; */ static constexpr float DEFAULT_ZOOM_RATIO = 1.0f / 49; +/** + * Constant values for the camera bounds. + * TODO: Make boundaries dynamic based on map size. + */ +static const float X_MIN = 12.25f, X_MAX = 32.25f, Z_MIN = -8.25f, Z_MAX = 12.25f; + } // namespace openage::renderer::camera diff --git a/libopenage/renderer/demo/demo_5.cpp b/libopenage/renderer/demo/demo_5.cpp index 465e61921a..85752db18c 100644 --- a/libopenage/renderer/demo/demo_5.cpp +++ b/libopenage/renderer/demo/demo_5.cpp @@ -17,7 +17,6 @@ #include "renderer/shader_program.h" #include "renderer/uniform_buffer.h" #include "renderer/uniform_input.h" -#include "renderer/stages/camera/manager.h" namespace openage::renderer::tests { @@ -39,7 +38,6 @@ void renderer_demo_5(const util::Path &path) { camera->resize(w, h); }); - camera::CameraManager camera_manager(camera); /* Display the subtextures using the meta information */ log::log(INFO << "Loading shaders..."); @@ -144,7 +142,7 @@ void renderer_demo_5(const util::Path &path) { switch (key) { case Qt::Key_W: { // forward - camera_manager.move_frame(camera::MoveDirection::FORWARD, 0.5f); + camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), 0.5f); cam_update = true; log::log(INFO << "Camera moved forward."); @@ -152,13 +150,13 @@ void renderer_demo_5(const util::Path &path) { case Qt::Key_A: { // left // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - camera_manager.move_frame(camera::MoveDirection::LEFT, 0.25f); + camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), 0.25f); cam_update = true; log::log(INFO << "Camera moved left."); } break; case Qt::Key_S: { // back - camera_manager.move_frame(camera::MoveDirection::BACKWARD, 0.5f); + camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), 0.5f); cam_update = true; log::log(INFO << "Camera moved back."); @@ -166,7 +164,7 @@ void renderer_demo_5(const util::Path &path) { case Qt::Key_D: { // right // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - camera_manager.move_frame(camera::MoveDirection::RIGHT, 0.25f); + camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), 0.25f); cam_update = true; log::log(INFO << "Camera moved right."); diff --git a/libopenage/renderer/demo/demo_6.cpp b/libopenage/renderer/demo/demo_6.cpp index 96f894d69d..9844503e64 100644 --- a/libopenage/renderer/demo/demo_6.cpp +++ b/libopenage/renderer/demo/demo_6.cpp @@ -23,7 +23,6 @@ #include "renderer/shader_program.h" #include "renderer/texture.h" #include "renderer/uniform_buffer.h" -#include "renderer/stages/camera/manager.h" #include "time/clock.h" #include "util/path.h" #include "util/vector.h" @@ -53,16 +52,16 @@ void renderer_demo_6(const util::Path &path) { // move_frame moves the camera in the specified direction in the next drawn frame switch (key) { case Qt::Key_W: { // forward - render_mgr.camera_manager->move_frame(camera::MoveDirection::FORWARD, 0.2f); + render_mgr.camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), 0.2f); } break; case Qt::Key_A: { // left - render_mgr.camera_manager->move_frame(camera::MoveDirection::LEFT, 0.1f); + render_mgr.camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), 0.1f); } break; case Qt::Key_S: { // back - render_mgr.camera_manager->move_frame(camera::MoveDirection::BACKWARD, 0.2f); + render_mgr.camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), 0.2f); } break; case Qt::Key_D: { // right - render_mgr.camera_manager->move_frame(camera::MoveDirection::RIGHT, 0.1f); + render_mgr.camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), 0.1f); } break; default: break; @@ -439,7 +438,6 @@ void RenderManagerDemo6::create_camera() { 1.0f / static_cast(viewport_size[1])}; camera_unifs->update("inv_viewport_size", viewport_size_vec); this->camera->get_uniform_buffer()->update_uniforms(camera_unifs); - this->camera_manager = std::make_shared(camera); } void RenderManagerDemo6::create_render_passes() { diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 4d96d36af9..380657b6db 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -5,7 +5,6 @@ #include #include -#include "renderer/camera/camera.h" #include "renderer/uniform_buffer.h" #include "renderer/uniform_input.h" @@ -16,15 +15,13 @@ CameraManager::CameraManager(const std::shared_ptr &ca move_motion_directions{static_cast(MoveDirection::NONE)}, zoom_motion_direction{static_cast(ZoomDirection::NONE)}, move_motion_speed{0.2f}, - zoom_motion_speed{0.05f} { + zoom_motion_speed{0.05f}, + camera_boundaries(CameraBoundaries(X_MIN, X_MAX, Z_MIN, Z_MAX)) { this->uniforms = this->camera->get_uniform_buffer()->new_uniform_input( "view", camera->get_view_matrix(), "proj", camera->get_projection_matrix()); - - x_bounds = std::make_pair(XMIN, XMAX); - z_bounds = std::make_pair(ZMIN, ZMAX); } void CameraManager::update() { @@ -37,18 +34,18 @@ void CameraManager::move_frame(MoveDirection direction, float speed) { case MoveDirection::LEFT: // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), this->x_bounds, this->z_bounds, speed / 2); + this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), speed / 2, this->camera_boundaries); break; case MoveDirection::RIGHT: // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), this->x_bounds, this->z_bounds, speed / 2); + this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), speed / 2, this->camera_boundaries); break; case MoveDirection::FORWARD: - this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), this->x_bounds, this->z_bounds, speed); + this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), speed, this->camera_boundaries); break; case MoveDirection::BACKWARD: - this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), this->x_bounds, this->z_bounds, speed); + this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), speed, this->camera_boundaries); break; default: @@ -87,7 +84,7 @@ void CameraManager::update_motion() { move_dir += Eigen::Vector3f(1.0f, 0.0f, 1.0f); } - this->camera->move_rel(move_dir, this->x_bounds, this->z_bounds, this->move_motion_speed); + this->camera->move_rel(move_dir, this->move_motion_speed, this->camera_boundaries); } if (this->zoom_motion_direction != static_cast(ZoomDirection::NONE)) { diff --git a/libopenage/renderer/stages/camera/manager.h b/libopenage/renderer/stages/camera/manager.h index 73bb06a506..22773a3b25 100644 --- a/libopenage/renderer/stages/camera/manager.h +++ b/libopenage/renderer/stages/camera/manager.h @@ -5,6 +5,7 @@ #include #include +#include "renderer/camera/camera.h" namespace openage::renderer { class UniformBufferInput; @@ -12,6 +13,7 @@ class UniformBufferInput; namespace camera { class Camera; +struct CameraBoundaries; enum class MoveDirection { NONE = 0x0000, @@ -146,15 +148,10 @@ class CameraManager { std::shared_ptr uniforms; /** - * x and z bounds for the camera. + * Camera boundaries for X and Z movement. Contains minimum and maximum values for each axes. */ - std::pair x_bounds, z_bounds; + struct CameraBoundaries camera_boundaries; - /** - * Constant values for the camera bounds. - * TODO: Make boundaries dynamic based on map size. - */ - const float XMIN = 12.25f, XMAX = 32.25f, ZMIN = -8.25f, ZMAX = 12.25f; }; } // namespace camera From b907a7dae80a74c7b19d68b4e25800a4daaf30ca Mon Sep 17 00:00:00 2001 From: edvin Date: Fri, 18 Oct 2024 14:55:44 +0200 Subject: [PATCH 03/11] Added constructor for CameraBoundaries. --- libopenage/renderer/camera/camera.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libopenage/renderer/camera/camera.h b/libopenage/renderer/camera/camera.h index 1ba0e3bdfb..c477c184a2 100644 --- a/libopenage/renderer/camera/camera.h +++ b/libopenage/renderer/camera/camera.h @@ -30,6 +30,9 @@ namespace camera { */ struct CameraBoundaries { const float x_min, x_max, z_min, z_max; + + CameraBoundaries(float x_min, float x_max, float z_min, float z_max) + : x_min(x_min), x_max(x_max), z_min(z_min), z_max(z_max) {} }; /** From 780949f52f599e98dbb70975acce39e9dd5e26c6 Mon Sep 17 00:00:00 2001 From: edvin Date: Sun, 20 Oct 2024 15:51:48 +0200 Subject: [PATCH 04/11] Struct refactoring --- libopenage/renderer/camera/camera.cpp | 2 +- libopenage/renderer/camera/camera.h | 7 ++----- libopenage/renderer/stages/camera/manager.cpp | 2 +- libopenage/renderer/stages/camera/manager.h | 2 +- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/libopenage/renderer/camera/camera.cpp b/libopenage/renderer/camera/camera.cpp index 2ea7adc4c9..ec5a926594 100644 --- a/libopenage/renderer/camera/camera.cpp +++ b/libopenage/renderer/camera/camera.cpp @@ -80,7 +80,7 @@ void Camera::move_rel(Eigen::Vector3f direction, float delta) { this->move_to(this->scene_pos + (direction * delta)); } -void Camera::move_rel(Eigen::Vector3f direction, float delta, struct CameraBoundaries camera_boundaries) { +void Camera::move_rel(Eigen::Vector3f direction, float delta, CameraBoundaries camera_boundaries) { auto new_pos = calc_look_at(this->scene_pos + (direction * delta)); new_pos[0] = std::clamp(new_pos[0], camera_boundaries.x_min, camera_boundaries.x_max); diff --git a/libopenage/renderer/camera/camera.h b/libopenage/renderer/camera/camera.h index c477c184a2..132ee4ed81 100644 --- a/libopenage/renderer/camera/camera.h +++ b/libopenage/renderer/camera/camera.h @@ -29,10 +29,7 @@ namespace camera { * Defines constant boundaries for the camera's view in the X and Z axes. */ struct CameraBoundaries { - const float x_min, x_max, z_min, z_max; - - CameraBoundaries(float x_min, float x_max, float z_min, float z_max) - : x_min(x_min), x_max(x_max), z_min(z_min), z_max(z_max) {} + float x_min, x_max, z_min, z_max; }; /** @@ -120,7 +117,7 @@ class Camera { * the positional vector. * @param camera_boundaries X and Z boundaries for the camera in the scene. */ - void move_rel(Eigen::Vector3f direction, float delta, struct CameraBoundaries camera_boundaries); + void move_rel(Eigen::Vector3f direction, float delta, CameraBoundaries camera_boundaries); /** * Set the zoom level of the camera. Values smaller than 1.0f let the diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 380657b6db..0574dcc47e 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -16,7 +16,7 @@ CameraManager::CameraManager(const std::shared_ptr &ca zoom_motion_direction{static_cast(ZoomDirection::NONE)}, move_motion_speed{0.2f}, zoom_motion_speed{0.05f}, - camera_boundaries(CameraBoundaries(X_MIN, X_MAX, Z_MIN, Z_MAX)) { + camera_boundaries({X_MIN, X_MAX, Z_MIN, Z_MAX}) { this->uniforms = this->camera->get_uniform_buffer()->new_uniform_input( "view", camera->get_view_matrix(), diff --git a/libopenage/renderer/stages/camera/manager.h b/libopenage/renderer/stages/camera/manager.h index 22773a3b25..d5dcf07b46 100644 --- a/libopenage/renderer/stages/camera/manager.h +++ b/libopenage/renderer/stages/camera/manager.h @@ -150,7 +150,7 @@ class CameraManager { /** * Camera boundaries for X and Z movement. Contains minimum and maximum values for each axes. */ - struct CameraBoundaries camera_boundaries; + CameraBoundaries camera_boundaries; }; From 15565029113a156b9f2d3fdd29281c312f44fa98 Mon Sep 17 00:00:00 2001 From: edvin Date: Thu, 24 Oct 2024 08:33:01 +0200 Subject: [PATCH 05/11] Refactoring and cleanup --- libopenage/renderer/camera/boundaries.h | 25 +++++++++++++++++ libopenage/renderer/camera/camera.cpp | 18 +++++-------- libopenage/renderer/camera/camera.h | 27 ++++--------------- libopenage/renderer/camera/definitions.h | 12 ++++++++- libopenage/renderer/demo/demo_6.h | 4 --- libopenage/renderer/stages/camera/manager.cpp | 2 +- libopenage/renderer/stages/camera/manager.h | 2 -- 7 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 libopenage/renderer/camera/boundaries.h diff --git a/libopenage/renderer/camera/boundaries.h b/libopenage/renderer/camera/boundaries.h new file mode 100644 index 0000000000..9368815bef --- /dev/null +++ b/libopenage/renderer/camera/boundaries.h @@ -0,0 +1,25 @@ +// Copyright 2022-2024 the openage authors. See copying.md for legal info. + +#pragma once + +namespace openage::renderer::camera { + +/** + * Defines boundaries for the camera's view. + */ +struct CameraBoundaries { + // The minimum boundary for the camera's X-coordinate. + float x_min; + // The maximum boundary for the camera's X-coordinate. + float x_max; + // The minimum boundary for the camera's Y-coordinate. + float y_min; + // The maximum boundary for the camera's Y-coordinate. + float y_max; + // The minimum boundary for the camera's Z-coordinate. + float z_min; + // The maximum boundary for the camera's Z-coordinate. + float z_max; +}; + +} // namespace openage::renderer::camera diff --git a/libopenage/renderer/camera/camera.cpp b/libopenage/renderer/camera/camera.cpp index ec5a926594..a0321f110f 100644 --- a/libopenage/renderer/camera/camera.cpp +++ b/libopenage/renderer/camera/camera.cpp @@ -71,22 +71,16 @@ void Camera::look_at_coord(coord::scene3 coord_pos) { this->look_at_scene(scene_pos); } -void Camera::move_to(Eigen::Vector3f scene_pos) { +void Camera::move_to(Eigen::Vector3f scene_pos, const CameraBoundaries &camera_boundaries) { + scene_pos[0] = std::clamp(scene_pos[0], camera_boundaries.x_min, camera_boundaries.x_max); + scene_pos[2] = std::clamp(scene_pos[2], camera_boundaries.z_min, camera_boundaries.z_max); + this->scene_pos = scene_pos; this->moved = true; } -void Camera::move_rel(Eigen::Vector3f direction, float delta) { - this->move_to(this->scene_pos + (direction * delta)); -} - -void Camera::move_rel(Eigen::Vector3f direction, float delta, CameraBoundaries camera_boundaries) { - auto new_pos = calc_look_at(this->scene_pos + (direction * delta)); - - new_pos[0] = std::clamp(new_pos[0], camera_boundaries.x_min, camera_boundaries.x_max); - new_pos[2] = std::clamp(new_pos[2], camera_boundaries.z_min, camera_boundaries.z_max); - - this->move_to(new_pos); +void Camera::move_rel(Eigen::Vector3f direction, float delta, const CameraBoundaries &camera_boundaries) { + this->move_to(this->scene_pos + (direction * delta), camera_boundaries); } void Camera::set_zoom(float zoom) { diff --git a/libopenage/renderer/camera/camera.h b/libopenage/renderer/camera/camera.h index 132ee4ed81..5e5759c06e 100644 --- a/libopenage/renderer/camera/camera.h +++ b/libopenage/renderer/camera/camera.h @@ -14,6 +14,7 @@ #include "coord/scene.h" #include "util/vector.h" +#include "renderer/camera/boundaries.h" #include "renderer/camera/definitions.h" #include "renderer/camera/frustum_2d.h" #include "renderer/camera/frustum_3d.h" @@ -25,13 +26,6 @@ class UniformBuffer; namespace camera { -/** - * Defines constant boundaries for the camera's view in the X and Z axes. - */ -struct CameraBoundaries { - float x_min, x_max, z_min, z_max; -}; - /** * Camera for selecting what part of the ingame world is displayed. * @@ -92,20 +86,9 @@ class Camera { * Move the camera position in the direction of a given vector. * * @param scene_pos New 3D position of the camera in the scene. + * @param camera_boundaries 3D boundaries for the camera. */ - void move_to(Eigen::Vector3f scene_pos); - - - /** - * Move the camera position in the direction of a given vector. - * - * @param direction Direction vector. Added to the current position. - * @param delta Delta for controlling the amount by which the camera is moved. The - * value is multiplied with the directional vector before its applied to - * the positional vector. - */ - void move_rel(Eigen::Vector3f direction, float delta = 1.0f); - + void move_to(Eigen::Vector3f scene_pos, const CameraBoundaries &camera_boundaries = DEFAULT_CAM_BOUNDARIES); /** * Move the camera position in the direction of a given vector taking the @@ -115,9 +98,9 @@ class Camera { * @param delta Delta for controlling the amount by which the camera is moved. The * value is multiplied with the directional vector before its applied to * the positional vector. - * @param camera_boundaries X and Z boundaries for the camera in the scene. + * @param camera_boundaries 3D boundaries for the camera. */ - void move_rel(Eigen::Vector3f direction, float delta, CameraBoundaries camera_boundaries); + void move_rel(Eigen::Vector3f direction, float delta = 1.0f, const CameraBoundaries &camera_boundaries = DEFAULT_CAM_BOUNDARIES); /** * Set the zoom level of the camera. Values smaller than 1.0f let the diff --git a/libopenage/renderer/camera/definitions.h b/libopenage/renderer/camera/definitions.h index 1a35c7fe5e..21f9e43ed7 100644 --- a/libopenage/renderer/camera/definitions.h +++ b/libopenage/renderer/camera/definitions.h @@ -3,7 +3,9 @@ #pragma once #include +#include +#include "renderer/camera/boundaries.h" namespace openage::renderer::camera { @@ -58,10 +60,18 @@ static constexpr float DEFAULT_MAX_ZOOM_OUT = 64.0f; */ static constexpr float DEFAULT_ZOOM_RATIO = 1.0f / 49; +static constexpr CameraBoundaries DEFAULT_CAM_BOUNDARIES{ + std::numeric_limits::min(), + std::numeric_limits::max(), + std::numeric_limits::min(), + std::numeric_limits::max(), + std::numeric_limits::min(), + std::numeric_limits::max()}; + /** * Constant values for the camera bounds. * TODO: Make boundaries dynamic based on map size. */ -static const float X_MIN = 12.25f, X_MAX = 32.25f, Z_MIN = -8.25f, Z_MAX = 12.25f; +static constexpr float X_MIN = 12.25f, X_MAX = 32.25f, Z_MIN = -8.25f, Z_MAX = 12.25f; } // namespace openage::renderer::camera diff --git a/libopenage/renderer/demo/demo_6.h b/libopenage/renderer/demo/demo_6.h index 279276122a..f86728819b 100644 --- a/libopenage/renderer/demo/demo_6.h +++ b/libopenage/renderer/demo/demo_6.h @@ -19,7 +19,6 @@ class Texture2d; namespace camera { class Camera; -class CameraManager; } namespace gui { @@ -73,9 +72,6 @@ class RenderManagerDemo6 { /// Camera std::shared_ptr camera; - /// Camera manager - std::shared_ptr camera_manager; - /// Render passes std::shared_ptr obj_2d_pass; std::shared_ptr obj_3d_pass; diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 0574dcc47e..88a5dea1ac 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -16,7 +16,7 @@ CameraManager::CameraManager(const std::shared_ptr &ca zoom_motion_direction{static_cast(ZoomDirection::NONE)}, move_motion_speed{0.2f}, zoom_motion_speed{0.05f}, - camera_boundaries({X_MIN, X_MAX, Z_MIN, Z_MAX}) { + camera_boundaries{X_MIN, X_MAX, 0.0f, 0.0f, Z_MIN, Z_MAX} { this->uniforms = this->camera->get_uniform_buffer()->new_uniform_input( "view", camera->get_view_matrix(), diff --git a/libopenage/renderer/stages/camera/manager.h b/libopenage/renderer/stages/camera/manager.h index d5dcf07b46..689f917416 100644 --- a/libopenage/renderer/stages/camera/manager.h +++ b/libopenage/renderer/stages/camera/manager.h @@ -13,7 +13,6 @@ class UniformBufferInput; namespace camera { class Camera; -struct CameraBoundaries; enum class MoveDirection { NONE = 0x0000, @@ -151,7 +150,6 @@ class CameraManager { * Camera boundaries for X and Z movement. Contains minimum and maximum values for each axes. */ CameraBoundaries camera_boundaries; - }; } // namespace camera From 0c129fb24428d19d23eb3a509f63d782dae368a8 Mon Sep 17 00:00:00 2001 From: edvin Date: Thu, 24 Oct 2024 08:59:48 +0200 Subject: [PATCH 06/11] Changed default minimum boundary to be largest negative value instead of smallest positive value. --- libopenage/renderer/camera/definitions.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libopenage/renderer/camera/definitions.h b/libopenage/renderer/camera/definitions.h index 21f9e43ed7..5a4d57c01a 100644 --- a/libopenage/renderer/camera/definitions.h +++ b/libopenage/renderer/camera/definitions.h @@ -61,11 +61,11 @@ static constexpr float DEFAULT_MAX_ZOOM_OUT = 64.0f; static constexpr float DEFAULT_ZOOM_RATIO = 1.0f / 49; static constexpr CameraBoundaries DEFAULT_CAM_BOUNDARIES{ - std::numeric_limits::min(), + std::numeric_limits::lowest(), std::numeric_limits::max(), - std::numeric_limits::min(), + std::numeric_limits::lowest(), std::numeric_limits::max(), - std::numeric_limits::min(), + std::numeric_limits::lowest(), std::numeric_limits::max()}; /** From 59cee6a417ebfa2b8e8b5bb3afae0bd053261860 Mon Sep 17 00:00:00 2001 From: edvin Date: Thu, 31 Oct 2024 09:00:31 +0100 Subject: [PATCH 07/11] Fixes for Y boundary --- libopenage/renderer/camera/boundaries.h | 12 ++++++------ libopenage/renderer/camera/camera.cpp | 1 + libopenage/renderer/camera/definitions.h | 2 +- libopenage/renderer/stages/camera/manager.cpp | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/libopenage/renderer/camera/boundaries.h b/libopenage/renderer/camera/boundaries.h index 9368815bef..63a63a7a39 100644 --- a/libopenage/renderer/camera/boundaries.h +++ b/libopenage/renderer/camera/boundaries.h @@ -8,17 +8,17 @@ namespace openage::renderer::camera { * Defines boundaries for the camera's view. */ struct CameraBoundaries { - // The minimum boundary for the camera's X-coordinate. + /// The minimum boundary for the camera's X-coordinate. float x_min; - // The maximum boundary for the camera's X-coordinate. + /// The maximum boundary for the camera's X-coordinate. float x_max; - // The minimum boundary for the camera's Y-coordinate. + /// The minimum boundary for the camera's Y-coordinate. float y_min; - // The maximum boundary for the camera's Y-coordinate. + /// The maximum boundary for the camera's Y-coordinate. float y_max; - // The minimum boundary for the camera's Z-coordinate. + /// The minimum boundary for the camera's Z-coordinate. float z_min; - // The maximum boundary for the camera's Z-coordinate. + /// The maximum boundary for the camera's Z-coordinate. float z_max; }; diff --git a/libopenage/renderer/camera/camera.cpp b/libopenage/renderer/camera/camera.cpp index a0321f110f..14504998e0 100644 --- a/libopenage/renderer/camera/camera.cpp +++ b/libopenage/renderer/camera/camera.cpp @@ -73,6 +73,7 @@ void Camera::look_at_coord(coord::scene3 coord_pos) { void Camera::move_to(Eigen::Vector3f scene_pos, const CameraBoundaries &camera_boundaries) { scene_pos[0] = std::clamp(scene_pos[0], camera_boundaries.x_min, camera_boundaries.x_max); + scene_pos[1] = std::clamp(scene_pos[1], camera_boundaries.y_min, camera_boundaries.y_max); scene_pos[2] = std::clamp(scene_pos[2], camera_boundaries.z_min, camera_boundaries.z_max); this->scene_pos = scene_pos; diff --git a/libopenage/renderer/camera/definitions.h b/libopenage/renderer/camera/definitions.h index 5a4d57c01a..4c801269c3 100644 --- a/libopenage/renderer/camera/definitions.h +++ b/libopenage/renderer/camera/definitions.h @@ -72,6 +72,6 @@ static constexpr CameraBoundaries DEFAULT_CAM_BOUNDARIES{ * Constant values for the camera bounds. * TODO: Make boundaries dynamic based on map size. */ -static constexpr float X_MIN = 12.25f, X_MAX = 32.25f, Z_MIN = -8.25f, Z_MAX = 12.25f; +static constexpr float X_MIN = 12.25f, X_MAX = 32.25f, Y_MIN = 0.0f, Y_MAX = 20.0f, Z_MIN = -8.25f, Z_MAX = 12.25f; } // namespace openage::renderer::camera diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 88a5dea1ac..207fdab319 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -16,7 +16,7 @@ CameraManager::CameraManager(const std::shared_ptr &ca zoom_motion_direction{static_cast(ZoomDirection::NONE)}, move_motion_speed{0.2f}, zoom_motion_speed{0.05f}, - camera_boundaries{X_MIN, X_MAX, 0.0f, 0.0f, Z_MIN, Z_MAX} { + camera_boundaries{X_MIN, X_MAX, Y_MIN, Y_MAX, Z_MIN, Z_MAX} { this->uniforms = this->camera->get_uniform_buffer()->new_uniform_input( "view", camera->get_view_matrix(), From af804e676281365efea44d82893953a9c043dc68 Mon Sep 17 00:00:00 2001 From: heinezen Date: Thu, 7 Nov 2024 22:36:53 +0100 Subject: [PATCH 08/11] renderer: Create boundaries.cpp file for compilation checks. --- libopenage/renderer/camera/CMakeLists.txt | 1 + libopenage/renderer/camera/boundaries.cpp | 9 +++++++++ libopenage/renderer/camera/boundaries.h | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 libopenage/renderer/camera/boundaries.cpp diff --git a/libopenage/renderer/camera/CMakeLists.txt b/libopenage/renderer/camera/CMakeLists.txt index aea6391e7a..71de5c7bd1 100644 --- a/libopenage/renderer/camera/CMakeLists.txt +++ b/libopenage/renderer/camera/CMakeLists.txt @@ -1,4 +1,5 @@ add_sources(libopenage + boundaries.cpp camera.cpp definitions.cpp frustum_2d.cpp diff --git a/libopenage/renderer/camera/boundaries.cpp b/libopenage/renderer/camera/boundaries.cpp new file mode 100644 index 0000000000..ca77344c7e --- /dev/null +++ b/libopenage/renderer/camera/boundaries.cpp @@ -0,0 +1,9 @@ +// Copyright 2024-2024 the openage authors. See copying.md for legal info. + +#include "boundaries.h" + + +namespace openage::renderer::camera { + + +} // namespace openage::renderer::camera diff --git a/libopenage/renderer/camera/boundaries.h b/libopenage/renderer/camera/boundaries.h index 63a63a7a39..484a398455 100644 --- a/libopenage/renderer/camera/boundaries.h +++ b/libopenage/renderer/camera/boundaries.h @@ -1,4 +1,4 @@ -// Copyright 2022-2024 the openage authors. See copying.md for legal info. +// Copyright 2024-2024 the openage authors. See copying.md for legal info. #pragma once From 179e1adfb7261983190a15673808fd0c9345ed19 Mon Sep 17 00:00:00 2001 From: heinezen Date: Thu, 7 Nov 2024 22:54:10 +0100 Subject: [PATCH 09/11] renderer: Add method to change camera boundaries in cam manager. --- libopenage/presenter/presenter.cpp | 10 ++++++++++ libopenage/renderer/demo/demo_3.cpp | 12 ++++++++++++ libopenage/renderer/stages/camera/manager.cpp | 9 +++++++-- libopenage/renderer/stages/camera/manager.h | 11 ++++++++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/libopenage/presenter/presenter.cpp b/libopenage/presenter/presenter.cpp index 33c798bcef..ffb215135f 100644 --- a/libopenage/presenter/presenter.cpp +++ b/libopenage/presenter/presenter.cpp @@ -121,7 +121,17 @@ void Presenter::init_graphics(bool debug) { this->camera->resize(w, h); }); + // Camera manager this->camera_manager = std::make_shared(this->camera); + // TODO: Make boundaries dynamic based on map size. + this->camera_manager->set_camera_boundaries( + renderer::camera::CameraBoundaries{ + renderer::camera::X_MIN, + renderer::camera::X_MAX, + renderer::camera::Y_MIN, + renderer::camera::Y_MAX, + renderer::camera::Z_MIN, + renderer::camera::Z_MAX}); // Skybox this->skybox_renderer = std::make_shared( diff --git a/libopenage/renderer/demo/demo_3.cpp b/libopenage/renderer/demo/demo_3.cpp index 9c294285f9..7de6372303 100644 --- a/libopenage/renderer/demo/demo_3.cpp +++ b/libopenage/renderer/demo/demo_3.cpp @@ -52,6 +52,18 @@ void renderer_demo_3(const util::Path &path) { // it is updated each frame before the render stages auto cam_manager = std::make_shared(camera); + // Set boundaries for camera movement in the scene + // this restricts camera movement to the area defined by the boundaries + // i.e. the map terrain in this case + cam_manager->set_camera_boundaries( + camera::CameraBoundaries{ + 12.25f, + 22.25f, + 0.0f, + 20.0f, + 2.25f, + 12.25f}); + // Render stages // every stage use a different subrenderer that manages renderables, // shaders, textures & more. diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 207fdab319..d3cbd3f3b2 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -10,13 +10,14 @@ namespace openage::renderer::camera { -CameraManager::CameraManager(const std::shared_ptr &camera) : +CameraManager::CameraManager(const std::shared_ptr &camera, + const CameraBoundaries &camera_boundaries) : camera{camera}, move_motion_directions{static_cast(MoveDirection::NONE)}, zoom_motion_direction{static_cast(ZoomDirection::NONE)}, move_motion_speed{0.2f}, zoom_motion_speed{0.05f}, - camera_boundaries{X_MIN, X_MAX, Y_MIN, Y_MAX, Z_MIN, Z_MAX} { + camera_boundaries{camera_boundaries} { this->uniforms = this->camera->get_uniform_buffer()->new_uniform_input( "view", camera->get_view_matrix(), @@ -67,6 +68,10 @@ void CameraManager::zoom_frame(ZoomDirection direction, float speed) { } } +void CameraManager::set_camera_boundaries(const CameraBoundaries &camera_boundaries) { + this->camera_boundaries = camera_boundaries; +} + void CameraManager::update_motion() { if (this->move_motion_directions != static_cast(MoveDirection::NONE)) { Eigen::Vector3f move_dir{0.0f, 0.0f, 0.0f}; diff --git a/libopenage/renderer/stages/camera/manager.h b/libopenage/renderer/stages/camera/manager.h index 689f917416..36d605e959 100644 --- a/libopenage/renderer/stages/camera/manager.h +++ b/libopenage/renderer/stages/camera/manager.h @@ -49,8 +49,10 @@ class CameraManager { * Create a new camera manager. * * @param camera Camera to manage. + * @param camera_boundaries Boundaries for the camera movement in the scene. */ - CameraManager(const std::shared_ptr &camera); + CameraManager(const std::shared_ptr &camera, + const CameraBoundaries &camera_boundaries = DEFAULT_CAM_BOUNDARIES); ~CameraManager() = default; /** @@ -105,6 +107,13 @@ class CameraManager { */ void set_zoom_motion_speed(float speed); + /** + * Set boundaries for camera movement in the scene. + * + * @param camera_boundaries XYZ boundaries for the camera movement. + */ + void set_camera_boundaries(const CameraBoundaries &camera_boundaries); + private: /** * Update the camera parameters. From 6d6a2dd168590ea1f4e04efd15af9e449a431e39 Mon Sep 17 00:00:00 2001 From: heinezen Date: Thu, 7 Nov 2024 22:59:58 +0100 Subject: [PATCH 10/11] renderer: Rename current hardcoded camera boundaries. --- libopenage/presenter/presenter.cpp | 12 ++++++------ libopenage/renderer/camera/definitions.h | 9 +++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/libopenage/presenter/presenter.cpp b/libopenage/presenter/presenter.cpp index ffb215135f..a37362be95 100644 --- a/libopenage/presenter/presenter.cpp +++ b/libopenage/presenter/presenter.cpp @@ -126,12 +126,12 @@ void Presenter::init_graphics(bool debug) { // TODO: Make boundaries dynamic based on map size. this->camera_manager->set_camera_boundaries( renderer::camera::CameraBoundaries{ - renderer::camera::X_MIN, - renderer::camera::X_MAX, - renderer::camera::Y_MIN, - renderer::camera::Y_MAX, - renderer::camera::Z_MIN, - renderer::camera::Z_MAX}); + renderer::camera::X_BOUND_MIN, + renderer::camera::X_BOUND_MAX, + renderer::camera::Y_BOUND_MIN, + renderer::camera::Y_BOUND_MAX, + renderer::camera::Z_BOUND_MIN, + renderer::camera::Z_BOUND_MAX}); // Skybox this->skybox_renderer = std::make_shared( diff --git a/libopenage/renderer/camera/definitions.h b/libopenage/renderer/camera/definitions.h index 4c801269c3..7c3bbdd5d3 100644 --- a/libopenage/renderer/camera/definitions.h +++ b/libopenage/renderer/camera/definitions.h @@ -69,9 +69,14 @@ static constexpr CameraBoundaries DEFAULT_CAM_BOUNDARIES{ std::numeric_limits::max()}; /** - * Constant values for the camera bounds. + * Constant values for the camera bounds (based on current fix terrain grid of 20x20). * TODO: Make boundaries dynamic based on map size. */ -static constexpr float X_MIN = 12.25f, X_MAX = 32.25f, Y_MIN = 0.0f, Y_MAX = 20.0f, Z_MIN = -8.25f, Z_MAX = 12.25f; +static constexpr float X_BOUND_MIN = 12.25f; +static constexpr float X_BOUND_MAX = 32.25f; +static constexpr float Y_BOUND_MIN = 0.0f; +static constexpr float Y_BOUND_MAX = 20.0f; +static constexpr float Z_BOUND_MIN = -8.25f; +static constexpr float Z_BOUND_MAX = 12.25f; } // namespace openage::renderer::camera From a3376ca234d4101ff104d2921a9e582834372a08 Mon Sep 17 00:00:00 2001 From: heinezen Date: Thu, 7 Nov 2024 23:07:26 +0100 Subject: [PATCH 11/11] Include missing email for #1691 --- copying.md | 1 + 1 file changed, 1 insertion(+) diff --git a/copying.md b/copying.md index 1002c8321a..ec1ddd2a46 100644 --- a/copying.md +++ b/copying.md @@ -155,6 +155,7 @@ _the openage authors_ are: | Michael Seibt | RoboSchmied | github à roboschmie dawt de | | Nikhil Ghosh | NikhilGhosh75 | nghosh606 à gmail dawt com | | Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com | +| Jeremiah Morgan | jere8184 | jeremiahmorgan dawt bham à outlook dawt com | If you're a first-time committer, add yourself to the above list. This is not just for legal reasons, but also to keep an overview of all those nicknames.