From c1e7703d788ffa543fa9d1238e62c8e866f21b98 Mon Sep 17 00:00:00 2001 From: Thorsten Renk Date: Tue, 16 Dec 2014 13:40:30 +0200 Subject: [PATCH] Shader support for aircraft shadows --- Effects/shadow.eff | 261 ++++++++++++++++++++++++++++++++++++++++ Shaders/shadow-ALS.frag | 19 +++ Shaders/shadow-ALS.vert | 75 ++++++++++++ 3 files changed, 355 insertions(+) create mode 100644 Effects/shadow.eff create mode 100644 Shaders/shadow-ALS.frag create mode 100644 Shaders/shadow-ALS.vert diff --git a/Effects/shadow.eff b/Effects/shadow.eff new file mode 100644 index 000000000..eefb4c90e --- /dev/null +++ b/Effects/shadow.eff @@ -0,0 +1,261 @@ + + + Effects/shadow + + + + white + + false + + 1 + + + 0 + + /environment/ground-visibility-m + /environment/visibility-m + /environment/ground-haze-thickness-m + /rendering/scene/scattering + /environment/surface/scattering + /environment/terminator-relative-position-m + /environment/mean-terrain-elevation-m + /rendering/scene/overcast + /sim/rendering/eye-altitude-m + /environment/cloud-self-shading + /environment/moonlight + /position/gear-agl-m + 1.5 + + + + + + + /sim/rendering/shaders/skydome + + + 2.0 + + + + GL_ARB_shader_objects + GL_ARB_shading_language_100 + GL_ARB_vertex_shader + GL_ARB_fragment_shader + + + + + + + true + + material/active + material/ambient + material/diffuse + material/specular + material/emissive + material/shininess + material/color-mode + + + blend/active + blend/source + blend/destination + + shade-model + cull-face + + 10 + DepthSortedBin + + rendering-hint + + + 0 + + texture[0]/type + texture[0]/image + texture[0]/filter + texture[0]/wrap-s + texture[0]/wrap-t + + + + vertex-program-two-side + + + Shaders/shadow-ALS.vert + Shaders/shadow-ALS.frag + + + visibility + float + visibility + + + avisibility + float + avisibility + + + hazeLayerAltitude + float + lthickness + + + scattering + float + scattering + + + terminator + float + terminator + + + ground_scattering + float + ground_scattering + + + terminator + float + terminator + + + terrain_alt + float + terrain_alt + + + overcast + float + overcast + + + eye_alt + float + eye_alt + + + cloud_self_shading + float + cloud_self_shading + + + moonlight + float + moonlight + + + alt_agl + float + alt_agl + + + gear_clearance + float + gear_clearance + + + greater + 0.01 + + + lequal + true + + + + + + + + true + + + material/active + + + material/ambient + + + material/diffuse + + + material/specular + + + material/emissive + + + material/shininess + + + material/color-mode + + + + + blend/active + + + blend/source + + + blend/destination + + + + shade-model + + + cull-face + + + rendering-hint + + + + texture[0]/active + + 0 + + texture[0]/image + + + texture[0]/type + + + texture[0]/filter + + + texture[0]/wrap-s + + + texture[0]/wrap-t + + + + modulate + + + + + + diff --git a/Shaders/shadow-ALS.frag b/Shaders/shadow-ALS.frag new file mode 100644 index 000000000..2bc1140c7 --- /dev/null +++ b/Shaders/shadow-ALS.frag @@ -0,0 +1,19 @@ +#version 120 + +uniform sampler2D texture; +uniform float scattering; + +void main() +{ + vec4 texel = texture2D(texture, gl_TexCoord[0].st); + texel.a = smoothstep(0.0,0.1, texel.a); + + float illumination = length(gl_Color.rgb); + texel = vec4 (0.1,0.1,0.1,texel.a); + texel.a *= illumination; + texel.a *=0.6 * smoothstep(0.5,0.8,scattering); + texel.a = min(0.8, texel.a); + + vec4 fragColor = texel; + gl_FragColor = fragColor; +} diff --git a/Shaders/shadow-ALS.vert b/Shaders/shadow-ALS.vert new file mode 100644 index 000000000..9110e4622 --- /dev/null +++ b/Shaders/shadow-ALS.vert @@ -0,0 +1,75 @@ +// -*-C++-*- + + +#version 120 + +uniform float hazeLayerAltitude; +uniform float terminator; +uniform float terrain_alt; +uniform float avisibility; +uniform float visibility; +uniform float overcast; +uniform float ground_scattering; +uniform float eye_alt; +uniform float moonlight; +uniform float alt_agl; +uniform float gear_clearance; + +const float EarthRadius = 5800000.0; +const float terminator_width = 200000.0; + +float light_func (in float x, in float a, in float b, in float c, in float d, in float e) +{ + +if (x < -15.0) {return 0.0;} + +return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); +} + +void main() +{ + + + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; + + vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); + vec3 relPos = gl_Vertex.xyz - ep.xyz; + + + // compute the strength of light + float vertex_alt = max(gl_Vertex.z,100.0); + float scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt); + vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz; + vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0)); + float yprime = -dot(relPos, lightHorizon); + float yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt); + float earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4; + float lightArg = (terminator-yprime_alt)/100000.0; + vec4 light_diffuse; + light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0); + light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); + light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); + light_diffuse.a = 1.0; + light_diffuse = light_diffuse * scattering; + float shade_depth = 1.0 * smoothstep (0.6,0.95,ground_scattering) * (1.0-smoothstep(0.1,0.5,overcast)) * smoothstep(0.4,1.5,earthShade); + + light_diffuse.rgb = light_diffuse.rgb * (1.0 + 1.2 * shade_depth); + + + // project the shadow onto the ground + vec4 pos = gl_Vertex; + pos.z-=0.95 * alt_agl-0.1; + pos.xy -= lightFull.xy * 0.95* alt_agl/lightFull.z; + + // enlargen the shadow for low light + if (dot(pos.xy, lightHorizon.xy)<0.0) + { + pos.xy -= lightFull.xy * gear_clearance/lightFull.z; + } + + + gl_Position = gl_ModelViewProjectionMatrix * pos; + + gl_FrontColor = light_diffuse; + gl_BackColor = gl_FrontColor; +}