diff options
| -rw-r--r-- | floor-mesh.cpp | 6 | ||||
| -rw-r--r-- | floor-mesh.hpp | 22 | ||||
| -rw-r--r-- | wall-mesh.cpp | 43 | ||||
| -rw-r--r-- | wall-mesh.hpp | 9 |
4 files changed, 42 insertions, 38 deletions
diff --git a/floor-mesh.cpp b/floor-mesh.cpp index 0886dc34..82a2c61f 100644 --- a/floor-mesh.cpp +++ b/floor-mesh.cpp @@ -25,14 +25,16 @@ void floor_mesh::set_tile(quad_data& data, tile& x) void floor_mesh::draw(tile_shader& shader, chunk& c) { + constexpr auto quad_index_count = _index_data[0].size(); std::array<quad_data, TILE_COUNT> data; c.foreach_tile([&](tile& x, std::size_t idx, local_coords) { set_tile(data[idx], x); }); - _vertex_buffer.setData(data, Magnum::GL::BufferUsage::DynamicDraw); + //_vertex_buffer.setData(data, Magnum::GL::BufferUsage::DynamicDraw); + _vertex_buffer.setSubData(0, Containers::arrayView(data.data(), data.size())); Magnum::GL::MeshView mesh{_mesh}; mesh.setCount(quad_index_count); - tile_atlas* last_tile_atlas = nullptr; + const tile_atlas* last_tile_atlas = nullptr; c.foreach_tile([&](tile& x, std::size_t i, local_coords) { mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*TILE_COUNT - 1); if (auto* atlas = x.ground_image.atlas.get(); atlas != last_tile_atlas) diff --git a/floor-mesh.hpp b/floor-mesh.hpp index bcc5a443..3bfbee3a 100644 --- a/floor-mesh.hpp +++ b/floor-mesh.hpp @@ -22,24 +22,18 @@ struct floor_mesh final void draw(tile_shader& shader, chunk& c); private: - static constexpr auto quad_index_count = 6; - - struct vertex_data final { - Vector2 texcoords; - }; + struct vertex_data final { Vector2 texcoords; }; using quad_data = std::array<vertex_data, 4>; - using quad_positions_data = std::array<Vector3, 4>; - using index_type = std::array<UnsignedShort, quad_index_count>; - - static void set_tile(quad_data& data, tile& x); - static const std::array<index_type, TILE_COUNT> _index_data; - static const std::array<quad_positions_data, TILE_COUNT> _position_data; + static const std::array<std::array<UnsignedShort, 6>, TILE_COUNT> _index_data; + static const std::array<std::array<Vector3, 4>, TILE_COUNT> _position_data; GL::Mesh _mesh; - GL::Buffer _vertex_buffer{{}, Magnum::GL::BufferUsage::DynamicDraw}, - _index_buffer{_index_data, Magnum::GL::BufferUsage::StaticDraw}, - _positions_buffer{_position_data, Magnum::GL::BufferUsage::StaticDraw}; + GL::Buffer _vertex_buffer{std::array<quad_data, TILE_COUNT>{} /*, Magnum::GL::BufferUsage::DynamicDraw*/}, + _index_buffer{_index_data}, + _positions_buffer{_position_data}; + + static void set_tile(quad_data& data, tile& x); }; } // namespace Magnum::Examples diff --git a/wall-mesh.cpp b/wall-mesh.cpp index 6ae45438..ae7cf0c8 100644 --- a/wall-mesh.cpp +++ b/wall-mesh.cpp @@ -24,51 +24,56 @@ wall_mesh::wall_mesh() CORRADE_INTERNAL_ASSERT(_mesh.isIndexed()); } -void wall_mesh::add_wall(vertex_array& data, std::size_t& pos_, tile_image& img, const position_array& positions) +void wall_mesh::add_wall(vertex_array& data, texture_array& textures, std::size_t& pos_, + tile_image& img, const position_array& positions) { const auto pos = pos_++; CORRADE_INTERNAL_ASSERT(pos < data.size()); auto texcoords = img.atlas->texcoords_for_id(img.variant); for (std::size_t i = 0; i < 4; i++) + { data[pos][i] = { texcoords[i], positions[i] }; + textures[pos] = &img.atlas->texture(); + } } -void wall_mesh::add_tile(vertex_array& data, std::size_t& pos, tile& x, local_coords pt) +void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, std::size_t& pos, tile& x, local_coords pt) { constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2]; constexpr Vector3 size = {X, Y, Z}; Vector3 center{(float)(X*pt.x), (float)(Y*pt.y), 0}; if (auto& wall = x.wall_north; wall.atlas) - add_wall(data, pos, wall, tile_atlas::wall_quad_N(center, size)); + add_wall(data, textures, pos, wall, tile_atlas::wall_quad_N(center, size)); if (auto& wall = x.wall_west; wall.atlas) - add_wall(data, pos, wall, tile_atlas::wall_quad_W(center, size)); + add_wall(data, textures, pos, wall, tile_atlas::wall_quad_W(center, size)); } void wall_mesh::draw(tile_shader& shader, chunk& c) { + texture_array textures = {}; + std::size_t pos = 0; { vertex_array data; - std::size_t pos = 0; c.foreach_tile([&](tile& x, std::size_t, local_coords pt) { - add_tile(data, pos, x, pt); + maybe_add_tile(data, textures, pos, x, pt); }); - _vertex_buffer.setData(data, Magnum::GL::BufferUsage::DynamicDraw); + _vertex_buffer.setSubData(0, Containers::arrayView(data.data(), pos)); } + + const GL::Texture2D* last_texture = nullptr; + Magnum::GL::MeshView mesh{_mesh}; + mesh.setCount(quad_index_count); + for (std::size_t i = 0; i < pos; i++) { - tile_atlas* last_tile_atlas = nullptr; - Magnum::GL::MeshView mesh{_mesh}; - mesh.setCount(quad_index_count); - c.foreach_tile([&](tile& x, std::size_t i, local_coords) { - mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*COUNT - 1); - if (auto* atlas = x.ground_image.atlas.get(); atlas != last_tile_atlas) - { - atlas->texture().bind(0); - last_tile_atlas = atlas; - } - shader.draw(mesh); - }); + auto* const tex = textures[i]; + CORRADE_INTERNAL_ASSERT(tex != nullptr); + mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*COUNT - 1); + if (tex != last_texture) + tex->bind(0); + last_texture = tex; } + shader.draw(mesh); } decltype(wall_mesh::_index_data) wall_mesh::make_index_array() diff --git a/wall-mesh.hpp b/wall-mesh.hpp index 905e7acc..02fafc27 100644 --- a/wall-mesh.hpp +++ b/wall-mesh.hpp @@ -36,12 +36,15 @@ private: using quad = std::array<vertex, 4>; using index_type = std::array<UnsignedShort, 6>; using vertex_array = std::array<quad, COUNT>; + using texture_array = std::array<GL::Texture2D*, COUNT>; - static void add_tile(vertex_array& data, std::size_t& pos, tile& x, local_coords pt); - static void add_wall(vertex_array& data, std::size_t& pos, tile_image& img, const position_array& positions); + static void maybe_add_tile(vertex_array& data, texture_array& textures, std::size_t& pos, + tile& x, local_coords pt); + static void add_wall(vertex_array& data, texture_array& textures, std::size_t& pos, + tile_image& img, const position_array& positions); GL::Mesh _mesh; - GL::Buffer _vertex_buffer{{}, Magnum::GL::BufferUsage::DynamicDraw}, + GL::Buffer _vertex_buffer{vertex_array{}, Magnum::GL::BufferUsage::StaticDraw}, _index_buffer{_index_data, Magnum::GL::BufferUsage::StaticDraw}; static const std::array<index_type, COUNT> _index_data; |
