summaryrefslogtreecommitdiffhomepage
path: root/src/wireframe-mesh.hpp
blob: 7a6892c2623145eac6c38206a42be14542f1de3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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
{

template<typename T>
concept traits = requires (const T& x) {
    {T::num_vertices} -> std::convertible_to<std::size_t>;
    {x.primitive} -> std::convertible_to<GL::MeshPrimitive>;
    {x.make_vertex_array() } -> std::same_as<std::array<Vector3, T::num_vertices>>;
};

struct null final
{
    static constexpr auto primitive = GL::MeshPrimitive::Triangles;
    static constexpr std::size_t num_vertices = 0;
    static GL::RectangleTexture make_constant_texture();
    static std::array<Vector3, 0> make_vertex_array() { return {}; }
};

struct quad final
{
    quad(Vector3 center, Vector2 size);
    constexpr quad() = default;

    static constexpr std::size_t num_vertices = 4;
    static constexpr GL::MeshPrimitive primitive = GL::MeshPrimitive::LineLoop;

    using vertex_array = std::array<Vector3, num_vertices>;
    vertex_array make_vertex_array() const;

private:
    Vector3 center = {};
    Vector2 size = { TILE_SIZE[0], TILE_SIZE[1] };
};

} // namespace wireframe

template<wireframe::traits T>
struct wireframe_mesh final
{
    wireframe_mesh();
    void draw(tile_shader& shader, T traits);

private:
    GL::Buffer _vertex_buffer{std::array<Vector3, T::num_vertices>{}}, _texcoords_buffer{std::array<Vector2, T::num_vertices>{}};
    GL::RectangleTexture _texture = wireframe::null::make_constant_texture();
    GL::Mesh _mesh;
};

extern template struct wireframe_mesh<wireframe::null>;
extern template struct wireframe_mesh<wireframe::quad>;

using wireframe_quad_mesh = wireframe_mesh<wireframe::quad>;

} // namespace Magnum::Examples