Skip to content

Commit

Permalink
renderer: Return copies instead of referenced in terrain render enity.
Browse files Browse the repository at this point in the history
Makes the updates thread-safe.
  • Loading branch information
heinezen committed Oct 3, 2024
1 parent 95c9d1a commit ee35b00
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
25 changes: 16 additions & 9 deletions libopenage/renderer/stages/terrain/chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ void TerrainChunk::fetch_updates(const time::time_t & /* time */) {
if (not this->render_entity->is_changed()) {
return;
}

// Get the terrain data from the render entity
auto terrain_size = this->render_entity->get_size();
auto terrain_paths = this->render_entity->get_terrain_paths();
auto tiles = this->render_entity->get_tiles();
auto heightmap_verts = this->render_entity->get_vertices();

// Recreate the mesh data
// TODO: Change mesh instead of recreating it
// TODO: Multiple meshes
this->meshes.clear();
for (const auto &terrain_path : this->render_entity->get_terrain_paths()) {
auto new_mesh = this->create_mesh(terrain_path);
for (const auto &terrain_path : terrain_paths) {
auto new_mesh = this->create_mesh(terrain_size, tiles, heightmap_verts, terrain_path);
new_mesh->create_model_matrix(this->offset);
this->meshes.push_back(new_mesh);
}
Expand All @@ -59,13 +67,12 @@ const std::vector<std::shared_ptr<TerrainRenderMesh>> &TerrainChunk::get_meshes(
return this->meshes;
}

std::shared_ptr<TerrainRenderMesh> TerrainChunk::create_mesh(const std::string &texture_path) {
auto size = this->render_entity->get_size();
auto v_width = size[0];
auto v_height = size[1];

auto tiles = this->render_entity->get_tiles();
auto heightmap_verts = this->render_entity->get_vertices();
std::shared_ptr<TerrainRenderMesh> TerrainChunk::create_mesh(const util::Vector2s vert_size,
const RenderEntity::tiles_t &tiles,
const std::vector<coord::scene3> &heightmap_verts,
const std::string &texture_path) {
auto v_width = vert_size[0];
auto v_height = vert_size[1];

// vertex data for the mesh
std::vector<float> mesh_verts{};
Expand Down
12 changes: 10 additions & 2 deletions libopenage/renderer/stages/terrain/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>

#include "coord/scene.h"
#include "renderer/stages/terrain/render_entity.h"
#include "time/time.h"
#include "util/vector.h"

Expand All @@ -19,7 +20,6 @@ class AssetManager;

namespace terrain {
class TerrainRenderMesh;
class RenderEntity;

/**
* Stores the state of a terrain chunk in the terrain render stage.
Expand Down Expand Up @@ -85,9 +85,17 @@ class TerrainChunk {
/**
* Create a terrain mesh from the data provided by the render entity.
*
* @param vert_size Size of the terrain in vertices.
* @param tiles Data for each tile (elevation, terrain path).
* @param heightmap_verts Position of each vertex in the chunk.
* @param texture_path Path to the texture for the terrain.
*
* @return New terrain mesh.
*/
std::shared_ptr<TerrainRenderMesh> create_mesh(const std::string &texture_path);
std::shared_ptr<TerrainRenderMesh> create_mesh(const util::Vector2s vert_size,
const RenderEntity::tiles_t &tiles,
const std::vector<coord::scene3> &heightmap_verts,
const std::string &texture_path);

/**
* Size of the chunk in tiles (width x height).
Expand Down
12 changes: 5 additions & 7 deletions libopenage/renderer/stages/terrain/render_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ RenderEntity::RenderEntity() :
size{0, 0},
tiles{},
terrain_paths{},
vertices{}
// terrain_path{nullptr, 0},
{
vertices{} {
}

void RenderEntity::update_tile(const util::Vector2s size,
Expand Down Expand Up @@ -107,25 +105,25 @@ void RenderEntity::update(const util::Vector2s size,
this->changed = true;
}

const std::vector<coord::scene3> &RenderEntity::get_vertices() {
const std::vector<coord::scene3> RenderEntity::get_vertices() {
std::shared_lock lock{this->mutex};

return this->vertices;
}

const RenderEntity::tiles_t &RenderEntity::get_tiles() {
const RenderEntity::tiles_t RenderEntity::get_tiles() {
std::shared_lock lock{this->mutex};

return this->tiles;
}

const std::unordered_set<std::string> &RenderEntity::get_terrain_paths() {
const std::unordered_set<std::string> RenderEntity::get_terrain_paths() {
std::shared_lock lock{this->mutex};

return this->terrain_paths;
}

const util::Vector2s &RenderEntity::get_size() {
const util::Vector2s RenderEntity::get_size() {
std::shared_lock lock{this->mutex};

return this->size;
Expand Down
20 changes: 16 additions & 4 deletions libopenage/renderer/stages/terrain/render_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class RenderEntity final : public renderer::RenderEntity {
* Update a single tile of the displayed terrain (chunk) with information from the
* gamestate.
*
* Updating the render entity with this method is thread-safe.
*
* @param size Size of the terrain in tiles (width x length)
* @param pos Position of the tile in the chunk.
* @param elevation Height of terrain tile.
Expand All @@ -46,6 +48,8 @@ class RenderEntity final : public renderer::RenderEntity {
* Update the full grid of the displayed terrain (chunk) with information from the
* gamestate.
*
* Updating the render entity with this method is thread-safe.
*
* @param size Size of the terrain in tiles (width x length)
* @param tiles Animation data for each tile (elevation, terrain path).
* @param time Simulation time of the update.
Expand All @@ -57,30 +61,38 @@ class RenderEntity final : public renderer::RenderEntity {
/**
* Get the vertices of the terrain.
*
* Accessing the terrain vertices is thread-safe.
*
* @return Vector of vertex coordinates.
*/
const std::vector<coord::scene3> &get_vertices();
const std::vector<coord::scene3> get_vertices();

/**
* Get the tiles of the terrain.
*
* Accessing the terrain tiles is thread-safe.
*
* @return Terrain tiles.
*/
const tiles_t &get_tiles();
const tiles_t get_tiles();

/**
* Get the terrain paths used in the terrain.
*
* Accessing the terrain paths is thread-safe.
*
* @return Terrain paths.
*/
const std::unordered_set<std::string> &get_terrain_paths();
const std::unordered_set<std::string> get_terrain_paths();

/**
* Get the number of vertices on each side of the terrain.
*
* Accessing the vertices size is thread-safe.
*
* @return Vector with width as first element and height as second element.
*/
const util::Vector2s &get_size();
const util::Vector2s get_size();

private:
/**
Expand Down

0 comments on commit ee35b00

Please sign in to comment.