Debian Codegraph @

Shaders/tree-ALS.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
21 // The constant term of the lighting equation that doesn't depend on
22 // the surface normal is passed in gl_{Front,Back}Color. The alpha
23 // component is set to 1 for front, 0 for back in order to work around
24 // bugs with gl_FrontFacing in the fragment shader.
25
26 varying vec3 relPos;
27 varying float yprime_alt;
28 varying float autumn_flag;
29
30 uniform int colorMode;
31 uniform int wind_effects;
32 uniform int forest_effects;
33 uniform int num_deciduous_trees;
34 uniform float hazeLayerAltitude;
35 uniform float terminator;
36 uniform float terrain_alt;
37 uniform float avisibility;
38 uniform float visibility;
39 uniform float overcast;
40 uniform float ground_scattering;
41 uniform float snow_level;
42 uniform float season;
43 uniform float forest_effect_size;
44 uniform float forest_effect_shape;
45 uniform float WindN;
46 uniform float WindE;
47
48 uniform float osg_SimulationTime;
49
50 uniform int cloud_shadow_flag;
51
52 uniform bool use_forest_effect;
53
54 float earthShade;
55 float mie_angle;
56
57 float shadow_func (in float x, in float y, in float noise, in float dist);
58 float VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand);
59
60 // This is the value used in the skydome scattering shader - use the same here for consistency?
61 const float EarthRadius = 5800000.0;
62 const float terminator_width = 200000.0;
63
64 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
65 {
66 //x = x - 0.5;
67
68 // use the asymptotics to shorten computations
69 if (x < -15.0) {return 0.0;}
70
71 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
72 }
73
74
75 void main()
76 {
77
78 vec4 light_ambient;
79
80 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
81
82 float yprime;
83 float lightArg;
84 float intensity;
85 float vertex_alt;
86 float scattering;
87
88 // this code is copied from tree.vert
89
90 float numVarieties = gl_Normal.z;
91 float texFract = floor(fract(instancePosition.x) * numVarieties) / numVarieties;
92
93 // determine whether the tree changes color in autumn
94 if (texFract < float(num_deciduous_trees)/float(numVarieties)) {autumn_flag = 0.5 + fract(instancePosition.x);}
95 else {autumn_flag = 0.0;}
96
97
98 texFract += floor(gl_MultiTexCoord0.x) / numVarieties;
99
100 // Determine the rotation for the tree.
101 float sr = sin(instancePosition.x);
102 float cr = cos(instancePosition.x);
103 gl_TexCoord[0] = vec4(texFract, gl_MultiTexCoord0.y, 0.0, 0.0);
104
105 // Determine the y texture coordinate based on whether it's summer, winter, snowy.
106 gl_TexCoord[0].y = gl_TexCoord[0].y + 0.25 * step(snow_level, instancePosition.z) + 0.5 * season;
107
108 // scaling
109 float scale = (fract(instancePosition.x) + fract(instancePosition.y))/2.0f + 0.5f;
110 vec3 position = gl_Vertex.xyz * gl_Normal.xxy * scale;
111
112 // Rotation of the generic quad to specific one for the tree.
113 position.xy = vec2(dot(position.xy, vec2(cr, sr)), dot(position.xy, vec2(-sr, cr)));
114
115
116 // Shear by wind. Note that this only applies to the top vertices
117 if (wind_effects > 0)
118 {
119 position.x = position.x + position.z * (sin(osg_SimulationTime * 1.8 + (instancePosition.x + instancePosition.y + instancePosition.z) * 0.01) + 1.0) * 0.0025 * WindN;
120 position.y = position.y + position.z * (sin(osg_SimulationTime * 1.8 + (instancePosition.x + instancePosition.y + instancePosition.z) * 0.01) + 1.0) * 0.0025 * WindE;
121 }
122
123 // Scale by random domains
124 float voronoi;
125 if ((forest_effects > 0)&& use_forest_effect)
126 {
127 voronoi = 0.5 + 1.0 * VoronoiNoise2D(instancePosition.xy, forest_effect_size, forest_effect_shape, forest_effect_shape);
128 position.xyz = position.xyz * voronoi;
129 }
130
131 // Move to correct location
132 position = position + instancePosition.xyz;
133 gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);
134
135 vec3 ecPosition = vec3(gl_ModelViewMatrix * vec4(position, 1.0));
136 //normal = normalize(-ecPosition);
137
138 //float n = dot(normalize(gl_LightSource[0].position.xyz), normalize(-ecPosition));
139
140
141 //vec4 diffuse_color = gl_FrontMaterial.diffuse * max(0.1, n);
142 //diffuse_color.a = 1.0;
143 vec4 ambient_color = gl_FrontMaterial.ambient;
144
145 // here start computations for the haze layer
146 // we need several geometrical quantities
147
148 // first current altitude of eye position in model space
149 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
150
151 // and relative position to vector
152 relPos = position - ep.xyz;
153
154 // unfortunately, we need the distance in the vertex shader, although the more accurate version
155 // is later computed in the fragment shader again
156 float dist = length(relPos);
157
158 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
159 vertex_alt = max(position.z,100.0);
160 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
161
162 // branch dependent on daytime
163
164 if (terminator < 1000000.0) // the full, sunrise and sunset computation
165 {
166
167
168 // establish coordinates relative to sun position
169
170 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
171 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
172
173
174
175 // yprime is the distance of the vertex into sun direction
176 yprime = -dot(relPos, lightHorizon);
177
178 // this gets an altitude correction, higher terrain gets to see the sun earlier
179 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
180
181 // two times terminator width governs how quickly light fades into shadow
182 // now the light-dimming factor
183 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
184
185 // parametrized version of the Flightgear ground lighting function
186 lightArg = (terminator-yprime_alt)/100000.0;
187
188 // directional scattering for low sun
189 if (lightArg < 10.0)
190 {mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
191 else
192 {mie_angle = 1.0;}
193
194
195 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
196 light_ambient.g = light_ambient.r * 0.4/0.33;
197 light_ambient.b = light_ambient.r * 0.5/0.33;
198 light_ambient.a = 1.0;
199
200
201
202
203
204
205 // correct ambient light intensity and hue before sunrise
206 if (earthShade < 0.5)
207 {
208 //light_ambient = light_ambient * (0.4 + 0.6 * smoothstep(0.2, 0.5, earthShade));
209 intensity = length(light_ambient.rgb);
210 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade) ));
211
212
213 }
214
215
216 // the haze gets the light at the altitude of the haze top if the vertex in view is below
217 // but the light at the vertex if the vertex is above
218
219 vertex_alt = max(vertex_alt,hazeLayerAltitude);
220
221 if (vertex_alt > hazeLayerAltitude)
222 {
223 if (dist > 0.8 * avisibility)
224 {
225 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
226 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
227 }
228 }
229 else
230 {
231 vertex_alt = hazeLayerAltitude;
232 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
233 }
234
235 }
236 else // the faster, full-day version without lightfields
237 {
238
239
240 earthShade = 1.0;
241 mie_angle = 1.0;
242
243 if (terminator > 3000000.0)
244 {light_ambient = vec4 (0.33, 0.4, 0.5, 1.0); }
245 else
246 {
247
248 lightArg = (terminator/100000.0 - 10.0)/20.0;
249
250 light_ambient.r = 0.316 + lightArg * 0.016;
251 light_ambient.g = light_ambient.r * 0.4/0.33;
252 light_ambient.b = light_ambient.r * 0.5/0.33;
253 light_ambient.a = 1.0;
254 }
255
256
257 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
258 }
259
260 light_ambient.rgb = light_ambient.rgb * (1.0 + smoothstep(1000000.0, 3000000.0,terminator));
261
262 // tree shader lighting
263
264 if (cloud_shadow_flag == 1)
265 {light_ambient.rgb = light_ambient.rgb * (0.5 + 0.5 * shadow_func(relPos.x, relPos.y, 1.0, dist));}
266
267
268 //vec4 ambientColor = gl_FrontLightModelProduct.sceneColor +
269 //gl_FrontColor = ambientColor;
270 gl_FrontColor = light_ambient * gl_FrontMaterial.ambient;
271 gl_FrontColor.a = mie_angle; gl_BackColor.a = mie_angle;
272
273
274
275
276 }
277