Debian Codegraph @

Shaders/terrain-ALS-ultra.vert

raw
1 // -*-C++-*-
2 #version 120
3
4 // Shader that uses OpenGL state values to do per-pixel lighting
5 //
6 // The only light used is gl_LightSource[0], which is assumed to be
7 // directional.
8 //
9 // Diffuse colors come from the gl_Color, ambient from the material. This is
10 // equivalent to osg::Material::DIFFUSE.
11 // Haze part added by Thorsten Renk, Oct. 2011
12
13
14 #define MODE_OFF 0
15 #define MODE_DIFFUSE 1
16 #define MODE_AMBIENT_AND_DIFFUSE 2
17
18 // The constant term of the lighting equation that doesn't depend on
19 // the surface normal is passed in gl_{Front,Back}Color. The alpha
20 // component is set to 1 for front, 0 for back in order to work around
21 // bugs with gl_FrontFacing in the fragment shader.
22 varying vec4 diffuse_term;
23 varying vec3 normal;
24 varying vec3 relPos;
25 varying vec2 rawPos;
26 varying vec3 worldPos;
27 varying vec3 ecViewdir;
28 varying vec2 grad_dir;
29 varying vec4 ecPosition;
30
31 varying float mie_angle;
32 varying float steepness;
33
34 uniform int colorMode;
35
36 uniform bool raise_vertex;
37
38 uniform float hazeLayerAltitude;
39 uniform float terminator;
40 uniform float terrain_alt;
41 uniform float avisibility;
42 uniform float visibility;
43 uniform float overcast;
44 uniform float ground_scattering;
45 uniform float eye_alt;
46 uniform float moonlight;
47
48 uniform bool use_IR_vision;
49
50 uniform mat4 osg_ViewMatrixInverse;
51
52 float earthShade;
53 float yprime_alt;
54
55 vec3 moonlight_perception (in vec3 light);
56
57 void setupShadows(vec4 eyeSpacePos);
58
59 // This is the value used in the skydome scattering shader - use the same here for consistency?
60 const float EarthRadius = 5800000.0;
61 const float terminator_width = 200000.0;
62
63 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
64 {
65 //x = x - 0.5;
66
67 // use the asymptotics to shorten computations
68 if (x < -15.0) {return 0.0;}
69
70 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
71 }
72
73
74 void main()
75 {
76
77
78 vec4 light_diffuse;
79 vec4 light_ambient;
80 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
81 vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight + vec3 (0.005, 0.005, 0.005);
82
83 moonLightColor = moonlight_perception (moonLightColor);
84
85
86 //float yprime_alt;
87 float yprime;
88 float lightArg;
89 float intensity;
90 float vertex_alt;
91 float scattering;
92
93 rawPos = gl_Vertex.xy;
94 worldPos = (osg_ViewMatrixInverse *gl_ModelViewMatrix * gl_Vertex).xyz;
95
96
97 steepness = dot(normalize(gl_Normal), vec3 (0.0, 0.0, 1.0));
98 grad_dir = normalize(gl_Normal.xy);
99
100 vec4 pos = gl_Vertex;
101 if (raise_vertex)
102 {
103 pos.z+=0.1;
104 gl_Position = gl_ModelViewProjectionMatrix * pos;
105 }
106 else gl_Position = ftransform();
107
108
109 // this code is copied from default.vert
110
111 ecPosition = gl_ModelViewMatrix * gl_Vertex;
112 //gl_Position = ftransform();
113 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
114 gl_TexCoord[2] = gl_TextureMatrix[2] * gl_MultiTexCoord2;
115 normal = gl_NormalMatrix * gl_Normal;
116 //nvec = (gl_NormalMatrix * gl_Normal).xy;
117 vec4 ambient_color, diffuse_color;
118 if (colorMode == MODE_DIFFUSE) {
119 diffuse_color = gl_Color;
120 ambient_color = gl_FrontMaterial.ambient;
121 } else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) {
122 diffuse_color = gl_Color;
123 ambient_color = gl_Color;
124 } else {
125 diffuse_color = gl_FrontMaterial.diffuse;
126 ambient_color = gl_FrontMaterial.ambient;
127 }
128
129
130
131 // here start computations for the haze layer
132 // we need several geometrical quantities
133
134
135 // first current altitude of eye position in model space
136 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
137
138 // and relative position to vector
139 relPos = gl_Vertex.xyz - ep.xyz;
140
141 ecViewdir = (gl_ModelViewMatrix * (ep - gl_Vertex)).xyz;
142 // unfortunately, we need the distance in the vertex shader, although the more accurate version
143 // is later computed in the fragment shader again
144 float dist = length(relPos);
145
146
147 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
148 vertex_alt = max(gl_Vertex.z,100.0);
149 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
150
151
152 // branch dependent on daytime
153
154 if (terminator < 1000000.0) // the full, sunrise and sunset computation
155 {
156
157
158 // establish coordinates relative to sun position
159
160 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
161 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
162
163
164
165 // yprime is the distance of the vertex into sun direction
166 yprime = -dot(relPos, lightHorizon);
167
168 // this gets an altitude correction, higher terrain gets to see the sun earlier
169 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
170
171 // two times terminator width governs how quickly light fades into shadow
172 // now the light-dimming factor
173 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
174
175 // parametrized version of the Flightgear ground lighting function
176 lightArg = (terminator-yprime_alt)/100000.0;
177
178 // directional scattering for low sun
179 if (lightArg < 10.0)
180 {mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
181 else
182 {mie_angle = 1.0;}
183
184
185
186
187 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
188 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
189 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
190 light_diffuse.a = 1.0;
191 light_diffuse = light_diffuse * scattering;
192
193 //light_ambient.b = light_func(lightArg, 0.000506, 0.131, -3.315, 0.000457, 0.5);
194 //light_ambient.g = light_func(lightArg, 2.264e-05, 0.134, 0.967, 3.66e-05, 0.4);
195 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
196 light_ambient.g = light_ambient.r * 0.4/0.33; //light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.4);
197 light_ambient.b = light_ambient.r * 0.5/0.33; //light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.5);
198 light_ambient.a = 1.0;
199
200
201
202
203 // correct ambient light intensity and hue before sunrise
204 if (earthShade < 0.5)
205 {
206 intensity = length(light_ambient.rgb);
207 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
208 light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
209
210 intensity = length(light_diffuse.rgb);
211 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
212 }
213
214
215 // the haze gets the light at the altitude of the haze top if the vertex in view is below
216 // but the light at the vertex if the vertex is above
217
218 vertex_alt = max(vertex_alt,hazeLayerAltitude);
219
220 if (vertex_alt > hazeLayerAltitude)
221 {
222 if (dist > 0.8 * avisibility)
223 {
224 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
225 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
226 }
227 }
228 else
229 {
230 vertex_alt = hazeLayerAltitude;
231 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
232 }
233
234 }
235 else // the faster, full-day version without lightfields
236 {
237 //vertex_alt = max(gl_Vertex.z,100.0);
238
239 earthShade = 1.0;
240 mie_angle = 1.0;
241
242 if (terminator > 3000000.0)
243 {light_diffuse = vec4 (1.0, 1.0, 1.0, 1.0);
244 light_ambient = vec4 (0.33, 0.4, 0.5, 1.0); }
245 else
246 {
247
248 lightArg = (terminator/100000.0 - 10.0)/20.0;
249 light_diffuse.b = 0.78 + lightArg * 0.21;
250 light_diffuse.g = 0.907 + lightArg * 0.091;
251 light_diffuse.r = 0.904 + lightArg * 0.092;
252 light_diffuse.a = 1.0;
253
254 //light_ambient.b = 0.41 + lightArg * 0.08;
255 //light_ambient.g = 0.333 + lightArg * 0.06;
256 light_ambient.r = 0.316 + lightArg * 0.016;
257 light_ambient.g = light_ambient.r * 0.4/0.33;
258 light_ambient.b = light_ambient.r * 0.5/0.33;
259 light_ambient.a = 1.0;
260 }
261
262 light_diffuse = light_diffuse * scattering;
263 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
264 }
265
266
267 // a sky/earth irradiation map model - the sky creates much more diffuse radiation than the ground, so
268 // steep faces end up shaded more
269
270 light_ambient = light_ambient * ((1.0+steepness)/2.0 * 1.2 + (1.0-steepness)/2.0 * 0.2);
271
272 // deeper shadows when there is lots of direct light
273
274 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);
275
276 light_ambient.rgb = light_ambient.rgb * (1.0 - shade_depth);
277 light_diffuse.rgb = light_diffuse.rgb * (1.0 + 1.2 * shade_depth);
278
279 if (use_IR_vision)
280 {
281 light_ambient.rgb = max(light_ambient.rgb, vec3 (0.5, 0.5, 0.5));
282 }
283
284
285 // default lighting based on texture and material using the light we have just computed
286
287 diffuse_term = diffuse_color * light_diffuse;
288 vec4 constant_term = gl_FrontMaterial.emission + ambient_color *
289 (gl_LightModel.ambient + light_ambient);
290
291 diffuse_term.a = yprime_alt;
292 gl_FrontColor.rgb = constant_term.rgb; //gl_FrontColor.a = 1.0;
293 gl_BackColor.rgb = constant_term.rgb; //gl_BackColor.a = 0.0;
294 gl_FrontColor.a = mie_angle;
295 gl_BackColor.a = mie_angle;
296
297 setupShadows(ecPosition);
298 }
299
300