From 94f6748d5f5b9fdc3047022fe59d66028bde63f3 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 9 Oct 2022 05:06:41 +0200 Subject: a --- CMakeLists.txt | 1 + draw/CMakeLists.txt | 9 +++++ draw/floor-mesh.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ draw/floor-mesh.hpp | 39 +++++++++++++++++++++ draw/wall-mesh.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ draw/wall-mesh.hpp | 43 +++++++++++++++++++++++ draw/wireframe-mesh.cpp | 47 +++++++++++++++++++++++++ draw/wireframe-mesh.hpp | 58 ++++++++++++++++++++++++++++++ draw/wireframe-quad.cpp | 25 +++++++++++++ draw/wireframe-quad.hpp | 32 +++++++++++++++++ main/CMakeLists.txt | 2 +- main/app.hpp | 11 +++--- src/floor-mesh.cpp | 78 ----------------------------------------- src/floor-mesh.hpp | 39 --------------------- src/wall-mesh.cpp | 93 ------------------------------------------------- src/wall-mesh.hpp | 43 ----------------------- src/wireframe-mesh.cpp | 47 ------------------------- src/wireframe-mesh.hpp | 58 ------------------------------ src/wireframe-quad.cpp | 25 ------------- src/wireframe-quad.hpp | 32 ----------------- 20 files changed, 432 insertions(+), 421 deletions(-) create mode 100644 draw/CMakeLists.txt create mode 100644 draw/floor-mesh.cpp create mode 100644 draw/floor-mesh.hpp create mode 100644 draw/wall-mesh.cpp create mode 100644 draw/wall-mesh.hpp create mode 100644 draw/wireframe-mesh.cpp create mode 100644 draw/wireframe-mesh.hpp create mode 100644 draw/wireframe-quad.cpp create mode 100644 draw/wireframe-quad.hpp delete mode 100644 src/floor-mesh.cpp delete mode 100644 src/floor-mesh.hpp delete mode 100644 src/wall-mesh.cpp delete mode 100644 src/wall-mesh.hpp delete mode 100644 src/wireframe-mesh.cpp delete mode 100644 src/wireframe-mesh.hpp delete mode 100644 src/wireframe-quad.cpp delete mode 100644 src/wireframe-quad.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index eabc854f..413b9d79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ if(NOT BOOTSTRAP_DEPENDS) include_directories(src) add_subdirectory(src) + add_subdirectory(draw) add_subdirectory(main) add_subdirectory(anim-crop-tool) add_subdirectory(test) diff --git a/draw/CMakeLists.txt b/draw/CMakeLists.txt new file mode 100644 index 00000000..9dd2e869 --- /dev/null +++ b/draw/CMakeLists.txt @@ -0,0 +1,9 @@ +set(self ${PROJECT_NAME}-draw) +file(GLOB sources "*.cpp" CONFIGURE_ARGS) +add_library(${self} STATIC "${sources}") +target_link_libraries(${self} PUBLIC + Magnum::GL + Magnum::Magnum + Magnum::Shaders + ${PROJECT_NAME} +) diff --git a/draw/floor-mesh.cpp b/draw/floor-mesh.cpp new file mode 100644 index 00000000..3698bc6e --- /dev/null +++ b/draw/floor-mesh.cpp @@ -0,0 +1,78 @@ +#include "floor-mesh.hpp" +#include "shaders/tile-shader.hpp" +#include "tile.hpp" +#include "chunk.hpp" +#include "tile-atlas.hpp" +#include + +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) +{ + if (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] }; + } + else + for (size_t i = 0; i < 4; i++) + data[i] = {}; +} + +void floor_mesh::draw(tile_shader& shader, chunk& c) +{ + std::array data; + for (auto& [x, idx, pt] : c) { + 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; + for (auto& [x, i, pt] : c) { + if (!x.ground_image) + continue; + 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, TILE_COUNT> floor_mesh::make_index_array() +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array, TILE_COUNT> array; + for (std::size_t i = 0; i < std::size(array); i++) + array[i] = tile_atlas::indices(i); + return array; +} + +std::array, TILE_COUNT> floor_mesh::make_position_array() +{ + std::array, 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 diff --git a/draw/floor-mesh.hpp b/draw/floor-mesh.hpp new file mode 100644 index 00000000..fed5867c --- /dev/null +++ b/draw/floor-mesh.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "tile-defs.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace Magnum::Examples { + +struct tile_shader; +struct chunk; +struct tile; + +struct floor_mesh final +{ + floor_mesh(); + floor_mesh(floor_mesh&&) = delete; + floor_mesh(const floor_mesh&) = delete; + + void draw(tile_shader& shader, chunk& c); + +private: + struct vertex_data final { Vector2 texcoords; }; + using quad_data = std::array; + + static std::array, TILE_COUNT> make_index_array(); + static std::array, TILE_COUNT> make_position_array(); + + GL::Mesh _mesh; + GL::Buffer _vertex_buffer{std::array{}, Magnum::GL::BufferUsage::DynamicDraw}, + _index_buffer{make_index_array()}, _positions_buffer{make_position_array()}; + + static void set_tile(quad_data& data, tile& x); +}; + +} // namespace Magnum::Examples 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 +#include + +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, wall_mesh::COUNT> wall_mesh::make_index_array() +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) + std::array, COUNT> array; + + for (std::size_t i = 0; i < std::size(array); i++) + array[i] = tile_atlas::indices(i); + return array; +} + +std::array, wall_mesh::COUNT> wall_mesh::make_position_array() +{ + std::array, 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/draw/wall-mesh.hpp b/draw/wall-mesh.hpp new file mode 100644 index 00000000..4a4e9615 --- /dev/null +++ b/draw/wall-mesh.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "tile.hpp" +#include +#include +#include +#include +#include +#include + +namespace Magnum::Examples { + +struct tile_shader; +struct chunk; + +struct wall_mesh final +{ + wall_mesh(); + void draw(tile_shader& shader, chunk& c); + +private: + static constexpr auto COUNT1 = TILE_MAX_DIM*2, COUNT = COUNT1 * COUNT1; + + struct vertex final { + Vector2 texcoords; + }; + + using quad = std::array; + using vertex_array = std::array; + using texture_array = std::array; + + 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()}, + _positions_buffer{make_position_array()}; + static std::array, COUNT> make_index_array(); + static std::array, COUNT> make_position_array(); +}; + +} // namespace Magnum::Examples diff --git a/draw/wireframe-mesh.cpp b/draw/wireframe-mesh.cpp new file mode 100644 index 00000000..1425df60 --- /dev/null +++ b/draw/wireframe-mesh.cpp @@ -0,0 +1,47 @@ +#include "wireframe-mesh.hpp" +#include "shaders/tile-shader.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Magnum::Examples::wireframe +{ + +GL::RectangleTexture mesh_base::make_constant_texture() +{ + const Vector4ub data[] = { {255, 255, 255, 255} }; + Trade::ImageData2D img{PixelStorage{}.setImageHeight(1).setRowLength(1).setAlignment(1), + PixelFormat::RGBA8Unorm, {1, 1}, {}, + Containers::arrayView(data, 1), {}, {}}; + GL::RectangleTexture tex; + tex.setWrapping(GL::SamplerWrapping::ClampToEdge) + .setMagnificationFilter(GL::SamplerFilter::Nearest) + .setMinificationFilter(GL::SamplerFilter::Nearest) + .setMaxAnisotropy(1) + .setStorage(GL::textureFormat(img.format()), img.size()) + .setSubImage({}, std::move(img)); + return tex; +} + +mesh_base::mesh_base(GL::MeshPrimitive primitive, std::size_t num_vertices) : + _texcoords_buffer{std::vector{num_vertices}} +{ + _mesh.setCount((int)num_vertices) + .setPrimitive(primitive) + .addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{}) + .addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{}); +} + +void mesh_base::draw(tile_shader& shader) +{ + _texture.bind(0); + shader.draw(_mesh); +} + + +} // namespace Magnum::Examples::wireframe diff --git a/draw/wireframe-mesh.hpp b/draw/wireframe-mesh.hpp new file mode 100644 index 00000000..0ae122f5 --- /dev/null +++ b/draw/wireframe-mesh.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include "tile-defs.hpp" +#include +#include +#include +#include +#include +#include +#include "Magnum/GL/RectangleTexture.h" + +namespace Magnum::Examples { + +struct tile_shader; + +namespace wireframe +{ + +template +concept traits = requires (const T& x) { + {T::num_vertices} -> std::convertible_to; + {x.primitive} -> std::convertible_to; + {x.make_vertex_array() } -> std::convertible_to>; + {x.on_draw()} -> std::same_as; +}; + +struct mesh_base +{ + static GL::RectangleTexture make_constant_texture(); + GL::Buffer _vertex_buffer, _texcoords_buffer; + GL::RectangleTexture _texture = make_constant_texture(); + GL::Mesh _mesh; + + mesh_base(GL::MeshPrimitive primitive, std::size_t num_vertices); + void draw(tile_shader& shader); +}; + +} // namespace wireframe + +template +struct wireframe_mesh final : private wireframe::mesh_base +{ + wireframe_mesh(); + void draw(tile_shader& shader, T traits); +}; + +template +wireframe_mesh::wireframe_mesh() : + wireframe::mesh_base{T::primitive, T::num_vertices} +{} + +template void wireframe_mesh::draw(tile_shader& shader, T x) +{ + _vertex_buffer.setData(x.make_vertex_array(), GL::BufferUsage::DynamicDraw); + mesh_base::draw(shader); +} + +} // namespace Magnum::Examples diff --git a/draw/wireframe-quad.cpp b/draw/wireframe-quad.cpp new file mode 100644 index 00000000..18b49628 --- /dev/null +++ b/draw/wireframe-quad.cpp @@ -0,0 +1,25 @@ +#include "wireframe-quad.hpp" +#include +#include + +namespace Magnum::Examples::wireframe { + +quad::vertex_array quad::make_vertex_array() const +{ + const float X = size[0]*.5f, Y = size[1]*.5f; + return {{ + { -X + center[0], -Y + center[1], center[2] }, + { X + center[0], -Y + center[1], center[2] }, + { X + center[0], Y + center[1], center[2] }, + { -X + center[0], Y + center[1], center[2] }, + }}; +} + +quad::quad(Vector3 center, Vector2 size) : center(center), size(size) {} + +void quad::on_draw() const +{ + GL::Renderer::setLineWidth(line_width); +} + +} // namespace Magnum::Examples::wireframe diff --git a/draw/wireframe-quad.hpp b/draw/wireframe-quad.hpp new file mode 100644 index 00000000..062bf913 --- /dev/null +++ b/draw/wireframe-quad.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +namespace Magnum::Examples::wireframe { + +template +struct wireframe_mesh; + +struct quad final +{ + quad(Vector3 center, Vector2 size); + + static constexpr std::size_t num_vertices = 4; + static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop; + + using vertex_array = std::array; + + vertex_array make_vertex_array() const; + void on_draw() const; + +private: + Vector3 center; + Vector2 size; + float line_width = 2; +}; + + + +} // namespace Magnum::Examples::wireframe diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 1d8da0a1..9723e38b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,7 +1,7 @@ set(self "${PROJECT_NAME}-main") file(GLOB sources "*.cpp" CONFIGURE_ARGS) -link_libraries(${PROJECT_NAME}) +link_libraries(${PROJECT_NAME} ${PROJECT_NAME}-draw) link_libraries(Magnum::Sdl2Application Magnum::Trade) corrade_add_resource(res ../resources.conf) diff --git a/main/app.hpp b/main/app.hpp index 07a64d2f..c691f85d 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -3,10 +3,10 @@ #include "chunk.hpp" #include "shaders/tile-shader.hpp" #include "src/loader.hpp" -#include "floor-mesh.hpp" -#include "wall-mesh.hpp" -#include "wireframe-mesh.hpp" -#include "wireframe-quad.hpp" +#include "draw/floor-mesh.hpp" +#include "draw/wall-mesh.hpp" +#include "draw/wireframe-mesh.hpp" +#include "draw/wireframe-quad.hpp" #include "compat/enum-bitset.hpp" #include #include @@ -44,13 +44,14 @@ struct app final : Platform::Application }; chunk make_test_chunk(); - const void* const _dummy = register_debug_callback(); + const void* _dummy = register_debug_callback(); tile_shader _shader; tile_atlas_ floor1 = loader.tile_atlas("share/game/images/metal1.tga", {2, 2}); tile_atlas_ floor2 = loader.tile_atlas("share/game/images/floor1.tga", {4, 4}); tile_atlas_ wall1 = loader.tile_atlas("share/game/images/wood2.tga", {2, 2}); tile_atlas_ wall2 = loader.tile_atlas("share/game/images/wood1.tga", {2, 2}); chunk _chunk = make_test_chunk(); + floor_mesh _floor_mesh; wall_mesh _wall_mesh; wireframe_mesh _wireframe_quad; diff --git a/src/floor-mesh.cpp b/src/floor-mesh.cpp deleted file mode 100644 index 3698bc6e..00000000 --- a/src/floor-mesh.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "floor-mesh.hpp" -#include "shaders/tile-shader.hpp" -#include "tile.hpp" -#include "chunk.hpp" -#include "tile-atlas.hpp" -#include - -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) -{ - if (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] }; - } - else - for (size_t i = 0; i < 4; i++) - data[i] = {}; -} - -void floor_mesh::draw(tile_shader& shader, chunk& c) -{ - std::array data; - for (auto& [x, idx, pt] : c) { - 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; - for (auto& [x, i, pt] : c) { - if (!x.ground_image) - continue; - 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, TILE_COUNT> floor_mesh::make_index_array() -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array, TILE_COUNT> array; - for (std::size_t i = 0; i < std::size(array); i++) - array[i] = tile_atlas::indices(i); - return array; -} - -std::array, TILE_COUNT> floor_mesh::make_position_array() -{ - std::array, 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 diff --git a/src/floor-mesh.hpp b/src/floor-mesh.hpp deleted file mode 100644 index fed5867c..00000000 --- a/src/floor-mesh.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "tile-defs.hpp" -#include -#include -#include -#include -#include -#include -#include - -namespace Magnum::Examples { - -struct tile_shader; -struct chunk; -struct tile; - -struct floor_mesh final -{ - floor_mesh(); - floor_mesh(floor_mesh&&) = delete; - floor_mesh(const floor_mesh&) = delete; - - void draw(tile_shader& shader, chunk& c); - -private: - struct vertex_data final { Vector2 texcoords; }; - using quad_data = std::array; - - static std::array, TILE_COUNT> make_index_array(); - static std::array, TILE_COUNT> make_position_array(); - - GL::Mesh _mesh; - GL::Buffer _vertex_buffer{std::array{}, Magnum::GL::BufferUsage::DynamicDraw}, - _index_buffer{make_index_array()}, _positions_buffer{make_position_array()}; - - static void set_tile(quad_data& data, tile& x); -}; - -} // namespace Magnum::Examples diff --git a/src/wall-mesh.cpp b/src/wall-mesh.cpp deleted file mode 100644 index d698b508..00000000 --- a/src/wall-mesh.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "wall-mesh.hpp" -#include "tile-atlas.hpp" -#include "shaders/tile-shader.hpp" -#include "chunk.hpp" -#include -#include - -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, wall_mesh::COUNT> wall_mesh::make_index_array() -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) - std::array, COUNT> array; - - for (std::size_t i = 0; i < std::size(array); i++) - array[i] = tile_atlas::indices(i); - return array; -} - -std::array, wall_mesh::COUNT> wall_mesh::make_position_array() -{ - std::array, 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 deleted file mode 100644 index 4a4e9615..00000000 --- a/src/wall-mesh.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "tile.hpp" -#include -#include -#include -#include -#include -#include - -namespace Magnum::Examples { - -struct tile_shader; -struct chunk; - -struct wall_mesh final -{ - wall_mesh(); - void draw(tile_shader& shader, chunk& c); - -private: - static constexpr auto COUNT1 = TILE_MAX_DIM*2, COUNT = COUNT1 * COUNT1; - - struct vertex final { - Vector2 texcoords; - }; - - using quad = std::array; - using vertex_array = std::array; - using texture_array = std::array; - - 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()}, - _positions_buffer{make_position_array()}; - static std::array, COUNT> make_index_array(); - static std::array, COUNT> make_position_array(); -}; - -} // namespace Magnum::Examples diff --git a/src/wireframe-mesh.cpp b/src/wireframe-mesh.cpp deleted file mode 100644 index 1425df60..00000000 --- a/src/wireframe-mesh.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "wireframe-mesh.hpp" -#include "shaders/tile-shader.hpp" -#include -#include -#include -#include -#include -#include -#include -#include - -namespace Magnum::Examples::wireframe -{ - -GL::RectangleTexture mesh_base::make_constant_texture() -{ - const Vector4ub data[] = { {255, 255, 255, 255} }; - Trade::ImageData2D img{PixelStorage{}.setImageHeight(1).setRowLength(1).setAlignment(1), - PixelFormat::RGBA8Unorm, {1, 1}, {}, - Containers::arrayView(data, 1), {}, {}}; - GL::RectangleTexture tex; - tex.setWrapping(GL::SamplerWrapping::ClampToEdge) - .setMagnificationFilter(GL::SamplerFilter::Nearest) - .setMinificationFilter(GL::SamplerFilter::Nearest) - .setMaxAnisotropy(1) - .setStorage(GL::textureFormat(img.format()), img.size()) - .setSubImage({}, std::move(img)); - return tex; -} - -mesh_base::mesh_base(GL::MeshPrimitive primitive, std::size_t num_vertices) : - _texcoords_buffer{std::vector{num_vertices}} -{ - _mesh.setCount((int)num_vertices) - .setPrimitive(primitive) - .addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{}) - .addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{}); -} - -void mesh_base::draw(tile_shader& shader) -{ - _texture.bind(0); - shader.draw(_mesh); -} - - -} // namespace Magnum::Examples::wireframe diff --git a/src/wireframe-mesh.hpp b/src/wireframe-mesh.hpp deleted file mode 100644 index 0ae122f5..00000000 --- a/src/wireframe-mesh.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "tile-defs.hpp" -#include -#include -#include -#include -#include -#include -#include "Magnum/GL/RectangleTexture.h" - -namespace Magnum::Examples { - -struct tile_shader; - -namespace wireframe -{ - -template -concept traits = requires (const T& x) { - {T::num_vertices} -> std::convertible_to; - {x.primitive} -> std::convertible_to; - {x.make_vertex_array() } -> std::convertible_to>; - {x.on_draw()} -> std::same_as; -}; - -struct mesh_base -{ - static GL::RectangleTexture make_constant_texture(); - GL::Buffer _vertex_buffer, _texcoords_buffer; - GL::RectangleTexture _texture = make_constant_texture(); - GL::Mesh _mesh; - - mesh_base(GL::MeshPrimitive primitive, std::size_t num_vertices); - void draw(tile_shader& shader); -}; - -} // namespace wireframe - -template -struct wireframe_mesh final : private wireframe::mesh_base -{ - wireframe_mesh(); - void draw(tile_shader& shader, T traits); -}; - -template -wireframe_mesh::wireframe_mesh() : - wireframe::mesh_base{T::primitive, T::num_vertices} -{} - -template void wireframe_mesh::draw(tile_shader& shader, T x) -{ - _vertex_buffer.setData(x.make_vertex_array(), GL::BufferUsage::DynamicDraw); - mesh_base::draw(shader); -} - -} // namespace Magnum::Examples diff --git a/src/wireframe-quad.cpp b/src/wireframe-quad.cpp deleted file mode 100644 index 18b49628..00000000 --- a/src/wireframe-quad.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "wireframe-quad.hpp" -#include -#include - -namespace Magnum::Examples::wireframe { - -quad::vertex_array quad::make_vertex_array() const -{ - const float X = size[0]*.5f, Y = size[1]*.5f; - return {{ - { -X + center[0], -Y + center[1], center[2] }, - { X + center[0], -Y + center[1], center[2] }, - { X + center[0], Y + center[1], center[2] }, - { -X + center[0], Y + center[1], center[2] }, - }}; -} - -quad::quad(Vector3 center, Vector2 size) : center(center), size(size) {} - -void quad::on_draw() const -{ - GL::Renderer::setLineWidth(line_width); -} - -} // namespace Magnum::Examples::wireframe diff --git a/src/wireframe-quad.hpp b/src/wireframe-quad.hpp deleted file mode 100644 index 062bf913..00000000 --- a/src/wireframe-quad.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace Magnum::Examples::wireframe { - -template -struct wireframe_mesh; - -struct quad final -{ - quad(Vector3 center, Vector2 size); - - static constexpr std::size_t num_vertices = 4; - static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop; - - using vertex_array = std::array; - - vertex_array make_vertex_array() const; - void on_draw() const; - -private: - Vector3 center; - Vector2 size; - float line_width = 2; -}; - - - -} // namespace Magnum::Examples::wireframe -- cgit v1.2.3