summaryrefslogtreecommitdiffhomepage
path: root/shaders/lightmap.hpp
blob: f9f2915694c4d420d0a1141e0620c9096633b922 (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
#pragma once

#include "light-falloff.hpp"
#include <array>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Optional.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector4.h>
#include <Magnum/Math/Color.h>
#include <Magnum/GL/AbstractShaderProgram.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/Framebuffer.h>
#include <Magnum/GL/Mesh.h>
//#include <Magnum/GL/Renderbuffer.h>
#include <Magnum/GL/Texture.h>

namespace floormat {

struct light_s final
{
    Vector2 center;
    float dist = 1;
    //float depth = -1 + 1e-4f;
    Math::Color4<uint8_t> color;
    light_falloff falloff = light_falloff::linear;

    bool operator==(const light_s&) const noexcept;
};

struct chunk;

struct lightmap_shader final : GL::AbstractShaderProgram
{
    using Position = GL::Attribute<0, Vector2>;

    explicit lightmap_shader();
    ~lightmap_shader() override;

    struct Framebuffer final {
        GL::Framebuffer fb{NoCreate};
        //GL::Renderbuffer depth{NoCreate};
        GL::Texture2D scratch{NoCreate}, accum{NoCreate};
    };

    void begin_light(Vector2i neighbor_offset, const light_s& light);
    void add_chunk(Vector2i neighbor_offset, chunk& c);
    void add_entities(Vector2i neighbor_offset, chunk& c);
    void add_geometry(Vector2i neighbor_offset, chunk& c);
    void add_rect(Vector2i neighbor_offset, Vector2 min, Vector2 max);
    void add_rect(Vector2i neighbor_offset, Pair<Vector2, Vector2> minmax);
    void finish_light_only();
    void finish_and_blend_light();
    GL::Texture2D& scratch_texture();
    GL::Texture2D& accum_texture();
    void begin_accum();
    void end_accum();

private:
    enum {
        ColorIntensityUniform = 0,
        CenterUniform         = 1,
        FalloffUniform        = 2,
        SizeUniform           = 3,
        ModeUniform           = 4,
        SamplerUniform        = 5,
    };

    enum : Int {
        TextureSampler = 1,
    };

    enum ShaderMode : uint32_t {
        DrawLightmapMode  = 1,
        BlendLightmapMode = 2,
    };

    static Framebuffer make_framebuffer(Vector2i size);
    GL::Mesh make_mesh();
    void add_light(Vector2i neighbor_offset, const light_s& light);
    void flush_vertexes(ShaderMode mode);
    void add_quad(const std::array<Vector2, 4>& quad);
    void bind();
    void clear_scratch();
    void clear();
    static std::array<UnsignedShort, 6> quad_indexes(size_t N);
    static Vector2 project_vertex(Vector2 light, Vector2 vertex, Vector2 length);

    Framebuffer framebuffer;
    Array<std::array<Vector2, 4>> _quads;
    Array<std::array<UnsignedShort, 6>> _indexes;
    size_t _count = (size_t)-1;
    GL::Buffer _vertex_buf{NoCreate}, _index_buf{NoCreate};
    GL::Mesh _mesh{NoCreate};
    Optional<Vector2> _light_center;
};

} // namespace floormat