summaryrefslogtreecommitdiffhomepage
path: root/src/floor-mesh.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-06 09:49:23 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-06 09:57:50 +0200
commit89acc4653fe7ea3a76dd49711b3ac6b704ed509d (patch)
treedcff8947d840f50ee348e375aca311829dad2e7a /src/floor-mesh.cpp
parent595c113b88dc1e81802b4b1381122f25c0eece74 (diff)
a
Diffstat (limited to 'src/floor-mesh.cpp')
-rw-r--r--src/floor-mesh.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/floor-mesh.cpp b/src/floor-mesh.cpp
new file mode 100644
index 00000000..7156661e
--- /dev/null
+++ b/src/floor-mesh.cpp
@@ -0,0 +1,72 @@
+#include "floor-mesh.hpp"
+#include "shaders/tile-shader.hpp"
+#include "tile.hpp"
+#include "chunk.hpp"
+#include "tile-atlas.hpp"
+#include <Magnum/GL/MeshView.h>
+
+namespace Magnum::Examples {
+
+constexpr auto quad_index_count = 6;
+
+floor_mesh::floor_mesh()
+{
+ _mesh.setCount((int)(quad_index_count * TILE_COUNT))
+ .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(quad_data& data, tile& x)
+{
+ CORRADE_INTERNAL_ASSERT(x.ground_image);
+
+ auto texcoords = x.ground_image.atlas->texcoords_for_id(x.ground_image.variant);
+ for (size_t i = 0; i < 4; i++)
+ data[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 idx, local_coords) {
+ set_tile(data[idx], x);
+ });
+ _vertex_buffer.setSubData(0, data);
+ Magnum::GL::MeshView mesh{_mesh};
+ mesh.setCount(quad_index_count);
+ 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)
+ {
+ atlas->texture().bind(0);
+ last_tile_atlas = atlas;
+ }
+ shader.draw(mesh);
+ });
+}
+
+std::array<std::array<UnsignedShort, 6>, TILE_COUNT> floor_mesh::make_index_array()
+{
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
+ std::array<std::array<UnsignedShort, quad_index_count>, TILE_COUNT> array;
+ for (std::size_t i = 0; i < std::size(array); i++)
+ array[i] = tile_atlas::indices(i);
+ return array;
+}
+
+std::array<std::array<Vector3, 4>, TILE_COUNT> floor_mesh::make_position_array()
+{
+ std::array<std::array<Vector3, 4>, TILE_COUNT> array;
+ constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1];
+ for (std::size_t j = 0, k = 0; j < TILE_MAX_DIM; j++)
+ for (std::size_t i = 0; i < TILE_MAX_DIM; i++, k++)
+ {
+ Vector3 center {(float)(X*i), (float)(Y*j), 0};
+ array[k] = { tile_atlas::floor_quad(center, {X, Y}) };
+ }
+ return array;
+}
+
+} // namespace Magnum::Examples