summaryrefslogtreecommitdiffhomepage
path: root/wall-mesh.cpp
blob: 31399aedc32ea9bd801383f9b133292aa24b6625 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "wall-mesh.hpp"
#include "tile-atlas.hpp"
#include "shaders/tile-shader.hpp"
#include "chunk.hpp"
#include <Magnum/GL/MeshView.h>

namespace Magnum::Examples {

wall_mesh::wall_mesh()
{
    _mesh.setCount((int)(_index_data.size() * _index_data[0].size()))
         .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{}, tile_shader::Position{})
         .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
    CORRADE_INTERNAL_ASSERT(_mesh.isIndexed());
}

void wall_mesh::add_wall(vertex_array& data, texture_array& textures, std::size_t& pos_,
                         tile_image& img, const position_array& positions)
{
    const auto pos = pos_++;
    CORRADE_INTERNAL_ASSERT(pos < data.size());
    auto texcoords = img.atlas->texcoords_for_id(img.variant);
    for (std::size_t i = 0; i < 4; i++)
    {
        data[pos][i] = { texcoords[i], positions[i] };
        textures[pos] = &img.atlas->texture();
    }
}

void wall_mesh::maybe_add_tile(vertex_array& data, texture_array& textures, std::size_t& pos, tile& x, local_coords pt)
{
    constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2];
    constexpr Vector3 size = {X, Y, Z};
    Vector3 center{(float)(X*pt.x), (float)(Y*pt.y), 0};

    if (auto& wall = x.wall_north; wall.atlas)
        add_wall(data, textures, pos, wall, tile_atlas::wall_quad_N(center, size));
    if (auto& wall = x.wall_west; wall.atlas)
        add_wall(data, textures, pos, wall, tile_atlas::wall_quad_W(center, size));
}

void wall_mesh::draw(tile_shader& shader, chunk& c)
{
    texture_array textures = {};
    std::size_t pos = 0;
    {
        vertex_array data;
        c.foreach_tile([&](tile& x, std::size_t, local_coords pt) {
          maybe_add_tile(data, textures, pos, x, pt);
        });
        //_vertex_buffer.setSubData(0, Containers::arrayView(data.data(), pos));
        _vertex_buffer.setData(data, Magnum::GL::BufferUsage::DynamicDraw);
    }

    const GL::Texture2D* last_texture = nullptr;
#if 0
    if (pos > 0)
    {
        Magnum::GL::MeshView mesh{_mesh};
        mesh.setCount((int)(pos * quad_index_count));
        mesh.setIndexRange(0);
        textures[0]->bind(0);
        shader.draw(mesh);
    }
#elif 0
    if (pos > 0)
    {
        textures[0]->bind(0);
        shader.draw(_mesh);
    }
#else
    Magnum::GL::MeshView mesh{_mesh};
    for (std::size_t i = 0; i < pos; i++)
    {
        auto* const tex = textures[i];
        CORRADE_INTERNAL_ASSERT(tex != nullptr);
        mesh.setCount(quad_index_count);
        mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*COUNT - 1);
        //mesh.setIndexRange((int)(i*quad_index_count));
        //Debug{} << "draw " << "i:" << i << "count:" << quad_index_count << "range:" << (i*quad_index_count);
        if (tex != last_texture)
            tex->bind(0);
        last_texture = tex;
        shader.draw(mesh);
    }
#endif
}

decltype(wall_mesh::_index_data) wall_mesh::make_index_array()
{
    std::array<std::array<UnsignedShort, 6>, COUNT> array = {};

    for (std::size_t i = 0; i < std::size(array); i++)
        array[i] = tile_atlas::indices(i);
    return array;
}

const decltype(wall_mesh::_index_data) wall_mesh::_index_data = wall_mesh::make_index_array();

} // namespace Magnum::Examples