summaryrefslogtreecommitdiffhomepage
path: root/src/wireframe-mesh.cpp
blob: e03723bff6f3a1de11855a33a4c9d00cc038894a (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
71
#include "wireframe-mesh.hpp"
#include "shaders/tile-shader.hpp"
#include "tile-atlas.hpp"
#include "compat/assert.hpp"
#include <Corrade/Containers/Array.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/GL/TextureFormat.h>
#include <Magnum/ImageFlags.h>
#include <Magnum/ImageView.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/PixelStorage.h>
#include <Magnum/Trade/ImageData.h>

namespace Magnum::Examples::wireframe
{

GL::RectangleTexture wireframe::null::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;
}

quad::vertex_array quad::make_vertex_array() const
{
    constexpr auto X = TILE_SIZE[0]*.5f, Y = TILE_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) {}

} // namespace Magnum::Examples::wireframe

namespace Magnum::Examples {

template <wireframe::traits T> wireframe_mesh<T>::wireframe_mesh()
{
    _mesh.setCount((int)T::num_vertices)
        .setPrimitive(T::primitive)
        .addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{})
        .addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{});
}

template <wireframe::traits T> void wireframe_mesh<T>::draw(tile_shader& shader, T x)
{
    GL::Renderer::setLineWidth(3);
    auto array = x.make_vertex_array();
    _vertex_buffer.setSubData(0, array);
    _texture.bind(0);
    shader.draw(_mesh);
}

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

} // namespace Magnum::Examples