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
10#version 120
11#define fps2kts 0.5925
12
13uniform sampler2D water_normalmap;
14uniform sampler2D water_reflection;
15uniform sampler2D water_dudvmap;
16uniform sampler2D water_reflection_grey;
17uniform sampler2D sea_foam;
18uniform sampler2D alpha_tex;
19uniform sampler2D bowwave_nmap;
20
21uniform float saturation, Overcast, WindE, WindN, spd, hdg;
22uniform float CloudCover0, CloudCover1, CloudCover2, CloudCover3, CloudCover4;
23uniform int 	Status;
24
25varying vec4 waterTex1; //moving texcoords
26varying vec4 waterTex2; //moving texcoords
27varying vec3 viewerdir;
28varying vec3 lightdir;
29varying vec3 normal;
30
31////fog "include" /////
32uniform int fogType;
33
34vec3 fog_Func(vec3 color, int type);
35//////////////////////
36
37/////// functions /////////
38
39float normalize_range(float _val)
40    {
41    if (_val > 180.0)
42        return _val - 360.0;
43    else
44        return _val;
45    }
46
47
48void relWind(out float rel_wind_speed_kts, out float rel_wind_from_rad)
49    {
50    //calculate the carrier speed north and east in kts
51    float speed_north_kts = cos(radians(hdg)) * spd ;
52    float speed_east_kts  = sin(radians(hdg)) * spd ;
53
54    //calculate the relative wind speed north and east in kts
55    float rel_wind_speed_from_east_kts = WindE*fps2kts + speed_east_kts;
56    float rel_wind_speed_from_north_kts = WindN*fps2kts + speed_north_kts;
57
58    //combine relative speeds north and east to get relative windspeed in kts
59    rel_wind_speed_kts = sqrt(rel_wind_speed_from_east_kts*rel_wind_speed_from_east_kts
60        + rel_wind_speed_from_north_kts*rel_wind_speed_from_north_kts);
61
62    //calculate the relative wind direction
63    float rel_wind_from_deg = degrees(atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts));
64    // rel_wind_from_rad = atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts);
65    float rel_wind = rel_wind_from_deg - hdg;
66    rel_wind = normalize_range(rel_wind);
67    rel_wind_from_rad = radians(rel_wind);
68    }
69
70void rotationmatrix(in float angle, out mat4 rotmat)
71    {
72    rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
73        sin( angle ),  cos( angle ), 0.0, 0.0,
74        0.0         ,  0.0         , 1.0, 0.0,
75        0.0         ,  0.0         , 0.0, 1.0 );
76    }
77
78//////////////////////
79
80void main(void)
81    {
82    const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
83    const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
84    const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
85
86    mat4 RotationMatrix;
87
88    float relWindspd=0;
89    float relWinddir=0;
90
91    // compute relative wind speed and direction
92    relWind (relWindspd, relWinddir);
93
94    rotationmatrix(relWinddir, RotationMatrix);
95
96    // compute direction to viewer
97    vec3 E = normalize(viewerdir);
98
99    // compute direction to light source
100    vec3 L = normalize(lightdir);
101
102    // half vector
103    vec3 H = normalize(L + E);
104
105    const float water_shininess = 240.0;
106    // approximate cloud cover
107    float cover = 0.0;
108    //bool Status = true;
109
110    float windEffect = relWindspd;                                              //wind speed in kt
111    //    float windEffect = sqrt(pow(abs(WindE),2)+pow(abs(WindN),2)) * 0.6;       //wind speed in kt
112    float windScale = 15.0/(5.0 + windEffect);                                  //wave scale
113    float waveRoughness = 0.05 + smoothstep(0.0, 50.0, windEffect);             //wave roughness filter
114
115
116    if (Status == 1){
117        cover = min(min(min(min(CloudCover0, CloudCover1),CloudCover2),CloudCover3),CloudCover4);
118        } else {
119            // hack to allow for Overcast not to be set by Local Weather
120
121            if (Overcast == 0){
122                cover = 5;
123                } else {
124                    cover = Overcast * 5;
125                }
126
127        }
128
129    //vec4 viewt = normalize(waterTex4);
130    vec4 viewt = vec4(-E, 0.0) * 0.6;
131
132    vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)* windScale * 2.0) * 2.0 - 1.0;
133    vec4 dist   = texture2D(water_dudvmap, vec2(waterTex1 + disdis*sca2)* windScale * 2.0) * 2.0 - 1.0;
134    vec4 fdist  = normalize(dist);
135    fdist = -fdist;
136    fdist *= sca;
137
138    //normalmap
139    rotationmatrix(-relWinddir, RotationMatrix);
140
141    vec4 nmap0 = texture2D(water_normalmap, vec2((waterTex1 + disdis*sca2) * RotationMatrix ) * windScale * 2.0) * 2.0 - 1.0;
142    vec4 nmap2 = texture2D(water_normalmap, vec2(waterTex2 * tscale * RotationMatrix ) * windScale * 2.0) * 2.0 - 1.0;
143    vec4 nmap3 = texture2D(bowwave_nmap, gl_TexCoord[0].st) * 2.0 - 1.0;
144    vec4 vNorm = normalize(mix(nmap3, nmap0 + nmap2, 0.3 )* waveRoughness);
145    vNorm = -vNorm;
146
147	//load reflection
148    vec4 tmp = vec4(lightdir, 0.0);
149    vec4 refTex = texture2D(water_reflection, vec2(tmp + waterTex1) * 32.0) ;
150    vec4 refTexGrey = texture2D(water_reflection_grey, vec2(tmp + waterTex1) * 32.0) ;
151    vec4 refl ;
152	//    cover = 0;
153
154	if(cover >= 1.5){
155		refl= normalize(refTex);
156		}
157	else
158		{
159		refl = normalize(refTexGrey);
160		refl.r *= (0.75 + 0.15 * cover);
161		refl.g *= (0.80 + 0.15 * cover);
162		refl.b *= (0.875 + 0.125 * cover);
163		refl.a  *= 1.0;
164		}
165
166    vec3 N0 = vec3(texture2D(water_normalmap, vec2((waterTex1 + disdis*sca2)* RotationMatrix) * windScale * 2.0) * 2.0 - 1.0);
167    vec3 N1 = vec3(texture2D(water_normalmap, vec2(waterTex2 * tscale * RotationMatrix ) * windScale * 2.0) * 2.0 - 1.0);
168    vec3 N2 = vec3(texture2D(bowwave_nmap, gl_TexCoord[0].st)*2.0-1.0);
169    //vec3 Nf = normalize((normal+N0+N1)*waveRoughness);
170    vec3 N  = normalize(mix(normal+N2, normal+N0+N1, 0.3)* waveRoughness);
171    N  = -N;
172
173    // specular
174    vec3 specular_color = vec3(gl_LightSource[0].diffuse)
175        * pow(max(0.0, dot(N, H)), water_shininess) * 6.0;
176    vec4 specular = vec4(specular_color, 0.5);
177
178    specular = specular * saturation * 0.3;
179
180    //calculate fresnel
181    vec4 invfres = vec4( dot(vNorm, viewt) );
182    vec4 fres = vec4(1.0) + invfres;
183    refl *= fres;
184
185    vec4 alpha0 = texture2D(alpha_tex, gl_TexCoord[0].st);
186
187    //calculate final colour
188    vec4 ambient_light = gl_LightSource[0].diffuse;
189    vec4 finalColor;
190
191    //    cover = 0;
192
193    if(cover >= 1.5){
194        finalColor = refl + specular;
195        } else {
196            finalColor = refl;
197        }
198
199    //add foam
200
201    float foamSlope = 0.05 + 0.01 * windScale;
202    //float waveSlope = mix(N0.g, N1.g, 0.25);
203
204    vec4 foam_texel = texture2D(sea_foam, vec2(waterTex2 * tscale) * 50.0);
205    float waveSlope = N.g;
206
207    if (windEffect >= 12.0)
208        if (waveSlope >= foamSlope){
209            finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel), smoothstep(foamSlope, 0.5, N.g));
210            }
211
212    //generate final colour
213        finalColor *= ambient_light+ alpha0 * 0.35;
214        finalColor.rgb = fog_Func(finalColor.rgb, fogType);
215        gl_FragColor = vec4(finalColor.rgb, alpha0.a * 1.35);
216
217    }
218