summaryrefslogtreecommitdiffhomepage
path: root/src/light.cpp
blob: ed9469f2e78c6d44768808683384b5d4915e8d66 (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
#include "light.hpp"
#include "shaders/shader.hpp"
#include "loader/loader.hpp"
#include "loader/vobj-info.hpp"
#include <cmath>

namespace floormat {

light_proto::light_proto()
{
    atlas = loader.vobj("light"_s).atlas;
    pass = pass_mode::pass;
    type = entity_type::light;
}

light_proto::light_proto(const light_proto&) = default;
light_proto& light_proto::operator=(const light_proto&) = default;
light_proto::~light_proto() noexcept = default;
bool light_proto::operator==(const light_proto&) const = default;

light::light(object_id id, struct chunk& c, const light_proto& proto) :
    entity{id, c, proto},
    half_dist{proto.half_dist},
    color{proto.color},
    falloff{proto.falloff}
{
}

float light::depth_offset() const
{
    constexpr auto ret = 4 / tile_shader::depth_tile_size;
    return ret;
}

Vector2 light::ordinal_offset(Vector2b) const
{
    constexpr auto ret = Vector2(TILE_COUNT, TILE_COUNT) * TILE_SIZE2;
    return ret;
}

entity_type light::type() const noexcept { return entity_type::light; }
bool light::update(size_t, float) { return false; }
bool light::is_dynamic() const { return true; }
bool light::is_virtual() const { return true; }

Vector2 light::intensity(Vector2 half_dist, light_falloff falloff)
{
    switch (falloff)
    {
    case light_falloff::linear: return 2 * half_dist;
    case light_falloff::quadratic: return Vector2{std::sqrt(2 * half_dist.x()), std::sqrt(2 * half_dist.y())};
    default: case light_falloff::constant: return Vector2{1, 1};
    }
}

Vector2 light::intensity() const
{
    return intensity(half_dist, falloff);
}

} // namespace floormat