diff --git a/libopenage/renderer/stages/terrain/chunk.cpp b/libopenage/renderer/stages/terrain/chunk.cpp index e080ec523b..58a95d2a95 100644 --- a/libopenage/renderer/stages/terrain/chunk.cpp +++ b/libopenage/renderer/stages/terrain/chunk.cpp @@ -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); } @@ -59,13 +67,12 @@ const std::vector> &TerrainChunk::get_meshes( return this->meshes; } -std::shared_ptr 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 TerrainChunk::create_mesh(const util::Vector2s vert_size, + const RenderEntity::tiles_t &tiles, + const std::vector &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 mesh_verts{}; diff --git a/libopenage/renderer/stages/terrain/chunk.h b/libopenage/renderer/stages/terrain/chunk.h index b34a3db880..7d3d9fcc70 100644 --- a/libopenage/renderer/stages/terrain/chunk.h +++ b/libopenage/renderer/stages/terrain/chunk.h @@ -7,6 +7,7 @@ #include #include "coord/scene.h" +#include "renderer/stages/terrain/render_entity.h" #include "time/time.h" #include "util/vector.h" @@ -19,7 +20,6 @@ class AssetManager; namespace terrain { class TerrainRenderMesh; -class RenderEntity; /** * Stores the state of a terrain chunk in the terrain render stage. @@ -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 create_mesh(const std::string &texture_path); + std::shared_ptr create_mesh(const util::Vector2s vert_size, + const RenderEntity::tiles_t &tiles, + const std::vector &heightmap_verts, + const std::string &texture_path); /** * Size of the chunk in tiles (width x height). diff --git a/libopenage/renderer/stages/terrain/render_entity.cpp b/libopenage/renderer/stages/terrain/render_entity.cpp index 2471ecdb6d..2316f5dab7 100644 --- a/libopenage/renderer/stages/terrain/render_entity.cpp +++ b/libopenage/renderer/stages/terrain/render_entity.cpp @@ -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, @@ -107,25 +105,25 @@ void RenderEntity::update(const util::Vector2s size, this->changed = true; } -const std::vector &RenderEntity::get_vertices() { +const std::vector 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 &RenderEntity::get_terrain_paths() { +const std::unordered_set 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; diff --git a/libopenage/renderer/stages/terrain/render_entity.h b/libopenage/renderer/stages/terrain/render_entity.h index 7b32c22401..0f726a2351 100644 --- a/libopenage/renderer/stages/terrain/render_entity.h +++ b/libopenage/renderer/stages/terrain/render_entity.h @@ -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. @@ -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. @@ -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 &get_vertices(); + const std::vector 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 &get_terrain_paths(); + const std::unordered_set 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: /**