summaryrefslogtreecommitdiffhomepage
path: root/draw/wireframe.hpp
blob: 7b33a7fc1bd1c6d9a1b061a7adb93aa476d1007c (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
#pragma once
#include "src/tile-defs.hpp"
#include <Corrade/Containers/ArrayView.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/Mesh.h>
#include "Magnum/GL/Texture.h"

namespace floormat { struct tile_shader; }

namespace floormat::wireframe {

GL::Texture2D make_constant_texture();

struct mesh_base
{
    static void set_line_width(float width);

protected:
    GL::Buffer _vertex_buffer{{}, GL::BufferUsage::DynamicDraw}, _constant_buffer, _index_buffer;
    GL::Texture2D* _texture;
    GL::Mesh _mesh;

    mesh_base(GL::MeshPrimitive primitive, ArrayView<const void> index_data,
              size_t num_vertices, size_t num_indexes, GL::Texture2D* texture);
    void draw(tile_shader& shader);
    void set_subdata(ArrayView<const void> array);
};

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

template<typename T>
wireframe_mesh<T>::wireframe_mesh(GL::Texture2D& constant_texture) :
    wireframe::mesh_base{T::primitive, T::make_index_array(), T::num_vertices, T::num_indexes, &constant_texture}
{
}

template <typename T> void wireframe_mesh<T>::draw(tile_shader& shader, T x)
{
    //_texcoord_buffer.setData({nullptr, sizeof(Vector3) * T::num_vertices}, GL::BufferUsage::DynamicDraw); // orphan the buffer
    set_subdata(x.make_vertex_array());
    x.on_draw();
    mesh_base::draw(shader);
}

} // namespace floormat::wireframe