-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Renderer: Added camera boundaries to CameraManager and clamping #1682. #1691
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<std::pair<float, float>> x_bounds, | ||
std::optional<std::pair<float, float>> 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The changes here might also break |
||
|
||
// 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<float, float> x_bounds, | ||
std::pair<float, float> z_bounds, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
float delta) { | ||
|
||
this->move_to(this->scene_pos + (direction * delta), x_bounds, z_bounds); | ||
} | ||
|
||
void Camera::set_zoom(float zoom) { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,6 +15,7 @@ | |||||||||||||||||||||||||||||||||
#include "renderer/camera/definitions.h" | ||||||||||||||||||||||||||||||||||
#include "renderer/camera/frustum_2d.h" | ||||||||||||||||||||||||||||||||||
#include "renderer/camera/frustum_3d.h" | ||||||||||||||||||||||||||||||||||
#include <optional> | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nitpick, but imports of standard libraries should be placed before local includes in our code. See our (arguably a bit hidden) code style examples: openage/doc/code_style/mom.cpp Lines 17 to 32 in 66098ce
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
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<std::pair<float, float>> x_bounds = std::nullopt, | ||||||||||||||||||||||||||||||||||
std::optional<std::pair<float, float>> z_bounds = std::nullopt); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should document arguments you add to methods in the docstrings above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if you code formatted this but if you didn't try running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using |
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* 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<float, float> x_bounds, | ||||||||||||||||||||||||||||||||||
std::pair<float, float> z_bounds, | ||||||||||||||||||||||||||||||||||
float delta = 1.0f); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here as above:
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* Set the zoom level of the camera. Values smaller than 1.0f let the | ||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The demos in this file should probably not use the camera manager because that introduces more unnecessary complexity to them. We try to show off specific features of the renderer in the code. As |
||
/* Display the subtextures using the meta information */ | ||
log::log(INFO << "Loading shaders..."); | ||
|
||
|
@@ -142,29 +144,29 @@ 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."); | ||
} break; | ||
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."); | ||
} break; | ||
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."); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<float>(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::CameraManager>(camera); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, using the camera manager is a bit overkill for this demo. |
||
} | ||
|
||
void RenderManagerDemo6::create_render_passes() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
#include "manager.h" | ||
|
||
#include <eigen3/Eigen/Dense> | ||
#include <numbers> | ||
|
||
#include "renderer/camera/camera.h" | ||
#include "renderer/uniform_buffer.h" | ||
|
@@ -21,6 +22,9 @@ CameraManager::CameraManager(const std::shared_ptr<renderer::camera::Camera> &ca | |
camera->get_view_matrix(), | ||
"proj", | ||
camera->get_projection_matrix()); | ||
|
||
x_bounds = std::make_pair(XMIN, XMAX); | ||
z_bounds = std::make_pair(ZMIN, ZMAX); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These members should be initialized in the initializer list in the constructor above. |
||
} | ||
|
||
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<int>(ZoomDirection::NONE)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
#pragma once | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
|
||
namespace openage::renderer { | ||
|
@@ -143,6 +144,17 @@ class CameraManager { | |
* Uniform buffer input for the camera. | ||
*/ | ||
std::shared_ptr<renderer::UniformBufferInput> uniforms; | ||
|
||
/** | ||
* x and z bounds for the camera. | ||
*/ | ||
std::pair<float, float> x_bounds, z_bounds; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nitpick but the docstring should explain more about what these bounds are. Also, you should mention that the first value is the min bound and the second value is the max bound. However, if you follow up on the idea to make this a struct, it would probably not be necessary to document the latter. |
||
|
||
/** | ||
* 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could move these to https://github.com/SFTtech/openage/blob/master/libopenage/renderer/camera/definitions.h but since they are meant only for testing, this doesn't really matter. |
||
}; | ||
|
||
} // namespace camera | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using two pairs for the bounds, it probably makes sense to make these parameters a struct and pass that.