summaryrefslogtreecommitdiffhomepage
path: root/atlas.cpp
blob: d6b6a0c2af6a521ff628abebac7ff6cbc7957bfe (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
#include "atlas.hpp"
#include "defs.hpp"
#include <Magnum/ImageView.h>
#include <Magnum/GL/TextureFormat.h>

namespace Magnum::Examples {

atlas_texture::atlas_texture(const Trade::ImageData2D& image, Vector2i dims) :
    size_{image.size()},
    dims_{dims},
    tile_size_{size_ / dims}
{
    CORRADE_INTERNAL_ASSERT(dims_[0] > 0 && dims_[1] > 0);
    CORRADE_INTERNAL_ASSERT(tile_size_ * dims_ == size_);
    CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2i{});
    CORRADE_INTERNAL_ASSERT(dims.product() < 256);
    tex_.setWrapping(GL::SamplerWrapping::ClampToEdge)
        .setMagnificationFilter(GL::SamplerFilter::Linear)
        .setMinificationFilter(GL::SamplerFilter::Linear)
        .setMaxAnisotropy(0)
        .setStorage(1, GL::textureFormat(image.format()), image.size())
        .setSubImage(0, {}, image);
}

std::array<Vector2, 4> atlas_texture::texcoords_for_id(int id_) const
{
    CORRADE_INTERNAL_ASSERT(id_ >= 0 && id_ < dims_.product());
    Vector2i id = { id_ % dims_[0], id_ / dims_[0] };
    auto p0 = Vector2(id * tile_size_) / Vector2(size_);
    auto p1 = (Vector2(Vector2i{1,1} * tile_size_) - Vector2{0.5f, 0.5f}) / Vector2(size_);
    auto x0 = p0.x(), x1 = p1.x(), y0 = p0.y(), y1 = p1.y();
    return {{
        { x0+x1, y0+y1 }, // bottom right
        { x0+x1, y0    }, // top right
        { x0,    y0+y1 }, // bottom left
        { x0,    y0    }  // top left
    }};
}

std::array<Vector3, 4> atlas_texture::floor_quad(Vector3 center, Vector2 size)
{
    float x = size[0]*.5f, y = 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]},
    }};
}

std::array<Vector3, 4> atlas_texture::wall_quad(Vector3 center, Vector3 size)
{
    float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
    return {{
        { x + center[0], -y + center[1], +    center[2] },
        { x + center[0], -y + center[1], z+    center[2] },
        {-x + center[0],  y + center[1], +    center[2] },
        {-x + center[0],  y + center[1], z+    center[2] },
    }};
}

std::array<UnsignedShort, 6> atlas_texture::indices(int N)
{
    CORRADE_INTERNAL_ASSERT(N >= 0);
    using u16 = UnsignedShort;
    return {                                        /* 3--1 1 */
        (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4),   /* | / /| */
        (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4),   /* |/ / | */
    };                                              /* 2 2--0 */
}

} // namespace Magnum::Examples