Debian Codegraph @

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