Space Sim
Toggle main menu visibility
Loading...
Searching...
No Matches
src
render
atmosphere.vert
Go to the documentation of this file.
1
#version 430 core
2
9
10
layout
(location = 0) in
vec3
aPosition;
11
layout
(location = 1) in
vec3
aNormal;
12
13
out
vec3
vPos
;
14
out
vec3
vNormal
;
15
out
vec3
c0
;
16
out
vec3
c1
;
17
out
vec3
t0
;
18
out
vec3
vViewDir
;
19
20
layout
(std140) uniform CameraData {
21
vec4
cam_pos;
22
vec4
cam_vel;
23
mat4
projection
;
24
mat4
view
;
25
mat4
inv_projection;
26
mat4
inv_view;
27
};
28
29
#define MAX_LAYERS 24
30
struct
WarpLayer
31
{
32
vec3
offset0
;
33
float
freq0
;
34
vec3
offset1
;
35
float
freq1
;
36
vec3
offset2
;
37
float
freq2
;
38
float
amp
;
39
float
pad0
;
40
float
pad1
;
41
float
pad3
;
42
};
43
44
struct
PerlinLayer
45
{
46
float
freq
;
47
float
amp
;
48
float
pad0
;
49
float
pad1
;
50
};
51
52
struct
FBMLayer
53
{
54
float
freq
;
55
float
amp
;
56
float
pad0
;
57
float
pad1
;
58
};
59
60
layout
(std430, binding=13) buffer
PlanetData
{
61
62
int
warp_count;
63
int
perlin_count;
64
int
fbm_count;
65
int
pad0;
66
vec4
sun_dir
;
67
vec4
sun_color
;
68
69
WarpLayer
warp_layers[
MAX_LAYERS
];
70
PerlinLayer
perlin_layers[
MAX_LAYERS
];
71
FBMLayer
fbm_layers[
MAX_LAYERS
];
72
73
};
74
75
uniform
vec3
uCamPosWorld
;
76
uniform
float
uInnerRadius
;
77
uniform
float
uInnerRadius2
;
78
uniform
float
uOuterRadius
;
79
uniform
float
uOuterRadius2
;
80
uniform
vec3
uInvWavelength
;
81
uniform
float
uFScale
;
82
uniform
float
uFScaleOverDepth
;
83
uniform
float
uFInvScaleDepth
;
84
uniform
float
uFScaleDepth
;
85
uniform
float
uG
;
86
uniform
float
uG2
;
87
uniform
float
ufKrESun
;
88
uniform
float
ufKmESun
;
89
uniform
float
ufKr4Pi
;
90
uniform
float
ufKm4Pi
;
91
uniform
mat4
uFakeView
;
92
uniform
mat4
uFakeProj
;
93
94
95
float
scale
(
float
fCos)
96
{
97
float
x = 1.0 - fCos;
98
return
0.25 * exp(-0.00287 + x * (0.459 + x * (3.83 + x * (-6.80 + x * 5.25))));
99
}
100
101
uniform
vec3
uCamPos
;
102
103
const
int
samples
= 4;
104
void
main
()
105
{
106
107
vec3
planet_center =
vec3
(0.0);
108
float
pscale = 1 / 10000;
109
vec3
scaled_cam =
uCamPosWorld
;
110
vec3
apos = aPosition;
111
vec3
pos = (aPosition);
112
pos *=
uOuterRadius
;
113
114
vec3
L
= normalize(-
sun_dir
.xyz);
115
vec3
sphere_ray_to_cam = pos - scaled_cam.xyz;
116
float
fFar = length(sphere_ray_to_cam);
117
sphere_ray_to_cam /= fFar;
118
float
b = 2.0 * (dot(scaled_cam.xyz, sphere_ray_to_cam));
119
float
c = dot(scaled_cam.xyz, scaled_cam.xyz) -
uOuterRadius
*
uOuterRadius
;
120
float
det = max(0.0, b * b - 4.0 * c);
121
122
float
fNear = 0.5 * (-b - sqrt(det));
123
fFar = 0.5 * (-b + sqrt(det));
124
125
fNear = max(fNear, 0.0);
126
127
vec3
start = scaled_cam.xyz + sphere_ray_to_cam * fNear;
128
fFar -= fNear;
129
130
float
start_height = length(start);
131
float
start_depth = exp(
uFScaleOverDepth
* (
uInnerRadius
- start_height));
132
float
start_angle = dot(sphere_ray_to_cam, start) / start_height;
133
float
start_offset = start_depth *
scale
(start_angle);
134
135
float
sample_length = fFar /
samples
;
136
float
scaled_length = sample_length *
uFScale
;
137
vec3
sample_ray = sphere_ray_to_cam * sample_length;
138
vec3
sample_point = start + sample_ray * 0.5;
139
140
vec3
color =
vec3
(0, 0, 0);
141
142
for
(
int
i = 0; i <
samples
; i++)
143
{
144
145
float
height = length(sample_point);
146
float
depth
= exp(
uFScaleOverDepth
* (
uInnerRadius
- height));
147
float
light_angle = dot(
L
, sample_point) / height;
148
float
camera_angle = dot(sphere_ray_to_cam, sample_point) / height;
149
float
scatter = (start_offset +
depth
* (
scale
(light_angle) -
scale
(camera_angle)));
150
vec3
att = exp(-scatter * (
uInvWavelength
*
ufKr4Pi
+
ufKm4Pi
));
151
color += att * (
depth
* scaled_length);
152
sample_point += sample_ray;
153
}
154
155
vPos
= aPosition;
156
vNormal
= aNormal;
157
c0
= color * (
uInvWavelength
*
ufKrESun
);
158
c1
= color *
ufKmESun
;
159
t0
= (-sphere_ray_to_cam);
160
161
vViewDir
= normalize(
vec3
(aPosition - cam_pos.xyz));
162
vec3
p2 = pos;
163
p2.
z
-=
uInnerRadius
;
164
p2.
z
-= scaled_cam.
z
-
uInnerRadius
;
165
vec4
worldPos =
uFakeProj
*
uFakeView
*
vec4
(p2+ cam_pos.xyz, 1.0);
166
gl_Position = worldPos;
167
168
}
t0
in vec3 t0
Definition
atmosphere.frag:14
c0
in vec3 c0
Definition
atmosphere.frag:12
uG2
uniform float uG2
Definition
atmosphere.frag:18
vPos
in vec3 vPos
Definition
atmosphere.frag:10
vNormal
in vec3 vNormal
Definition
atmosphere.frag:11
vViewDir
in vec3 vViewDir
Definition
atmosphere.frag:15
uG
uniform float uG
Definition
atmosphere.frag:17
c1
in vec3 c1
Definition
atmosphere.frag:13
uInnerRadius2
uniform float uInnerRadius2
Definition
atmosphere.vert:77
uOuterRadius
uniform float uOuterRadius
Definition
atmosphere.vert:78
ufKr4Pi
uniform float ufKr4Pi
Definition
atmosphere.vert:89
ufKm4Pi
uniform float ufKm4Pi
Definition
atmosphere.vert:90
uCamPosWorld
uniform vec3 uCamPosWorld
Definition
atmosphere.vert:75
ufKmESun
uniform float ufKmESun
Definition
atmosphere.vert:88
uFScale
uniform float uFScale
Definition
atmosphere.vert:81
ufKrESun
uniform float ufKrESun
Definition
atmosphere.vert:87
samples
const int samples
Definition
atmosphere.vert:103
uOuterRadius2
uniform float uOuterRadius2
Definition
atmosphere.vert:79
uInvWavelength
uniform vec3 uInvWavelength
Definition
atmosphere.vert:80
uInnerRadius
uniform float uInnerRadius
Definition
atmosphere.vert:76
uFScaleOverDepth
uniform float uFScaleOverDepth
Definition
atmosphere.vert:82
uFakeView
uniform mat4 uFakeView
Definition
atmosphere.vert:91
uFScaleDepth
uniform float uFScaleDepth
Definition
atmosphere.vert:84
uFInvScaleDepth
uniform float uFInvScaleDepth
Definition
atmosphere.vert:83
scale
float scale(float fCos)
Definition
atmosphere.vert:95
uFakeProj
uniform mat4 uFakeProj
Definition
atmosphere.vert:92
uCamPos
uniform vec3 uCamPos
Definition
atmosphere.vert:101
layout
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
depth
uniform int depth
Definition
com22.comp:76
sun_dir
vec3 sun_dir
Definition
main.c:237
sun_color
vec3 sun_color
Definition
main.c:238
main
int main(int argc, char *argv[])
Definition
main.c:1726
projection
uniform mat4 projection
Definition
shadow.vert:8
view
uniform mat4 view
Definition
shadow.vert:9
vec3::z
float z
Definition
main.h:5
MAX_LAYERS
#define MAX_LAYERS
Definition
types.h:407
PlanetData
Definition
types.h:409
mat4
Definition
types.h:51
vec3
Definition
main.h:5
vec4
Definition
types.h:47
L
#define L
Definition
water.comp:15
WarpLayer::offset2
vec3 offset2
Definition
types.h:378
FBMLayer::pad1
float pad1
Definition
types.h:403
FBMLayer::amp
float amp
Definition
types.h:401
WarpLayer::freq0
float freq0
Definition
types.h:375
WarpLayer::offset1
vec3 offset1
Definition
types.h:376
FBMLayer::freq
float freq
Definition
types.h:400
PerlinLayer::amp
float amp
Definition
types.h:391
WarpLayer::pad3
float pad3
Definition
types.h:383
WarpLayer::freq1
float freq1
Definition
types.h:377
WarpLayer::pad1
float pad1
Definition
types.h:382
PerlinLayer::pad0
float pad0
Definition
types.h:392
WarpLayer::freq2
float freq2
Definition
types.h:379
WarpLayer::offset0
vec3 offset0
Definition
types.h:374
PerlinLayer::pad1
float pad1
Definition
types.h:393
WarpLayer::pad0
float pad0
Definition
types.h:381
WarpLayer::amp
float amp
Definition
types.h:380
PerlinLayer::freq
float freq
Definition
types.h:390
FBMLayer::pad0
float pad0
Definition
types.h:402
FBMLayer
Definition
types.h:398
PerlinLayer
Definition
types.h:388
WarpLayer
Definition
types.h:372
Generated by
1.18.0