summaryrefslogtreecommitdiffhomepage
path: root/src/quads.cpp
blob: d39ae9daa31170eba77959e66852a4924a1d058a (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
#include "quads.hpp"

namespace floormat::Quads {

std::array<UnsignedShort, 6> quad_indexes(size_t N)
{
    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 */
}

quad floor_quad(const Vector3 center, const Vector2 size)
{
    float x = size.x()*.5f, y = size.y()*.5f;
    return {{
        { x + center.x(), -y + center.y(), center.z()},
        { x + center.x(),  y + center.y(), center.z()},
        {-x + center.x(), -y + center.y(), center.z()},
        {-x + center.x(),  y + center.y(), center.z()},
    }};
}

texcoords texcoords_at(Vector2ui pos_, Vector2ui size_, Vector2ui image_size_)
{
    auto pos = Vector2(pos_), size = Vector2(size_), image_size = Vector2(image_size_);
    auto offset = pos + Vector2(.5f), end = offset + size - Vector2(1);
    auto x0 = offset / image_size, x1 = end / image_size;
    return {{
        { x1.x(), 1.f - x1.y() }, // bottom right
        { x1.x(), 1.f - x0.y() }, // top right
        { x0.x(), 1.f - x1.y() }, // bottom left
        { x0.x(), 1.f - x0.y() }, // top left
    }};
}

} // namespace floormat::Quads