1
0
Fork 0
fgdata/Shaders/HDR/atmos-transmittance.frag

55 lines
1.7 KiB
GLSL
Raw Normal View History

#version 330 core
out vec4 fragColor;
in vec2 texCoord;
uniform float fg_EarthRadius;
const float ATMOSPHERE_RADIUS = 6471e3;
const int TRANSMITTANCE_STEPS = 40;
// atmos-include.frag
float ray_sphere_intersection(vec3 ro, vec3 rd, float radius);
void get_atmosphere_collision_coefficients(in float h,
out vec4 aerosol_absorption,
out vec4 aerosol_scattering,
out vec4 molecular_absorption,
out vec4 molecular_scattering,
out vec4 extinction);
void main()
{
float sun_cos_theta = texCoord.x * 2.0 - 1.0;
vec3 sun_dir = vec3(-sqrt(1.0 - sun_cos_theta*sun_cos_theta), 0.0, sun_cos_theta);
float distance_to_earth_center = mix(fg_EarthRadius, ATMOSPHERE_RADIUS, texCoord.y);
vec3 ray_origin = vec3(0.0, 0.0, distance_to_earth_center);
float t_d = ray_sphere_intersection(ray_origin, sun_dir, ATMOSPHERE_RADIUS);
float dt = t_d / float(TRANSMITTANCE_STEPS);
vec4 result = vec4(0.0);
for (int i = 0; i < TRANSMITTANCE_STEPS; ++i) {
float t = (float(i) + 0.5) * dt;
vec3 x_t = ray_origin + sun_dir * t;
float altitude = length(x_t) - fg_EarthRadius;
vec4 aerosol_absorption, aerosol_scattering;
vec4 molecular_absorption, molecular_scattering;
vec4 extinction;
get_atmosphere_collision_coefficients(
altitude,
aerosol_absorption, aerosol_scattering,
molecular_absorption, molecular_scattering,
extinction);
result += extinction * dt;
}
vec4 transmittance = exp(-result);
fragColor = transmittance;
}