| 1 |
// This shader is mostly an adaptation of the shader found at |
| 2 |
// http://www.bonzaisoftware.com/water_tut.html and its glsl conversion |
| 3 |
// available at http://forum.bonzaisoftware.com/viewthread.php?tid=10 |
| 4 |
// © Michael Horsch - 2005 |
| 5 |
// Major update and revisions - 2011-10-07 |
| 6 |
// © Emilian Huminiuc and Vivian Meazza |
| 7 |
// Optimisation - 2012-5-05 |
| 8 |
// © Emilian Huminiuc and Vivian Meazza |
| 9 |
// Ported to the Atmospheric Light Scattering Framework |
| 10 |
// by Thorsten Renk, Aug. 2013 |
| 11 |
|
| 12 |
#version 120 |
| 13 |
#define fps2kts 0.5925 |
| 14 |
|
| 15 |
varying vec4 waterTex1; |
| 16 |
varying vec4 waterTex2; |
| 17 |
varying vec3 relPos; |
| 18 |
varying vec3 rawPos; |
| 19 |
varying vec3 viewerdir; |
| 20 |
varying vec3 lightdir; |
| 21 |
varying vec3 normal; |
| 22 |
|
| 23 |
varying float steepness; |
| 24 |
varying float earthShade; |
| 25 |
varying float yprime_alt; |
| 26 |
varying float mie_angle; |
| 27 |
|
| 28 |
uniform float osg_SimulationTime; |
| 29 |
uniform float WindE, WindN, spd, hdg; |
| 30 |
uniform float hazeLayerAltitude; |
| 31 |
uniform float terminator; |
| 32 |
uniform float terrain_alt; |
| 33 |
uniform float avisibility; |
| 34 |
uniform float visibility; |
| 35 |
uniform float overcast; |
| 36 |
uniform float ground_scattering; |
| 37 |
|
| 38 |
uniform mat4 osg_ViewMatrixInverse; |
| 39 |
|
| 40 |
vec3 specular_light; |
| 41 |
|
| 42 |
// This is the value used in the skydome scattering shader - use the same here for consistency? |
| 43 |
const float EarthRadius = 5800000.0; |
| 44 |
const float terminator_width = 200000.0; |
| 45 |
|
| 46 |
///////////////////////// |
| 47 |
|
| 48 |
|
| 49 |
/////// functions ///////// |
| 50 |
|
| 51 |
void relWind(out float rel_wind_speed_kts, float rel_wind_from_deg) |
| 52 |
{ |
| 53 |
//calculate the carrier speed north and east in kts |
| 54 |
float speed_north_kts = cos(radians(hdg)) * spd ; |
| 55 |
float speed_east_kts = sin(radians(hdg)) * spd ; |
| 56 |
|
| 57 |
//calculate the relative wind speed north and east in kts |
| 58 |
float rel_wind_speed_from_east_kts = WindE*fps2kts + speed_east_kts; |
| 59 |
float rel_wind_speed_from_north_kts = WindN*fps2kts + speed_north_kts; |
| 60 |
|
| 61 |
//combine relative speeds north and east to get relative windspeed in kts |
| 62 |
rel_wind_speed_kts = sqrt((rel_wind_speed_from_east_kts * rel_wind_speed_from_east_kts) + (rel_wind_speed_from_north_kts * rel_wind_speed_from_north_kts)); |
| 63 |
|
| 64 |
//calculate the relative wind direction |
| 65 |
rel_wind_from_deg = degrees(atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts)); |
| 66 |
} |
| 67 |
|
| 68 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 69 |
{ |
| 70 |
//x = x - 0.5; |
| 71 |
|
| 72 |
// use the asymptotics to shorten computations |
| 73 |
if (x < -15.0) {return 0.0;} |
| 74 |
|
| 75 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
void main(void) |
| 81 |
{ |
| 82 |
|
| 83 |
float relWindspd=0; |
| 84 |
float relWinddir=0; |
| 85 |
//compute relative wind speed and direction |
| 86 |
relWind (relWindspd, relWinddir); |
| 87 |
|
| 88 |
vec3 N = normalize(gl_Normal); |
| 89 |
normal = N; |
| 90 |
|
| 91 |
viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex); |
| 92 |
lightdir = normalize(vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position)); |
| 93 |
|
| 94 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 95 |
rawPos = (osg_ViewMatrixInverse *gl_ModelViewMatrix * gl_Vertex).xyz; |
| 96 |
|
| 97 |
vec4 t1 = vec4(osg_SimulationTime*0.005217, 0.0, 0.0, 0.0); |
| 98 |
vec4 t2 = vec4(osg_SimulationTime*-0.0012, 0.0, 0.0, 0.0); |
| 99 |
|
| 100 |
float windFactor = -relWindspd * 0.1; |
| 101 |
// float windFactor = sqrt(pow(abs(WindE),2)+pow(abs(WindN),2)) * 0.6; |
| 102 |
|
| 103 |
waterTex1 = gl_MultiTexCoord0 + t1 * windFactor; |
| 104 |
waterTex2 = gl_MultiTexCoord0 + t2 * windFactor; |
| 105 |
|
| 106 |
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; |
| 107 |
gl_Position = ftransform(); |
| 108 |
|
| 109 |
// here start computations for the haze layer |
| 110 |
|
| 111 |
|
| 112 |
float yprime; |
| 113 |
float lightArg; |
| 114 |
float intensity; |
| 115 |
float vertex_alt; |
| 116 |
float scattering; |
| 117 |
|
| 118 |
// we need several geometrical quantities |
| 119 |
|
| 120 |
// first current altitude of eye position in model space |
| 121 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 122 |
|
| 123 |
// and relative position to vector |
| 124 |
relPos = gl_Vertex.xyz - ep.xyz; |
| 125 |
|
| 126 |
|
| 127 |
// unfortunately, we need the distance in the vertex shader, although the more accurate version |
| 128 |
// is later computed in the fragment shader again |
| 129 |
float dist = length(relPos); |
| 130 |
|
| 131 |
|
| 132 |
// altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m |
| 133 |
vertex_alt = max(gl_Vertex.z,100.0); |
| 134 |
scattering = 0.5 + 0.5 * ground_scattering + 0.5* (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt); |
| 135 |
|
| 136 |
// branch dependent on daytime |
| 137 |
|
| 138 |
if (terminator < 1000000.0) // the full, sunrise and sunset computation |
| 139 |
{ |
| 140 |
|
| 141 |
|
| 142 |
// establish coordinates relative to sun position |
| 143 |
vec3 lightHorizon = normalize(vec3(lightdir.x,lightdir.y, 0.0)); |
| 144 |
|
| 145 |
// yprime is the distance of the vertex into sun direction |
| 146 |
yprime = -dot(relPos, lightHorizon); |
| 147 |
|
| 148 |
// this gets an altitude correction, higher terrain gets to see the sun earlier |
| 149 |
yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt); |
| 150 |
|
| 151 |
// two times terminator width governs how quickly light fades into shadow |
| 152 |
// now the light-dimming factor |
| 153 |
earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4; |
| 154 |
|
| 155 |
// parametrized version of the Flightgear ground lighting function |
| 156 |
lightArg = (terminator-yprime_alt)/100000.0; |
| 157 |
|
| 158 |
specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0); |
| 159 |
specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 160 |
specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); |
| 161 |
|
| 162 |
specular_light = max(specular_light * scattering, vec3 (0.05, 0.05, 0.05)); |
| 163 |
|
| 164 |
intensity = length(specular_light.rgb); |
| 165 |
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,ground_scattering) )); |
| 166 |
|
| 167 |
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.5, 0.7,earthShade))); |
| 168 |
|
| 169 |
|
| 170 |
// directional scattering for low sun |
| 171 |
if (lightArg < 10.0) |
| 172 |
{mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;} |
| 173 |
else |
| 174 |
{mie_angle = 1.0;} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
// the haze gets the light at the altitude of the haze top if the vertex in view is below |
| 181 |
// but the light at the vertex if the vertex is above |
| 182 |
|
| 183 |
vertex_alt = max(vertex_alt,hazeLayerAltitude); |
| 184 |
|
| 185 |
if (vertex_alt > hazeLayerAltitude) |
| 186 |
{ |
| 187 |
if (dist > 0.8 * avisibility) |
| 188 |
{ |
| 189 |
vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist)); |
| 190 |
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); |
| 191 |
} |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
vertex_alt = hazeLayerAltitude; |
| 196 |
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); |
| 197 |
} |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
else // the faster, full-day version without lightfields |
| 202 |
|
| 203 |
{ |
| 204 |
//vertex_alt = max(gl_Vertex.z,100.0); |
| 205 |
|
| 206 |
earthShade = 1.0; |
| 207 |
mie_angle = 1.0; |
| 208 |
|
| 209 |
if (terminator > 3000000.0) |
| 210 |
{specular_light = vec3 (1.0, 1.0, 1.0);} |
| 211 |
else |
| 212 |
{ |
| 213 |
|
| 214 |
lightArg = (terminator/100000.0 - 10.0)/20.0; |
| 215 |
specular_light.b = 0.78 + lightArg * 0.21; |
| 216 |
specular_light.g = 0.907 + lightArg * 0.091; |
| 217 |
specular_light.r = 0.904 + lightArg * 0.092; |
| 218 |
} |
| 219 |
|
| 220 |
specular_light = specular_light * scattering; |
| 221 |
|
| 222 |
yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
gl_FrontColor.rgb = specular_light; |
| 227 |
gl_BackColor.rgb = gl_FrontColor.rgb; |
| 228 |
|
| 229 |
} |