Debian Codegraph @

Shaders/generic_ALS_base.glsl

raw
1 // -*-C++-*-
2 #version 120
3
4 // Duplicated from generic-ALS-base.vert, but in the form of
5 // library functions that can be used by other shader cpde
6
7 // Shader that uses OpenGL state values to do per-pixel lighting
8 //
9 // The only light used is gl_LightSource[0], which is assumed to be
10 // directional.
11 //
12 // Diffuse colors come from the gl_Color, ambient from the material. This is
13 // equivalent to osg::Material::DIFFUSE.
14 // Haze part added by Thorsten Renk, Oct. 2011
15
16
17 #define MODE_OFF 0
18 #define MODE_DIFFUSE 1
19 #define MODE_AMBIENT_AND_DIFFUSE 2
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 varying vec4 diffuse_term;
26 varying vec3 normal;
27 varying vec3 relPos;
28 varying vec4 ecPosition;
29
30 varying float yprime_alt;
31 varying float mie_angle;
32
33 uniform int colorMode;
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 moonlight;
42
43 // This is the value used in the skydome scattering shader - use the same here for consistency?
44 const float EarthRadius = 5800000.0;
45 const float terminator_width = 200000.0;
46
47
48 float earthShade;
49
50 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
51 {
52 // use the asymptotics to shorten computations
53 if (x < -15.0) {
54 return 0.0;
55 }
56
57 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
58 }
59
60 void render_ALS_base(in vec3 position)
61 {
62 vec4 light_diffuse;
63 vec4 light_ambient;
64 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
65 vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight;
66
67 //float yprime_alt;
68 float yprime;
69 float lightArg;
70 float intensity;
71 float vertex_alt;
72 float scattering;
73
74 // this code is copied from default.vert
75 ecPosition = gl_ModelViewMatrix * vec4(position, gl_Vertex.w);
76 // Project to world space
77 gl_Position = gl_ModelViewProjectionMatrix * vec4(position, gl_Vertex.w);
78 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
79 gl_TexCoord[2] = gl_TextureMatrix[2] * gl_MultiTexCoord2;
80
81 vec4 ambient_color, diffuse_color;
82 if (colorMode == MODE_DIFFUSE) {
83 diffuse_color = gl_Color;
84 ambient_color = gl_FrontMaterial.ambient;
85 } else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) {
86 diffuse_color = gl_Color;
87 ambient_color = gl_Color;
88 } else {
89 diffuse_color = gl_FrontMaterial.diffuse;
90 ambient_color = gl_FrontMaterial.ambient;
91 }
92
93 // here start computations for the haze layer
94 // we need several geometrical quantities
95
96 // first current altitude of eye position in model space
97 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
98
99 // and relative position to vector
100 relPos = position.xyz - ep.xyz;
101
102 // unfortunately, we need the distance in the vertex shader, although the more accurate version
103 // is later computed in the fragment shader again
104 float dist = length(relPos);
105
106 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
107 vertex_alt = max(position.z,100.0);
108 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
109
110
111 // branch dependent on daytime
112
113 if (terminator < 1000000.0) // the full, sunrise and sunset computation
114 {
115 // establish coordinates relative to sun position
116 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
117 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
118
119 // yprime is the distance of the vertex into sun direction
120 yprime = -dot(relPos, lightHorizon);
121
122 // this gets an altitude correction, higher terrain gets to see the sun earlier
123 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
124
125 // two times terminator width governs how quickly light fades into shadow
126 // now the light-dimming factor
127 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
128
129 // parametrized version of the Flightgear ground lighting function
130 lightArg = (terminator-yprime_alt)/100000.0;
131
132 // directional scattering for low sun
133 if (lightArg < 10.0) {
134 mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;
135 } else {
136 mie_angle = 1.0;
137 }
138
139 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
140 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
141 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
142 light_diffuse.a = 1.0;
143 light_diffuse = light_diffuse * scattering;
144
145 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
146 light_ambient.g = light_ambient.r * 0.4/0.33;
147 light_ambient.b = light_ambient.r * 0.5/0.33;
148 light_ambient.a = 1.0;
149
150 // correct ambient light intensity and hue before sunrise
151 if (earthShade < 0.5) {
152 //light_ambient = light_ambient * (0.7 + 0.3 * smoothstep(0.2, 0.5, earthShade));
153 intensity = length(light_ambient.xyz);
154
155 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
156 light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
157
158 intensity = length(light_diffuse.xyz);
159 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
160 }
161
162
163 // the haze gets the light at the altitude of the haze top if the vertex in view is below
164 // but the light at the vertex if the vertex is above
165 vertex_alt = max(vertex_alt,hazeLayerAltitude);
166
167 if (vertex_alt > hazeLayerAltitude) {
168 if (dist > 0.8 * avisibility) {
169 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
170 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
171 }
172 } else {
173 vertex_alt = hazeLayerAltitude;
174 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
175 }
176 }
177 else // the faster, full-day version without lightfields
178 {
179 //vertex_alt = max(gl_Vertex.z,100.0);
180
181 earthShade = 1.0;
182 mie_angle = 1.0;
183
184 if (terminator > 3000000.0)
185 {
186 light_diffuse = vec4 (1.0, 1.0, 1.0, 0.0);
187 light_ambient = vec4 (0.33, 0.4, 0.5, 0.0);
188 } else {
189 lightArg = (terminator/100000.0 - 10.0)/20.0;
190 light_diffuse.b = 0.78 + lightArg * 0.21;
191 light_diffuse.g = 0.907 + lightArg * 0.091;
192 light_diffuse.r = 0.904 + lightArg * 0.092;
193 light_diffuse.a = 1.0;
194
195 light_ambient.r = 0.316 + lightArg * 0.016;
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 light_diffuse = light_diffuse * scattering;
202 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
203 }
204
205 // default lighting based on texture and material using the light we have just computed
206 diffuse_term = diffuse_color* light_diffuse;
207 vec4 constant_term = gl_FrontMaterial.emission + ambient_color *
208 (gl_LightModel.ambient + light_ambient);
209
210 // Super hack: if diffuse material alpha is less than 1, assume a
211 // transparency animation is at work
212 if (gl_FrontMaterial.diffuse.a < 1.0)
213 diffuse_term.a = gl_FrontMaterial.diffuse.a;
214 else
215 diffuse_term.a = gl_Color.a;
216
217 // Another hack for supporting two-sided lighting without using
218 // gl_FrontFacing in the fragment shader.
219 gl_FrontColor.rgb = constant_term.rgb; gl_FrontColor.a = 1.0;
220 gl_BackColor.rgb = constant_term.rgb; gl_BackColor.a = 0.0;
221 }