nngn
Loading...
Searching...
No Matches
light_frag.h
Go to the documentation of this file.
1
#include "
../const.h
"
2
3
LAYOUT2
(
set
= 0, binding = 1) uniform
sampler2DArray
lights_shadow_map;
4
#ifdef HAS_CUBE_ARRAY
5
LAYOUT2
(
set
= 0, binding = 2)
6
uniform samplerCubeArray
lights_shadow_cube
;
7
#else
8
LAYOUT2
(
set
= 0, binding = 2)
9
uniform samplerCube
lights_shadow_cube
[
NNGN_MAX_LIGHTS
];
10
#endif
11
12
LAYOUT
(location = 1) in
vec3
frag_pos;
13
LAYOUT
(location = 2) in
vec3
frag_pos_bias;
14
LAYOUT
(location = 3) flat in
vec3
frag_normal;
15
LAYOUT
(location = 4) in
vec3
frag_pos_light[
NNGN_MAX_LIGHTS
];
16
17
float
attenuation
(uint
i
,
float
d) {
18
return
1.0f / (
19
lights
.point.att_cutoff[
i
][0]
20
+
lights
.point.att_cutoff[
i
][1] * d
21
+
lights
.point.att_cutoff[
i
][2] * d * d);
22
}
23
24
float
spot
(uint
i
,
vec3
d) {
25
float
inner =
lights
.point.att_cutoff[
i
].w;
26
if
(inner == 0.0)
27
return
1.0;
28
const
float
penumbra = 0.1;
29
float
outer = inner - penumbra;
30
float
a
= dot(-d,
lights
.point.dir[
i
].xyz);
31
return
a
> outer ?
clamp
((
a
- outer) / penumbra, 0.0, 1.0) : 0.0;
32
}
33
34
float
light_spec
(
float
spec,
vec3
dir,
vec3
view_dir) {
35
return
spec * pow(
36
max(dot(view_dir, reflect(dir, frag_normal)), 0.0f),
37
32.0f);
38
}
39
40
float
sample_shadow
(uint
i
,
vec2
v
) {
41
return
texture
(lights_shadow_map,
vec3
(
v
,
i
)).r;
42
}
43
44
float
sample_shadow_point
(uint
i
,
vec3
v
) {
45
#ifdef HAS_CUBE_ARRAY
46
return
texture
(
lights_shadow_cube
, vec4(
v
,
i
)).r;
47
#else
48
switch
(
i
) {
49
case
0u:
return
texture
(
lights_shadow_cube
[0],
v
).r;
50
case
1u:
return
texture
(
lights_shadow_cube
[1],
v
).r;
51
case
2u:
return
texture
(
lights_shadow_cube
[2],
v
).r;
52
case
3u:
return
texture
(
lights_shadow_cube
[3],
v
).r;
53
case
4u:
return
texture
(
lights_shadow_cube
[4],
v
).r;
54
case
5u:
return
texture
(
lights_shadow_cube
[5],
v
).r;
55
case
6u:
return
texture
(
lights_shadow_cube
[6],
v
).r;
56
case
7u:
return
texture
(
lights_shadow_cube
[7],
v
).r;
57
};
58
#endif
59
}
60
61
bool
in_shadow
(uint
i
) {
62
if
((
lights
.flags &
NNGN_SHADOWS_ENABLED_BIT
) == 0u)
63
return
false
;
64
vec3
p = frag_pos_light[
i
] * 0.5 + 0.5;
65
return
p.z >
sample_shadow
(
i
, p.xy);
66
}
67
68
bool
in_shadow_point
(uint
i
) {
69
if
((
lights
.flags &
NNGN_SHADOWS_ENABLED_BIT
) == 0u)
70
return
false
;
71
vec3
dir = frag_pos_bias -
lights
.point.pos[
i
].xyz;
72
vec3
abs_dir = abs(dir);
73
float
z = max(abs_dir.x, max(abs_dir.y, abs_dir.z));
74
z =
lights
.depth_transform0 -
lights
.depth_transform1 / z;
75
return
z >
sample_shadow_point
(
i
, dir);
76
}
77
78
vec3
light
() {
79
vec3
view_dir = normalize(
lights
.view_pos - frag_pos);
80
vec3
light
=
lights
.ambient;
81
for
(uint
i
= 0u;
i
<
lights
.n_dir; ++
i
) {
82
float
a
= dot(frag_normal, -
lights
.dir.dir[
i
].xyz);
83
if
(
a
<= 0.0f ||
in_shadow
(
i
))
84
continue
;
85
light
+=
lights
.dir.color_spec[
i
].xyz * (
86
a
+
light_spec
(
87
lights
.dir.color_spec[
i
].w,
88
lights
.dir.dir[
i
].xyz,
89
view_dir));
90
}
91
for
(uint
i
= 0u;
i
<
lights
.n_point; ++
i
) {
92
float
a
= dot(frag_normal,
lights
.point.pos[
i
].xyz - frag_pos);
93
if
(
a
<= 0.0f ||
in_shadow_point
(
i
))
94
continue
;
95
vec3
d =
lights
.point.pos[
i
].xyz - frag_pos;
96
float
att =
attenuation
(
i
, length(d));
97
d = normalize(d);
98
float
diff = max(0.0f, dot(frag_normal, d));
99
float
spec =
light_spec
(
lights
.point.color_spec[
i
].w, -d, view_dir);
100
light
+=
lights
.point.color_spec[
i
].xyz
101
* att *
spot
(
i
, d) * (diff + spec);
102
}
103
return
light
;
104
}
LAYOUT
#define LAYOUT(x)
Definition
common.h:11
sampler2DArray
precision highp sampler2DArray
Definition
common.h:16
LAYOUT2
#define LAYOUT2(x, y)
Definition
common.h:12
const.h
NNGN_SHADOWS_ENABLED_BIT
#define NNGN_SHADOWS_ENABLED_BIT
Definition
const.h:7
NNGN_MAX_LIGHTS
#define NNGN_MAX_LIGHTS
Definition
const.h:6
i
for i
Definition
font.lua:5
vec2
local vec2
Definition
grid.lua:3
lights_shadow_cube
uniform samplerCube lights_shadow_cube[8]
Definition
light_frag.h:9
light_spec
float light_spec(float spec, vec3 dir, vec3 view_dir)
Definition
light_frag.h:34
attenuation
float attenuation(uint i, float d)
Definition
light_frag.h:17
sample_shadow
float sample_shadow(uint i, vec2 v)
Definition
light_frag.h:40
spot
float spot(uint i, vec3 d)
Definition
light_frag.h:24
in_shadow_point
bool in_shadow_point(uint i)
Definition
light_frag.h:68
light
vec3 light()
Definition
light_frag.h:78
in_shadow
bool in_shadow(uint i)
Definition
light_frag.h:61
sample_shadow_point
float sample_shadow_point(uint i, vec3 v)
Definition
light_frag.h:44
lights
lights
Definition
light_ubo.h:25
texture
local texture
Definition
particles.lua:4
set
set
Definition
camera.lua:47
a
local a
Definition
input.lua:35
v
v[1]
Definition
math.lua:22
clamp
local function clamp(v, min, max) return math.max(min
vec3
local vec3
Definition
zelda.lua:8
src
glsl
light_frag.h
Generated by
1.11.0