Skip to content

Commit

Permalink
Refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
EdvinLndh committed Oct 24, 2024
1 parent 6c715af commit 712c732
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 42 deletions.
25 changes: 25 additions & 0 deletions libopenage/renderer/camera/boundaries.h
Original file line number Diff line number Diff line change
@@ -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
18 changes: 6 additions & 12 deletions libopenage/renderer/camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 5 additions & 22 deletions libopenage/renderer/camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion libopenage/renderer/camera/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#pragma once

#include <eigen3/Eigen/Dense>
#include <limits>

#include "renderer/camera/boundaries.h"

namespace openage::renderer::camera {

Expand Down Expand Up @@ -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<float>::min(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::min(),
std::numeric_limits<float>::max(),
std::numeric_limits<float>::min(),
std::numeric_limits<float>::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
4 changes: 0 additions & 4 deletions libopenage/renderer/demo/demo_6.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Texture2d;

namespace camera {
class Camera;
class CameraManager;
}

namespace gui {
Expand Down Expand Up @@ -73,9 +72,6 @@ class RenderManagerDemo6 {
/// Camera
std::shared_ptr<camera::Camera> camera;

/// Camera manager
std::shared_ptr<camera::CameraManager> camera_manager;

/// Render passes
std::shared_ptr<renderer::RenderPass> obj_2d_pass;
std::shared_ptr<renderer::RenderPass> obj_3d_pass;
Expand Down
2 changes: 1 addition & 1 deletion libopenage/renderer/stages/camera/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CameraManager::CameraManager(const std::shared_ptr<renderer::camera::Camera> &ca
zoom_motion_direction{static_cast<int>(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(),
Expand Down
2 changes: 0 additions & 2 deletions libopenage/renderer/stages/camera/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class UniformBufferInput;
namespace camera {

class Camera;
struct CameraBoundaries;

enum class MoveDirection {
NONE = 0x0000,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 712c732

Please sign in to comment.