| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
varying float fogFactor; |
| 5 |
varying vec3 hazeColor; |
| 6 |
varying vec3 relVector; |
| 7 |
|
| 8 |
uniform float range; // From /sim/rendering/clouds3d-vis-range |
| 9 |
uniform float scattering; |
| 10 |
uniform float terminator; |
| 11 |
uniform float altitude; |
| 12 |
uniform float flash; |
| 13 |
uniform float lightning_pos_x; |
| 14 |
uniform float lightning_pos_y; |
| 15 |
uniform float lightning_range; |
| 16 |
|
| 17 |
|
| 18 |
float shade = 0.8; |
| 19 |
float cloud_height = 1000.0; |
| 20 |
const float EarthRadius = 5800000.0; |
| 21 |
|
| 22 |
// light_func is a generalized logistic function fit to the light intensity as a function |
| 23 |
// of scaled terminator position obtained from Flightgear core |
| 24 |
|
| 25 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 26 |
{ |
| 27 |
x = x-0.5; |
| 28 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 29 |
} |
| 30 |
|
| 31 |
void main(void) |
| 32 |
{ |
| 33 |
|
| 34 |
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; |
| 35 |
//gl_TexCoord[0] = gl_MultiTexCoord0 + vec4(textureIndexX, textureIndexY, 0.0, 0.0); |
| 36 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 37 |
vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0); |
| 38 |
vec3 u = normalize(ep.xyz - l.xyz); |
| 39 |
|
| 40 |
gl_Position = vec4(0.0, 0.0, 0.0, 1.0); |
| 41 |
gl_Position.x = gl_Vertex.x; |
| 42 |
gl_Position.y += gl_Vertex.y; |
| 43 |
gl_Position.z += gl_Vertex.z; |
| 44 |
gl_Position.xyz += gl_Color.xyz; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
// Determine a lighting normal based on the vertex position from the |
| 49 |
// center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker. |
| 50 |
float n = dot(normalize(-gl_LightSource[0].position.xyz), |
| 51 |
normalize(mat3x3(gl_ModelViewMatrix) * (- gl_Position.xyz)));; |
| 52 |
|
| 53 |
// Determine the position - used for fog and shading calculations |
| 54 |
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Position); |
| 55 |
float fogCoord = abs(ecPosition.z); |
| 56 |
float fract = smoothstep(0.0, cloud_height, gl_Position.z + cloud_height); |
| 57 |
|
| 58 |
relVector = gl_Position.xyz - ep.xyz; |
| 59 |
gl_Position = gl_ModelViewProjectionMatrix * gl_Position; |
| 60 |
|
| 61 |
|
| 62 |
// Light at the final position |
| 63 |
|
| 64 |
// first obtain normal to sun position |
| 65 |
|
| 66 |
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz; |
| 67 |
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0)); |
| 68 |
|
| 69 |
// yprime is the distance of the vertex into sun direction, corrected for altitude |
| 70 |
float vertex_alt = max(altitude * 0.30480 + relVector.z,100.0); |
| 71 |
float yprime = -dot(relVector, lightHorizon); |
| 72 |
float yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); |
| 73 |
|
| 74 |
// compute the light at the position |
| 75 |
vec4 light_diffuse; |
| 76 |
|
| 77 |
float lightArg = (terminator-yprime_alt)/100000.0; |
| 78 |
|
| 79 |
light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 2.827, 1.08e-05, 1.0); |
| 80 |
light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 81 |
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); |
| 82 |
light_diffuse.a = 0.0; |
| 83 |
|
| 84 |
//light_diffuse = light_diffuse * scattering; |
| 85 |
float intensity = length(light_diffuse); |
| 86 |
light_diffuse = intensity * normalize(mix(light_diffuse, 2.0*vec4 (0.55, 0.6, 0.8, 1.0), (1.0 - smoothstep(0.3,0.8, scattering)))); |
| 87 |
|
| 88 |
// Determine the shading of the sprite based on its vertical position and position relative to the sun. |
| 89 |
n = min(smoothstep(-0.5, 0.0, n), fract); |
| 90 |
// Determine the shading based on a mixture from the backlight to the front |
| 91 |
vec4 backlight = gl_LightSource[0].diffuse * shade; |
| 92 |
|
| 93 |
gl_FrontColor = mix(backlight, gl_LightSource[0].diffuse, n); |
| 94 |
gl_FrontColor += gl_FrontLightModelProduct.sceneColor; |
| 95 |
|
| 96 |
// two times terminator width governs how quickly light fades into shadow |
| 97 |
float terminator_width = 200000.0; |
| 98 |
float earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1; |
| 99 |
gl_FrontColor.rgb = gl_FrontColor.rgb * earthShade; |
| 100 |
|
| 101 |
// lightning |
| 102 |
vec2 lightningRelVector = relVector.xy - vec2(lightning_pos_x, lightning_pos_y); |
| 103 |
float rCoord = length(lightningRelVector); |
| 104 |
|
| 105 |
vec3 flashColor = 2.0 * flash * vec3 (0.43, 0.57, 1.0) * (1.0 - smoothstep(lightning_range, 5.0 * lightning_range, rCoord)); |
| 106 |
|
| 107 |
gl_FrontColor.rgb += flashColor; |
| 108 |
gl_FrontColor.rgb = clamp(gl_FrontColor.rgb,0.0,1.0); |
| 109 |
|
| 110 |
// As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out. |
| 111 |
gl_FrontColor.a = min(smoothstep(100.0, 250.0, fogCoord), 1.0 - smoothstep(range*0.9, range, fogCoord)); |
| 112 |
gl_BackColor = gl_FrontColor; |
| 113 |
|
| 114 |
// Fog doesn't affect rain as much as other objects. |
| 115 |
//fogFactor = exp( -gl_Fog.density * fogCoord * 0.4); |
| 116 |
//fogFactor = clamp(fogFactor, 0.0, 1.0); |
| 117 |
|
| 118 |
float fadeScale = 0.05 + 0.2 * log(fogCoord/1000.0); |
| 119 |
if (fadeScale < 0.05) fadeScale = 0.05; |
| 120 |
fogFactor = exp( -gl_Fog.density * 1.0 * fogCoord * fadeScale); |
| 121 |
|
| 122 |
hazeColor = light_diffuse.rgb; |
| 123 |
hazeColor.x = hazeColor.x * 0.83; |
| 124 |
hazeColor.y = hazeColor.y * 0.9; |
| 125 |
hazeColor = hazeColor * scattering; |
| 126 |
|
| 127 |
// change haze color to blue hue for strong fogging |
| 128 |
intensity = length(hazeColor); |
| 129 |
hazeColor = intensity * normalize(mix(hazeColor, 2.0 * vec3 (0.55, 0.6, 0.8), (1.0-smoothstep(0.3,0.8,scattering)))); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
hazeColor = clamp(hazeColor * earthShade, 0.0,1.0); |
| 134 |
|
| 135 |
|
| 136 |
} |