Debian Codegraph @

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