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;
16 varying vec3 viewerdir;
17 varying vec3 lightdir;
20 varying float earthShade;
21 varying float yprime_alt;
22 varying float mie_angle;
24 uniform float osg_SimulationTime;
25 uniform float WindE, WindN;
26 uniform float hazeLayerAltitude;
27 uniform float terminator;
28 uniform float terrain_alt;
29 uniform float avisibility;
30 uniform float visibility;
31 uniform float overcast;
32 uniform float ground_scattering;
34 uniform mat4 osg_ViewMatrixInverse;
38 // This is the value used in the skydome scattering shader - use the same here for consistency?
39 const float EarthRadius = 5800000.0;
40 const float terminator_width = 200000.0;
42 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
46 // use the asymptotics to shorten computations
47 if (x < -15.0) {return 0.0;}
49 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
53 ////fog "include"////////
54 // uniform int fogType;
56 // void fog_Func(int type);
57 /////////////////////////
59 /////// functions /////////
61 void rotationmatrix(in float angle, out mat4 rotmat)
63 rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
64 sin( angle ), cos( angle ), 0.0, 0.0,
66 0.0 , 0.0 , 0.0, 1.0 );
75 vec3 shadedFogColor = vec3(0.65, 0.67, 0.78);
76 rawPos = (osg_ViewMatrixInverse *gl_ModelViewMatrix * gl_Vertex).xyz;
78 vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
80 viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex);
81 lightdir = normalize(vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position));
83 waterTex4 = vec4( ecPosition.xzy, 0.0 );
85 vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
86 vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
90 float windFactor = sqrt(WindE * WindE + WindN * WindN) * 0.05;
91 if (WindN == 0.0 && WindE == 0.0) {
94 Angle = atan(-WindN, WindE) - atan(1.0);
97 rotationmatrix(Angle, RotationMatrix);
98 waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
100 rotationmatrix(Angle, RotationMatrix);
101 waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
103 // fog_Func(fogType);
104 gl_Position = ftransform();
108 // here start computations for the haze layer
117 // we need several geometrical quantities
119 // first current altitude of eye position in model space
120 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
122 // and relative position to vector
123 relPos = gl_Vertex.xyz - ep.xyz;
125 // unfortunately, we need the distance in the vertex shader, although the more accurate version
126 // is later computed in the fragment shader again
127 float dist = length(relPos);
130 // altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
131 vertex_alt = max(gl_Vertex.z,100.0);
132 scattering = 0.5 + 0.5 * ground_scattering + 0.5* (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
134 // branch dependent on daytime
136 if (terminator < 1000000.0) // the full, sunrise and sunset computation
140 // establish coordinates relative to sun position
142 //vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
143 //vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
144 vec3 lightHorizon = normalize(vec3(lightdir.x,lightdir.y, 0.0));
147 // yprime is the distance of the vertex into sun direction
148 yprime = -dot(relPos, lightHorizon);
150 // this gets an altitude correction, higher terrain gets to see the sun earlier
151 yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
153 // two times terminator width governs how quickly light fades into shadow
154 // now the light-dimming factor
155 earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
157 // parametrized version of the Flightgear ground lighting function
158 lightArg = (terminator-yprime_alt)/100000.0;
160 specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
161 specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
162 specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
164 specular_light = max(specular_light * scattering, vec3 (0.05, 0.05, 0.05));
166 intensity = length(specular_light.rgb);
167 specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,ground_scattering) ));
169 // correct ambient light intensity and hue before sunrise - seems unnecessary and create artefacts though...
170 //if (earthShade < 0.5)
172 //specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.7,earthShade) ));
175 // directional scattering for low sun
177 {mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;}
185 // the haze gets the light at the altitude of the haze top if the vertex in view is below
186 // but the light at the vertex if the vertex is above
188 vertex_alt = max(vertex_alt,hazeLayerAltitude);
190 if (vertex_alt > hazeLayerAltitude)
192 if (dist > 0.8 * avisibility)
194 vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
195 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
200 vertex_alt = hazeLayerAltitude;
201 yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
205 else // the faster, full-day version without lightfields
207 //vertex_alt = max(gl_Vertex.z,100.0);
212 if (terminator > 3000000.0)
213 {specular_light = vec3 (1.0, 1.0, 1.0);}
217 lightArg = (terminator/100000.0 - 10.0)/20.0;
218 specular_light.b = 0.78 + lightArg * 0.21;
219 specular_light.g = 0.907 + lightArg * 0.091;
220 specular_light.r = 0.904 + lightArg * 0.092;
223 specular_light = specular_light * scattering;
225 yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
229 gl_FrontColor.rgb = specular_light;
230 gl_BackColor.rgb = gl_FrontColor.rgb;