summaryrefslogtreecommitdiffhomepage
path: root/src/chunk.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-09 19:45:46 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-09 19:45:46 +0100
commit9482f370e56397d50e5d7efef81a33af7390d07a (patch)
treebc1f4cec23cd4a31ee90e2bbaf592ac5d54f7625 /src/chunk.cpp
parent5907a8902e6f01774cebbb515349a66c86a47fb3 (diff)
now sorting both wall types together
Diffstat (limited to 'src/chunk.cpp')
-rw-r--r--src/chunk.cpp69
1 files changed, 25 insertions, 44 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp
index e736fe42..353446f3 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -14,7 +14,7 @@ bool chunk::empty(bool force) const noexcept
for (std::size_t i = 0; i < TILE_COUNT; i++)
{
- if (_ground_atlases[i] || _wall_north_atlases[i] || _wall_west_atlases[i])
+ if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1])
{
_maybe_empty = false;
return false;
@@ -25,8 +25,7 @@ bool chunk::empty(bool force) const noexcept
}
tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept { return _ground_atlases[i].get(); }
-tile_atlas* chunk::wall_n_atlas_at(std::size_t i) const noexcept { return _wall_north_atlases[i].get(); }
-tile_atlas* chunk::wall_w_atlas_at(std::size_t i) const noexcept { return _wall_west_atlases[i].get(); }
+tile_atlas* chunk::wall_atlas_at(std::size_t i) const noexcept { return _wall_atlases[i].get(); }
static auto make_index_array(std::size_t offset)
{
@@ -84,53 +83,35 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple
auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple
{
if (!_walls_modified)
- return { wall_mesh, wall_n_indexes, wall_w_indexes };
+ return { wall_mesh, wall_indexes };
_walls_modified = false;
- for (std::size_t i = 0; i < TILE_COUNT; i++)
- wall_n_indexes[i] = std::uint8_t(i);
- for (std::size_t i = 0; i < TILE_COUNT; i++)
- wall_w_indexes[i] = std::uint8_t(i);
+ for (std::size_t i = 0; i < TILE_COUNT*2; i++)
+ wall_indexes[i] = std::uint16_t(i);
- std::sort(wall_n_indexes.begin(), wall_n_indexes.end(), [this](std::uint8_t a, std::uint8_t b) {
- return _wall_north_atlases[a].get() < _wall_north_atlases[b].get();
+ std::sort(wall_indexes.begin(), wall_indexes.end(), [this](std::uint16_t a, std::uint16_t b) {
+ return _wall_atlases[a] < _wall_atlases[b];
});
- std::sort(wall_w_indexes.begin(), wall_w_indexes.end(), [this](std::uint8_t a, std::uint8_t b) {
- return _wall_west_atlases[a].get() < _wall_west_atlases[b].get();
- });
-
- std::array<std::array<vertex, 4>, TILE_COUNT> vertexes[2] = {};
- using ids_ = std::array<std::uint8_t, TILE_COUNT>;
- using a_ = std::array<std::shared_ptr<tile_atlas>, TILE_COUNT>;
- using vs_ = std::array<variant_t, TILE_COUNT>;
- using verts_ = std::array<std::array<vertex, 4>, TILE_COUNT>;
- constexpr auto do_walls = [](const ids_& ids, const a_& as, const vs_& vs, verts_& verts, const auto& fn) {
- for (std::size_t k = 0; k < TILE_COUNT; k++)
+ std::array<std::array<vertex, 4>, TILE_COUNT*2> vertexes;
+ for (std::size_t k = 0; k < TILE_COUNT*2; k++)
+ {
+ const std::uint16_t i = wall_indexes[k];
+ if (const auto& atlas = _wall_atlases[i]; !atlas)
+ vertexes[k] = {};
+ else
{
- const std::uint8_t i = ids[k];
- if (const auto& atlas = as[i]; !atlas)
- verts[k] = {};
- else
- {
- const local_coords pos{i};
- const float depth = tile_shader::depth_value(pos);
- const std::array<Vector3, 4> quad = fn(*atlas, pos);
- const auto texcoords = atlas->texcoords_for_id(vs[i] % atlas->num_tiles());
- auto& v = verts[k];
- for (std::size_t j = 0; j < 4; j++)
- v[j] = { quad[j], texcoords[j], depth, };
- }
+ const auto& variant = _wall_variants[i];
+ const local_coords pos{i / 2u};
+ const auto center = Vector3(pos.x, pos.y, 0) * TILE_SIZE;
+ const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE);
+ const float depth = tile_shader::depth_value(pos);
+ const auto texcoords = atlas->texcoords_for_id(variant % atlas->num_tiles());
+ auto& v = vertexes[k];
+ for (std::size_t j = 0; j < 4; j++)
+ v[j] = { quad[j], texcoords[j], depth, };
}
- };
- do_walls(wall_n_indexes, _wall_north_atlases, _wall_north_variants, vertexes[0],
- [](const tile_atlas& a, local_coords pos) {
- return a.wall_quad_N(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE);
- });
- do_walls(wall_w_indexes, _wall_west_atlases, _wall_west_variants, vertexes[1],
- [](const tile_atlas& a, local_coords pos) {
- return a.wall_quad_W(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE);
- });
+ }
using index_t = std::array<std::array<UnsignedShort, 6>, TILE_COUNT>;
const index_t indexes[2] = { make_index_array(0), make_index_array(TILE_COUNT) };
@@ -140,7 +121,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple
.setIndexBuffer(GL::Buffer{indexes}, 0, GL::MeshIndexType::UnsignedShort)
.setCount(6 * TILE_COUNT);
wall_mesh = Utility::move(mesh);
- return { wall_mesh, wall_n_indexes, wall_w_indexes };
+ return { wall_mesh, wall_indexes };
}
fm_noinline