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