diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-08 21:52:49 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-08 21:52:49 +0200 |
commit | 4c151b5d580e6c855f05583a04fbf5e4220e11cf (patch) | |
tree | aea029a944df3ab6c404bb48472bd9498210116b /src | |
parent | 4a21f53f53f6982b0aec725bd9ae4eebfb1ce39c (diff) |
a
Diffstat (limited to 'src')
-rw-r--r-- | src/tile-atlas.hpp | 2 | ||||
-rw-r--r-- | src/tile-defs.hpp | 1 | ||||
-rw-r--r-- | src/wireframe-mesh.cpp | 68 | ||||
-rw-r--r-- | src/wireframe-mesh.hpp | 78 |
4 files changed, 147 insertions, 2 deletions
diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp index 2d3461cc..af9e505b 100644 --- a/src/tile-atlas.hpp +++ b/src/tile-atlas.hpp @@ -8,8 +8,6 @@ namespace std::filesystem { class path; } namespace Magnum::Examples { -constexpr inline Vector3 TILE_SIZE = { 64, 64, 64 }; - struct tile_atlas final { using quad = std::array<Vector3, 4>; diff --git a/src/tile-defs.hpp b/src/tile-defs.hpp index 40d1809b..05aaf82d 100644 --- a/src/tile-defs.hpp +++ b/src/tile-defs.hpp @@ -5,5 +5,6 @@ namespace Magnum::Examples { constexpr inline std::size_t TILE_MAX_DIM = 16; constexpr inline std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM; +constexpr inline float TILE_SIZE[3] = { 64, 64, 64 }; } // namespace Magnum::Examples diff --git a/src/wireframe-mesh.cpp b/src/wireframe-mesh.cpp new file mode 100644 index 00000000..5b60cead --- /dev/null +++ b/src/wireframe-mesh.cpp @@ -0,0 +1,68 @@ +#include "wireframe-mesh.hpp" +#include "shaders/tile-shader.hpp" +#include <Corrade/Containers/Array.h> +#include <Magnum/ImageView.h> +#include <Magnum/GL/TextureFormat.h> +#include <Magnum/PixelFormat.h> +#include <Magnum/Trade/ImageData.h> + +namespace Magnum::Examples::wireframe_traits { + +GL::RectangleTexture wireframe_traits::null::make_constant_texture() +{ + Trade::ImageData2D img{PixelFormat::RGBA8UI, {1, 1}, // NOLINT(misc-const-correctness) + Containers::Array<char>{Corrade::DirectInit, 4, (char)(unsigned char)255}}; + + GL::RectangleTexture tex; + tex.setWrapping(GL::SamplerWrapping::Repeat) + .setMagnificationFilter(GL::SamplerFilter::Nearest) + .setMinificationFilter(GL::SamplerFilter::Nearest) + .setMaxAnisotropy(0) + .setStorage(GL::textureFormat(img.format()), img.size()) + .setSubImage({}, std::move(img)); + return tex; +} + +quad::vertex_array quad::make_vertex_positions_array() const +{ + constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1]; + constexpr float Z = 0; + return {{ + { -X + center[0], -Y + center[1], Z + center[2] }, + { X + center[0], -Y + center[1], Z + center[2] }, + { X + center[0], Y + center[1], Z + center[2] }, + { -X + center[0], Y + center[1], Z + center[2] }, + }}; +} + +quad::quad(Vector3 center, Vector2 size) : center(center), size(size) {} + +} // namespace Magnum::Examples::wireframe_traits + +namespace Magnum::Examples { + +using wireframe_traits::traits; + +template <traits T> wireframe_mesh<T>::wireframe_mesh() +{ + _mesh.setCount((int)T::num_vertices) + .addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{}) + .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{}); + if constexpr(T::num_indices > 0) + _mesh.setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort); + CORRADE_INTERNAL_ASSERT(T::num_indices > 0 == _mesh.isIndexed()); +}; + +template <traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x) +{ + _positions_buffer.setData(x.make_vertex_positions_array(), GL::BufferUsage::DynamicDraw); + if constexpr(T::num_indices > 0) + _index_buffer.setData(x.make_index_array(), GL::BufferUsage::DynamicDraw); + shader.draw(_mesh); +} + +template struct wireframe_mesh<wireframe_traits::null>; +template struct wireframe_mesh<wireframe_traits::quad>; + +} // namespace Magnum::Examples + diff --git a/src/wireframe-mesh.hpp b/src/wireframe-mesh.hpp new file mode 100644 index 00000000..c31ee1d0 --- /dev/null +++ b/src/wireframe-mesh.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include "tile-atlas.hpp" +#include "tile-defs.hpp" +#include <array> +#include <utility> +#include <Corrade/Containers/ArrayViewStl.h> +#include <Magnum/Math/Vector2.h> +#include <Magnum/GL/Buffer.h> +#include <Magnum/GL/Mesh.h> +#include "Magnum/GL/RectangleTexture.h" + +namespace Magnum::Examples { + +struct tile_shader; + +namespace wireframe_traits { + +template<typename T> +concept traits = requires (const T& x) { + {T::num_vertices} -> std::convertible_to<std::size_t>; + {T::num_indices} -> std::convertible_to<std::size_t>; + {x.primitive} -> std::convertible_to<GL::MeshPrimitive>; + {x.make_vertex_positions_array() } -> std::same_as<std::array<Vector3, T::num_vertices>>; + {x.make_index_array() } -> std::same_as<std::array<UnsignedShort, T::num_indices>>; +}; + + +struct null final +{ + static constexpr auto primitive = GL::MeshPrimitive::Triangles; + static constexpr std::size_t num_vertices = 0, num_indices = 0; + static GL::RectangleTexture make_constant_texture(); + static std::array<Vector3, 0> make_vertex_positions_array() { return {}; } + static std::array<UnsignedShort, 0> make_index_array() { return {}; } +}; + +struct quad final +{ + quad(Vector3 center, Vector2 size = {TILE_SIZE[0], TILE_SIZE[1]}); + constexpr quad() = default; + + static constexpr std::size_t num_vertices = 4; + static constexpr std::size_t num_indices = 0; + static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop; + + using vertex_array = std::array<Vector3, num_vertices>; + using index_array = std::array<UnsignedShort, num_indices>; + + vertex_array make_vertex_positions_array() const; + static index_array make_index_array() { return {}; } + +private: + Vector3 center = {}; + Vector2 size = { TILE_SIZE[0], TILE_SIZE[1] }; +}; + +} // namespace wireframe_traits + + +template<wireframe_traits::traits T> +struct wireframe_mesh final +{ + wireframe_mesh(); + void draw(tile_shader& shader, T traits); + +private: + GL::Buffer _positions_buffer{}, + _texcoords_buffer{std::array<Vector2, T::num_vertices>{}}, + _index_buffer{T::num_indices == 0 ? GL::Buffer{NoCreate} : GL::Buffer{}}; + GL::RectangleTexture _texture = wireframe_traits::null::make_constant_texture(); + GL::Mesh _mesh; +}; + +extern template struct wireframe_mesh<wireframe_traits::null>; +extern template struct wireframe_mesh<wireframe_traits::quad>; + +} // namespace Magnum::Examples |