2021-07-26 10:10:58 +00:00
|
|
|
#version 330 core
|
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
layout(location = 0) out vec4 fragColor;
|
2021-07-26 10:10:58 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
in vec2 texcoord;
|
2021-07-26 10:10:58 +00:00
|
|
|
|
2021-07-28 07:40:04 +00:00
|
|
|
uniform sampler2D transmittance_lut;
|
2023-04-07 06:17:37 +00:00
|
|
|
|
2021-08-19 10:50:01 +00:00
|
|
|
uniform float fg_SunZenithCosTheta;
|
|
|
|
uniform float fg_CameraDistanceToEarthCenter;
|
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
const int SKY_STEPS = 32;
|
2021-07-26 10:10:58 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
// math.glsl
|
|
|
|
float M_2PI();
|
|
|
|
float M_PI_2();
|
|
|
|
// atmos.glsl
|
2023-04-13 03:57:01 +00:00
|
|
|
float get_ray_end(vec3 ray_origin, vec3 ray_dir, float t_max);
|
2023-02-19 15:47:55 +00:00
|
|
|
vec4 compute_inscattering(in vec3 ray_origin,
|
|
|
|
in vec3 ray_dir,
|
|
|
|
in float t_max,
|
|
|
|
in vec3 sun_dir,
|
|
|
|
in int steps,
|
|
|
|
in sampler2D transmittance_lut,
|
|
|
|
out vec4 transmittance);
|
2021-07-26 10:10:58 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2023-02-19 15:47:55 +00:00
|
|
|
// Always leave the Sun right in the middle of the sky texture.
|
|
|
|
// The skydome model implemented in SimGear already takes care of rotating
|
|
|
|
// the Sun for us.
|
|
|
|
vec3 sun_dir = vec3(
|
|
|
|
-sqrt(1.0 - fg_SunZenithCosTheta*fg_SunZenithCosTheta),
|
|
|
|
0.0,
|
|
|
|
fg_SunZenithCosTheta);
|
2021-07-26 10:10:58 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
float azimuth = M_2PI() * texcoord.x; // [0, 2pi]
|
2021-07-26 10:10:58 +00:00
|
|
|
// Apply a non-linear transformation to the elevation to dedicate more
|
2023-02-19 15:47:55 +00:00
|
|
|
// texels to the horizon, where having more detail matters.
|
2023-04-07 06:17:37 +00:00
|
|
|
float l = texcoord.y * 2.0 - 1.0;
|
|
|
|
float elev = l*l * sign(l) * M_PI_2(); // [-pi/2, pi/2]
|
2021-07-28 07:40:04 +00:00
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec3 ray_dir = vec3(cos(elev) * cos(azimuth),
|
|
|
|
cos(elev) * sin(azimuth),
|
|
|
|
sin(elev));
|
2021-07-28 07:40:04 +00:00
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec3 ray_origin = vec3(0.0, 0.0, fg_CameraDistanceToEarthCenter);
|
2021-07-28 07:40:04 +00:00
|
|
|
|
2023-04-13 03:57:01 +00:00
|
|
|
float t_max = get_ray_end(ray_origin, ray_dir, 1e7);
|
|
|
|
if (t_max < 0.0) {
|
|
|
|
fragColor = vec4(0.0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec4 transmittance;
|
|
|
|
vec4 L = compute_inscattering(ray_origin,
|
|
|
|
ray_dir,
|
2023-04-13 03:57:01 +00:00
|
|
|
t_max,
|
2023-02-19 15:47:55 +00:00
|
|
|
sun_dir,
|
|
|
|
SKY_STEPS,
|
|
|
|
transmittance_lut,
|
|
|
|
transmittance);
|
2021-07-28 07:40:04 +00:00
|
|
|
fragColor = L;
|
2021-07-26 10:10:58 +00:00
|
|
|
}
|