summaryrefslogtreecommitdiffhomepage
path: root/draw/wireframe-mesh.hpp
blob: 016a92af7e8b8f06a4db22539af864f4919feb80 (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
#pragma once

#include "tile-defs.hpp"
#include <array>
#include <utility>
#include <Corrade/Containers/ArrayView.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>;
    {T::num_indexes} -> std::convertible_to<std::size_t>;
    {x.primitive} -> std::convertible_to<GL::MeshPrimitive>;
    {x.make_vertex_array() } -> std::convertible_to<Containers::ArrayView<const void>>;
    {T::make_index_array() } -> std::convertible_to<Containers::ArrayView<const void>>;
    {x.on_draw()} -> std::same_as<void>;
};

struct mesh_base
{
    static GL::RectangleTexture make_constant_texture();
    GL::Buffer _vertex_buffer, _texcoords_buffer, _index_buffer;
    GL::RectangleTexture _texture = make_constant_texture();
    GL::Mesh _mesh;

    mesh_base(GL::MeshPrimitive primitive, Containers::ArrayView<const void> index_data,
              std::size_t num_vertices, std::size_t num_indexes);
    void draw(tile_shader& shader);
};

} // namespace wireframe

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

template<wireframe::traits T>
wireframe_mesh<T>::wireframe_mesh() :
      wireframe::mesh_base{T::primitive, T::make_index_array(), T::num_vertices, T::num_indexes}
{
}

template <wireframe::traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x)
{
    _vertex_buffer.setSubData(0, x.make_vertex_array());
    mesh_base::draw(shader);
}

} // namespace Magnum::Examples