summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-06 18:14:06 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-06 18:14:06 +0200
commit5dbc992e2128029eca31b908eb799871c80cdef6 (patch)
tree3240eda4771fb6270f09becd0ea6265891cbec2f /src
parent93142642132f71154b474ef01b0ea44fc7cacddd (diff)
a
Diffstat (limited to 'src')
-rw-r--r--src/wall-mesh.cpp47
-rw-r--r--src/wall-mesh.hpp13
2 files changed, 35 insertions, 25 deletions
diff --git a/src/wall-mesh.cpp b/src/wall-mesh.cpp
index ebbfee75..68d8733e 100644
--- a/src/wall-mesh.cpp
+++ b/src/wall-mesh.cpp
@@ -12,54 +12,49 @@ constexpr auto quad_index_count = 6;
wall_mesh::wall_mesh()
{
_mesh.setCount((int)(quad_index_count * COUNT))
- .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}, tile_shader::Position{})
+ .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, std::size_t& pos_,
- tile_image& img, const position_array& positions)
+void wall_mesh::add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos)
{
- const auto pos = 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], positions[i] };
+ data[pos][i] = { texcoords[i] };
textures[pos] = &img.atlas->texture();
}
}
-void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, std::size_t& pos, tile& x, local_coords pt)
+void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos)
{
- constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2];
- constexpr Vector3 size = {X, Y, Z};
- Vector3 center{(float)(X*pt.x), (float)(Y*pt.y), 0};
-
if (auto& wall = x.wall_north; wall.atlas)
- add_wall(data, textures, pos, wall, tile_atlas::wall_quad_N(center, size));
+ add_wall(data, textures, wall, pos * 2 + 0);
if (auto& wall = x.wall_west; wall.atlas)
- add_wall(data, textures, pos, wall, tile_atlas::wall_quad_W(center, size));
+ add_wall(data, textures, wall, pos * 2 + 1);
}
void wall_mesh::draw(tile_shader& shader, chunk& c)
{
texture_array textures = {};
- std::size_t pos = 0;
{
vertex_array data;
- c.foreach_tile([&](tile& x, std::size_t, local_coords pt) {
- maybe_add_tile(data, textures, pos, x, pt);
+ c.foreach_tile([&](tile& x, std::size_t idx, local_coords) {
+ maybe_add_tile(data, textures, x, idx);
});
- _vertex_buffer.setSubData(0, {data.data(), pos});
+ _vertex_buffer.setSubData(0, data);
}
const GL::RectangleTexture* last_texture = nullptr;
Magnum::GL::MeshView mesh{_mesh};
- for (std::size_t i = 0; i < pos; i++)
+ for (std::size_t i = 0; i < COUNT; i++)
{
auto* const tex = textures[i];
- CORRADE_INTERNAL_ASSERT(tex != nullptr);
+ 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)
@@ -79,4 +74,20 @@ std::array<std::array<UnsignedShort, 6>, wall_mesh::COUNT> wall_mesh::make_index
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
diff --git a/src/wall-mesh.hpp b/src/wall-mesh.hpp
index ce1ff30a..56bd78ee 100644
--- a/src/wall-mesh.hpp
+++ b/src/wall-mesh.hpp
@@ -19,29 +19,28 @@ struct wall_mesh final
void draw(tile_shader& shader, chunk& c);
private:
- static constexpr auto COUNT = TILE_MAX_DIM*2 * TILE_MAX_DIM*2;
+ static constexpr auto COUNT1 = TILE_MAX_DIM*2, COUNT = COUNT1 * COUNT1;
using texcoords_array = std::array<Vector2, 4>;
using position_array = std::array<Vector3, 4>;
struct vertex final {
typename texcoords_array::value_type texcoords;
- typename position_array::value_type position;
};
using quad = std::array<vertex, 4>;
using vertex_array = std::array<quad, COUNT>;
using texture_array = std::array<GL::RectangleTexture*, COUNT>;
- static void maybe_add_tile(vertex_array& data, texture_array& textures, std::size_t& pos,
- tile& x, local_coords pt);
- static void add_wall(vertex_array& data, texture_array& textures, std::size_t& pos,
- tile_image& img, const position_array& positions);
+ static void maybe_add_tile(vertex_array& data, texture_array& textures, tile& x, std::size_t pos);
+ static void add_wall(vertex_array& data, texture_array& textures, tile_image& img, std::size_t pos);
GL::Mesh _mesh;
GL::Buffer _vertex_buffer{vertex_array{}, Magnum::GL::BufferUsage::DynamicDraw},
- _index_buffer{make_index_array(), Magnum::GL::BufferUsage::StaticDraw};
+ _index_buffer{make_index_array(), Magnum::GL::BufferUsage::StaticDraw},
+ _positions_buffer{make_position_array()};
static std::array<std::array<UnsignedShort, 6>, COUNT> make_index_array();
+ static std::array<std::array<Vector3, 4>, COUNT> make_position_array();
};
} // namespace Magnum::Examples