diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-09 11:45:34 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-09 11:45:34 +0100 |
commit | ff3a18b1a251a5e85057e52303efa0cdd79e8a66 (patch) | |
tree | 44c2f3a6f69bc922d71063debd14ba669c74ca0d /draw | |
parent | b4770eb85369e91cbf800e8192dac0d8c0c627cf (diff) |
add floor mesh to struct chunk
Diffstat (limited to 'draw')
-rw-r--r-- | draw/floor.cpp | 51 | ||||
-rw-r--r-- | draw/floor.hpp | 13 | ||||
-rw-r--r-- | draw/wall.cpp | 2 | ||||
-rw-r--r-- | draw/wireframe.hpp | 2 |
4 files changed, 8 insertions, 60 deletions
diff --git a/draw/floor.cpp b/draw/floor.cpp index 861336c7..ee7b4014 100644 --- a/draw/floor.cpp +++ b/draw/floor.cpp @@ -11,42 +11,20 @@ constexpr auto quad_index_count = 6; floor_mesh::floor_mesh() { - _mesh.setCount((int)(quad_index_count * TILE_COUNT)) - .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}) - .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}) - .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); -} - -void floor_mesh::set_tile(quad_data& data, tile_ref& x) -{ - if (auto ground = x.ground(); ground) - { - auto texcoords = ground.atlas->texcoords_for_id(ground.variant); - for (size_t i = 0; i < 4; i++) - data[i] = { texcoords[i] }; - } - else - for (size_t i = 0; i < 4; i++) - data[i] = {}; } void floor_mesh::draw(tile_shader& shader, chunk& c) { - //_vertex_buffer.setData({nullptr, sizeof(quad_data) * TILE_COUNT}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer - std::array<quad_data, TILE_COUNT> data; - for (auto [x, idx, pt] : c) { - set_tile(data[idx], x); - } - _vertex_buffer.setSubData(0, data); - Magnum::GL::MeshView mesh{_mesh}; + auto [mesh_, ids] = c.ensure_ground_mesh(); tile_atlas* last_atlas = nullptr; std::size_t last_pos = 0; + GL::MeshView mesh{mesh_}; const auto do_draw = [&](std::size_t i, tile_atlas* atlas) { if (atlas == last_atlas) return; - if (auto len = i - last_pos; last_atlas != nullptr && len > 0) + if (auto len = i - last_pos; last_atlas && len > 0) { last_atlas->texture().bind(0); mesh.setCount((int)(quad_index_count * len)); @@ -57,27 +35,10 @@ void floor_mesh::draw(tile_shader& shader, chunk& c) last_pos = i; }; - for (auto [x, i, pt] : c) - do_draw(i, x.ground_atlas().get()); + for (std::size_t k = 0; k < TILE_COUNT; k++) + if (auto* atlas = c.ground_atlas_at(ids[k])) + do_draw(k, atlas); do_draw(TILE_COUNT, nullptr); } -std::array<std::array<UnsignedShort, 6>, TILE_COUNT> floor_mesh::make_index_array() -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array<std::array<UnsignedShort, quad_index_count>, TILE_COUNT> array; - for (std::size_t i = 0; i < std::size(array); i++) - array[i] = tile_atlas::indices(i); - return array; -} - -std::array<std::array<Vector3, 4>, TILE_COUNT> floor_mesh::make_position_array() -{ - std::array<std::array<Vector3, 4>, TILE_COUNT> array; - for (std::uint8_t j = 0, k = 0; j < TILE_MAX_DIM; j++) - for (std::uint8_t i = 0; i < TILE_MAX_DIM; i++, k++) - array[k] = { tile_atlas::floor_quad(Vector3(i, j, 0) * TILE_SIZE, TILE_SIZE2) }; - return array; -} - } // namespace floormat diff --git a/draw/floor.hpp b/draw/floor.hpp index 651cee61..07e183d6 100644 --- a/draw/floor.hpp +++ b/draw/floor.hpp @@ -21,19 +21,6 @@ struct floor_mesh final floor_mesh(const floor_mesh&) = delete; void draw(tile_shader& shader, chunk& c); - -private: - struct vertex_data final { Vector2 texcoords; }; - using quad_data = std::array<vertex_data, 4>; - - static std::array<std::array<UnsignedShort, 6>, TILE_COUNT> make_index_array(); - static std::array<std::array<Vector3, 4>, TILE_COUNT> make_position_array(); - - GL::Mesh _mesh; - GL::Buffer _vertex_buffer{std::array<quad_data, TILE_COUNT>{}, Magnum::GL::BufferUsage::DynamicDraw}, - _index_buffer{make_index_array()}, _positions_buffer{make_position_array()}; - - static void set_tile(quad_data& data, tile_ref& x); }; } // namespace floormat diff --git a/draw/wall.cpp b/draw/wall.cpp index 9bcfe9d4..4c25b267 100644 --- a/draw/wall.cpp +++ b/draw/wall.cpp @@ -41,7 +41,7 @@ void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, tile void wall_mesh::draw(tile_shader& shader, chunk& c) { - //_vertex_buffer.setData({nullptr, sizeof(vertex_array)}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer + //_texcoord_buffer.setData({nullptr, sizeof(vertex_array)}, Magnum::GL::BufferUsage::DynamicDraw); // orphan the buffer texture_array textures = {}; { vertex_array data; diff --git a/draw/wireframe.hpp b/draw/wireframe.hpp index 9d736419..1d58ff43 100644 --- a/draw/wireframe.hpp +++ b/draw/wireframe.hpp @@ -52,7 +52,7 @@ wireframe_mesh<T>::wireframe_mesh() : template <wireframe::traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x) { - //_vertex_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer + //_texcoord_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer set_subdata(x.make_vertex_array()); x.on_draw(); mesh_base::draw(shader); |