1// -*-C++-*-
2#version 120
3
4varying vec2 rawPos;
5varying vec2 nPos;
6varying vec3 vertPos;
7varying vec3 normal;
8varying vec3 light_diffuse;
9varying vec3 refl_vec;
10varying float splash_angle;
11varying float Mie;
12varying float ambient_fraction;
13
14varying float flogz;
15
16uniform float ground_scattering;
17uniform float hazeLayerAltitude;
18uniform float moonlight;
19uniform float terminator;
20uniform float splash_x;
21uniform float splash_y;
22uniform float splash_z;
23
24const float EarthRadius = 5800000.0;
25const float terminator_width = 200000.0;
26
27float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
28{
29//x = x - 0.5;
30
31// use the asymptotics to shorten computations
32if (x < -15.0) {return 0.0;}
33
34return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
35}
36
37
38void main()
39{
40
41vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
42vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight;
43
44// geometry for lighting
45vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
46vec3 relPos = gl_Vertex.xyz - ep.xyz;
47vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
48vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
49float dist = length(relPos);
50float vertex_alt = max(gl_Vertex.z,100.0);
51float scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
52float yprime_alt = - sqrt(2.0 * EarthRadius * vertex_alt);
53float earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
54float lightArg = (terminator-yprime_alt)/100000.0;
55
56// light computation
57
58vec3 light_ambient;
59
60light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
61light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
62light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
63light_diffuse = light_diffuse * scattering;
64
65
66light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
67light_ambient.g = light_ambient.r * 0.4/0.33;
68light_ambient.b = light_ambient.r * 0.5/0.33;
69
70float intensity;
71
72if (earthShade < 0.5)
73	{
74	intensity = length(light_ambient.xyz);
75
76	light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb,  shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
77	light_ambient.rgb = light_ambient.rgb +   moonLightColor *  (1.0 - smoothstep(0.4, 0.5, earthShade));
78
79	intensity = length(light_diffuse.xyz);
80	light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb,  shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
81	}
82
83
84float MieFactor =   dot(normalize(lightFull), normalize(relPos));
85Mie =  smoothstep(0.9,1.0, MieFactor) * earthShade * earthShade * scattering;
86
87
88// get a reflection vector for cube map
89
90vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
91normal = normalize(gl_NormalMatrix * gl_Normal);
92vec4 reflect_eye = vec4(reflect(ecPosition.xyz, normal), 0.0);
93vec3 reflVec_stat = normalize(gl_ModelViewMatrixInverse * reflect_eye).xyz;
94refl_vec = reflVec_stat;
95
96// get a projection plane orthogonal to the splash vector
97
98vec3 splash_vec = vec3 (splash_x, splash_y, splash_z);
99vec3 corrected_splash = normalize(splash_vec);
100
101float angle = abs(dot(corrected_splash, gl_Normal));
102
103
104//corrected_splash = normalize(corrected_splash + 0.4* gl_Normal );
105
106
107vec3 base_1 = vec3 (-corrected_splash.y, corrected_splash.x, 0.0);
108vec3 base_2 = cross (corrected_splash, base_1);
109
110base_1 = normalize(base_1);
111base_2 = normalize(base_2);
112
113rawPos = vec2 (dot(gl_Vertex.xyz, base_1), dot(gl_Vertex.xyz, base_2));
114
115base_1 = vec3 (-gl_Normal.y, gl_Normal.x, 0.0);
116base_2 = cross(gl_Normal, base_1);
117
118base_1 = normalize(base_1);
119base_2 = normalize(base_2);
120
121nPos = vec2 (dot(gl_Vertex.xyz, base_1), dot(gl_Vertex.xyz, base_2));
122
123vertPos = gl_Vertex.xyz;
124
125splash_angle = dot(gl_Normal, corrected_splash);
126
127ambient_fraction = length(light_ambient.rgb)/(length(light_diffuse.rgb +light_ambient.rgb ) + 0.01);
128
129
130gl_Position = ftransform();
131// logarithmic depth
132flogz = 1.0 + gl_Position.w;
133
134vec4 diffuse_color = gl_FrontMaterial.diffuse;
135vec4 ambient_color = gl_FrontMaterial.ambient;
136
137vec4 constant_term = gl_FrontMaterial.emission + ambient_color * vec4 (light_diffuse.rgb + light_ambient.rgb,1.0);
138constant_term.a = min(diffuse_color.a, ambient_color.a);
139
140gl_FrontColor = constant_term;
141gl_BackColor = gl_FrontColor;
142
143}
144