| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
// written by Thorsten Renk, Oct 2011, based on default.frag |
| 5 |
// Ambient term comes in gl_Color.rgb. |
| 6 |
varying vec4 diffuse_term; |
| 7 |
varying vec3 normal; |
| 8 |
varying vec3 relPos; |
| 9 |
|
| 10 |
|
| 11 |
uniform sampler2D texture; |
| 12 |
uniform sampler2D orthophotoTexture; |
| 13 |
|
| 14 |
|
| 15 |
varying float yprime_alt; |
| 16 |
varying float mie_angle; |
| 17 |
varying vec4 ecPosition; |
| 18 |
|
| 19 |
|
| 20 |
uniform float visibility; |
| 21 |
uniform float avisibility; |
| 22 |
uniform float scattering; |
| 23 |
uniform float terminator; |
| 24 |
uniform float terrain_alt; |
| 25 |
uniform float hazeLayerAltitude; |
| 26 |
uniform float overcast; |
| 27 |
uniform float eye_alt; |
| 28 |
uniform float cloud_self_shading; |
| 29 |
|
| 30 |
uniform bool orthophotoAvailable; |
| 31 |
|
| 32 |
const float EarthRadius = 5800000.0; |
| 33 |
const float terminator_width = 200000.0; |
| 34 |
|
| 35 |
float alt; |
| 36 |
float eShade; |
| 37 |
|
| 38 |
float fog_func (in float targ, in float alt); |
| 39 |
vec3 get_hazeColor(in float light_arg); |
| 40 |
vec3 filter_combined (in vec3 color); |
| 41 |
|
| 42 |
float getShadowing(); |
| 43 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 44 |
|
| 45 |
float luminance(vec3 color) |
| 46 |
{ |
| 47 |
return dot(vec3(0.212671, 0.715160, 0.072169), color); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
void main() |
| 53 |
{ |
| 54 |
|
| 55 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 56 |
// this is taken from default.frag |
| 57 |
vec3 n; |
| 58 |
float NdotL, NdotHV, fogFactor; |
| 59 |
vec4 color = gl_Color; |
| 60 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 61 |
vec3 halfVector = gl_LightSource[0].halfVector.xyz; |
| 62 |
vec4 texel; |
| 63 |
vec4 fragColor; |
| 64 |
vec4 specular = vec4(0.0); |
| 65 |
float intensity; |
| 66 |
|
| 67 |
float effective_scattering = min(scattering, cloud_self_shading); |
| 68 |
|
| 69 |
|
| 70 |
vec4 light_specular = gl_LightSource[0].specular; |
| 71 |
|
| 72 |
// If gl_Color.a == 0, this is a back-facing polygon and the |
| 73 |
// normal should be reversed. |
| 74 |
n = (2.0 * gl_Color.a - 1.0) * normal; |
| 75 |
n = normalize(n); |
| 76 |
|
| 77 |
NdotL = dot(n, lightDir); |
| 78 |
if (NdotL > 0.0) { |
| 79 |
float shadowmap = getShadowing(); |
| 80 |
color += diffuse_term * NdotL * shadowmap; |
| 81 |
NdotHV = max(dot(n, halfVector), 0.0); |
| 82 |
if (gl_FrontMaterial.shininess > 0.0) |
| 83 |
specular.rgb = (gl_FrontMaterial.specular.rgb |
| 84 |
* light_specular.rgb |
| 85 |
* pow(NdotHV, gl_FrontMaterial.shininess) |
| 86 |
* shadowmap); |
| 87 |
} |
| 88 |
color.a = diffuse_term.a; |
| 89 |
// This shouldn't be necessary, but our lighting becomes very |
| 90 |
// saturated. Clamping the color before modulating by the texture |
| 91 |
// is closer to what the OpenGL fixed function pipeline does. |
| 92 |
color = clamp(color, 0.0, 1.0); |
| 93 |
texel = texture2D(texture, gl_TexCoord[0].st); |
| 94 |
|
| 95 |
if (orthophotoAvailable) { |
| 96 |
vec4 sat_texel = texture2D(orthophotoTexture, gl_TexCoord[2].st); |
| 97 |
if (sat_texel.a > 0) { |
| 98 |
texel.rgb = sat_texel.rgb; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
fragColor = color * texel + specular; |
| 103 |
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
// here comes the terrain haze model |
| 108 |
|
| 109 |
|
| 110 |
float delta_z = hazeLayerAltitude - eye_alt; |
| 111 |
float dist = length(relPos); |
| 112 |
|
| 113 |
float mvisibility = min(visibility,avisibility); |
| 114 |
|
| 115 |
if (dist > 0.04 * mvisibility) |
| 116 |
{ |
| 117 |
|
| 118 |
alt = eye_alt; |
| 119 |
|
| 120 |
|
| 121 |
float transmission; |
| 122 |
float vAltitude; |
| 123 |
float delta_zv; |
| 124 |
float H; |
| 125 |
float distance_in_layer; |
| 126 |
float transmission_arg; |
| 127 |
|
| 128 |
// angle with horizon |
| 129 |
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist; |
| 130 |
|
| 131 |
|
| 132 |
// we solve the geometry what part of the light path is attenuated normally and what is through the haze layer |
| 133 |
|
| 134 |
if (delta_z > 0.0) // we're inside the layer |
| 135 |
{ |
| 136 |
if (ct < 0.0) // we look down |
| 137 |
{ |
| 138 |
distance_in_layer = dist; |
| 139 |
vAltitude = min(distance_in_layer,mvisibility) * ct; |
| 140 |
delta_zv = delta_z - vAltitude; |
| 141 |
} |
| 142 |
else // we may look through upper layer edge |
| 143 |
{ |
| 144 |
H = dist * ct; |
| 145 |
if (H > delta_z) {distance_in_layer = dist/H * delta_z;} |
| 146 |
else {distance_in_layer = dist;} |
| 147 |
vAltitude = min(distance_in_layer,visibility) * ct; |
| 148 |
delta_zv = delta_z - vAltitude; |
| 149 |
} |
| 150 |
} |
| 151 |
else // we see the layer from above, delta_z < 0.0 |
| 152 |
{ |
| 153 |
H = dist * -ct; |
| 154 |
if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading |
| 155 |
{ |
| 156 |
distance_in_layer = 0.0; |
| 157 |
delta_zv = 0.0; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
vAltitude = H + delta_z; |
| 162 |
distance_in_layer = vAltitude/H * dist; |
| 163 |
vAltitude = min(distance_in_layer,visibility) * (-ct); |
| 164 |
delta_zv = vAltitude; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
// ground haze cannot be thinner than aloft visibility in the model, |
| 170 |
// so we need to use aloft visibility otherwise |
| 171 |
|
| 172 |
|
| 173 |
transmission_arg = (dist-distance_in_layer)/avisibility; |
| 174 |
|
| 175 |
|
| 176 |
float eqColorFactor; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if (visibility < avisibility) |
| 181 |
{ |
| 182 |
transmission_arg = transmission_arg + (distance_in_layer/visibility); |
| 183 |
// this combines the Weber-Fechner intensity |
| 184 |
eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering); |
| 185 |
|
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
transmission_arg = transmission_arg + (distance_in_layer/avisibility); |
| 190 |
// this combines the Weber-Fechner intensity |
| 191 |
eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
transmission = fog_func(transmission_arg, alt); |
| 197 |
|
| 198 |
// there's always residual intensity, we should never be driven to zero |
| 199 |
if (eqColorFactor < 0.2) {eqColorFactor = 0.2;} |
| 200 |
|
| 201 |
|
| 202 |
float lightArg = (terminator-yprime_alt)/100000.0; |
| 203 |
vec3 hazeColor = get_hazeColor(lightArg); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
// now dim the light for haze |
| 208 |
eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt); |
| 209 |
|
| 210 |
// Mie-like factor |
| 211 |
|
| 212 |
if (lightArg < 10.0) |
| 213 |
{intensity = length(hazeColor); |
| 214 |
float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt)); |
| 215 |
hazeColor = intensity * ((1.0 - mie_magnitude) + mie_magnitude * mie_angle) * normalize(mix(hazeColor, vec3 (0.5, 0.58, 0.65), mie_magnitude * (0.5 - 0.5 * mie_angle)) ); |
| 216 |
} |
| 217 |
|
| 218 |
// high altitude desaturation of the haze color |
| 219 |
|
| 220 |
intensity = length(hazeColor); |
| 221 |
hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt))); |
| 222 |
|
| 223 |
// blue hue of haze |
| 224 |
|
| 225 |
hazeColor.x = hazeColor.x * 0.83; |
| 226 |
hazeColor.y = hazeColor.y * 0.9; |
| 227 |
|
| 228 |
|
| 229 |
// additional blue in indirect light |
| 230 |
float fade_out = max(0.65 - 0.3 *overcast, 0.45); |
| 231 |
intensity = length(hazeColor); |
| 232 |
hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) )); |
| 233 |
|
| 234 |
// change haze color to blue hue for strong fogging |
| 235 |
//intensity = length(hazeColor); |
| 236 |
hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor)))); |
| 237 |
|
| 238 |
|
| 239 |
// reduce haze intensity when looking at shaded surfaces, only in terminator region |
| 240 |
|
| 241 |
float shadow = mix( min(1.0 + dot(normal,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission)); |
| 242 |
hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator)); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
// don't let the light fade out too rapidly |
| 248 |
lightArg = (terminator + 200000.0)/100000.0; |
| 249 |
float minLightIntensity = min(0.2,0.16 * lightArg + 0.5); |
| 250 |
vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4); |
| 251 |
hazeColor *= eqColorFactor * eShade; |
| 252 |
hazeColor.rgb = max(hazeColor.rgb, minLight.rgb); |
| 253 |
|
| 254 |
// determine the right mix of transmission and haze |
| 255 |
|
| 256 |
fragColor.rgb = mix(hazeColor, fragColor.rgb,transmission); |
| 257 |
} |
| 258 |
|
| 259 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 260 |
|
| 261 |
gl_FragColor = fragColor; |
| 262 |
|
| 263 |
} |
| 264 |
|