Debian Codegraph @

Shaders/ws30-ALS.vert

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