diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-02 18:41:48 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-02 18:41:48 +0200 |
commit | fc0dbf31301f42eceb33a452e703d6b41dd80756 (patch) | |
tree | 0190ef253d4d343f1db66604aecbc74fced0554c | |
parent | c8e9f5289ea1d04f169f270c22237c656236c4ea (diff) |
a
-rw-r--r-- | floor-mesh.cpp | 18 | ||||
-rw-r--r-- | floor-mesh.hpp | 14 | ||||
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | shaders/tile-shader.hpp | 3 |
4 files changed, 15 insertions, 24 deletions
diff --git a/floor-mesh.cpp b/floor-mesh.cpp index e9dd57b1..5c797ef7 100644 --- a/floor-mesh.cpp +++ b/floor-mesh.cpp @@ -1,5 +1,4 @@ #include "floor-mesh.hpp" -#include "tile-atlas.hpp" #include "shaders/tile-shader.hpp" #include "tile.hpp" #include "chunk.hpp" @@ -9,30 +8,31 @@ namespace Magnum::Examples { floor_mesh::floor_mesh() { - _mesh.setCount((int)(quad_index_count * _index_data.size())) + _mesh.setCount((int)(_index_data.size() * _index_data[0].size())) .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(tile& x, const local_coords pt) +void floor_mesh::set_tile(std::array<quad_data, TILE_COUNT>& data, tile& x, const local_coords pt) { CORRADE_INTERNAL_ASSERT(x.ground_image); const auto idx = pt.to_index(); auto texcoords = x.ground_image.atlas->texcoords_for_id(x.ground_image.variant); for (std::size_t i = 0; i < 4; i++) - _vertex_data[idx][i] = { texcoords[i] }; + data[idx][i] = { texcoords[i] }; } void floor_mesh::draw(tile_shader& shader, chunk& c) { + std::array<quad_data, TILE_COUNT> data; c.foreach_tile([&](tile& x, std::size_t, local_coords pt) { - set_tile(x, pt); + set_tile(data, x, pt); }); - _vertex_buffer.setData(_vertex_data, Magnum::GL::BufferUsage::DynamicDraw); + _vertex_buffer.setData(data, Magnum::GL::BufferUsage::DynamicDraw); #if 1 - Magnum::GL::MeshView mesh{ _mesh }; + Magnum::GL::MeshView mesh{_mesh}; mesh.setCount(quad_index_count); tile_atlas* last_tile_atlas = nullptr; c.foreach_tile([&](tile& x, std::size_t i, local_coords) { @@ -52,9 +52,7 @@ void floor_mesh::draw(tile_shader& shader, chunk& c) static auto make_index_array() { - constexpr auto quad_index_count = std::tuple_size_v<decltype(tile_atlas::indices(0))>; - std::array<std::array<UnsignedShort, quad_index_count>, TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) - + std::array<std::array<UnsignedShort, 6>, TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) for (std::size_t i = 0; i < std::size(array); i++) array[i] = tile_atlas::indices(i); return array; diff --git a/floor-mesh.hpp b/floor-mesh.hpp index 7d7eff6f..7b717613 100644 --- a/floor-mesh.hpp +++ b/floor-mesh.hpp @@ -19,25 +19,25 @@ struct floor_mesh final floor_mesh(floor_mesh&&) = delete; floor_mesh(const floor_mesh&) = delete; - void set_tile(tile& x, local_coords pt); void draw(tile_shader& shader, chunk& c); private: + static constexpr auto quad_index_count = 6; + struct vertex_data final { Vector2 texcoords; }; using quad_data = std::array<vertex_data, 4>; - using vertex_positions_data = std::array<Vector3, 4>; - static constexpr auto quad_index_count = std::tuple_size_v<decltype(tile_atlas::indices(0))>; + using quad_positions_data = std::array<Vector3, 4>; using index_type = std::array<UnsignedShort, quad_index_count>; + static void set_tile(std::array<quad_data, TILE_COUNT>& data, tile& x, local_coords pt); + static const std::array<index_type, TILE_COUNT> _index_data; - static const std::array<vertex_positions_data, TILE_COUNT> _position_data; + static const std::array<quad_positions_data, TILE_COUNT> _position_data; GL::Mesh _mesh; - std::array<quad_data, TILE_COUNT> _vertex_data = {}; - - GL::Buffer _vertex_buffer{_vertex_data, Magnum::GL::BufferUsage::DynamicDraw}, + GL::Buffer _vertex_buffer{{}, Magnum::GL::BufferUsage::DynamicDraw}, _index_buffer{_index_data, Magnum::GL::BufferUsage::StaticDraw}, _positions_buffer{_position_data, Magnum::GL::BufferUsage::StaticDraw}; }; @@ -86,10 +86,6 @@ chunk app::make_test_chunk() void app::draw_chunk(chunk& c) { constexpr auto N = TILE_MAX_DIM; - - for (std::size_t j = 0, k = 0; j < N; j++) - for (std::size_t i = 0; i < N; i++, k++) - _floor_mesh.set_tile(c[k], {i, j}); _floor_mesh.draw(_shader, c); } diff --git a/shaders/tile-shader.hpp b/shaders/tile-shader.hpp index a1975daf..27126964 100644 --- a/shaders/tile-shader.hpp +++ b/shaders/tile-shader.hpp @@ -1,5 +1,4 @@ #pragma once -#include "tile-atlas.hpp" #include <vector> #include <utility> #include <Magnum/GL/AbstractShaderProgram.h> @@ -8,8 +7,6 @@ #include <Magnum/Math/Vector2.h> #include <Magnum/Math/Matrix4.h> -struct tile_atlas; - namespace Magnum::Examples { struct tile_shader : GL::AbstractShaderProgram |