Debian Codegraph @

Shaders/cloud-static-ALS-detailed.vert

raw
1 // -*-C++-*-
2 #version 120
3
4 varying float fogFactor;
5 varying float mie_frag;
6 varying float eShade;
7 varying vec3 hazeColor;
8
9 uniform float terminator;
10 uniform float altitude;
11 uniform float cloud_self_shading;
12 uniform float moonlight;
13
14 const float shade = 1.0;
15 const float cloud_height = 1000.0;
16 const float EarthRadius = 5800000.0;
17
18 // light_func is a generalized logistic function fit to the light intensity as a function
19 // of scaled terminator position obtained from Flightgear core
20
21 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
22 {
23 x = x-0.5;
24
25 // use the asymptotics to shorten computations
26 if (x > 30.0) {return e;}
27 if (x < -15.0) {return 0.03;}
28
29
30 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
31 }
32
33 void main(void)
34 {
35
36 vec3 shadedFogColor = vec3 (0.55, 0.67, 0.88);
37 vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight;
38
39 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
40 //gl_TexCoord[0] = gl_MultiTexCoord0 + vec4(textureIndexX, textureIndexY, 0.0, 0.0);
41 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
42 vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0);
43 vec3 u = normalize(ep.xyz - l.xyz);
44
45 gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
46 gl_Position.x = gl_Vertex.x;
47 gl_Position.y += gl_Vertex.y;
48 gl_Position.z += gl_Vertex.z;
49 gl_Position.xyz += gl_Color.xyz;
50
51
52 // Determine a lighting normal based on the vertex position from the
53 // center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker.
54 float n = dot(normalize(-gl_LightSource[0].position.xyz),
55 normalize(mat3x3(gl_ModelViewMatrix) * (- gl_Position.xyz)));;
56
57 // Determine the position - used for fog and shading calculations
58 vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Position);
59 float fogCoord = abs(ecPosition.z);
60 float fract = smoothstep(0.0, cloud_height, gl_Position.z + cloud_height);
61
62 vec3 relVector = gl_Position.xyz - ep.xyz;
63 gl_Position = gl_ModelViewProjectionMatrix * gl_Position;
64
65 // Light at the final position
66
67 // first obtain normal to sun position
68
69 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
70 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
71
72
73 mie_frag = dot(normalize(lightFull), normalize(relVector));
74
75
76 // yprime is the distance of the vertex into sun direction, corrected for altitude
77 //float vertex_alt = max(altitude * 0.30480 + relVector.z,100.0);
78 float vertex_alt = altitude + relVector.z;
79 float yprime = -dot(relVector, lightHorizon);
80 float yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
81
82 // compute the light at the position
83 vec4 light_diffuse;
84
85 float lightArg = (terminator-yprime_alt)/100000.0;
86
87 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 2.227, 1.08e-05, 1.0);
88 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
89 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
90 light_diffuse.a = 1.0;
91
92 float intensity = length(light_diffuse.rgb);
93 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, (1.0 - smoothstep(0.5,0.9, cloud_self_shading ))));
94
95 // Determine the shading of the sprite based on its vertical position and position relative to the sun.
96 n = min(smoothstep(-0.5, 0.0, n), fract);
97 // Determine the shading based on a mixture from the backlight to the front
98 vec4 backlight = light_diffuse * shade;
99
100 gl_FrontColor = mix(backlight, light_diffuse, n);
101 gl_FrontColor += gl_FrontLightModelProduct.sceneColor;
102
103 // As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out.
104 gl_FrontColor.a = min(smoothstep(100.0, 250.0, fogCoord), 1.0 - smoothstep(70000.0, 75000.0, fogCoord));
105
106 // Fog doesn't affect rain as much as other objects.
107 //fogFactor = exp( -gl_Fog.density * fogCoord * 0.4);
108 //fogFactor = clamp(fogFactor, 0.0, 1.0);
109
110 float fadeScale = 0.05 + 0.2 * log(fogCoord/1000.0);
111 if (fadeScale < 0.05) fadeScale = 0.05;
112 fogFactor = exp( -gl_Fog.density * fogCoord * fadeScale);
113
114 hazeColor = light_diffuse.rgb;
115 hazeColor.r = hazeColor.r * 0.83;
116 hazeColor.g = hazeColor.g * 0.9;
117
118 // in sunset or sunrise conditions, do extra shading of clouds
119
120
121
122 // two times terminator width governs how quickly light fades into shadow
123 float terminator_width = 200000.0;
124
125 // now dim the light
126 float earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
127
128 eShade = earthShade;
129
130 if (earthShade < 0.8)
131 {
132 intensity = length(light_diffuse.rgb);
133 gl_FrontColor.rgb = intensity * normalize(mix(gl_FrontColor.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade ) ));
134 }
135
136 hazeColor = hazeColor * earthShade;
137 gl_FrontColor.rgb = gl_FrontColor.rgb * earthShade;
138 gl_FrontColor.rgb = gl_FrontColor.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
139 hazeColor.rgb = hazeColor.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
140 gl_BackColor = gl_FrontColor;
141
142 }