Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

EdvinLndh
Copy link
Contributor

@EdvinLndh EdvinLndh commented Sep 25, 2024

Camera boundaries are set using constants for now, will probably look into setting the boundaries dynamically soon. #1682

@heinezen heinezen added area: renderer Concerns our graphics renderer improvement Enhancement of an existing component nice new thing ☺ A new feature that was not there before lang: c++ Done in C++ code labels Sep 25, 2024
@heinezen heinezen linked an issue Sep 25, 2024 that may be closed by this pull request
4 tasks
Copy link
Member

@heinezen heinezen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far!

I have a few comments regarding the current status.

Comment on lines 108 to 114
// 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move_to should not do the look at calculations because it is intended to just move the camera to a position in the scene. Looking at a scene coordinate is already implemented in look_at_scene. This might break existing code where move_to is currently used.

The changes here might also break look_at_scene's behavior, since the method calls move_to but with the already computed camera position. Therefore, the look at calculations would be done a second time on the already translated camera position.

Comment on lines 106 to 107
std::optional<std::pair<float, float>> x_bounds,
std::optional<std::pair<float, float>> z_bounds) {
Copy link
Member

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.

Comment on lines 131 to 132
std::pair<float, float> x_bounds,
std::pair<float, float> z_bounds,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In move_to you made them optional, but here they are non-optional. Is there a reason for this?

@@ -15,6 +15,7 @@
#include "renderer/camera/definitions.h"
#include "renderer/camera/frustum_2d.h"
#include "renderer/camera/frustum_3d.h"
#include <optional>
Copy link
Member

Choose a reason for hiding this comment

The 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:

// The associated header file comes first!
#include "mom.h"
// C++ std library includes follow, sorted alphabetically
#include <cmath>
#include <cstdio>
#include <iostream>
// External libraries are next, sorted alphabetically
#include <eigen3/Eigen/Dense>
#include <epoxy/gl.h>
// Local includes come last, sorted alphabetically
#include "../valve.h"
#include "half_life.h"
#include "log/log.h"

Comment on lines 86 to 89
* @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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should document arguments you add to methods in the docstrings above.

@@ -38,6 +39,7 @@ void renderer_demo_5(const util::Path &path) {
camera->resize(w, h);
});

camera::CameraManager camera_manager(camera);
Copy link
Member

Choose a reason for hiding this comment

The 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 CameraManager is an interface between the input system and the renderer, it is technically an additional layer that does a bunch of updates which are unnecessary for the demo.

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Comment on lines 26 to 27
x_bounds = std::make_pair(XMIN, XMAX);
z_bounds = std::make_pair(ZMIN, ZMAX);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Comment on lines 148 to 151
/**
* x and z bounds for the camera.
*/
std::pair<float, float> x_bounds, z_bounds;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Comment on lines 153 to 157
/**
* 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;
Copy link
Member

Choose a reason for hiding this comment

The 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.

@heinezen heinezen mentioned this pull request Oct 18, 2024
4 tasks
@heinezen
Copy link
Member

@EdvinLndh Are you planning to continue this one soon-ish?

@EdvinLndh
Copy link
Contributor Author

Sorry, I've been out of town for a few weeks, I'll look into it

* 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void move_rel(Eigen::Vector3f direction, float delta, struct CameraBoundaries camera_boundaries);
void move_rel(Eigen::Vector3f direction, float delta, CameraBoundaries camera_boundaries);

struct is only necessary in C, but you can omit it in C++

Comment on lines 34 to 35
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) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For structs, we usually don't declare a constructor and use a braced initializer list to create objects, especially for trivial structs like this one. Thus, you don't need to define it here.

* 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it makes sense to declare all members as const, since there's no reason why the camera boundaries should be immutable changed at all time. If we wanted a an immutable CameraBoundaries struct, we could just declare that as const instead with const CameraBoundaries example{..};

@heinezen heinezen marked this pull request as draft October 20, 2024 14:49
@heinezen
Copy link
Member

I have converted this into a draft while you work on it. Once you are done, you can set it as ready with the "Ready for review" button above.

@EdvinLndh EdvinLndh marked this pull request as ready for review October 20, 2024 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: renderer Concerns our graphics renderer improvement Enhancement of an existing component lang: c++ Done in C++ code nice new thing ☺ A new feature that was not there before
Projects
Status: 🏗 In progress
Development

Successfully merging this pull request may close these issues.

Boundaries for camera movement
2 participants