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 };
27 Type type = POINT;
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,
64 SHADOW_MAPS_UPDATED = 1u << 6,
65 };
66 Math *math = nullptr;
67 Flags<Flag> flags = {Flag::ENABLED | Flag::UPDATE_SUN | Flag::UPDATED};
68 vec3 view_pos = {};
70 LightAnimation m_ambient_anim = {};
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;
76 mat4 m_dir_proj = {}, m_point_proj = {};
77 std::array<mat4, 7 * MAX_LIGHTS> views = {};
78 LightsUBO m_ubo = {};
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);
109 void set_sun_light(Light *l) { this->m_sun_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)
size_t n_point
Definition: light.h:71
bool update_sun(void) const
Definition: light.h:84
void remove_light(Light *l)
size_t n_dir
Definition: light.h:71
Light * sun_light()
Definition: light.h:92
std::span< const Light > point_lights() const
mat4 m_point_proj
Definition: light.h:76
float shadow_map_near() const
Definition: light.h:94
void set_shadow_map_far(float f)
const LightsUBO & ubo() const
Definition: light.h:101
std::span< Light > point_lights()
Light * add_light(Light::Type t)
void update_view(const vec3 &eye)
vec4 m_ambient_light
Definition: light.h:69
void set_enabled(bool b)
void set_zsprites(bool b)
static constexpr size_t MAX_LIGHTS
Definition: light.h:55
Light * m_sun_light
Definition: light.h:73
float shadow_map_far() const
Definition: light.h:95
void init(Math *m)
Definition: light.h:81
Sun * sun()
Definition: light.h:102
void set_ambient_anim(LightAnimation a)
vec3 view_pos
Definition: light.h:68
Sun m_sun
Definition: light.h:79
float m_shadow_map_near
Definition: light.h:75
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)
void set_shadow_map_proj_size(float s)
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
std::array< Light, MAX_LIGHTS > m_dir_lights
Definition: light.h:72
Math * math
Definition: light.h:66
std::array< Light, MAX_LIGHTS > m_point_lights
Definition: light.h:72
bool update(const Timing &t)
Flag
Definition: light.h:57
@ UPDATE_SUN
Definition: light.h:61
@ ENABLED
Definition: light.h:58
@ UPDATED
Definition: light.h:62
@ SHADOW_MAPS_UPDATED
Definition: light.h:64
@ VIEW_UPDATED
Definition: light.h:63
@ ZSPRITES
Definition: light.h:60
@ SHADOWS_ENABLED
Definition: light.h:59
const vec4 & ambient_light() const
Definition: light.h:87
bool shadows_enabled(void) const
Definition: light.h:85
std::array< mat4, 7 *MAX_LIGHTS > views
Definition: light.h:77
void set_shadows_enabled(bool b)
const Light * sun_light() const
Definition: light.h:93
std::span< Light > dir_lights()
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)
LightAnimation m_ambient_anim
Definition: light.h:70
Flags< Flag > flags
Definition: light.h:67
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
for i
Definition: font.lua:5
r
Definition: gamma.lua:7
c
Definition: gamma.lua:11
l
Definition: light.lua:23
pos
Definition: lua_audio.cpp:17
#define NNGN_SHADOWS_ENABLED_BIT
Definition: const.h:7
#define NNGN_MAX_LIGHTS
Definition: const.h:6
a
Definition: input.lua:31
m
Definition: input.lua:23
p
Definition: input.lua:29
function f()) end
v[1]
Definition: math.lua:19
std::chrono::seconds s
Definition: timing.cpp:6
Definition: audio.cpp:7
vec4_base< float > vec4
Definition: vec4.h:128
mat4_base< float > mat4
Definition: mat4.h:62
vec3_base< float > vec3
Definition: vec3.h:51
Definition: entity.h:24
Type
Supported parameter types for kernel execution.
Definition: compute.h:133
Wrapper for an unsigned integral representing flags.
Definition: flags.h:18
constexpr bool is_set(AT a) const
Definition: flags.h:26
Definition: light.h:25
void set_cutoff(float v)
static constexpr vec3 att_for_range(float r)
Definition: light.h:34
mat4 ortho_view(float far) const
float range() const
Definition: light.h:44
void set_att(lua_State *L)
Entity * e
Definition: light.h:28
void write_to_ubo_dir(LightsUBO *ubo, size_t i, float far, const mat4 &proj) const
void write_to_ubo_point(LightsUBO *ubo, size_t i, bool zsprites) const
Type type
Definition: light.h:27
Type
Definition: light.h:26
@ POINT
Definition: light.h:26
@ DIR
Definition: light.h:26
bool updated
Definition: light.h:33
float spec
Definition: light.h:32
void set_color(const vec4 &c)
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)
vec3 ortho_view_pos(float far) const
float cutoff
Definition: light.h:32
vec4 color
Definition: light.h:30
constexpr Light()=default
mat4 persp_view(int face, bool zsprites) const
void set_pos(const vec3 &p)
void set_dir(const vec3 &p)
Definition: graphics.h:114
Definition: timing.h:20