Debian Codegraph @

Shaders/building-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 attribute vec3 instanceScale; // (width, depth, height)
21 attribute vec3 attrib1; // Generic packed attributes
22 attribute vec3 attrib2;
23
24 // The constant term of the lighting equation that doesn't depend on
25 // the surface normal is passed in gl_{Front,Back}Color. The alpha
26 // component is set to 1 for front, 0 for back in order to work around
27 // bugs with gl_FrontFacing in the fragment shader.
28 varying vec4 diffuse_term;
29 varying vec3 normal;
30 varying vec3 relPos;
31 varying vec4 ecPosition;
32
33 //varying float earthShade;
34 //varying float yprime;
35 //varying float vertex_alt;
36 varying float yprime_alt;
37 varying float mie_angle;
38
39 uniform int colorMode;
40 uniform float hazeLayerAltitude;
41 uniform float terminator;
42 uniform float terrain_alt;
43 uniform float avisibility;
44 uniform float visibility;
45 uniform float overcast;
46 //uniform float scattering;
47 uniform float ground_scattering;
48
49 uniform bool use_IR_vision;
50
51 // This is the value used in the skydome scattering shader - use the same here for consistency?
52 const float EarthRadius = 5800000.0;
53 const float terminator_width = 200000.0;
54
55
56 float earthShade;
57 //float mie_angle;
58
59 const float EPSILON = 1e-7;
60
61 void setupShadows(vec4 eyeSpacePos);
62
63 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
64 {
65 //x = x - 0.5;
66
67 // use the asymptotics to shorten computations
68 if (x < -15.0) {return 0.0;}
69
70 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
71 }
72
73 const float c_precision = 128.0;
74 const float c_precisionp1 = c_precision + 1.0;
75
76 vec3 float2vec(float value) {
77 vec3 val;
78 val.x = mod(value, c_precisionp1) / c_precision;
79 val.y = mod(floor(value / c_precisionp1), c_precisionp1) / c_precision;
80 val.z = floor(value / (c_precisionp1 * c_precisionp1)) / c_precision;
81 return val;
82 }
83
84 void main()
85 {
86
87 vec4 light_diffuse;
88 vec4 light_ambient;
89
90 float yprime;
91 float lightArg;
92 float intensity;
93 float vertex_alt;
94 float scattering;
95 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
96
97 // Unpack generic attributes
98 vec3 attr1 = float2vec(attrib1.x);
99 vec3 attr2 = float2vec(attrib1.z);
100 vec3 attr3 = float2vec(attrib2.x);
101
102 // Determine the rotation for the building.
103 float sr = sin(6.28 * attr1.x);
104 float cr = cos(6.28 * attr1.x);
105
106 vec3 position = gl_Vertex.xyz;
107 // Adjust the very top of the roof to match the rooftop scaling. This shapes
108 // the rooftop - gambled, gabled etc. These vertices are identified by gl_Color.z
109 position.x = (1.0 - gl_Color.z) * position.x + gl_Color.z * ((position.x + 0.5) * attr3.z - 0.5);
110 position.y = (1.0 - gl_Color.z) * position.y + gl_Color.z * (position.y * attrib2.y );
111
112 // Adjust pitch of roof to the correct height. These vertices are identified by gl_Color.z
113 // Scale down by the building height (instanceScale.z) because
114 // immediately afterwards we will scale UP the vertex to the correct scale.
115 position.z = position.z + gl_Color.z * attrib1.y / instanceScale.z;
116 position = position * instanceScale.xyz;
117
118 // Rotation of the building and movement into position
119 position.xy = vec2(dot(position.xy, vec2(cr, sr)), dot(position.xy, vec2(-sr, cr)));
120 position = position + instancePosition.xyz;
121
122 gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);
123
124 // Texture coordinates are stored as:
125 // - a separate offset (x0, y0) for the wall (wtex0x, wtex0y), and roof (rtex0x, rtex0y)
126 // - a semi-shared (x1, y1) so that the front and side of the building can have
127 // different texture mappings
128 //
129 // The vertex color value selects between them:
130 // gl_Color.x=1 indicates front/back walls
131 // gl_Color.y=1 indicates roof
132 // gl_Color.z=1 indicates top roof vertexs (used above)
133 // gl_Color.a=1 indicates sides
134 // Finally, the roof texture is on the right of the texture sheet
135 float wtex0x = attr1.y; // Front/Side texture X0
136 float wtex0y = attr1.z; // Front/Side texture Y0
137 float rtex0x = attr2.z; // Roof texture X0
138 float rtex0y = attr3.x; // Roof texture Y0
139 float wtex1x = attr2.x; // Front/Roof texture X1
140 float stex1x = attr3.y; // Side texture X1
141 float wtex1y = attr2.y; // Front/Roof/Side texture Y1
142
143 // We use the sign of the x texture coordinate to offset things later
144 // - roof texture coordinates are negative (0 -> -1)
145 // - wall texture coordinates are positive (0 -> 1)
146 // We want to nudge mtcx is the appropriate direction so that all of the
147 // offsets are applied in the correct direction
148 float mtcx = gl_MultiTexCoord0.x - sign(gl_Color.y - EPSILON) * EPSILON;
149
150 // Adjust the top texture coordinates to match roof shape
151 float is_roof_top_vertex = gl_Color.z;
152 float is_roof_bottom_vertex = 1.0 - is_roof_top_vertex;
153 float rooftop_scale_x = attr3.z;
154 float rooftop_scale_y = attrib2.y;
155
156 // Front/Back rooftop scaling differs from Left/Right scaling
157 float is_front_or_back = max(sign(EPSILON - abs(gl_Normal.y)), 0.0); // abs(gl_Normal.y) < EPSILON;
158
159 mtcx = is_front_or_back * (is_roof_bottom_vertex * (mtcx) + is_roof_top_vertex * ((mtcx + 0.5) * rooftop_scale_y - 0.5)) +
160 (1.0 - is_front_or_back) * (is_roof_bottom_vertex * (mtcx) + is_roof_top_vertex * ((mtcx + 0.5) * rooftop_scale_x - 0.5));
161
162 vec2 tex0 = vec2(sign(mtcx) * (gl_Color.x*wtex0x + gl_Color.y*rtex0x + gl_Color.a*wtex0x),
163 gl_Color.x*wtex0y + gl_Color.y*rtex0y + gl_Color.a*wtex0y);
164
165 vec2 tex1 = vec2(
166 // x-offsets
167 gl_Color.x*wtex1x + // main body front/back faces
168 gl_Color.a*stex1x + // main body left/right faces
169 gl_Color.y*(
170 is_front_or_back * wtex1x + // roof front/back faces
171 (1.0f - is_front_or_back) * stex1x // roof left/right faces
172 ),
173 // y-offsets
174 wtex1y
175 );
176
177 gl_TexCoord[0].x = tex0.x + mtcx * tex1.x;
178 gl_TexCoord[0].y = tex0.y + gl_MultiTexCoord0.y * tex1.y;
179
180 // Rotate the normal.
181 normal = gl_Normal;
182 normal.xy = vec2(dot(normal.xy, vec2(cr, sr)), dot(normal.xy, vec2(-sr, cr)));
183 normal = gl_NormalMatrix * normal;
184
185
186 vec4 ambient_color, diffuse_color;
187 if (colorMode == MODE_DIFFUSE) {
188 diffuse_color = vec4(1.0,1.0,1.0,1.0);
189 ambient_color = gl_FrontMaterial.ambient;
190 } else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) {
191 diffuse_color = vec4(1.0,1.0,1.0,1.0);
192 ambient_color = vec4(1.0,1.0,1.0,1.0);
193 } else {
194 diffuse_color = gl_FrontMaterial.diffuse;
195 ambient_color = gl_FrontMaterial.ambient;
196 }
197
198 // here start computations for the haze layer
199 // we need several geometrical quantities
200
201 // first current altitude of eye position in model space
202 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
203
204 // and relative position to vector
205 relPos = position - ep.xyz;
206
207 // unfortunately, we need the distance in the vertex shader, although the more accurate version
208 // is later computed in the fragment shader again
209 float dist = length(relPos);
210
211 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
212 vertex_alt = max(gl_Vertex.z + gl_Color.z,100.0);
213 scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
214
215 // branch dependent on daytime
216
217 if (terminator < 1000000.0) // the full, sunrise and sunset computation
218 {
219
220
221 // establish coordinates relative to sun position
222
223 vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
224 vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
225
226
227
228 // yprime is the distance of the vertex into sun direction
229 yprime = -dot(relPos, lightHorizon);
230
231 // this gets an altitude correction, higher terrain gets to see the sun earlier
232 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
233
234 // two times terminator width governs how quickly light fades into shadow
235 // now the light-dimming factor
236 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
237
238 // parametrized version of the Flightgear ground lighting function
239 lightArg = (terminator-yprime_alt)/100000.0;
240
241 // directional scattering for low sun
242 if (lightArg < 10.0)
243 {mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
244 else
245 {mie_angle = 1.0;}
246
247
248
249
250 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
251 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
252 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
253 light_diffuse.a = 1.0;
254 light_diffuse = light_diffuse * scattering;
255
256 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
257 light_ambient.g = light_ambient.r * 0.4/0.33;
258 light_ambient.b = light_ambient.r * 0.5/0.33;
259 light_ambient.a = 1.0;
260
261 // correct ambient light intensity and hue before sunrise
262 if (earthShade < 0.5)
263 {
264 //light_ambient = light_ambient * (0.4 + 0.6 * smoothstep(0.2, 0.5, earthShade));
265 intensity = length(light_ambient.rgb);
266 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
267
268 intensity = length(light_diffuse.rgb);
269 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
270 }
271
272
273 // the haze gets the light at the altitude of the haze top if the vertex in view is below
274 // but the light at the vertex if the vertex is above
275
276 vertex_alt = max(vertex_alt,hazeLayerAltitude);
277
278 if (vertex_alt > hazeLayerAltitude)
279 {
280 if (dist > 0.8 * avisibility)
281 {
282 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
283 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
284 }
285 }
286 else
287 {
288 vertex_alt = hazeLayerAltitude;
289 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
290 }
291
292 }
293 else // the faster, full-day version without lightfields
294 {
295 //vertex_alt = max(gl_Vertex.z,100.0);
296
297 earthShade = 1.0;
298 mie_angle = 1.0;
299
300 if (terminator > 3000000.0)
301 {light_diffuse = vec4 (1.0, 1.0, 1.0, 1.0);
302 light_ambient = vec4 (0.33, 0.4, 0.5, 1.0); }
303 else
304 {
305
306 lightArg = (terminator/100000.0 - 10.0)/20.0;
307 light_diffuse.b = 0.78 + lightArg * 0.21;
308 light_diffuse.g = 0.907 + lightArg * 0.091;
309 light_diffuse.r = 0.904 + lightArg * 0.092;
310 light_diffuse.a = 1.0;
311
312 light_ambient.r = 0.316 + lightArg * 0.016;
313 light_ambient.g = light_ambient.r * 0.4/0.33;
314 light_ambient.b = light_ambient.r * 0.5/0.33;
315 light_ambient.a = 1.0;
316 }
317
318 light_diffuse = light_diffuse * scattering;
319 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
320 }
321
322 if (use_IR_vision)
323 {
324 light_ambient.rgb = max(light_ambient.rgb, vec3 (0.5, 0.5, 0.5));
325 }
326
327
328 // default lighting based on texture and material using the light we have just computed
329
330 diffuse_term = diffuse_color* light_diffuse;
331 vec4 constant_term = gl_FrontMaterial.emission + ambient_color *
332 (gl_LightModel.ambient + light_ambient);
333 // Super hack: if diffuse material alpha is less than 1, assume a
334 // transparency animation is at work
335 if (gl_FrontMaterial.diffuse.a < 1.0)
336 diffuse_term.a = gl_FrontMaterial.diffuse.a;
337 else
338 diffuse_term.a = 1.0;
339 // Another hack for supporting two-sided lighting without using
340 // gl_FrontFacing in the fragment shader.
341 gl_FrontColor.rgb = constant_term.rgb;
342 gl_BackColor.rgb = constant_term.rgb;
343 //gl_FrontColor.a = mie_angle; gl_BackColor.a = mie_angle;
344
345 ecPosition = gl_ModelViewMatrix * vec4(position, 1.0);
346 setupShadows(ecPosition);
347 }