Debian Codegraph @

Shaders/ws30-ALS-ultra.vert

raw
1 // WS30 VERTEX SHADER
2 // -*-C++-*-
3 #version 120
4
5 // Shader that uses OpenGL state values to do per-pixel lighting
6 //
7 // The only light used is gl_LightSource[0], which is assumed to be
8 // directional.
9 //
10 // Colors are not assigned in this shader, as they will come from
11 // the landclass lookup in the fragment shader.
12 // Haze part added by Thorsten Renk, Oct. 2011
13
14
15 #define MODE_OFF 0
16 #define MODE_DIFFUSE 1
17 #define MODE_AMBIENT_AND_DIFFUSE 2
18
19
20 // The constant term of the lighting equation that doesn't depend on
21 // the surface normal is passed in gl_{Front,Back}Color. The alpha
22 // component is set to 1 for front, 0 for back in order to work around
23 // bugs with gl_FrontFacing in the fragment shader.
24 varying vec4 light_diffuse_comp;
25 varying vec3 normal;
26 varying vec3 relPos;
27 varying vec2 ground_tex_coord;
28 varying vec2 rawPos;
29 varying vec3 worldPos;
30 varying vec3 ecViewdir;
31 varying vec2 grad_dir;
32 varying vec4 ecPosition;
33 varying vec3 vertVec;
34
35 // For water calculations
36 varying float earthShade;
37 varying vec3 lightdir;
38 varying vec4 waterTex1;
39 varying vec4 waterTex2;
40 varying vec4 waterTex4;
41 varying vec3 specular_light;
42
43 uniform float osg_SimulationTime;
44 uniform float WindN;
45 uniform float WindE;
46
47 // Sent packed into alpha channels
48 //varying float yprime_alt;
49 varying float mie_angle;
50
51 varying float steepness;
52
53 uniform int colorMode;
54
55 uniform bool raise_vertex;
56
57 uniform float hazeLayerAltitude;
58 uniform float terminator;
59 uniform float terrain_alt;
60 uniform float avisibility;
61 uniform float visibility;
62 uniform float overcast;
63 uniform float ground_scattering;
64 uniform float eye_alt;
65 uniform float moonlight;
66
67 uniform bool use_IR_vision;
68
69 uniform mat4 osg_ViewMatrixInverse;
70
71 // From VPBTechnique.cxx
72 uniform mat4 fg_zUpTransform;
73 uniform vec3 fg_modelOffset;
74
75 float yprime_alt;
76
77 vec3 moonlight_perception (in vec3 light);
78
79 void setupShadows(vec4 eyeSpacePos);
80
81 // This is the value used in the skydome scattering shader - use the same here for consistency?
82 const float EarthRadius = 5800000.0;
83 const float terminator_width = 200000.0;
84
85
86
87
88 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
89 {
90 //x = x - 0.5;
91 // use the asymptotics to shorten computations
92 if (x < -15.0) {return 0.0;}
93 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
94 }
95
96 void createRotationMatrix(in float angle, out mat4 rotmat)
97 {
98 rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
99 sin( angle ), cos( angle ), 0.0, 0.0,
100 0.0 , 0.0 , 1.0, 0.0,
101 0.0 , 0.0 , 0.0, 1.0 );
102 }
103
104 void main()
105 {
106
107 vec4 light_diffuse;
108 vec4 light_ambient;
109 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
110 vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight + vec3 (0.005, 0.005, 0.005);
111
112 moonLightColor = moonlight_perception (moonLightColor);
113
114
115 //float yprime_alt;
116 float yprime;
117 float lightArg;
118 float intensity;
119 float vertex_alt;
120 float scattering;
121
122 // The ALS code assumes that units are in meters - e.g. model space vertices (gl_Vertex) are in meters
123
124 // WS30 model space, Nov 21, 2021:
125 // Coordinate axes are the same for geocentric, but not the origin.
126 // +z direction points from the Earth center to North pole.
127 // +x direction points from the Earth center to longitude = 0 on the equator.
128 // +y direction points from the Earth center to logntitude = East on the equator.
129 // Model space origin is at sea level. Units are in meters.
130 // Each tile, for each LoD level, its own model origin
131 // modelOffset is the model origin relative to the Earth center. It is in a geocentric
132 // space with the same axes, but with the Earth center as the origin. Units are in meters.
133
134
135 vec4 pos = gl_Vertex;
136 if (raise_vertex)
137 {
138 pos.z+=0.1;
139 gl_Position = gl_ModelViewProjectionMatrix * pos;
140 }
141 else gl_Position = ftransform();
142
143
144 // this code is copied from default.vert
145
146 ecPosition = gl_ModelViewMatrix * gl_Vertex;
147 //gl_Position = ftransform();
148 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
149 normal = gl_NormalMatrix * gl_Normal;
150
151 // Required for water calculations
152 lightdir = normalize(vec3(fg_zUpTransform * vec4(gl_ModelViewMatrixInverse * gl_LightSource[0].position)));
153 waterTex4 = vec4( ecPosition.xzy, 0.0 );
154
155 vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
156 vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
157
158 float Angle;
159
160 float windFactor = sqrt(WindE * WindE + WindN * WindN) * 0.05;
161 if (WindN == 0.0 && WindE == 0.0) {
162 Angle = 0.0;
163 } else {
164 Angle = atan(-WindN, WindE) - atan(1.0);
165 }
166
167 mat4 RotationMatrix;
168 createRotationMatrix(Angle, RotationMatrix);
169 waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
170 waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
171
172
173 ///////////////////////////////////////////
174 // Test phase code:
175 //
176 // Coords for ground textures
177 // Due to precision issues coordinates should restart (i.e. go to zero) every 5000m or so.
178 const float restart_dist_m = 5000.0;
179
180 // Model position
181 vec3 mp = gl_Vertex.xyz;
182
183 // Temporary approximation to get shaders to compile:
184 ground_tex_coord = gl_TexCoord[0].st;
185
186 //
187 // End test phase code
188 ///////////////////////////////////////////
189
190 // WS2:
191 // first current altitude of eye position in model space
192 // vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
193 // and relative position to vector
194 //relPos = gl_Vertex.xyz - ep.xyz;
195
196
197 // Transform for frame of reference where:
198 // +z is in the up direction.
199 // The orientation of x and y axes are unknown currently.
200 // The origin is at the same position as the model space origin.
201 // The units are in meters.
202 mat4 viewSpaceToZUpSpace = fg_zUpTransform * gl_ModelViewMatrixInverse;
203
204 vec4 vertexZUp = fg_zUpTransform * gl_Vertex;
205
206 // WS2: rawPos = gl_Vertex.xy;
207 rawPos = vertexZUp.xy;
208
209 // WS2: worldPos = (osg_ViewMatrixInverse *gl_ModelViewMatrix * gl_Vertex).xyz;
210 worldPos = fg_modelOffset + gl_Vertex.xyz;
211
212 steepness = abs(dot(normalize(vec3(fg_zUpTransform * vec4(gl_Normal,1.0))), vec3 (0.0, 0.0, 1.0)));
213 // Gradient direction used for small scale noise. In the same space as noise coords, rawpos.xy.
214 grad_dir = normalize(gl_Normal.xy);
215
216 // here start computations for the haze layer
217 // we need several geometrical quantities
218
219
220 // Eye position in z up space
221 vec4 epZUp = viewSpaceToZUpSpace * vec4(0.0,0.0,0.0,1.0);
222
223 // Position of vertex relative to the eye position in z up space
224 vec3 relPosZUp = (vertexZUp - epZUp).xyz;
225
226
227 // first current altitude of eye position in model space
228 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
229
230
231 // Eye position in model space
232 vec4 epMS = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
233
234 /*
235 //old: and relative position to vector. This is also used for cloud shadow positioning.
236 relPosOld = (fg_zUpTransform * vec4(gl_Vertex - ep)).xyz;
237 if (any(notEqual(relPosOld, relPosZUp))) relPos = vec3(1000000.0);
238 */
239
240 relPos = relPosZUp;
241 vertVec = relPosZUp;
242
243 ecViewdir = (gl_ModelViewMatrix * (epMS - gl_Vertex)).xyz;
244 // unfortunately, we need the distance in the vertex shader, although the more accurate version
245 // is later computed in the fragment shader again
246 float dist = length(relPos);
247
248 // Altitude of the vertex above mean sea level in meters.
249 // This is equal to vertexZUp.z as the model space origin is at mean sea level.
250 // Somehow zero leads to artefacts, so ensure it is at least 100m.
251 //WS2: vertex_alt = max(gl_Vertex.z,100.0);
252 vertex_alt = max(vertexZUp.z,100.0);
253 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
254
255
256 // branch dependent on daytime
257
258 if (terminator < 1000000.0) // the full, sunrise and sunset computation
259 {
260 // establish coordinates relative to sun position
261
262 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
263 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
264
265 // yprime is the distance of the vertex into sun direction
266 yprime = -dot(relPos, lightHorizon);
267
268 // this gets an altitude correction, higher terrain gets to see the sun earlier
269 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
270
271 // two times terminator width governs how quickly light fades into shadow
272 // now the light-dimming factor
273 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
274
275 // parametrized version of the Flightgear ground lighting function
276 lightArg = (terminator-yprime_alt)/100000.0;
277
278 // directional scattering for low sun
279 if (lightArg < 10.0)
280 {mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
281 else
282 {mie_angle = 1.0;}
283
284
285 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
286 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
287 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
288 light_diffuse.a = 1.0;
289 light_diffuse = light_diffuse * scattering;
290
291 //light_ambient.b = light_func(lightArg, 0.000506, 0.131, -3.315, 0.000457, 0.5);
292 //light_ambient.g = light_func(lightArg, 2.264e-05, 0.134, 0.967, 3.66e-05, 0.4);
293 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
294 light_ambient.g = light_ambient.r * 0.4/0.33; //light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.4);
295 light_ambient.b = light_ambient.r * 0.5/0.33; //light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.5);
296 light_ambient.a = 1.0;
297
298 // Water specular calculations
299 specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
300 specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
301 specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
302 specular_light = max(specular_light * scattering, vec3 (0.05, 0.05, 0.05));
303 intensity = length(specular_light.rgb);
304 specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,ground_scattering) ));
305 specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.5, 0.7,earthShade)));
306
307
308
309 // correct ambient light intensity and hue before sunrise
310 if (earthShade < 0.5)
311 {
312 intensity = length(light_ambient.rgb);
313 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
314 light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
315
316 intensity = length(light_diffuse.rgb);
317 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
318 }
319
320 // directional scattering for low sun
321 if (lightArg < 10.0) {
322 mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;
323 } else {
324 mie_angle = 1.0;
325 }
326
327
328 // the haze gets the light at the altitude of the haze top if the vertex in view is below
329 // but the light at the vertex if the vertex is above
330
331 vertex_alt = max(vertex_alt,hazeLayerAltitude);
332
333 if (vertex_alt > hazeLayerAltitude)
334 {
335 if (dist > 0.8 * avisibility)
336 {
337 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
338 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
339 }
340 }
341 else
342 {
343 vertex_alt = hazeLayerAltitude;
344 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
345 }
346
347 } // End if (terminator < 1000000.0)
348 else // the faster, full-day version without lightfields
349 {
350 //vertex_alt = max(gl_Vertex.z,100.0);
351
352 earthShade = 1.0;
353 mie_angle = 1.0;
354
355 if (terminator > 3000000.0)
356 {
357 light_diffuse = vec4 (1.0, 1.0, 1.0, 1.0);
358 light_ambient = vec4 (0.33, 0.4, 0.5, 1.0);
359 specular_light = vec3 (1.0, 1.0, 1.0);
360 }
361 else
362 {
363 lightArg = (terminator/100000.0 - 10.0)/20.0;
364 light_diffuse.b = 0.78 + lightArg * 0.21;
365 light_diffuse.g = 0.907 + lightArg * 0.091;
366 light_diffuse.r = 0.904 + lightArg * 0.092;
367 light_diffuse.a = 1.0;
368
369 //light_ambient.b = 0.41 + lightArg * 0.08;
370 //light_ambient.g = 0.333 + lightArg * 0.06;
371 light_ambient.r = 0.316 + lightArg * 0.016;
372 light_ambient.g = light_ambient.r * 0.4/0.33;
373 light_ambient.b = light_ambient.r * 0.5/0.33;
374 light_ambient.a = 1.0;
375
376 specular_light.b = 0.78 + lightArg * 0.21;
377 specular_light.g = 0.907 + lightArg * 0.091;
378 specular_light.r = 0.904 + lightArg * 0.092;
379 }
380
381 light_diffuse = light_diffuse * scattering;
382 specular_light = specular_light * scattering;
383
384 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
385
386 } //End the faster, full-day version without lightfields
387
388
389 // a sky/earth irradiation map model - the sky creates much more diffuse radiation than the ground, so
390 // steep faces end up shaded more
391
392 light_ambient = light_ambient * ((1.0+steepness)/2.0 * 1.2 + (1.0-steepness)/2.0 * 0.2);
393
394 // deeper shadows when there is lots of direct light
395
396 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);
397
398 light_ambient.rgb = light_ambient.rgb * (1.0 - shade_depth);
399 light_diffuse.rgb = light_diffuse.rgb * (1.0 + 1.2 * shade_depth);
400 specular_light.rgb *= (1.0 + 1.2 * shade_depth);
401
402 if (use_IR_vision)
403 {
404 light_ambient.rgb = max(light_ambient.rgb, vec3 (0.5, 0.5, 0.5));
405 }
406
407
408 // default lighting based on texture and material using the light we have just computed
409
410 light_diffuse_comp = light_diffuse;
411 //Testing phase code: ambient colours are not sent to fragement shader yet.
412 // They are all default except for water/ocean etc. currently
413 // Emission is all set to the default of vec4(0.0, 0.0, 0.0, 1.0)
414 //To do: Fix this once ambient colour becomes available in the fragment shaders.
415 //const vec4 ambient_color = vec4(0.2, 0.2, 0.2, 1.0);
416 const vec4 ambient_color = vec4(1.0);
417 vec4 constant_term = ambient_color * (gl_LightModel.ambient + light_ambient);
418
419 light_diffuse_comp.a = yprime_alt;
420 gl_FrontColor.rgb = constant_term.rgb; // gl_FrontColor.a = 1.0;
421 gl_BackColor.rgb = constant_term.rgb; // gl_BackColor.a = 0.0;
422 gl_FrontColor.a = mie_angle;
423 gl_BackColor.a = mie_angle;
424
425 setupShadows(ecPosition);
426 }
427
428
429
430