| 1 |
// -*-C++-*- |
| 2 |
// © Vivian Meazza - 2011 |
| 3 |
// adapted to Atmospheric Light Scattering by Thorsten Renk 2013 |
| 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 |
|
| 13 |
#version 120 |
| 14 |
#define fps2kts 0.5925 |
| 15 |
|
| 16 |
#define MODE_OFF 0 |
| 17 |
#define MODE_DIFFUSE 1 |
| 18 |
#define MODE_AMBIENT_AND_DIFFUSE 2 |
| 19 |
|
| 20 |
// The ambient term of the lighting equation that doesn't depend on |
| 21 |
// the surface normal is passed in gl_{Front,Back}Color. The alpha |
| 22 |
// component is set to 1 for front, 0 for back in order to work around |
| 23 |
// bugs with gl_FrontFacing in the fragment shader. |
| 24 |
varying vec4 diffuse_term; |
| 25 |
varying vec3 normal; |
| 26 |
varying vec3 relPos; |
| 27 |
varying vec4 ecPosition; |
| 28 |
|
| 29 |
varying float yprime_alt; |
| 30 |
varying float mie_angle; |
| 31 |
|
| 32 |
|
| 33 |
uniform int colorMode; |
| 34 |
uniform float osg_SimulationTime; |
| 35 |
uniform float Offset, AmpFactor, WindE, WindN, spd, hdg; |
| 36 |
uniform sampler3D Noise; |
| 37 |
uniform float hazeLayerAltitude; |
| 38 |
uniform float terminator; |
| 39 |
uniform float terrain_alt; |
| 40 |
uniform float avisibility; |
| 41 |
uniform float visibility; |
| 42 |
uniform float overcast; |
| 43 |
uniform float ground_scattering; |
| 44 |
uniform float moonlight; |
| 45 |
|
| 46 |
// This is the value used in the skydome scattering shader - use the same here for consistency? |
| 47 |
const float EarthRadius = 5800000.0; |
| 48 |
const float terminator_width = 200000.0; |
| 49 |
|
| 50 |
float earthShade; |
| 51 |
|
| 52 |
void setupShadows(vec4 eyeSpacePos); |
| 53 |
|
| 54 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 55 |
{ |
| 56 |
//x = x - 0.5; |
| 57 |
|
| 58 |
// use the asymptotics to shorten computations |
| 59 |
if (x < -15.0) {return 0.0;} |
| 60 |
|
| 61 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 62 |
} |
| 63 |
|
| 64 |
float normalize_range(float _val) |
| 65 |
{ |
| 66 |
if (_val > 180.0) |
| 67 |
return _val - 360.0; |
| 68 |
else |
| 69 |
return _val; |
| 70 |
} |
| 71 |
|
| 72 |
void relWind(out float rel_wind_speed_kts, out float rel_wind_from_rad) |
| 73 |
{ |
| 74 |
//calculate speed north and east in kts |
| 75 |
float speed_north_kts = cos(radians(hdg)) * spd ; |
| 76 |
float speed_east_kts = sin(radians(hdg)) * spd ; |
| 77 |
|
| 78 |
//calculate the relative wind speed north and east in kts |
| 79 |
float rel_wind_speed_from_east_kts = WindE*fps2kts + speed_east_kts; |
| 80 |
float rel_wind_speed_from_north_kts = WindN*fps2kts + speed_north_kts; |
| 81 |
|
| 82 |
//combine relative speeds north and east to get relative windspeed in kts |
| 83 |
rel_wind_speed_kts = sqrt(pow(abs(rel_wind_speed_from_east_kts), 2.0) |
| 84 |
+ pow(abs(rel_wind_speed_from_north_kts), 2.0)); |
| 85 |
|
| 86 |
//calculate the relative wind direction |
| 87 |
float rel_wind_from_deg = degrees(atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts)); |
| 88 |
//rel_wind_from_rad = atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts); |
| 89 |
float rel_wind = rel_wind_from_deg - hdg; |
| 90 |
rel_wind = normalize_range(rel_wind); |
| 91 |
rel_wind_from_rad = radians(rel_wind); |
| 92 |
} |
| 93 |
|
| 94 |
void rotationmatrix(in float angle, out mat4 rotmat) |
| 95 |
{ |
| 96 |
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0, |
| 97 |
sin( angle ), cos( angle ), 0.0, 0.0, |
| 98 |
0.0 , 0.0 , 1.0, 0.0, |
| 99 |
0.0 , 0.0 , 0.0, 1.0 ); |
| 100 |
} |
| 101 |
|
| 102 |
void main() |
| 103 |
{ |
| 104 |
vec4 light_diffuse; |
| 105 |
vec4 light_ambient; |
| 106 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 107 |
vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight; |
| 108 |
|
| 109 |
|
| 110 |
float yprime; |
| 111 |
float lightArg; |
| 112 |
float intensity; |
| 113 |
float vertex_alt; |
| 114 |
float scattering; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
mat4 RotationMatrix; |
| 119 |
|
| 120 |
float relWindspd=0.0; |
| 121 |
float relWinddir=0.0; |
| 122 |
|
| 123 |
// compute relative wind speed and direction |
| 124 |
relWind (relWindspd, relWinddir); |
| 125 |
|
| 126 |
// map noise vector |
| 127 |
vec4 noisevec = texture3D(Noise, gl_Vertex.xyz); |
| 128 |
|
| 129 |
//waving effect |
| 130 |
float tsec = osg_SimulationTime; |
| 131 |
vec4 pos = gl_Vertex; |
| 132 |
vec4 oldpos = gl_Vertex; |
| 133 |
|
| 134 |
float freq = (10.0 * relWindspd) + 10.0; |
| 135 |
pos.y = sin((pos.x * 5.0 + tsec * freq )/5.0) * 0.5 ; |
| 136 |
pos.y += sin((pos.z * 5.0 + tsec * freq/2.0)/5.0) * 0.125 ; |
| 137 |
|
| 138 |
pos.y *= pow(pos.x - Offset, 2.0) * AmpFactor; |
| 139 |
|
| 140 |
//rotate the flag to align with relative wind |
| 141 |
rotationmatrix(-relWinddir, RotationMatrix); |
| 142 |
pos *= RotationMatrix; |
| 143 |
gl_Position = gl_ModelViewProjectionMatrix * pos; |
| 144 |
|
| 145 |
//do the colour and fog |
| 146 |
ecPosition = gl_ModelViewMatrix * gl_Vertex; |
| 147 |
|
| 148 |
|
| 149 |
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; |
| 150 |
normal = gl_NormalMatrix * gl_Normal; |
| 151 |
vec4 ambient_color, diffuse_color; |
| 152 |
|
| 153 |
if (colorMode == MODE_DIFFUSE) { |
| 154 |
diffuse_color = gl_Color; |
| 155 |
ambient_color = gl_FrontMaterial.ambient; |
| 156 |
} else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) { |
| 157 |
diffuse_color = gl_Color; |
| 158 |
ambient_color = gl_Color; |
| 159 |
} else { |
| 160 |
diffuse_color = gl_FrontMaterial.diffuse; |
| 161 |
ambient_color = gl_FrontMaterial.ambient; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
// first current altitude of eye position in model space |
| 166 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 167 |
|
| 168 |
// and relative position to vector |
| 169 |
relPos = gl_Vertex.xyz - ep.xyz; |
| 170 |
|
| 171 |
// unfortunately, we need the distance in the vertex shader, although the more accurate version |
| 172 |
// is later computed in the fragment shader again |
| 173 |
float dist = length(relPos); |
| 174 |
|
| 175 |
// altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m |
| 176 |
vertex_alt = max(gl_Vertex.z,100.0); |
| 177 |
scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt); |
| 178 |
|
| 179 |
|
| 180 |
// branch dependent on daytime |
| 181 |
|
| 182 |
if (terminator < 1000000.0) // the full, sunrise and sunset computation |
| 183 |
{ |
| 184 |
|
| 185 |
|
| 186 |
// establish coordinates relative to sun position |
| 187 |
|
| 188 |
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz; |
| 189 |
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0)); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
// yprime is the distance of the vertex into sun direction |
| 194 |
yprime = -dot(relPos, lightHorizon); |
| 195 |
|
| 196 |
// this gets an altitude correction, higher terrain gets to see the sun earlier |
| 197 |
yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt); |
| 198 |
|
| 199 |
// two times terminator width governs how quickly light fades into shadow |
| 200 |
// now the light-dimming factor |
| 201 |
earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4; |
| 202 |
|
| 203 |
// parametrized version of the Flightgear ground lighting function |
| 204 |
lightArg = (terminator-yprime_alt)/100000.0; |
| 205 |
|
| 206 |
// directional scattering for low sun |
| 207 |
if (lightArg < 10.0) |
| 208 |
{mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;} |
| 209 |
else |
| 210 |
{mie_angle = 1.0;} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0); |
| 216 |
light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 217 |
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); |
| 218 |
light_diffuse.a = 1.0; |
| 219 |
light_diffuse = light_diffuse * scattering; |
| 220 |
|
| 221 |
|
| 222 |
light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33); |
| 223 |
light_ambient.g = light_ambient.r * 0.4/0.33; |
| 224 |
light_ambient.b = light_ambient.r * 0.5/0.33; |
| 225 |
light_ambient.a = 1.0; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
// correct ambient light intensity and hue before sunrise |
| 231 |
if (earthShade < 0.5) |
| 232 |
{ |
| 233 |
//light_ambient = light_ambient * (0.7 + 0.3 * smoothstep(0.2, 0.5, earthShade)); |
| 234 |
intensity = length(light_ambient.xyz); |
| 235 |
|
| 236 |
light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade) )); |
| 237 |
light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade)); |
| 238 |
|
| 239 |
intensity = length(light_diffuse.xyz); |
| 240 |
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.7,earthShade) )); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
// the haze gets the light at the altitude of the haze top if the vertex in view is below |
| 245 |
// but the light at the vertex if the vertex is above |
| 246 |
|
| 247 |
vertex_alt = max(vertex_alt,hazeLayerAltitude); |
| 248 |
|
| 249 |
if (vertex_alt > hazeLayerAltitude) |
| 250 |
{ |
| 251 |
if (dist > 0.8 * avisibility) |
| 252 |
{ |
| 253 |
vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist)); |
| 254 |
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); |
| 255 |
} |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
vertex_alt = hazeLayerAltitude; |
| 260 |
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
else // the faster, full-day version without lightfields |
| 265 |
{ |
| 266 |
//vertex_alt = max(gl_Vertex.z,100.0); |
| 267 |
|
| 268 |
earthShade = 1.0; |
| 269 |
mie_angle = 1.0; |
| 270 |
|
| 271 |
if (terminator > 3000000.0) |
| 272 |
{light_diffuse = vec4 (1.0, 1.0, 1.0, 0.0); |
| 273 |
light_ambient = vec4 (0.33, 0.4, 0.5, 0.0); } |
| 274 |
else |
| 275 |
{ |
| 276 |
|
| 277 |
lightArg = (terminator/100000.0 - 10.0)/20.0; |
| 278 |
light_diffuse.b = 0.78 + lightArg * 0.21; |
| 279 |
light_diffuse.g = 0.907 + lightArg * 0.091; |
| 280 |
light_diffuse.r = 0.904 + lightArg * 0.092; |
| 281 |
light_diffuse.a = 1.0; |
| 282 |
|
| 283 |
light_ambient.r = 0.316 + lightArg * 0.016; |
| 284 |
light_ambient.g = light_ambient.r * 0.4/0.33; |
| 285 |
light_ambient.b = light_ambient.r * 0.5/0.33; |
| 286 |
light_ambient.a = 1.0; |
| 287 |
} |
| 288 |
|
| 289 |
light_diffuse = light_diffuse * scattering; |
| 290 |
yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
diffuse_term = diffuse_color * light_diffuse; |
| 298 |
vec4 ambient_term = ambient_color * light_ambient; |
| 299 |
|
| 300 |
// Super hack: if diffuse material alpha is less than 1, assume a |
| 301 |
// transparency animation is at work |
| 302 |
if (gl_FrontMaterial.diffuse.a < 1.0) |
| 303 |
diffuse_term.a = gl_FrontMaterial.diffuse.a; |
| 304 |
else |
| 305 |
diffuse_term.a = gl_Color.a; |
| 306 |
|
| 307 |
// Another hack for supporting two-sided lighting without using |
| 308 |
// gl_FrontFacing in the fragment shader. |
| 309 |
gl_FrontColor.rgb = ambient_term.rgb; gl_FrontColor.a = 0.0; |
| 310 |
gl_BackColor.rgb = ambient_term.rgb; gl_FrontColor.a = 1.0; |
| 311 |
// fogCoord = abs(ecPosition.z / ecPosition.w); |
| 312 |
|
| 313 |
//fog_Func(fogType); |
| 314 |
setupShadows(ecPosition); |
| 315 |
} |