Terrain Haze v1.3
[fg:toms-fgdata.git] / Shaders / water_lightfield.vert
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
8 #version 120
9
10 varying vec4 waterTex1;
11 varying vec4 waterTex2;
12 varying vec4 waterTex4;
13 //varying vec4 ecPosition;
14 varying vec3 relPos;
15 varying vec3 specular_light;
16
17 varying vec3 viewerdir;
18 varying vec3 lightdir;
19 //varying vec3 normal;
20
21 varying float earthShade;
22 varying float yprime_alt;
23 varying float mie_angle;
24
25 uniform float osg_SimulationTime;
26 uniform float WindE, WindN;
27
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;
35
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;
39
40 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
41 {
42 //x = x - 0.5;
43
44 // use the asymptotics to shorten computations
45 if (x < -15.0) {return 0.0;}
46
47 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
48 }
49
50
51 ////fog "include"////////
52 // uniform int fogType;
53 //
54 // void fog_Func(int type);
55 /////////////////////////
56
57 /////// functions /////////
58
59 void rotationmatrix(in float angle, out mat4 rotmat)
60 {
61     rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
62         sin( angle ),  cos( angle ), 0.0, 0.0,
63         0.0         ,  0.0         , 1.0, 0.0,
64         0.0         ,  0.0         , 0.0, 1.0 );
65 }
66
67 void main(void)
68 {
69
70     mat4 RotationMatrix;
71    // vec3 N = normalize(gl_Normal);
72    // normal = N;
73
74     vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
75
76     viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex);
77     lightdir = normalize(vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position));
78
79     waterTex4 = vec4( ecPosition.xzy, 0.0 );
80
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);
83
84     float Angle;
85
86     float windFactor = sqrt(pow(abs(WindE),2)+pow(abs(WindN),2)) * 0.05;
87     if (WindN == 0.0 && WindE == 0.0) {
88         Angle = 0.0;
89     }else{
90         Angle = atan(-WindN, WindE) - atan(1.0);
91     }
92
93     rotationmatrix(Angle, RotationMatrix);
94     waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
95
96     rotationmatrix(Angle, RotationMatrix);
97     waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
98
99 //     fog_Func(fogType);
100     gl_Position = ftransform();
101
102
103
104 // here start computations for the haze layer
105
106
107   float yprime;
108   float lightArg;
109   float intensity;
110   float vertex_alt;
111   float scattering;
112
113     // we need several geometrical quantities
114
115     // first current altitude of eye position in model space
116     vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
117     
118     // and relative position to vector
119     relPos = gl_Vertex.xyz - ep.xyz;
120
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);
124  
125
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); 
129
130     // branch dependent on daytime
131
132 if (terminator < 1000000.0) // the full, sunrise and sunset computation
133 {
134
135     
136     // establish coordinates relative to sun position
137
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));
141
142     
143     // yprime is the distance of the vertex into sun direction
144     yprime = -dot(relPos, lightHorizon);
145
146     // this gets an altitude correction, higher terrain gets to see the sun earlier
147     yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
148
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;
152   
153    // parametrized version of the Flightgear ground lighting function
154     lightArg = (terminator-yprime_alt)/100000.0;
155
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);
159
160         specular_light = specular_light * scattering;
161
162         // correct ambient light intensity and hue before sunrise
163         if (earthShade < 0.5)
164         {
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) ));
167         }
168
169     // directional scattering for low sun
170     if (lightArg < 5.0)
171         //{mie_angle = (0.5 *  dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
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 else // the faster, full-day version without lightfields
201 {
202     //vertex_alt = max(gl_Vertex.z,100.0);
203  
204     earthShade = 1.0;
205     mie_angle = 1.0;
206
207     if (terminator > 3000000.0)
208         {specular_light = vec3 (1.0, 1.0, 1.0);}
209     else
210         {
211
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;
216         }
217    
218    specular_light = specular_light * scattering;
219
220     yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
221
222 }
223
224
225
226 }