Debian Codegraph @

Shaders/space-ALS-base.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // written by Thorsten Renk, Oct 2011, based on default.frag
5 // Ambient term comes in gl_Color.rgb.
6 varying vec4 diffuse_term;
7 varying vec3 normal;
8 varying vec3 relPos;
9
10
11 uniform sampler2D texture;
12
13
14 varying float yprime_alt;
15 varying float mie_angle;
16
17
18 uniform float visibility;
19 uniform float avisibility;
20 uniform float scattering;
21 uniform float terminator;
22 uniform float terrain_alt;
23 uniform float hazeLayerAltitude;
24 uniform float overcast;
25 uniform float eye_alt;
26 uniform float cloud_self_shading;
27 uniform float air_pollution;
28 uniform float landing_light1_offset;
29 uniform float landing_light2_offset;
30 uniform float landing_light3_offset;
31
32 uniform float geo_light_x;
33 uniform float geo_light_y;
34 uniform float geo_light_z;
35 uniform float geo_light_radius;
36 uniform float geo_ambience;
37
38 uniform float geo_light_r;
39 uniform float geo_light_g;
40 uniform float geo_light_b;
41
42 uniform int quality_level;
43 uniform int tquality_level;
44 uniform int use_searchlight;
45 uniform int use_landing_light;
46 uniform int use_alt_landing_light;
47 uniform int use_geo_light;
48
49
50 const float EarthRadius = 5800000.0;
51 const float terminator_width = 200000.0;
52
53 float alt;
54 float eShade;
55
56
57 float fog_func (in float targ, in float alt);
58 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
59 float alt_factor(in float eye_alt, in float vertex_alt);
60 float light_distance_fading(in float dist);
61 float fog_backscatter(in float avisibility);
62
63
64 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
65 vec3 get_hazeColor(in float light_arg);
66 vec3 searchlight();
67 vec3 landing_light(in float offset, in float offsetv);
68 vec3 filter_combined (in vec3 color);
69
70 float luminance(vec3 color)
71 {
72 return dot(vec3(0.212671, 0.715160, 0.072169), color);
73 }
74
75
76 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
77 {
78 x = x - 0.5;
79
80 // use the asymptotics to shorten computations
81 if (x > 30.0) {return e;}
82 if (x < -15.0) {return 0.0;}
83
84 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
85 }
86
87 // this determines how light is attenuated in the distance
88 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
89 // for distance > visibility
90
91
92
93
94 void main()
95 {
96
97 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
98 // this is taken from default.frag
99 vec3 n;
100 float NdotL, NdotHV, fogFactor;
101 vec4 color = gl_Color;
102 vec3 lightDir = gl_LightSource[0].position.xyz;
103 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
104 vec4 texel;
105 vec4 fragColor;
106 vec4 specular = vec4(0.0);
107 float intensity;
108
109 float effective_scattering = min(scattering, cloud_self_shading);
110
111 eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
112 vec4 light_specular = gl_LightSource[0].specular * (eShade - 0.1);
113
114 // If gl_Color.a == 0, this is a back-facing polygon and the
115 // normal should be reversed.
116 n = (2.0 * gl_Color.a - 1.0) * normal;
117 n = normalize(n);
118
119 NdotL = dot(n, lightDir);
120 if (NdotL > 0.0) {
121 color += diffuse_term * NdotL;
122 NdotHV = max(dot(n, halfVector), 0.0);
123 if (gl_FrontMaterial.shininess > 0.0)
124 specular.rgb = (gl_FrontMaterial.specular.rgb
125 * light_specular.rgb
126 * pow(NdotHV, gl_FrontMaterial.shininess));
127 }
128
129 //vec3 up = (gl_ModelViewMatrix * vec4(0.0,0.0,1.0,0.0)).xyz;
130
131 //vec3 sky_blue = vec3 (0.17, 0.52, 0.87);
132
133 //NdotL = dot(n, -up);
134 //if (NdotL > 0.0)
135 //{
136 //color.rgb += sky_blue * NdotL;
137 //}
138
139 color.a = diffuse_term.a;
140 // This shouldn't be necessary, but our lighting becomes very
141 // saturated. Clamping the color before modulating by the texture
142 // is closer to what the OpenGL fixed function pipeline does.
143 color = clamp(color, 0.0, 1.0);
144
145 float dist = length(relPos);
146 vec3 secondary_light = vec3 (0.0,0.0,0.0);
147
148 if ((quality_level > 5) && (tquality_level > 5))
149 {
150 if (use_searchlight == 1)
151 {
152 secondary_light += searchlight();
153 }
154 if (use_landing_light == 1)
155 {
156 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
157 }
158 if (use_alt_landing_light == 1)
159 {
160 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
161 }
162
163 vec3 geo_light = vec3 (0.0, 0.0, 0.0);
164
165 if (use_geo_light == 1)
166 {
167 vec3 geo_light_vec = vec3 (geo_light_x, geo_light_y, geo_light_z);
168 vec3 geo_light_rel_vec = geo_light_vec - (relPos + (gl_ModelViewMatrixInverse * vec4 (0.0, 0.0, 0.0, 1.0)).xyz);
169
170 vec3 geo_lightdir = (gl_ModelViewMatrix * vec4 (geo_light_rel_vec, 0.0)).xyz;
171 float geo_light_incidence = geo_ambience + (1.0- geo_ambience) * clamp(dot(n, geo_lightdir),0.0, 1.0);
172
173 geo_light = vec3 (geo_light_r, geo_light_g, geo_light_b) * (1.0 - smoothstep(0.5 * geo_light_radius, geo_light_radius, length(geo_light_rel_vec))) * geo_light_incidence;
174 }
175
176 if (dist > 2.0) // we don't want to light the cockpit...
177 {color.rgb +=secondary_light * light_distance_fading(dist) + geo_light ;}
178 }
179
180
181
182
183 texel = texture2D(texture, gl_TexCoord[0].st);
184 fragColor = color * texel + specular;
185
186
187 float lightArg = (terminator-yprime_alt)/100000.0;
188
189 vec3 hazeColor = get_hazeColor(lightArg);
190
191
192 // Rayleigh color shift due to in-scattering
193
194 if ((quality_level > 5) && (tquality_level > 5))
195 {
196 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
197 float outscatter = 1.0-exp(-dist/rayleigh_length);
198 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
199 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
200 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
201 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
202 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
203 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
204 }
205
206
207 // here comes the terrain haze model
208
209
210 float delta_z = hazeLayerAltitude - eye_alt;
211 float mvisibility = min(visibility, avisibility);
212
213 if (dist > 0.04 * mvisibility)
214 {
215
216 alt = eye_alt;
217
218
219 float transmission;
220 float vAltitude;
221 float delta_zv;
222 float H;
223 float distance_in_layer;
224 float transmission_arg;
225
226 // angle with horizon
227 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
228
229
230 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
231
232 if (delta_z > 0.0) // we're inside the layer
233 {
234 if (ct < 0.0) // we look down
235 {
236 distance_in_layer = dist;
237 vAltitude = min(distance_in_layer,mvisibility) * ct;
238 delta_zv = delta_z - vAltitude;
239 }
240 else // we may look through upper layer edge
241 {
242 H = dist * ct;
243 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
244 else {distance_in_layer = dist;}
245 vAltitude = min(distance_in_layer,visibility) * ct;
246 delta_zv = delta_z - vAltitude;
247 }
248 }
249 else // we see the layer from above, delta_z < 0.0
250 {
251 H = dist * -ct;
252 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
253 {
254 distance_in_layer = 0.0;
255 delta_zv = 0.0;
256 }
257 else
258 {
259 vAltitude = H + delta_z;
260 distance_in_layer = vAltitude/H * dist;
261 vAltitude = min(distance_in_layer,visibility) * (-ct);
262 delta_zv = vAltitude;
263 }
264 }
265
266
267 // ground haze cannot be thinner than aloft visibility in the model,
268 // so we need to use aloft visibility otherwise
269
270
271 transmission_arg = (dist-distance_in_layer)/avisibility;
272
273
274 float eqColorFactor;
275
276 //float scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, relPos.z + eye_alt);
277
278 if (visibility < avisibility)
279 {
280 transmission_arg = transmission_arg + (distance_in_layer/visibility);
281 // this combines the Weber-Fechner intensity
282 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
283
284 }
285 else
286 {
287 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
288 // this combines the Weber-Fechner intensity
289 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
290 }
291
292
293
294 transmission = fog_func(transmission_arg, alt);
295
296 // there's always residual intensity, we should never be driven to zero
297 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
298
299
300
301
302
303
304
305 // Mie-like factor
306
307 if (lightArg < 10.0)
308 {intensity = length(hazeColor);
309 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
310 hazeColor = intensity * ((1.0 - mie_magnitude) + mie_magnitude * mie_angle) * normalize(mix(hazeColor, vec3 (0.5, 0.58, 0.65), mie_magnitude * (0.5 - 0.5 * mie_angle)) );
311 }
312
313 // high altitude desaturation of the haze color
314
315 intensity = length(hazeColor);
316 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
317
318 // blue hue of haze
319
320 hazeColor.x = hazeColor.x * 0.83;
321 hazeColor.y = hazeColor.y * 0.9;
322
323
324 // additional blue in indirect light
325 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
326 intensity = length(hazeColor);
327 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
328
329 // change haze color to blue hue for strong fogging
330 //intensity = length(hazeColor);
331 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
332
333
334 // reduce haze intensity when looking at shaded surfaces, only in terminator region
335
336 float shadow = mix( min(1.0 + dot(normal,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
337 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
338
339 // don't let the light fade out too rapidly
340
341 lightArg = (terminator + 200000.0)/100000.0;
342 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
343 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
344 hazeColor *= eqColorFactor * eShade;
345 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
346
347 // determine the right mix of transmission and haze
348
349
350 fragColor.rgb = mix(hazeColor + secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
351
352
353
354 }
355
356 fragColor.rgb = filter_combined(fragColor.rgb);
357
358 gl_FragColor = fragColor;
359
360 }
361