Debian Codegraph @

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