Skip to content

Commit 9d0af50

Browse files
committed
Renderer: Camera Bounds
1 parent 865bd54 commit 9d0af50

File tree

12 files changed

+132
-0
lines changed

12 files changed

+132
-0
lines changed

libopenage/gamestate/game.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const std::shared_ptr<GameState> &Game::get_state() const {
5454

5555
void Game::attach_renderer(const std::shared_ptr<renderer::RenderFactory> &render_factory) {
5656
this->universe->attach_renderer(render_factory);
57+
this->state->get_map()->set_camera_render_entity(render_factory->add_camera_render_entity());
5758
this->state->get_map()->get_terrain()->attach_renderer(render_factory);
5859
}
5960

libopenage/gamestate/map.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <nyan/nyan.h>
66

77
#include "gamestate/api/terrain.h"
8+
#include "gamestate/definitions.h"
89
#include "gamestate/game_state.h"
910
#include "gamestate/terrain.h"
1011
#include "gamestate/terrain_chunk.h"
@@ -13,6 +14,7 @@
1314
#include "pathfinding/pathfinder.h"
1415
#include "pathfinding/sector.h"
1516

17+
#include "renderer/camera/definitions.h"
1618

1719
namespace openage::gamestate {
1820
Map::Map(const std::shared_ptr<GameState> &state,
@@ -78,4 +80,22 @@ path::grid_id_t Map::get_grid_id(const nyan::fqon_t &path_grid) const {
7880
return this->grid_lookup.at(path_grid);
7981
}
8082

83+
84+
const void Map::set_camera_render_entity(std::shared_ptr<renderer::camera::RenderEntity> renderer_entity,
85+
const std::optional<renderer::camera::CameraBoundaries> &boundaries,
86+
const time::time_t time) {
87+
this->camera_render_entity = renderer_entity;
88+
this->upadate_camera_render_entity(boundaries, time);
89+
}
90+
91+
const void Map::upadate_camera_render_entity(const std::optional<renderer::camera::CameraBoundaries> &boundaries, const time::time_t time) {
92+
if (this->camera_render_entity) {
93+
if (boundaries.has_value())
94+
this->camera_render_entity->update(boundaries.value(), time);
95+
else
96+
this->camera_render_entity->update(openage::renderer::camera::DEFAULT_CAM_BOUNDARIES, time); // TODO: caluclate bounds, from origin and map size
97+
}
98+
}
99+
100+
81101
} // namespace openage::gamestate

libopenage/gamestate/map.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
#pragma once
44

55
#include <memory>
6+
#include <optional>
67
#include <unordered_map>
78

89
#include <nyan/nyan.h>
910

1011
#include "pathfinding/types.h"
1112
#include "util/vector.h"
1213

14+
#include "renderer/render_factory.h"
15+
#include "renderer/stages/camera/render_entity.h"
16+
1317

1418
namespace openage {
1519
namespace path {
@@ -65,6 +69,12 @@ class Map {
6569
*/
6670
path::grid_id_t get_grid_id(const nyan::fqon_t &path_grid) const;
6771

72+
const void set_camera_render_entity(std::shared_ptr<renderer::camera::RenderEntity> renderer_entity,
73+
const std::optional<renderer::camera::CameraBoundaries> &boundaries = std::nullopt,
74+
const time::time_t time = time::TIME_ZERO);
75+
76+
const void upadate_camera_render_entity(const std::optional<renderer::camera::CameraBoundaries> &boundaries, const time::time_t time);
77+
6878
private:
6979
/**
7080
* Terrain.
@@ -80,6 +90,8 @@ class Map {
8090
* Lookup table for mapping path grid objects in nyan to grid indices.
8191
*/
8292
std::unordered_map<nyan::fqon_t, path::grid_id_t> grid_lookup;
93+
94+
std::shared_ptr<renderer::camera::RenderEntity> camera_render_entity;
8395
};
8496

8597
} // namespace gamestate
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
22

3+
#include <tuple>
4+
35
#include "boundaries.h"
46

57

68
namespace openage::renderer::camera {
79

10+
bool CameraBoundaries::operator==(const CameraBoundaries &rhs) {
11+
return (std::tie(x_min, x_max, y_min, y_max, z_min, z_max) == std::tie(rhs.x_min, rhs.x_max, rhs.y_min, rhs.y_max, rhs.z_min, rhs.z_max));
12+
}
813

914
} // namespace openage::renderer::camera

libopenage/renderer/camera/boundaries.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct CameraBoundaries {
2020
float z_min;
2121
/// The maximum boundary for the camera's Z-coordinate.
2222
float z_max;
23+
24+
bool operator==(const CameraBoundaries &rhs);
2325
};
2426

2527
} // namespace openage::renderer::camera

libopenage/renderer/render_factory.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "render_factory.h"
44

55
#include "coord/phys.h"
6+
#include "renderer/stages/camera/manager.h"
67
#include "renderer/stages/terrain/render_entity.h"
78
#include "renderer/stages/terrain/render_stage.h"
89
#include "renderer/stages/world/render_entity.h"
@@ -30,4 +31,10 @@ std::shared_ptr<world::RenderEntity> RenderFactory::add_world_render_entity() {
3031
return entity;
3132
}
3233

34+
std::shared_ptr<camera::RenderEntity> RenderFactory::add_camera_render_entity() {
35+
auto entity = std::make_shared<camera::RenderEntity>();
36+
this->camera_manager->add_render_entity(entity);
37+
return entity;
38+
}
39+
3340
} // namespace openage::renderer

libopenage/renderer/render_factory.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class WorldRenderStage;
1919
class RenderEntity;
2020
} // namespace world
2121

22+
namespace camera {
23+
class CameraManager;
24+
class RenderEntity;
25+
} // namespace camera
26+
27+
2228
/**
2329
* Creates render entities that push animation updates from the
2430
* engine to the renderer. Also registers the render entities with
@@ -57,6 +63,10 @@ class RenderFactory {
5763
*/
5864
std::shared_ptr<world::RenderEntity> add_world_render_entity();
5965

66+
67+
std::shared_ptr<camera::RenderEntity> add_camera_render_entity();
68+
69+
6070
private:
6171
/**
6272
* Render stage for terrain drawing.
@@ -67,6 +77,12 @@ class RenderFactory {
6777
* Render stage for game entity drawing.
6878
*/
6979
std::shared_ptr<world::WorldRenderStage> world_renderer;
80+
81+
82+
/**
83+
* Render stage for camera entity drawing.
84+
*/
85+
std::shared_ptr<camera::CameraManager> camera_manager;
7086
};
7187

7288
} // namespace openage::renderer
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_sources(libopenage
22
manager.cpp
3+
render_entity.cpp
34
)

libopenage/renderer/stages/camera/manager.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CameraManager::CameraManager(const std::shared_ptr<renderer::camera::Camera> &ca
2828
void CameraManager::update() {
2929
this->update_motion();
3030
this->update_uniforms();
31+
this->poll_render_entity(); // should we need to this every update?
3132
}
3233

3334
void CameraManager::move_frame(MoveDirection direction, float speed) {
@@ -72,6 +73,13 @@ void CameraManager::set_camera_boundaries(const CameraBoundaries &camera_boundar
7273
this->camera_boundaries = camera_boundaries;
7374
}
7475

76+
void CameraManager::poll_render_entity() {
77+
if (this->render_entity->is_changed()) [[unlikely]] {
78+
this->set_camera_boundaries(this->render_entity->get_camera_boundaries());
79+
this->render_entity->clear_changed_flag();
80+
}
81+
}
82+
7583
void CameraManager::update_motion() {
7684
if (this->move_motion_directions != static_cast<int>(MoveDirection::NONE)) {
7785
Eigen::Vector3f move_dir{0.0f, 0.0f, 0.0f};
@@ -141,4 +149,10 @@ void CameraManager::set_zoom_motion_speed(float speed) {
141149
this->zoom_motion_speed = speed;
142150
}
143151

152+
void CameraManager::add_render_entity(const std::shared_ptr<RenderEntity> entity) {
153+
// std::unique_lock lock{this->mutex};
154+
this->render_entity = entity;
155+
}
156+
157+
144158
} // namespace openage::renderer::camera

libopenage/renderer/stages/camera/manager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <utility>
77

88
#include "renderer/camera/camera.h"
9+
#include "renderer/stages/camera/render_entity.h"
910

1011
namespace openage::renderer {
1112
class UniformBufferInput;
@@ -114,6 +115,10 @@ class CameraManager {
114115
*/
115116
void set_camera_boundaries(const CameraBoundaries &camera_boundaries);
116117

118+
void add_render_entity(const std::shared_ptr<RenderEntity> entity);
119+
120+
void poll_render_entity();
121+
117122
private:
118123
/**
119124
* Update the camera parameters.
@@ -159,6 +164,8 @@ class CameraManager {
159164
* Camera boundaries for X and Z movement. Contains minimum and maximum values for each axes.
160165
*/
161166
CameraBoundaries camera_boundaries;
167+
168+
std::shared_ptr<RenderEntity> render_entity;
162169
};
163170

164171
} // namespace camera

0 commit comments

Comments
 (0)