Debian Codegraph @

Shaders/generic-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 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
40 void setupShadows(vec4 eyeSpacePos);
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 ecPosition = gl_ModelViewMatrix * gl_Vertex;
79 gl_Position = ftransform();
80 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
81 gl_TexCoord[2] = gl_TextureMatrix[2] * gl_MultiTexCoord2;
82 normal = gl_NormalMatrix * gl_Normal;
83 vec4 ambient_color, diffuse_color;
84 if (colorMode == MODE_DIFFUSE) {
85 diffuse_color = gl_Color;
86 ambient_color = gl_FrontMaterial.ambient;
87 } else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) {
88 diffuse_color = gl_Color;
89 ambient_color = gl_Color;
90 } else {
91 diffuse_color = gl_FrontMaterial.diffuse;
92 ambient_color = gl_FrontMaterial.ambient;
93 }
94
95
96
97 // here start computations for the haze layer
98 // we need several geometrical quantities
99
100 // first current altitude of eye position in model space
101 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
102
103 // and relative position to vector
104 relPos = gl_Vertex.xyz - ep.xyz;
105
106 // unfortunately, we need the distance in the vertex shader, although the more accurate version
107 // is later computed in the fragment shader again
108 float dist = length(relPos);
109
110 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
111 vertex_alt = max(gl_Vertex.z,100.0);
112 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
113
114
115 // branch dependent on daytime
116
117 if (terminator < 1000000.0) // the full, sunrise and sunset computation
118 {
119
120
121 // establish coordinates relative to sun position
122
123 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
124 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
125
126
127
128 // yprime is the distance of the vertex into sun direction
129 yprime = -dot(relPos, lightHorizon);
130
131 // this gets an altitude correction, higher terrain gets to see the sun earlier
132 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
133
134 // two times terminator width governs how quickly light fades into shadow
135 // now the light-dimming factor
136 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
137
138 // parametrized version of the Flightgear ground lighting function
139 lightArg = (terminator-yprime_alt)/100000.0;
140
141 // directional scattering for low sun
142 if (lightArg < 10.0)
143 {mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
144 else
145 {mie_angle = 1.0;}
146
147
148
149
150 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
151 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
152 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
153 light_diffuse.a = 1.0;
154 light_diffuse = light_diffuse * scattering;
155
156
157 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
158 light_ambient.g = light_ambient.r * 0.4/0.33;
159 light_ambient.b = light_ambient.r * 0.5/0.33;
160 light_ambient.a = 1.0;
161
162
163
164
165 // correct ambient light intensity and hue before sunrise
166 if (earthShade < 0.5)
167 {
168 //light_ambient = light_ambient * (0.7 + 0.3 * smoothstep(0.2, 0.5, earthShade));
169 intensity = length(light_ambient.xyz);
170
171 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
172 light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
173
174 intensity = length(light_diffuse.xyz);
175 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
176 }
177
178
179 // the haze gets the light at the altitude of the haze top if the vertex in view is below
180 // but the light at the vertex if the vertex is above
181
182 vertex_alt = max(vertex_alt,hazeLayerAltitude);
183
184 if (vertex_alt > hazeLayerAltitude)
185 {
186 if (dist > 0.8 * avisibility)
187 {
188 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
189 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
190 }
191 }
192 else
193 {
194 vertex_alt = hazeLayerAltitude;
195 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
196 }
197
198 }
199 else // the faster, full-day version without lightfields
200 {
201 //vertex_alt = max(gl_Vertex.z,100.0);
202
203 earthShade = 1.0;
204 mie_angle = 1.0;
205
206 if (terminator > 3000000.0)
207 {light_diffuse = vec4 (1.0, 1.0, 1.0, 0.0);
208 light_ambient = vec4 (0.33, 0.4, 0.5, 0.0); }
209 else
210 {
211
212 lightArg = (terminator/100000.0 - 10.0)/20.0;
213 light_diffuse.b = 0.78 + lightArg * 0.21;
214 light_diffuse.g = 0.907 + lightArg * 0.091;
215 light_diffuse.r = 0.904 + lightArg * 0.092;
216 light_diffuse.a = 1.0;
217
218 light_ambient.r = 0.316 + lightArg * 0.016;
219 light_ambient.g = light_ambient.r * 0.4/0.33;
220 light_ambient.b = light_ambient.r * 0.5/0.33;
221 light_ambient.a = 1.0;
222 }
223
224 light_diffuse = light_diffuse * scattering;
225 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
226 }
227
228
229 // default lighting based on texture and material using the light we have just computed
230
231 diffuse_term = diffuse_color* light_diffuse;
232 vec4 constant_term = gl_FrontMaterial.emission + ambient_color *
233 (gl_LightModel.ambient + light_ambient);
234 // Super hack: if diffuse material alpha is less than 1, assume a
235 // transparency animation is at work
236 if (gl_FrontMaterial.diffuse.a < 1.0)
237 diffuse_term.a = gl_FrontMaterial.diffuse.a;
238 else
239 diffuse_term.a = gl_Color.a;
240 // Another hack for supporting two-sided lighting without using
241 // gl_FrontFacing in the fragment shader.
242 gl_FrontColor.rgb = constant_term.rgb; gl_FrontColor.a = 1.0;
243 gl_BackColor.rgb = constant_term.rgb; gl_BackColor.a = 0.0;
244
245 setupShadows(ecPosition);
246 }
247
248
249
250