Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libopenage/gamestate/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const std::shared_ptr<GameState> &Game::get_state() const {

void Game::attach_renderer(const std::shared_ptr<renderer::RenderFactory> &render_factory) {
this->universe->attach_renderer(render_factory);
this->state->get_map()->set_camera_render_entity(render_factory->add_camera_render_entity());
this->state->get_map()->get_terrain()->attach_renderer(render_factory);
}

Expand Down
20 changes: 20 additions & 0 deletions libopenage/gamestate/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <nyan/nyan.h>

#include "gamestate/api/terrain.h"
#include "gamestate/definitions.h"
#include "gamestate/game_state.h"
#include "gamestate/terrain.h"
#include "gamestate/terrain_chunk.h"
Expand All @@ -13,6 +14,7 @@
#include "pathfinding/pathfinder.h"
#include "pathfinding/sector.h"

#include "renderer/camera/definitions.h"

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


const void Map::set_camera_render_entity(std::shared_ptr<renderer::camera::RenderEntity> renderer_entity,
const std::optional<renderer::camera::CameraBoundaries> &boundaries,
const time::time_t time) {
this->camera_render_entity = renderer_entity;
this->upadate_camera_render_entity(boundaries, time);
}

const void Map::upadate_camera_render_entity(const std::optional<renderer::camera::CameraBoundaries> &boundaries, const time::time_t time) {
if (this->camera_render_entity) {
if (boundaries.has_value())
this->camera_render_entity->update(boundaries.value(), time);
else
this->camera_render_entity->update(openage::renderer::camera::DEFAULT_CAM_BOUNDARIES, time); // TODO: caluclate bounds, from origin and map size
}
}


} // namespace openage::gamestate
12 changes: 12 additions & 0 deletions libopenage/gamestate/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
#pragma once

#include <memory>
#include <optional>
#include <unordered_map>

#include <nyan/nyan.h>

#include "pathfinding/types.h"
#include "util/vector.h"

#include "renderer/render_factory.h"
#include "renderer/stages/camera/render_entity.h"


namespace openage {
namespace path {
Expand Down Expand Up @@ -65,6 +69,12 @@ class Map {
*/
path::grid_id_t get_grid_id(const nyan::fqon_t &path_grid) const;

const void set_camera_render_entity(std::shared_ptr<renderer::camera::RenderEntity> renderer_entity,
const std::optional<renderer::camera::CameraBoundaries> &boundaries = std::nullopt,
const time::time_t time = time::TIME_ZERO);

const void upadate_camera_render_entity(const std::optional<renderer::camera::CameraBoundaries> &boundaries, const time::time_t time);

private:
/**
* Terrain.
Expand All @@ -80,6 +90,8 @@ class Map {
* Lookup table for mapping path grid objects in nyan to grid indices.
*/
std::unordered_map<nyan::fqon_t, path::grid_id_t> grid_lookup;

std::shared_ptr<renderer::camera::RenderEntity> camera_render_entity;
};

} // namespace gamestate
Expand Down
5 changes: 5 additions & 0 deletions libopenage/renderer/camera/boundaries.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Copyright 2024-2024 the openage authors. See copying.md for legal info.

#include <tuple>

#include "boundaries.h"


namespace openage::renderer::camera {

bool CameraBoundaries::operator==(const CameraBoundaries &rhs) {
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));
}

} // namespace openage::renderer::camera
2 changes: 2 additions & 0 deletions libopenage/renderer/camera/boundaries.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct CameraBoundaries {
float z_min;
/// The maximum boundary for the camera's Z-coordinate.
float z_max;

bool operator==(const CameraBoundaries &rhs);
};

} // namespace openage::renderer::camera
7 changes: 7 additions & 0 deletions libopenage/renderer/render_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "render_factory.h"

#include "coord/phys.h"
#include "renderer/stages/camera/manager.h"
#include "renderer/stages/terrain/render_entity.h"
#include "renderer/stages/terrain/render_stage.h"
#include "renderer/stages/world/render_entity.h"
Expand Down Expand Up @@ -30,4 +31,10 @@ std::shared_ptr<world::RenderEntity> RenderFactory::add_world_render_entity() {
return entity;
}

std::shared_ptr<camera::RenderEntity> RenderFactory::add_camera_render_entity() {
auto entity = std::make_shared<camera::RenderEntity>();
this->camera_manager->add_render_entity(entity);
return entity;
}

} // namespace openage::renderer
16 changes: 16 additions & 0 deletions libopenage/renderer/render_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class WorldRenderStage;
class RenderEntity;
} // namespace world

namespace camera {
class CameraManager;
class RenderEntity;
} // namespace camera


/**
* Creates render entities that push animation updates from the
* engine to the renderer. Also registers the render entities with
Expand Down Expand Up @@ -57,6 +63,10 @@ class RenderFactory {
*/
std::shared_ptr<world::RenderEntity> add_world_render_entity();


std::shared_ptr<camera::RenderEntity> add_camera_render_entity();


private:
/**
* Render stage for terrain drawing.
Expand All @@ -67,6 +77,12 @@ class RenderFactory {
* Render stage for game entity drawing.
*/
std::shared_ptr<world::WorldRenderStage> world_renderer;


/**
* Render stage for camera entity drawing.
*/
std::shared_ptr<camera::CameraManager> camera_manager;
};

} // namespace openage::renderer
1 change: 1 addition & 0 deletions libopenage/renderer/stages/camera/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_sources(libopenage
manager.cpp
render_entity.cpp
)
14 changes: 14 additions & 0 deletions libopenage/renderer/stages/camera/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CameraManager::CameraManager(const std::shared_ptr<renderer::camera::Camera> &ca
void CameraManager::update() {
this->update_motion();
this->update_uniforms();
this->poll_render_entity(); // should we need to this every update?
}

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

void CameraManager::poll_render_entity() {
if (this->render_entity->is_changed()) [[unlikely]] {
this->set_camera_boundaries(this->render_entity->get_camera_boundaries());
this->render_entity->clear_changed_flag();
}
}

void CameraManager::update_motion() {
if (this->move_motion_directions != static_cast<int>(MoveDirection::NONE)) {
Eigen::Vector3f move_dir{0.0f, 0.0f, 0.0f};
Expand Down Expand Up @@ -141,4 +149,10 @@ void CameraManager::set_zoom_motion_speed(float speed) {
this->zoom_motion_speed = speed;
}

void CameraManager::add_render_entity(const std::shared_ptr<RenderEntity> entity) {
// std::unique_lock lock{this->mutex};
this->render_entity = entity;
}


} // namespace openage::renderer::camera
7 changes: 7 additions & 0 deletions libopenage/renderer/stages/camera/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <utility>

#include "renderer/camera/camera.h"
#include "renderer/stages/camera/render_entity.h"

namespace openage::renderer {
class UniformBufferInput;
Expand Down Expand Up @@ -114,6 +115,10 @@ class CameraManager {
*/
void set_camera_boundaries(const CameraBoundaries &camera_boundaries);

void add_render_entity(const std::shared_ptr<RenderEntity> entity);

void poll_render_entity();

private:
/**
* Update the camera parameters.
Expand Down Expand Up @@ -159,6 +164,8 @@ class CameraManager {
* Camera boundaries for X and Z movement. Contains minimum and maximum values for each axes.
*/
CameraBoundaries camera_boundaries;

std::shared_ptr<RenderEntity> render_entity;
};

} // namespace camera
Expand Down
21 changes: 21 additions & 0 deletions libopenage/renderer/stages/camera/render_entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024-2025 the openage authors. See copying.md for legal info.

#pragma once

#include "render_entity.h"

namespace openage::renderer::camera {

void RenderEntity::update(const CameraBoundaries& boundaries, const time::time_t time)
{
std::unique_lock lock{this->mutex};

this->camera_boundaries = camera_boundaries;
this->last_update = time;
this->changed = true;
}

const CameraBoundaries &RenderEntity::get_camera_boundaries() {
return this->camera_boundaries;
}
}
26 changes: 26 additions & 0 deletions libopenage/renderer/stages/camera/render_entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024-2025 the openage authors. See copying.md for legal info.

#pragma once

#include "renderer/camera/boundaries.h"
#include "gamestate/definitions.h"
#include "renderer/stages/render_entity.h"


namespace openage::renderer::camera {

class RenderEntity final : public renderer::RenderEntity {
public:
RenderEntity() = default;
~RenderEntity() = default;

void update(const CameraBoundaries &camera_boundaries, const time::time_t time = 0.0);

const CameraBoundaries &get_camera_boundaries();


private:
CameraBoundaries camera_boundaries;
};

}
Loading