summaryrefslogtreecommitdiffhomepage
path: root/shaders/shader.hpp
blob: a8f778da7a858262581bd34817d87e2e14d52271 (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
#pragma once
#include "compat/defs.hpp"
#include "tile-defs.hpp"
#include <Magnum/GL/AbstractShaderProgram.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/Math/Vector4.h>

namespace floormat {

struct local_coords;

struct tile_shader : GL::AbstractShaderProgram
{
    using Position           = GL::Attribute<0, Vector3>;
    using TextureCoordinates = GL::Attribute<1, Vector2>;
    using Depth              = GL::Attribute<2, float>;

    fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(tile_shader);
    fm_DECLARE_DELETED_COPY_ASSIGNMENT(tile_shader);

    explicit tile_shader();
    ~tile_shader() override;

    Vector2 scale() const { return _scale; }
    tile_shader& set_scale(const Vector2& scale);
    Vector2d camera_offset() const { return _camera_offset; }
    tile_shader& set_camera_offset(const Vector2d& camera_offset, float depth_offset);
    Vector4 tint() const { return _tint; }
    tile_shader& set_tint(const Vector4& tint);
    float depth_offset() const { return _depth_offset; }
    static float depth_value(const local_coords& xy, float offset = 0) noexcept;

    template<typename T = float> static constexpr Math::Vector2<T> project(const Math::Vector3<T>& pt);
    template<typename T = float> static constexpr Math::Vector2<T> unproject(const Math::Vector2<T>& px);

    template<typename T, typename... Xs>
    decltype(auto) draw(T&& mesh, Xs&&... xs);

    static constexpr Vector2s max_screen_tiles = {8, 8};
    static constexpr float character_depth_offset = 1 + 1./64;
    static constexpr float scenery_depth_offset = 1 + 1./64;
    static constexpr float wall_depth_offset = 1;
    static constexpr float z_depth_offset = 1 + 2./64;
    static constexpr float depth_tile_size = 1/(double)(TILE_MAX_DIM * 2 * max_screen_tiles.product());

private:
    void _draw();

    Vector2d _camera_offset;
    Vector4 _tint, _real_tint;
    Vector2 _scale;
    Vector3 _real_camera_offset;
    float _depth_offset = 0;

    enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, };
};

template<typename T, typename... Xs>
decltype(auto) tile_shader::draw(T&& mesh, Xs&&... xs)
{
    _draw();
    return GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...);
}

template<typename T>
constexpr Math::Vector2<T> tile_shader::project(const Math::Vector3<T>& pt)
{
    static_assert(std::is_floating_point_v<T>);
    const auto x = pt[0], y = pt[1], z = -pt[2];
    return { x-y, (x+y+z*2)*T(.59) };
}

template<typename T>
constexpr Math::Vector2<T> tile_shader::unproject(const Math::Vector2<T>& px)
{
    const auto X = px[0], Y = px[1];
    return { X + 100 * Y / 59, 100 * Y / 59 - X };
}

} // namespace floormat