diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-09 05:06:41 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-09 05:06:41 +0200 |
commit | 94f6748d5f5b9fdc3047022fe59d66028bde63f3 (patch) | |
tree | de3dd2e7944c9dea12523702773ce5a2f93beed5 /draw/wall-mesh.cpp | |
parent | e3b2c9267e7a8da5adca522a48a9f9a8457f89d8 (diff) |
a
Diffstat (limited to 'draw/wall-mesh.cpp')
-rw-r--r-- | draw/wall-mesh.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/draw/wall-mesh.cpp b/draw/wall-mesh.cpp new file mode 100644 index 00000000..d698b508 --- /dev/null +++ b/draw/wall-mesh.cpp @@ -0,0 +1,93 @@ +#include "wall-mesh.hpp" +#include "tile-atlas.hpp" +#include "shaders/tile-shader.hpp" +#include "chunk.hpp" +#include <Magnum/GL/RectangleTexture.h> +#include <Magnum/GL/MeshView.h> + +namespace Magnum::Examples { + +constexpr auto quad_index_count = 6; + +wall_mesh::wall_mesh() +{ + _mesh.setCount((int)(quad_index_count * COUNT)) + .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}) + .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}) + .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); + CORRADE_INTERNAL_ASSERT(_mesh.isIndexed()); +} + +void wall_mesh::add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos) +{ + CORRADE_INTERNAL_ASSERT(pos < data.size()); + auto texcoords = img.atlas->texcoords_for_id(img.variant); + for (std::size_t i = 0; i < 4; i++) + { + data[pos][i] = { texcoords[i] }; + textures[pos] = &img.atlas->texture(); + } +} + +void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos) +{ + if (auto& wall = x.wall_north; wall.atlas) + add_wall(data, textures, wall, pos * 2 + 0); + if (auto& wall = x.wall_west; wall.atlas) + add_wall(data, textures, wall, pos * 2 + 1); +} + +void wall_mesh::draw(tile_shader& shader, chunk& c) +{ + texture_array textures = {}; + { + vertex_array data; + for (auto& [x, idx, pt] : c) { + maybe_add_tile(data, textures, x, idx); + } + _vertex_buffer.setSubData(0, data); + } + + const GL::RectangleTexture* last_texture = nullptr; + Magnum::GL::MeshView mesh{_mesh}; + for (std::size_t i = 0; i < COUNT; i++) + { + auto* const tex = textures[i]; + if (!tex) + continue; + mesh.setCount(quad_index_count); + mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*COUNT - 1); + if (tex != last_texture) + tex->bind(0); + last_texture = tex; + shader.draw(mesh); + } +} + +std::array<std::array<UnsignedShort, 6>, wall_mesh::COUNT> wall_mesh::make_index_array() +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array<std::array<UnsignedShort, 6>, 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>, wall_mesh::COUNT> wall_mesh::make_position_array() +{ + std::array<std::array<Vector3, 4>, COUNT> array; + constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2]; + constexpr Vector3 size = {X, Y, Z}; + for (std::size_t j = 0; j < TILE_MAX_DIM; j++) + for (std::size_t i = 0; i < TILE_MAX_DIM; i++) + { + const auto idx = (j*TILE_MAX_DIM + i) * 2; + Vector3 center{(float)(X*i), (float)(Y*j), 0}; + array[idx + 0] = tile_atlas::wall_quad_N(center, size); + array[idx + 1] = tile_atlas::wall_quad_W(center, size); + } + return array; +} + +} // namespace Magnum::Examples |