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