summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-04-09 22:01:05 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-04-09 22:01:05 +0200
commit8f77947d974ebbeb6b551dbf22b92028eb8e9109 (patch)
treec3cbb0a35655e09028d13d114281f56699da4405 /src
parent3c6e95bd369270e90472bc25f91dff76d0811b71 (diff)
Revert "draw: do batching in an easier way for now"
This reverts commit 3c6e95bd369270e90472bc25f91dff76d0811b71.
Diffstat (limited to 'src')
-rw-r--r--src/chunk-scenery.cpp50
-rw-r--r--src/chunk-scenery.hpp2
-rw-r--r--src/chunk.hpp2
3 files changed, 52 insertions, 2 deletions
diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp
index 40a99487..66f6a733 100644
--- a/src/chunk-scenery.cpp
+++ b/src/chunk-scenery.cpp
@@ -132,7 +132,55 @@ auto chunk::ensure_scenery_mesh(Array<entity_draw_order>& array) noexcept -> sce
std::sort(array.begin(), array.begin() + size, [](const auto& a, const auto& b) { return a.ord < b.ord; });
topological_sort(array, size);
- return { ArrayView<entity_draw_order>{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)
+ ret += !e->is_dynamic();
+ return ret;
+ );
+
+ scenery_indexes.clear();
+ scenery_indexes.reserve(count);
+ scenery_vertexes.clear();
+ scenery_vertexes.reserve(count);
+
+ for (const auto& [e, ord, _data] : es)
+ {
+ if (e->is_dynamic())
+ continue;
+
+ const auto i = scenery_indexes.size();
+ scenery_indexes.emplace_back(tile_atlas::indices(i));
+ const auto& atlas = e->atlas;
+ const auto& fr = *e;
+ const auto pos = e->coord.local();
+ const auto coord = Vector3(pos) * TILE_SIZE + Vector3(Vector2(fr.offset), 0);
+ const auto quad = atlas->frame_quad(coord, fr.r, fr.frame);
+ const auto& group = atlas->group(fr.r);
+ const auto texcoords = atlas->texcoords_for_frame(fr.r, fr.frame, !group.mirror_from.isEmpty());
+ const float depth = tile_shader::depth_value(pos, tile_shader::scenery_depth_offset);
+ scenery_vertexes.emplace_back();
+ auto& v = scenery_vertexes.back();
+ for (auto j = 0uz; j < 4; j++)
+ v[j] = { quad[j], texcoords[j], depth };
+ }
+
+ GL::Mesh mesh{GL::MeshPrimitive::Triangles};
+ mesh.addVertexBuffer(GL::Buffer{scenery_vertexes}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{})
+ .setIndexBuffer(GL::Buffer{scenery_indexes}, 0, GL::MeshIndexType::UnsignedShort)
+ .setCount(int32_t(6 * count));
+ scenery_mesh = Utility::move(mesh);
+ }
+
+ fm_assert(!size || es);
+
+ return { scenery_mesh, es, size };
}
void chunk::ensure_scenery_draw_array(Array<entity_draw_order>& array)
diff --git a/src/chunk-scenery.hpp b/src/chunk-scenery.hpp
index 067f176a..cbe2ea83 100644
--- a/src/chunk-scenery.hpp
+++ b/src/chunk-scenery.hpp
@@ -25,7 +25,9 @@ struct chunk::entity_draw_order
topo_sort_data data;
};
struct chunk::scenery_mesh_tuple {
+ GL::Mesh& mesh;
ArrayView<entity_draw_order> array;
+ size_t size;
};
} // namespace floormat
diff --git a/src/chunk.hpp b/src/chunk.hpp
index db6f96f7..be90c89a 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -125,7 +125,7 @@ private:
std::vector<std::array<vertex, 4>> scenery_vertexes;
struct world* _world;
- GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate};
+ GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}, scenery_mesh{NoCreate};
RTree _rtree;