| 1 |
// -*-C++-*- |
| 2 |
|
| 3 |
#version 120 |
| 4 |
|
| 5 |
// Atmospheric scattering shader for flightgear |
| 6 |
// Written by Lauri Peltonen (Zan) |
| 7 |
// Implementation of O'Neil's algorithm |
| 8 |
|
| 9 |
|
| 10 |
uniform mat4 osg_ViewMatrix; |
| 11 |
uniform mat4 osg_ViewMatrixInverse; |
| 12 |
uniform float hazeLayerAltitude; |
| 13 |
uniform float terminator; |
| 14 |
uniform float avisibility; |
| 15 |
uniform float visibility; |
| 16 |
uniform float terrain_alt; |
| 17 |
uniform float air_pollution; |
| 18 |
uniform float radius_modifier; |
| 19 |
|
| 20 |
varying vec3 rayleigh; |
| 21 |
varying vec3 mie; |
| 22 |
varying vec3 eye; |
| 23 |
varying vec3 hazeColor; |
| 24 |
varying vec3 viewVector; |
| 25 |
varying float ct; |
| 26 |
varying float cphi; |
| 27 |
varying float delta_z; |
| 28 |
varying float alt; |
| 29 |
varying float earthShade; |
| 30 |
|
| 31 |
|
| 32 |
// Dome parameters from FG and screen |
| 33 |
const float domeSize = 80000.0; |
| 34 |
const float realDomeSize = 100000.0; |
| 35 |
const float groundRadius = 0.984503332 * domeSize; |
| 36 |
const float altitudeScale = domeSize - groundRadius; |
| 37 |
const float EarthRadius = 5800000.0; |
| 38 |
|
| 39 |
// Dome parameters when calculating scattering |
| 40 |
// Assuming dome size is 5.0 |
| 41 |
const float groundLevel = 0.984503332 * 5.0; |
| 42 |
const float heightScale = (5.0 - groundLevel); |
| 43 |
|
| 44 |
// Integration parameters |
| 45 |
const int nSamples = 7; |
| 46 |
const float fSamples = float(nSamples); |
| 47 |
|
| 48 |
// Scattering parameters |
| 49 |
uniform float rK = 0.0003; //0.00015; |
| 50 |
uniform float mK = 0.003; //0.0025; |
| 51 |
uniform float density = 0.5; //1.0 |
| 52 |
//vec3 rayleighK = rK * vec3(5.602, 7.222, 19.644); |
| 53 |
vec3 rayleighK = rK * vec3(4.5, 8.62, 17.3); |
| 54 |
vec3 mieK = vec3(mK); |
| 55 |
vec3 sunIntensity = 10.0*vec3(120.0, 125.0, 130.0); |
| 56 |
|
| 57 |
// light_func is a generalized logistic function fit to the light intensity as a function |
| 58 |
// of scaled terminator position obtained from Flightgear core |
| 59 |
|
| 60 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 61 |
{ |
| 62 |
x = x - 0.5; |
| 63 |
|
| 64 |
// use the asymptotics to shorten computations |
| 65 |
if (x > 30.0) {return e;} |
| 66 |
if (x < -15.0) {return 0.0;} |
| 67 |
|
| 68 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
// Find intersections of ray to skydome |
| 73 |
// ray must be normalized |
| 74 |
// cheight is camera height |
| 75 |
float intersection (in float cheight, in vec3 ray, in float rad2) |
| 76 |
{ |
| 77 |
float B = 2.0 * cheight*ray.y; |
| 78 |
float C = cheight*cheight - rad2; // 25.0 is skydome radius * radius |
| 79 |
float fDet = max(0.0, B*B - 4.0 * C); |
| 80 |
return 0.5 * (-B - sqrt(fDet)); |
| 81 |
} |
| 82 |
|
| 83 |
// Return the scale function at height = 0 for different thetas |
| 84 |
float outscatterscale(in float costheta) |
| 85 |
{ |
| 86 |
|
| 87 |
|
| 88 |
float x = 1.0 - costheta; |
| 89 |
|
| 90 |
float a = 1.16941; |
| 91 |
float b = 0.618989; |
| 92 |
float c = 6.34484; |
| 93 |
float d = -31.4138; |
| 94 |
float e = 75.3249; |
| 95 |
float f = -80.1643; |
| 96 |
float g = 32.2878; |
| 97 |
|
| 98 |
return exp(a+x*(b+x*(c+x*(d+x*(e+x*(f+x*g)))))); |
| 99 |
} |
| 100 |
|
| 101 |
// Return the amount of outscatter for different heights and thetas |
| 102 |
// assuming view ray hits the skydome |
| 103 |
// height is 0 at ground level and 1 at space |
| 104 |
// Assuming average density of atmosphere is at 1/4 height |
| 105 |
// and atmosphere height is 100 km |
| 106 |
float outscatter(in float costheta, in float height) |
| 107 |
{ |
| 108 |
return density * outscatterscale(costheta) * exp(-4.0 * height); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
void main() |
| 113 |
{ |
| 114 |
// Make sure the dome is of a correct size |
| 115 |
vec4 realVertex = gl_Vertex; //vec4(normalize(gl_Vertex.xyz) * domeSize, 1.0); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
// Ground point (skydome center) in eye coordinates |
| 120 |
vec4 groundPoint = gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0); |
| 121 |
|
| 122 |
|
| 123 |
// Calculate altitude as the distance from skydome center to camera |
| 124 |
// Make it so that 0.0 is ground level and 1.0 is 100km (space) level |
| 125 |
// the correction ensures compatibility with Earthview for orbits farther out |
| 126 |
float altitude = distance(groundPoint, vec4(0.0, 0.0, 0.0, 1.0)) * (1.0 -0.0013); |
| 127 |
float scaledAltitude = altitude / realDomeSize; |
| 128 |
|
| 129 |
// the local horizon angle |
| 130 |
float radiusEye = EarthRadius + altitude; |
| 131 |
float ctterrain = -sqrt(radiusEye * radiusEye - EarthRadius * EarthRadius)/radiusEye; |
| 132 |
|
| 133 |
|
| 134 |
// Camera's position, z is up! |
| 135 |
float cameraRealAltitude = groundLevel + heightScale*scaledAltitude; |
| 136 |
vec3 camera = vec3(0.0, 0.0, cameraRealAltitude); |
| 137 |
vec3 sample = 5.0 * realVertex.xyz / domeSize; // Sample is the dome vertex |
| 138 |
vec3 relativePosition = camera - sample; // Relative position |
| 139 |
|
| 140 |
viewVector = (sample-camera).xyz; |
| 141 |
|
| 142 |
// Find intersection of skydome and view ray |
| 143 |
float space = intersection(cameraRealAltitude, -normalize(relativePosition), 25.0); |
| 144 |
if(space > 0.0) { |
| 145 |
// We are in space, calculate correct positiondelta! |
| 146 |
relativePosition -= space * normalize(relativePosition); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
vec3 positionDelta = relativePosition / fSamples; |
| 151 |
float deltaLength = length(positionDelta); // Should multiply by something? |
| 152 |
|
| 153 |
vec3 lightDirection = gl_LightSource[0].position.xyz; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
// Cos theta of camera's position and sample point |
| 158 |
// Since camera is 0,0,z, dot product is just the z coordinate |
| 159 |
float cameraCosTheta; |
| 160 |
|
| 161 |
// If sample is above camera, reverse ray direction |
| 162 |
if(positionDelta.z < 0.0) cameraCosTheta = -positionDelta.z / deltaLength; |
| 163 |
else cameraCosTheta = positionDelta.z / deltaLength; |
| 164 |
|
| 165 |
|
| 166 |
float cameraCosTheta1 = -positionDelta.z / deltaLength; |
| 167 |
|
| 168 |
|
| 169 |
// Total attenuation from camera to skydome |
| 170 |
float totalCameraScatter = outscatter(cameraCosTheta, scaledAltitude); |
| 171 |
|
| 172 |
|
| 173 |
// Do numerical integration of scattering function from skydome to camera |
| 174 |
vec3 color = vec3(0.0); |
| 175 |
|
| 176 |
// no scattering integrations where terrain is later drawn |
| 177 |
if (cameraCosTheta1 > (ctterrain-0.05)) |
| 178 |
{ |
| 179 |
for(int i = 0; i < nSamples; i++) |
| 180 |
{ |
| 181 |
// Altitude of the sample point 0...1 |
| 182 |
float sampleAltitude = (length(sample) - groundLevel) / heightScale; |
| 183 |
|
| 184 |
// Cosine between the angle of sample's up vector and sun |
| 185 |
// Since lightDirection is in eye space, we must transform sample too |
| 186 |
vec3 sampleUp = gl_NormalMatrix * normalize(sample); |
| 187 |
float cosTheta = dot(sampleUp, lightDirection); |
| 188 |
|
| 189 |
// Scattering from sky to sample point |
| 190 |
float skyScatter = outscatter(cosTheta, sampleAltitude); |
| 191 |
|
| 192 |
// Calculate the attenuation from this point to camera |
| 193 |
// Again, reverse the direction if vertex is over the camera |
| 194 |
float cameraScatter; |
| 195 |
if(relativePosition.z < 0.0) { // Vertex is over the camera |
| 196 |
cameraCosTheta = -dot(normalize(positionDelta), normalize(sample)); |
| 197 |
|
| 198 |
cameraScatter = totalCameraScatter - outscatter(cameraCosTheta, sampleAltitude); |
| 199 |
} else { // Vertex is below camera |
| 200 |
cameraCosTheta = dot(normalize(positionDelta), normalize(sample)); |
| 201 |
cameraScatter = outscatter(cameraCosTheta, sampleAltitude) - totalCameraScatter; |
| 202 |
} |
| 203 |
|
| 204 |
// Total attenuation |
| 205 |
vec3 totalAttenuate = 4.0 * 3.14159 * (rayleighK + mieK) * (-skyScatter - cameraScatter); |
| 206 |
|
| 207 |
vec3 inScatter = exp(totalAttenuate - sampleAltitude*4.0); |
| 208 |
|
| 209 |
color += inScatter * deltaLength; |
| 210 |
sample += positionDelta; |
| 211 |
} |
| 212 |
} |
| 213 |
color *= sunIntensity; |
| 214 |
ct = cameraCosTheta1; |
| 215 |
rayleigh = rayleighK * color; |
| 216 |
mie = mieK * color; |
| 217 |
eye = gl_NormalMatrix * positionDelta; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
// We need to move the camera so that the dome appears to be centered around earth |
| 223 |
// to make the dome render correctly! |
| 224 |
float moveDown = -altitude; // Center dome on camera |
| 225 |
moveDown += groundRadius; |
| 226 |
moveDown += scaledAltitude * altitudeScale; // And move correctly according to altitude |
| 227 |
|
| 228 |
// Vertex transformed correctly so that at 100km we are at space border |
| 229 |
vec4 finalVertex = realVertex - vec4(0.0, 0.0, 1.0, 0.0) * moveDown; |
| 230 |
|
| 231 |
|
| 232 |
// prepare some stuff for a ground haze layer |
| 233 |
|
| 234 |
delta_z = hazeLayerAltitude - altitude; |
| 235 |
alt = altitude; |
| 236 |
|
| 237 |
|
| 238 |
// establish coordinates relative to sun position |
| 239 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 240 |
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz; |
| 241 |
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0) ); |
| 242 |
|
| 243 |
|
| 244 |
vec3 relVector = normalize(finalVertex.xyz - ep.xyz); |
| 245 |
|
| 246 |
// and compute the twilight shading |
| 247 |
|
| 248 |
|
| 249 |
// yprime is the coordinate from/towards terminator |
| 250 |
float yprime; |
| 251 |
|
| 252 |
if (alt > hazeLayerAltitude) // we're looking from above and can see far |
| 253 |
{ |
| 254 |
if (ct < 0.0) |
| 255 |
{ |
| 256 |
yprime = -dot(relVector,lightHorizon) * altitude/-ct;//(ct-0.001); |
| 257 |
yprime = yprime -sqrt(2.0 * EarthRadius * hazeLayerAltitude); |
| 258 |
} |
| 259 |
else // the only haze we see looking up is overcast, assume its altitude |
| 260 |
{ |
| 261 |
yprime = -dot(relVector,lightHorizon) * avisibility; |
| 262 |
yprime = yprime -sqrt(2.0 * EarthRadius * 10000.0); |
| 263 |
} |
| 264 |
} |
| 265 |
else |
| 266 |
{yprime = -dot(relVector,lightHorizon) * avisibility; |
| 267 |
yprime = yprime -sqrt(2.0 * EarthRadius * hazeLayerAltitude); |
| 268 |
} |
| 269 |
|
| 270 |
if (terminator > 1000000.0){yprime = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);} |
| 271 |
|
| 272 |
float terminator_width = 200000.0; |
| 273 |
earthShade = 0.9 * smoothstep((terminator_width+ terminator), (-terminator_width + terminator), yprime) + 0.1; |
| 274 |
|
| 275 |
float lightArg = (terminator-yprime)/100000.0; |
| 276 |
|
| 277 |
hazeColor.r = light_func(lightArg, 8.305e-06, 0.161, 4.827-3.0*air_pollution, 3.04e-05, 1.0); |
| 278 |
hazeColor.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 279 |
hazeColor.b = light_func(lightArg, 1.330e-05, 0.264, 1.527+2.0*air_pollution, 1.08e-05, 1.0); |
| 280 |
|
| 281 |
//new |
| 282 |
//hazeColor.r = light_func(lightArg, 3.495e-05, 0.161, 3.878, 0.000129, 1.0); |
| 283 |
//hazeColor.g = light_func(lightArg, 1.145e-05, 0.161, 3.827, 1.783e-05, 1.0); |
| 284 |
//hazeColor.b = light_func(lightArg, 0.234, 0.141, 2.572, 0.257, 1.0); |
| 285 |
|
| 286 |
float intensity = length(hazeColor.xyz); |
| 287 |
float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator -sqrt(2.0 * EarthRadius * terrain_alt)); |
| 288 |
cphi = dot(normalize(relVector), normalize(lightHorizon)); |
| 289 |
float mie_angle = (0.5 * dot(normalize(relVector), normalize(lightFull)) ) + 0.5; |
| 290 |
|
| 291 |
float mie_postfactor = clamp(mie_magnitude * (0.5 - 0.5 * mie_angle),0.001,1.0); |
| 292 |
|
| 293 |
hazeColor = intensity * ((1.0 - mie_magnitude) + mie_magnitude * mie_angle) * normalize(mix(hazeColor, vec3 (0.5, 0.58, 0.65), mie_postfactor ) ); |
| 294 |
|
| 295 |
|
| 296 |
// Transform |
| 297 |
gl_Position = gl_ModelViewProjectionMatrix * finalVertex; |
| 298 |
} |