nngn
Loading...
Searching...
No Matches
light.h
Go to the documentation of this file.
1#ifndef NNGN_LIGHT_H
2#define NNGN_LIGHT_H
3
4#include <chrono>
5#include <vector>
6
7#include "const.h"
8
9#include "graphics/graphics.h"
10#include "math/mat4.h"
11#include "math/vec2.h"
12#include "math/vec3.h"
13#include "utils/flags.h"
14
15#include "animation.h"
16#include "sun.h"
17
18struct lua_State;
19struct Entity;
20
21namespace nngn {
22
23struct Timing;
24
25struct Light {
26 enum Type : uint8_t { POINT, DIR };
28 Entity *e = nullptr;
29 vec3 pos = {}, dir = {};
30 vec4 color = {};
31 vec3 att = {1, 0, 0};
32 float spec = 1.0f, cutoff = 0.0f;
33 bool updated = true;
34 static constexpr vec3 att_for_range(float r)
35 { return {1, 4.5f / r, 75.0f / (r * r)}; }
36 constexpr Light() = default;
37 explicit constexpr Light(Type t) noexcept : type(t) {}
38 void set_pos(const vec3 &p);
39 void set_dir(const vec3 &p);
40 void set_color(const vec4 &c);
41 void set_att(lua_State *L);
42 void set_spec(float v);
43 void set_cutoff(float v);
44 float range() const { return 4.5f / this->att[1]; }
45 vec3 ortho_view_pos(float far) const;
46 mat4 ortho_view(float far) const;
47 mat4 persp_view(int face, bool zsprites) const;
49 LightsUBO *ubo, size_t i, float far, const mat4 &proj) const;
50 void write_to_ubo_point(LightsUBO *ubo, size_t i, bool zsprites) const;
51};
52
53class Lighting {
54public:
55 static constexpr size_t MAX_LIGHTS = NNGN_MAX_LIGHTS;
56private:
57 enum Flag : uint8_t {
58 ENABLED = 1u << 0,
60 ZSPRITES = 1u << 2,
61 UPDATE_SUN = 1u << 3,
62 UPDATED = 1u << 4,
63 VIEW_UPDATED = 1u << 5,
65 };
66 Math *math = nullptr;
67 Flags<Flag> flags = {Flag::ENABLED | Flag::UPDATE_SUN | Flag::UPDATED};
71 size_t n_dir = 0, n_point = 0;
72 std::array<Light, MAX_LIGHTS> m_dir_lights = {}, m_point_lights = {};
73 Light *m_sun_light = nullptr;
74 float m_shadow_map_proj_size = 128.0f;
75 float m_shadow_map_near = 1.0f, m_shadow_map_far = 1024.0f;
77 std::array<mat4, 7 * MAX_LIGHTS> views = {};
79 Sun m_sun = {};
80public:
81 void init(Math *m) { this->math = m; }
82 bool enabled() const { return this->flags.is_set(Flag::ENABLED); }
83 bool zsprites(void) const { return this->flags.is_set(Flag::ZSPRITES); }
84 bool update_sun(void) const { return this->flags.is_set(Flag::UPDATE_SUN); }
85 bool shadows_enabled(void) const
86 { return this->flags.is_set(Flag::SHADOWS_ENABLED); }
87 const vec4 &ambient_light() const { return this->m_ambient_light; }
88 std::span<const Light> dir_lights() const;
89 std::span<Light> dir_lights();
90 std::span<const Light> point_lights() const;
91 std::span<Light> point_lights();
92 Light *sun_light() { return this->m_sun_light; }
93 const Light *sun_light() const { return this->m_sun_light; }
94 float shadow_map_near() const { return this->m_shadow_map_near; }
95 float shadow_map_far() const { return this->m_shadow_map_far; }
96 const mat4 &dir_proj() const { return this->m_dir_proj; }
97 const mat4 &point_proj() const { return this->m_point_proj; }
98 const mat4 &dir_view(size_t i) const { return this->views[i]; }
99 const mat4 &point_view(size_t i, size_t f) const
100 { return this->views[MAX_LIGHTS + i * 6 + f]; }
101 const LightsUBO &ubo() const { return this->m_ubo; }
102 Sun *sun() { return &this->m_sun; }
103 void set_enabled(bool b);
104 void set_zsprites(bool b);
105 void set_update_sun(bool b);
106 void set_shadows_enabled(bool b);
107 void set_ambient_light(const vec4 &v);
109 void set_sun_light(Light *l) { this->m_sun_light = l; }
110 void set_shadow_map_proj_size(float s);
111 void set_shadow_map_near(float f);
112 void set_shadow_map_far(float f);
114 void remove_light(Light *l);
115 bool update(const Timing &t);
116 void update_view(const vec3 &eye);
117};
118
119inline std::span<const Light> Lighting::dir_lights() const
120 { return std::span{this->m_dir_lights}.subspan(0, this->n_dir); }
121inline std::span<Light> Lighting::dir_lights()
122 { return std::span{this->m_dir_lights}.subspan(0, this->n_dir); }
123inline std::span<const Light> Lighting::point_lights() const
124 { return std::span{this->m_point_lights}.subspan(0, this->n_point); }
125inline std::span<Light> Lighting::point_lights()
126 { return std::span{this->m_point_lights}.subspan(0, this->n_point); }
127
128}
129
130#endif
Definition animation.h:71
Definition light.h:53
float m_shadow_map_far
Definition light.h:75
void set_update_sun(bool b)
Definition light.cpp:85
size_t n_point
Definition light.h:71
bool update_sun(void) const
Definition light.h:84
void remove_light(Light *l)
Definition light.cpp:139
size_t n_dir
Definition light.h:71
std::array< mat4, 7 *MAX_LIGHTS > views
Definition light.h:77
Light * sun_light()
Definition light.h:92
std::span< const Light > point_lights() const
Definition light.h:123
mat4 m_point_proj
Definition light.h:76
float shadow_map_near() const
Definition light.h:94
void set_shadow_map_far(float f)
Definition light.cpp:114
const LightsUBO & ubo() const
Definition light.h:101
Light * add_light(Light::Type t)
Definition light.cpp:119
void update_view(const vec3 &eye)
Definition light.cpp:247
vec4 m_ambient_light
Definition light.h:69
void set_enabled(bool b)
Definition light.cpp:75
void set_zsprites(bool b)
Definition light.cpp:80
static constexpr size_t MAX_LIGHTS
Definition light.h:55
float shadow_map_far() const
Definition light.h:95
void init(Math *m)
Definition light.h:81
Math * math
Definition light.h:66
Sun * sun()
Definition light.h:102
Light * m_sun_light
Definition light.h:73
void set_ambient_anim(LightAnimation a)
Definition light.cpp:100
vec3 view_pos
Definition light.h:68
Sun m_sun
Definition light.h:79
float m_shadow_map_near
Definition light.h:75
std::array< Light, MAX_LIGHTS > m_point_lights
Definition light.h:72
bool enabled() const
Definition light.h:82
const mat4 & point_view(size_t i, size_t f) const
Definition light.h:99
void set_shadow_map_near(float f)
Definition light.cpp:109
void set_shadow_map_proj_size(float s)
Definition light.cpp:104
const mat4 & dir_proj() const
Definition light.h:96
const mat4 & dir_view(size_t i) const
Definition light.h:98
std::span< const Light > dir_lights() const
Definition light.h:119
std::array< Light, MAX_LIGHTS > m_dir_lights
Definition light.h:72
Flag
Definition light.h:57
@ ZSPRITES
Definition light.h:60
@ ENABLED
Definition light.h:58
@ SHADOWS_ENABLED
Definition light.h:59
@ UPDATE_SUN
Definition light.h:61
@ UPDATED
Definition light.h:62
@ VIEW_UPDATED
Definition light.h:63
@ SHADOW_MAPS_UPDATED
Definition light.h:64
const vec4 & ambient_light() const
Definition light.h:87
Flags< Flag > flags
Definition light.h:67
bool shadows_enabled(void) const
Definition light.h:85
void set_shadows_enabled(bool b)
Definition light.cpp:90
const Light * sun_light() const
Definition light.h:93
void set_sun_light(Light *l)
Definition light.h:109
mat4 m_dir_proj
Definition light.h:76
float m_shadow_map_proj_size
Definition light.h:74
void set_ambient_light(const vec4 &v)
Definition light.cpp:95
LightAnimation m_ambient_anim
Definition light.h:70
const mat4 & point_proj() const
Definition light.h:97
bool zsprites(void) const
Definition light.h:83
LightsUBO m_ubo
Definition light.h:78
Definition math.h:22
Definition sun.h:10
#define NNGN_SHADOWS_ENABLED_BIT
Definition const.h:7
#define NNGN_MAX_LIGHTS
Definition const.h:6
for i
Definition font.lua:5
local c
Definition gamma.lua:11
local r
Definition gamma.lua:7
update
Definition img_common.lua:42
pos
Definition lua_audio.cpp:17
local l
Definition light.lua:23
Definition audio.cpp:7
vec4_base< float > vec4
Definition vec4.h:128
local a
Definition input.lua:35
local m
Definition input.lua:28
v[1]
Definition math.lua:22
local function f()) end
Definition entity.h:24
Wrapper for a small unsigned integral representing flags.
Definition flags.h:18
constexpr bool is_set(AT a) const
Definition flags.h:45
Definition light.h:25
void set_cutoff(float v)
Definition light.cpp:23
static constexpr vec3 att_for_range(float r)
Definition light.h:34
mat4 ortho_view(float far) const
Definition light.cpp:40
float range() const
Definition light.h:44
void set_att(lua_State *L)
Definition light.cpp:26
void write_to_ubo_dir(LightsUBO *ubo, size_t i, float far, const mat4 &proj) const
Definition light.cpp:59
void write_to_ubo_point(LightsUBO *ubo, size_t i, bool zsprites) const
Definition light.cpp:66
Type type
Definition light.h:27
Type
Definition light.h:26
@ DIR
Definition light.h:26
@ POINT
Definition light.h:26
bool updated
Definition light.h:33
float spec
Definition light.h:32
Entity * e
Definition light.h:28
void set_color(const vec4 &c)
Definition light.cpp:19
vec3 att
Definition light.h:31
constexpr Light(Type t) noexcept
Definition light.h:37
vec3 dir
Definition light.h:29
void set_spec(float v)
Definition light.cpp:21
vec3 ortho_view_pos(float far) const
Definition light.cpp:36
float cutoff
Definition light.h:32
vec4 color
Definition light.h:30
constexpr Light()=default
mat4 persp_view(int face, bool zsprites) const
Definition light.cpp:44
void set_pos(const vec3 &p)
Definition light.cpp:15
void set_dir(const vec3 &p)
Definition light.cpp:17
Definition graphics.h:114
Definition timing.h:20
std::chrono::seconds s
Definition timing.cpp:6