Debian Codegraph @

Shaders/tree-ALS-shadow.vert

raw
1 // -*-C++-*-
2 #version 120
3 #extension GL_EXT_draw_instanced : enable
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 // Diffuse colors come from the gl_Color, ambient from the material. This is
11 // equivalent to osg::Material::DIFFUSE.
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 attribute vec3 instancePosition; // (x,y,z)
20 attribute vec3 instanceTerrainNormal;
21
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
28
29 varying vec3 relPos;
30 varying float yprime_alt;
31 varying float is_shadow;
32 varying float autumn_flag;
33
34 uniform int colorMode;
35 uniform int wind_effects;
36 uniform int forest_effects;
37 uniform int num_deciduous_trees;
38 uniform float hazeLayerAltitude;
39 uniform float terminator;
40 uniform float terrain_alt;
41 uniform float avisibility;
42 uniform float visibility;
43 uniform float overcast;
44 uniform float ground_scattering;
45 uniform float snow_level;
46 uniform float season;
47 uniform float forest_effect_size;
48 uniform float forest_effect_shape;
49 uniform float WindN;
50 uniform float WindE;
51
52 uniform bool use_tree_shadows;
53 uniform bool use_forest_effect;
54 uniform bool use_optimization;
55 uniform bool tree_patches;
56
57 uniform float osg_SimulationTime;
58
59 uniform int cloud_shadow_flag;
60
61 float earthShade;
62 float mie_angle;
63
64 float shadow_func (in float x, in float y, in float noise, in float dist);
65 float VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand);
66
67 // This is the value used in the skydome scattering shader - use the same here for consistency?
68 const float EarthRadius = 5800000.0;
69 const float terminator_width = 200000.0;
70
71 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
72 {
73 //x = x - 0.5;
74
75 // use the asymptotics to shorten computations
76 if (x < -15.0) {return 0.0;}
77
78 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
79 }
80
81
82 void main()
83 {
84
85 //vec4 light_diffuse;
86 vec4 light_ambient;
87
88 vec3 shadedFogColor = vec3(0.65, 0.67, 0.78);
89
90 float yprime;
91 float lightArg;
92 float intensity;
93 float vertex_alt;
94 float scattering;
95 is_shadow = -1.0;
96
97 // establish coordinates relative to sun position
98
99 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
100 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
101
102 // eye position in model space
103 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
104
105 float rn_dist = length(instancePosition.xyz - ep.xyz) + 300.0 * mod(10.0 * instancePosition.x,1.0);
106 float rn = mod(100.0 * instancePosition.x + 100.0 * instancePosition.y,1.0);
107
108 float numVarieties = gl_Normal.z;
109
110 bool cull_flag = false;
111
112 float factor = 1.0;
113 float factor1 = 1.0;
114
115
116 if ((rn_dist > 2000.0) && (tree_patches == true) && (use_optimization == true))
117 {
118
119 if (rn > 0.15)
120 {cull_flag = true;}
121 else
122 {
123 numVarieties *=0.25;
124 factor = 5.2;
125 }
126 if (gl_FogCoord !=0.0) {cull_flag = true;}
127 }
128
129
130
131 if (cull_flag)
132 {
133 // move everything out of the view frustrum
134 gl_Position = vec4 (0.0,0.0,10.0,1.0);
135 gl_FrontColor.a = 0.0;
136 }
137 else
138 {
139
140 float texFract = floor(fract(instancePosition.x) * numVarieties) / numVarieties;
141
142 // determine whether the tree changes color in autumn
143 if (texFract < float(num_deciduous_trees)/float(numVarieties)) {autumn_flag = 0.5 + fract(instancePosition.x);}
144 else {autumn_flag = 0.0;}
145
146 texFract += floor(gl_MultiTexCoord0.x) / numVarieties;
147
148 // Determine the rotation for the tree.
149 float sr = sin(instancePosition.x);
150 float cr = cos(instancePosition.x);
151
152 if (gl_Color.r > 0.0)
153 {
154 sr = dot(lightHorizon.xy, vec2 (0.0,1.0));
155 cr = dot(lightHorizon.xy, vec2 (-1.0,0.0));
156 }
157
158
159 gl_TexCoord[0] = vec4(texFract, gl_MultiTexCoord0.y, 0.0, 0.0);
160
161 // Determine the y texture coordinate based on whether it's summer, winter, snowy.
162 gl_TexCoord[0].y = gl_TexCoord[0].y + 0.25 * step(snow_level, instancePosition.z) + 0.5 * season;
163
164 // scaling
165 float scale = (fract(instancePosition.x) + fract(instancePosition.y))/2.0f + 0.5f;
166 vec3 position = gl_Vertex.xyz * gl_Normal.xxy * scale;
167
168 // Rotation of the generic quad to specific one for the tree.
169 position.xy = factor * vec2(dot(position.xy, vec2(cr, sr)), dot(position.xy, vec2(-sr, cr)));
170
171
172 // Shear by wind. Note that this only applies to the top vertices
173 if (wind_effects > 0)
174 {
175 position.x = position.x + position.z * (sin(osg_SimulationTime * 1.8 + (instancePosition.x + instancePosition.y + instancePosition.z) * 0.01) + 1.0) * 0.0025 * WindN;
176 position.y = position.y + position.z * (sin(osg_SimulationTime * 1.8 + (instancePosition.x + instancePosition.y + instancePosition.z) * 0.01) + 1.0) * 0.0025 * WindE;
177 }
178
179
180
181
182
183
184 // Scale by random domains
185 float voronoi;
186 if ((forest_effects > 0)&& use_forest_effect)
187 {
188 voronoi = 0.5 + 1.0 * VoronoiNoise2D(instancePosition.xy, forest_effect_size, forest_effect_shape, forest_effect_shape);
189 position.xyz = position.xyz * voronoi;
190 }
191
192 // check if this is a shadow quad
193 if ((gl_Color.r > 0.0)&&(use_tree_shadows))
194 {
195 is_shadow = 1.0;
196 float sinAlpha = dot(lightFull, vec3 (0.0,0.0,1.0));
197 float cosAlpha = sqrt(1.0 - sinAlpha*sinAlpha);
198 float slope = dot(instanceTerrainNormal, vec3(0.0,0.0,1.0));
199 //float slope = 1.0;
200 position.x += position.z * clamp(cosAlpha/sinAlpha,-5.0,5.0) * -dot(lightHorizon.xy, vec2(1.0,0.0));
201 position.y += position.z * clamp(cosAlpha/sinAlpha,-5.0,5.0) * -dot(lightHorizon.xy, vec2 (0.0,1.0));
202 if (position.z > 3.0) // we deal with an upper vertex
203 {
204 vec3 terrainNormal = instanceTerrainNormal;
205 position.z = 0.4 + 10.0*(1.0 - slope) ;
206 float sinPhi = dot(terrainNormal, vec3(1.0,0.0,0.0));
207 float sinPsi = dot(terrainNormal, vec3(0.0,1.0,0.0));
208 position.z -= position.x * sinPhi;
209 position.z -= position.y * sinPsi;
210 }
211 else
212 {position.z = 0.4 + 10.0* (1.0-slope);}
213
214 }
215
216
217 // Move to correct location
218 position = position + instancePosition.xyz;
219 gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);
220
221 vec3 ecPosition = vec3(gl_ModelViewMatrix * vec4(position, 1.0));
222 //normal = normalize(-ecPosition);
223
224 //float n = dot(normalize(gl_LightSource[0].position.xyz), normalize(-ecPosition));
225
226
227 //vec4 diffuse_color = gl_FrontMaterial.diffuse * max(0.1, n);
228 //diffuse_color.a = 1.0;
229 vec4 ambient_color = gl_FrontMaterial.ambient;
230
231 // here start computations for the haze layer
232 // we need several geometrical quantities
233
234
235
236 relPos = position - ep.xyz;
237
238 // unfortunately, we need the distance in the vertex shader, although the more accurate version
239 // is later computed in the fragment shader again
240 float dist = length(relPos);
241
242 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
243 vertex_alt = max(position.z,100.0);
244 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
245
246 // check whether we should see a shadow
247
248 if (is_shadow >0.0)
249 {
250 float view_angle = dot ((instanceTerrainNormal), normalize(relPos));
251 if (view_angle < 0.0) {is_shadow = -view_angle;}
252 else {is_shadow = 5.0;}
253
254 // the surface element will be in shadow
255 if (dot(normalize(lightFull),(instanceTerrainNormal)) < 0.0)
256 { is_shadow = 5.0;}
257 }
258
259 // branch dependent on daytime
260
261 if (terminator < 1000000.0) // the full, sunrise and sunset computation
262 {
263
264 // yprime is the distance of the vertex into sun direction
265 yprime = -dot(relPos, lightHorizon);
266
267 // this gets an altitude correction, higher terrain gets to see the sun earlier
268 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
269
270 // two times terminator width governs how quickly light fades into shadow
271 // now the light-dimming factor
272 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
273
274 // parametrized version of the Flightgear ground lighting function
275 lightArg = (terminator-yprime_alt)/100000.0;
276
277 // directional scattering for low sun
278 if (lightArg < 10.0)
279 {mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
280 else
281 {mie_angle = 1.0;}
282
283
284 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
285 light_ambient.g = light_ambient.r * 0.4/0.33;
286 light_ambient.b = light_ambient.r * 0.5/0.33;
287 light_ambient.a = 1.0;
288
289
290
291
292
293
294 // correct ambient light intensity and hue before sunrise
295 if (earthShade < 0.5)
296 {
297 //light_ambient = light_ambient * (0.4 + 0.6 * smoothstep(0.2, 0.5, earthShade));
298 intensity = length(light_ambient.rgb);
299 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade) ));
300
301
302 }
303
304
305 // the haze gets the light at the altitude of the haze top if the vertex in view is below
306 // but the light at the vertex if the vertex is above
307
308 vertex_alt = max(vertex_alt,hazeLayerAltitude);
309
310 if (vertex_alt > hazeLayerAltitude)
311 {
312 if (dist > 0.8 * avisibility)
313 {
314 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
315 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
316 }
317 }
318 else
319 {
320 vertex_alt = hazeLayerAltitude;
321 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
322 }
323
324 }
325 else // the faster, full-day version without lightfields
326 {
327
328
329 earthShade = 1.0;
330 mie_angle = 1.0;
331
332 if (terminator > 3000000.0)
333 {light_ambient = vec4 (0.33, 0.4, 0.5, 1.0); }
334 else
335 {
336
337 lightArg = (terminator/100000.0 - 10.0)/20.0;
338
339 light_ambient.r = 0.316 + lightArg * 0.016;
340 light_ambient.g = light_ambient.r * 0.4/0.33;
341 light_ambient.b = light_ambient.r * 0.5/0.33;
342 light_ambient.a = 1.0;
343 }
344
345
346 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
347 }
348
349 light_ambient.rgb = light_ambient.rgb * (1.0 + smoothstep(1000000.0, 3000000.0,terminator));
350
351 // tree shader lighting
352
353 if (cloud_shadow_flag == 1)
354 {light_ambient.rgb = light_ambient.rgb * (0.5 + 0.5 * shadow_func(relPos.x, relPos.y, 1.0, dist));}
355
356
357 gl_FrontColor = light_ambient * gl_FrontMaterial.ambient;
358 gl_FrontColor.a = mie_angle; gl_BackColor.a = mie_angle;
359
360 }
361
362 }
363