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
10 varying vec4 waterTex1;
11 varying vec4 waterTex2;
12 varying vec4 waterTex4;
13 //varying vec4 ecPosition;
15 varying vec3 specular_light;
17 varying vec3 viewerdir;
18 varying vec3 lightdir;
19 //varying vec3 normal;
21 varying float earthShade;
22 varying float yprime_alt;
23 varying float mie_angle;
25 uniform float osg_SimulationTime;
26 uniform float WindE, WindN;
28 uniform float hazeLayerAltitude;
29 uniform float terminator;
30 uniform float terrain_alt;
31 uniform float avisibility;
32 uniform float visibility;
33 uniform float overcast;
34 uniform float ground_scattering;
36 // This is the value used in the skydome scattering shader - use the same here for consistency?
37 const float EarthRadius = 5800000.0;
38 const float terminator_width = 200000.0;
40 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
44 // use the asymptotics to shorten computations
45 if (x < -15.0) {return 0.0;}
47 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
51 ////fog "include"////////
52 // uniform int fogType;
54 // void fog_Func(int type);
55 /////////////////////////
57 /////// functions /////////
59 void rotationmatrix(in float angle, out mat4 rotmat)
61 rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
62 sin( angle ), cos( angle ), 0.0, 0.0,
64 0.0 , 0.0 , 0.0, 1.0 );
71 // vec3 N = normalize(gl_Normal);
74 vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
76 viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex);
77 lightdir = normalize(vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position));
79 waterTex4 = vec4( ecPosition.xzy, 0.0 );
81 vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
82 vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
86 float windFactor = sqrt(pow(abs(WindE),2)+pow(abs(WindN),2)) * 0.05;
87 if (WindN == 0.0 && WindE == 0.0) {
90 Angle = atan(-WindN, WindE) - atan(1.0);
93 rotationmatrix(Angle, RotationMatrix);
94 waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
96 rotationmatrix(Angle, RotationMatrix);
97 waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
100 gl_Position = ftransform();
104 // here start computations for the haze layer
113 // we need several geometrical quantities
115 // first current altitude of eye position in model space
116 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
118 // and relative position to vector
119 relPos = gl_Vertex.xyz - ep.xyz;
121 // unfortunately, we need the distance in the vertex shader, although the more accurate version
122 // is later computed in the fragment shader again
123 float dist = length(relPos);
126 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
127 vertex_alt = max(gl_Vertex.z,100.0);
128 scattering = 0.5 + 0.5 * ground_scattering + 0.5* (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
130 // branch dependent on daytime
132 if (terminator < 1000000.0) // the full, sunrise and sunset computation
136 // establish coordinates relative to sun position
138 //vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
139 //vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
140 vec3 lightHorizon = normalize(vec3(lightdir.x,lightdir.y, 0.0));
143 // yprime is the distance of the vertex into sun direction
144 yprime = -dot(relPos, lightHorizon);
146 // this gets an altitude correction, higher terrain gets to see the sun earlier
147 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
149 // two times terminator width governs how quickly light fades into shadow
150 // now the light-dimming factor
151 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
153 // parametrized version of the Flightgear ground lighting function
154 lightArg = (terminator-yprime_alt)/100000.0;
156 specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
157 specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
158 specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
160 specular_light = specular_light * scattering;
162 // correct ambient light intensity and hue before sunrise
163 if (earthShade < 0.5)
165 intensity = length(specular_light.rgb);
166 specular_light.xyz = intensity * normalize(mix(specular_light.xyz, vec3 (0.45, 0.6, 0.8), 1.0 -smoothstep(0.1, 0.5,earthShade) ));
169 // directional scattering for low sun
171 //{mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
172 {mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;}
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
183 vertex_alt = max(vertex_alt,hazeLayerAltitude);
185 if (vertex_alt > hazeLayerAltitude)
187 if (dist > 0.8 * avisibility)
189 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
190 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
195 vertex_alt = hazeLayerAltitude;
196 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
200 else // the faster, full-day version without lightfields
202 //vertex_alt = max(gl_Vertex.z,100.0);
207 if (terminator > 3000000.0)
208 {specular_light = vec3 (1.0, 1.0, 1.0);}
212 lightArg = (terminator/100000.0 - 10.0)/20.0;
213 specular_light.b = 0.78 + lightArg * 0.21;
214 specular_light.g = 0.907 + lightArg * 0.091;
215 specular_light.r = 0.904 + lightArg * 0.092;
218 specular_light = specular_light * scattering;
220 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);