Debian Codegraph @

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