Make water efect use png textures by default, and rely on their dds counterparts...
[fg:toms-fgdata.git] / Shaders / water_sine.frag
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 //  Based on ideas by Thorsten Renk
9 //  © Emilian Huminiuc and Vivian Meazza
10
11 #version 120
12
13 uniform sampler2D water_normalmap;
14 uniform sampler2D water_reflection;
15 uniform sampler2D water_dudvmap;
16 uniform sampler2D water_reflection_grey;
17 uniform sampler2D sea_foam;
18 uniform sampler2D perlin_normalmap;
19
20 uniform sampler3D Noise;
21
22 uniform float saturation, Overcast, WindE, WindN;
23 uniform float CloudCover0, CloudCover1, CloudCover2, CloudCover3, CloudCover4;
24 uniform float osg_SimulationTime;
25 uniform int Status;
26
27 varying vec4 waterTex1; //moving texcoords
28 varying vec4 waterTex2; //moving texcoords
29
30 varying vec3 viewerdir;
31 varying vec3 lightdir;
32 varying vec3 normal;
33
34 uniform    float WaveFreq ;
35 uniform    float WaveAmp ;
36 uniform    float WaveSharp ;
37 uniform    float WaveAngle ;
38 uniform    float WaveFactor ;
39 uniform    float WaveDAngle ;
40 uniform    float normalmap_dds;
41
42 ////fog "include" /////
43 uniform int fogType;
44
45 vec3 fog_Func(vec3 color, int type);
46 //////////////////////
47
48 const vec4 AllOnes = vec4(1.0);
49
50 /////// functions /////////
51
52 void rotationmatrix(in float angle, out mat4 rotmat)
53         {
54         rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
55                 sin( angle ),  cos( angle ), 0.0, 0.0,
56                 0.0         ,  0.0         , 1.0, 0.0,
57                 0.0         ,  0.0         , 0.0, 1.0 );
58         }
59
60 // wave functions ///////////////////////
61
62 struct Wave {
63         float freq;  // 2*PI / wavelength
64         float amp;   // amplitude
65         float phase; // speed * 2*PI / wavelength
66         vec2 dir;
67         };
68
69 Wave wave0 = Wave(1.0, 1.0, 0.5, vec2(0.97, 0.25));
70 Wave wave1 = Wave(2.0, 0.5, 1.3, vec2(0.97, -0.25));
71 Wave wave2 = Wave(1.0, 1.0, 0.6, vec2(0.95, -0.3));
72 Wave wave3 = Wave(2.0, 0.5, 1.4, vec2(0.99, 0.1));
73
74 float evaluateWave(in Wave w, vec2 pos, float t)
75         {
76         return w.amp * sin( dot(w.dir, pos) * w.freq + t * w.phase);
77         }
78
79 // derivative of wave function
80 float evaluateWaveDeriv(Wave w, vec2 pos, float t)
81         {
82         return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t*w.phase);
83         }
84
85 // sharp wave functions
86 float evaluateWaveSharp(Wave w, vec2 pos, float t, float k)
87         {
88         return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);
89         }
90
91 float evaluateWaveDerivSharp(Wave w, vec2 pos, float t, float k)
92         {
93         return k*w.freq*w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k - 1) * cos( dot(w.dir, pos)*w.freq + t*w.phase);
94         }
95
96 void sumWaves(float angle, float dangle, float windScale, float factor, out float ddx, float ddy)
97         {
98         mat4 RotationMatrix;
99         float deriv;
100         vec4 P = waterTex1 * 1024.0;
101
102         rotationmatrix(radians(angle + dangle * windScale + 0.6 * sin(P.x * factor)), RotationMatrix);
103         P *= RotationMatrix;
104
105         P.y += evaluateWave(wave0, P.xz, osg_SimulationTime);
106         deriv = evaluateWaveDeriv(wave0, P.xz, osg_SimulationTime );
107         ddx = deriv * wave0.dir.x;
108         ddy = deriv * wave0.dir.y;
109
110         P.y += evaluateWave(wave1, P.xz, osg_SimulationTime);
111         deriv = evaluateWaveDeriv(wave1, P.xz, osg_SimulationTime);
112         ddx += deriv * wave1.dir.x;
113         ddy += deriv * wave1.dir.y;
114
115         P.y += evaluateWaveSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
116         deriv = evaluateWaveDerivSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
117         ddx += deriv * wave2.dir.x;
118         ddy += deriv * wave2.dir.y;
119
120         P.y += evaluateWaveSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
121         deriv = evaluateWaveDerivSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
122         ddx += deriv * wave3.dir.x;
123         ddy += deriv * wave3.dir.y;
124         }
125
126 void main(void)
127         {
128         const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
129         const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
130         const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
131
132         mat4 RotationMatrix;
133
134         // compute direction to viewer
135         vec3 E = normalize(viewerdir);
136
137         // compute direction to light source
138         vec3 L = normalize(lightdir);
139
140         // half vector
141         vec3 H = normalize(L + E);
142
143         vec3 Normal = normalize(normal);
144
145         const float water_shininess = 240.0;
146
147         float range = gl_ProjectionMatrix[3].z/(gl_FragCoord.z * -2.0 + 1.0 - gl_ProjectionMatrix[2].z);
148
149         // approximate cloud cover
150         float cover = 0.0;
151         //bool Status = true;
152
153         float windEffect = sqrt( WindE*WindE + WindN*WindN ) * 0.6;             //wind speed in kt
154         float windScale =  15.0/(3.0 + windEffect);                                             //wave scale
155         float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect);    //low windspeed wave filter
156         float waveRoughness = 0.01 + smoothstep(0.0, 40.0, windEffect);                 //wave roughness filter
157
158         float mixFactor = 0.2 + 0.02 * smoothstep(0.0, 50.0, windEffect);
159         //mixFactor = 0.2;
160         mixFactor = clamp(mixFactor, 0.3, 0.8);
161
162         // sine waves
163
164         // Test data
165         //float WaveFreq =1.0;
166         //float WaveAmp = 1000.0;
167         //float WaveSharp = 10.0;
168
169           vec4 ddxVec = vec4(0.0);
170           vec4 ddyVec = vec4(0.0);
171           int detailFlag = 0;
172
173         //uncomment to test
174         //range = -20000;
175
176         if (range > -15000 || dot(Normal,H) > 0.95 ) {
177
178                 float ddx = 0.0, ddy = 0.0;
179                 float ddx1 = 0.0, ddy1 = 0.0;
180                 float ddx2 = 0.0, ddy2 = 0.0;
181                 float ddx3 = 0.0, ddy3 = 0.0;
182                 float waveamp;
183
184                 float angle = 0.0;
185
186                 wave0.freq = WaveFreq ;
187                 wave0.amp = WaveAmp;
188                 wave0.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
189
190                 angle -= 45;
191                 wave1.freq = WaveFreq * 2.0 ;
192                 wave1.amp = WaveAmp * 1.25;
193                 wave1.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
194
195                 angle += 30;
196                 wave2.freq = WaveFreq * 3.5;
197                 wave2.amp = WaveAmp * 0.75;
198                 wave2.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
199
200                 angle -= 50;
201                 wave3.freq = WaveFreq * 3.0 ;
202                 wave3.amp = WaveAmp * 0.75;
203                 wave3.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
204
205                 // sum waves
206
207                 ddx = 0.0, ddy = 0.0;
208                 sumWaves(WaveAngle, -1.5, windScale, WaveFactor, ddx, ddy);
209
210                 ddx1 = 0.0, ddy1 = 0.0;
211                 sumWaves(WaveAngle, 1.5, windScale, WaveFactor, ddx1, ddy1);
212
213                 //reset the waves
214                 angle = 0.0;
215                 waveamp = WaveAmp * 0.75;
216
217                 wave0.freq = WaveFreq ;
218                 wave0.amp = waveamp;
219                 wave0.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
220
221                 angle -= 20;
222                 wave1.freq = WaveFreq * 2.0 ;
223                 wave1.amp = waveamp * 1.25;
224                 wave1.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
225
226                 angle += 35;
227                 wave2.freq = WaveFreq * 3.5;
228                 wave2.amp = waveamp * 0.75;
229                 wave2.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
230
231                 angle -= 45;
232                 wave3.freq = WaveFreq * 3.0 ;
233                 wave3.amp = waveamp * 0.75;
234                 wave3.dir =  vec2(cos(radians(angle)), sin(radians(angle)));
235
236                 // sum waves
237                 ddx2 = 0.0, ddy2 = 0.0;
238                 sumWaves(WaveAngle + WaveDAngle, -1.5, windScale, WaveFactor, ddx2, ddy2);
239
240                 ddx3 = 0.0, ddy3 = 0.0;
241                 sumWaves(WaveAngle + WaveDAngle, 1.5, windScale, WaveFactor, ddx3, ddy3);
242
243                 ddxVec = vec4(ddx, ddx1, ddx2, ddx3);
244                 ddyVec = vec4(ddy, ddy1, ddy2, ddy3);
245
246                 //toggle detailFlag
247                 detailFlag = 1;
248                 }       // end sine stuff
249
250         float ddxSum = dot(ddxVec, AllOnes);
251         float ddySum = dot(ddyVec, AllOnes);
252
253         if (Status == 1){
254                 cover = min(min(min(min(CloudCover0, CloudCover1),CloudCover2),CloudCover3),CloudCover4);
255                 } else {
256                         // hack to allow for Overcast not to be set by Local Weather
257                         if (Overcast == 0){
258                                 cover = 5;
259                                 } else {
260                                         cover = Overcast * 5;
261                                 }
262                 }
263
264 //      vec4 viewt = normalize(waterTex4);
265         vec4 viewt = vec4(-E, 0.0) * 0.6;
266
267         vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)* windScale) * 2.0 - 1.0;
268
269         //normalmaps
270         vec4 nmap   = texture2D(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0;
271         vec4 nmap1  = texture2D(perlin_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0;
272
273         rotationmatrix(radians(3.0 * sin(osg_SimulationTime * 0.0075)), RotationMatrix);
274         nmap  += texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * tscale) * windScale) * 2.0 - 1.0;
275         nmap1 += texture2D(perlin_normalmap, vec2(waterTex2 * RotationMatrix * tscale) * windScale) * 2.0 - 1.0;
276
277         nmap  *= windEffect_low;
278         nmap1 *= windEffect_low;
279
280         // mix water and noise, modulated by factor
281         vec4 vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness);
282         vNorm.r += ddxSum;
283
284     if (normalmap_dds > 0)
285         vNorm = -vNorm;         //dds fix
286
287         //load reflection
288         vec4 tmp = vec4(lightdir, 0.0);
289         vec4 refTex = texture2D(water_reflection, vec2(tmp + waterTex1) * 32.0) ;
290         vec4 refTexGrey = texture2D(water_reflection_grey, vec2(tmp + waterTex1) * 32.0) ;
291         vec4 refl ;
292
293         // Test data
294         // cover = 0;
295
296         if(cover >= 1.5){
297                 refl = normalize(refTex);
298                 refl.a = 1.0;
299                 }
300         else
301                 {
302                 refl = normalize(refTexGrey);
303                 refl.r *= (0.75 + 0.15 * cover);
304                 refl.g *= (0.80 + 0.15 * cover);
305                 refl.b *= (0.875 + 0.125 * cover);
306                 refl.a  = 1.0;
307                 }
308
309         vec3 N0 = vec3(texture2D(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0);
310         vec3 N1 = vec3(texture2D(perlin_normalmap, vec2(waterTex1 + disdis * sca) * windScale) * 2.0 - 1.0);
311
312         N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * tscale) * windScale) * 2.0 - 1.0);
313         N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2 * tscale) * windScale) * 2.0 - 1.0);
314
315         rotationmatrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), RotationMatrix);
316         N0 += vec3(texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
317         N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
318
319         rotationmatrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), RotationMatrix);
320         N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale) * 2.0 - 1.0);
321         N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca) * windScale) * 2.0 - 1.0);
322
323         if(detailFlag > 0)
324                 {
325                         N0 *= windEffect_low;
326                         N1 *= windEffect_low;
327                         //N0.r += (ddx + ddx1 + ddx2 + ddx3);
328                         //N0.g += (ddy + ddy1 + ddy2 + ddy3);
329
330                         N0.r += ddxSum;
331                         N0.g += ddySum;
332
333                         Normal = normalize(mix(Normal + N0, Normal + N1, mixFactor) * waveRoughness);
334
335             if (normalmap_dds > 0)
336                 Normal = -Normal; //dds fix
337                 }
338
339
340         // specular
341         vec3 specular_color = vec3(gl_LightSource[0].diffuse)
342                                                   * pow(max(0.0, dot(Normal, H)), water_shininess) * 6.0;
343         vec4 specular = vec4(specular_color, 0.5);
344
345         specular = specular * saturation * 0.3 ;
346
347         //calculate fresnel
348         vec4 invfres = vec4( dot(vNorm, viewt) );
349         vec4 fres = vec4(1.0) + invfres;
350         refl *= fres;
351
352         //calculate final colour
353         vec4 ambient_light = gl_LightSource[0].diffuse;
354         vec4 finalColor;
355
356         if(cover >= 1.5){
357                 finalColor = refl + specular;
358                 } else {
359                         finalColor = refl;
360                 }
361
362         //add foam
363         vec4 foam_texel = texture2D(sea_foam, vec2(waterTex2 * tscale) * 25.0);
364         if (range > -10000.0){
365
366                 float foamSlope = 0.1 + 0.1 * windScale;
367                 //float waveSlope = mix(N0.g, N1.g, 0.25);
368                 float waveSlope = Normal.g;
369
370                 if (windEffect >= 8.0)
371
372                         if (waveSlope >= foamSlope){
373                                 finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel),
374                                                                                           smoothstep(0.01, 0.50, Normal.g));
375                         }
376
377         } // end range
378
379
380         finalColor *= ambient_light;
381
382         //gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor);
383         finalColor.rgb = fog_Func(finalColor.rgb, fogType);
384         gl_FragColor = finalColor;
385         }