diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-04-09 22:33:16 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-04-09 22:33:16 +0200 |
commit | c04906db429b7c626a753d49e2d64c51135c6039 (patch) | |
tree | d38ea8ca3c2bec1438cf5ab60825deaa053ff1b6 /src | |
parent | b2f0c4532872e630b8c543d85dbb56709c625b99 (diff) |
draw: now fix reordering properly
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk-scenery.cpp | 37 | ||||
-rw-r--r-- | src/chunk-scenery.hpp | 2 | ||||
-rw-r--r-- | src/chunk.hpp | 2 |
3 files changed, 23 insertions, 18 deletions
diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp index 66f6a733..f2c1c80a 100644 --- a/src/chunk-scenery.cpp +++ b/src/chunk-scenery.cpp @@ -61,7 +61,9 @@ static void topo_dfs(Array<chunk::entity_draw_order>& array, size_t& output, siz topo_dfs(array, output, j, size); } fm_assert(output < size); - array[output++].e = data_i.in; + array[output].e = data_i.in; + array[output].mesh_idx = data_i.in_mesh_idx; + output++; } static void topological_sort(Array<chunk::entity_draw_order>& array, size_t size) @@ -74,7 +76,7 @@ static void topological_sort(Array<chunk::entity_draw_order>& array, size_t size fm_assert(output == size); } -auto chunk::make_topo_sort_data(entity& e) -> topo_sort_data +auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data { const auto& a = *e.atlas; const auto& f = a.frame(e.r, e.frame); @@ -83,8 +85,10 @@ auto chunk::make_topo_sort_data(entity& e) -> topo_sort_data const auto px_start = pos - Vector2(e.bbox_offset) - Vector2(f.ground), px_end = px_start + Vector2(f.size); topo_sort_data data = { .in = &e, - .min = Vector2i(px_start), .max = Vector2i(px_end), + .min = Vector2i(px_start), + .max = Vector2i(px_end), .center = Vector2i(pos), + .in_mesh_idx = mesh_idx, .ord = e.ordinal(), }; if (e.type() == entity_type::scenery && !e.is_dynamic()) @@ -124,23 +128,13 @@ auto chunk::ensure_scenery_mesh(Array<entity_draw_order>& array) noexcept -> sce { fm_assert(_entities_sorted); - const auto size = _entities.size(); - - ensure_scenery_draw_array(array); - for (auto i = 0uz; const auto& e : _entities) - array[i++] = { e.get(), e->ordinal(), make_topo_sort_data(*e) }; - std::sort(array.begin(), array.begin() + size, [](const auto& a, const auto& b) { return a.ord < b.ord; }); - topological_sort(array, size); - - const auto es = ArrayView<entity_draw_order>{array, size}; - if (_scenery_modified) { _scenery_modified = false; const auto count = fm_begin( size_t ret = 0; - for (const auto& [e, ord, _data] : es) + for (const auto& e : _entities) ret += !e->is_dynamic(); return ret; ); @@ -150,7 +144,7 @@ auto chunk::ensure_scenery_mesh(Array<entity_draw_order>& array) noexcept -> sce scenery_vertexes.clear(); scenery_vertexes.reserve(count); - for (const auto& [e, ord, _data] : es) + for (const auto& e : _entities) { if (e->is_dynamic()) continue; @@ -178,9 +172,18 @@ auto chunk::ensure_scenery_mesh(Array<entity_draw_order>& array) noexcept -> sce scenery_mesh = Utility::move(mesh); } - fm_assert(!size || es); + const auto size = _entities.size(); + ensure_scenery_draw_array(array); + uint32_t j = 0; + for (uint32_t i = 0; const auto& e : _entities) + { + auto index = e->is_dynamic() ? (uint32_t)-1 : j++; + array[i++] = { e.get(), (uint32_t)-1, e->ordinal(), make_topo_sort_data(*e, index) }; + } + std::sort(array.begin(), array.begin() + size, [](const auto& a, const auto& b) { return a.ord < b.ord; }); + topological_sort(array, size); - return { scenery_mesh, es, size }; + return { scenery_mesh, ArrayView<entity_draw_order>{array, size}, j }; } void chunk::ensure_scenery_draw_array(Array<entity_draw_order>& array) diff --git a/src/chunk-scenery.hpp b/src/chunk-scenery.hpp index cbe2ea83..1363479b 100644 --- a/src/chunk-scenery.hpp +++ b/src/chunk-scenery.hpp @@ -11,6 +11,7 @@ struct chunk::topo_sort_data entity* in = nullptr; Vector2i min, max, center; + uint32_t in_mesh_idx; float slope = 0, ord; Vector2s bb_min = {}, bb_max = {}; m mode : 2 = mode_none; @@ -21,6 +22,7 @@ struct chunk::topo_sort_data struct chunk::entity_draw_order { entity *e; + uint32_t mesh_idx; float ord; topo_sort_data data; }; diff --git a/src/chunk.hpp b/src/chunk.hpp index ae19ad62..c98ae9a3 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -138,7 +138,7 @@ private: _entities_sorted : 1 = true; void ensure_scenery_draw_array(Array<entity_draw_order>& array); - static topo_sort_data make_topo_sort_data(entity& e); + static topo_sort_data make_topo_sort_data(entity& e, uint32_t mesh_idx); struct bbox final // NOLINT(cppcoreguidelines-pro-type-member-init) { |