Skip to content

Instantly share code, notes, and snippets.

@bzgeb
Created August 3, 2012 21:07
Show Gist options
  • Save bzgeb/3251545 to your computer and use it in GitHub Desktop.
Save bzgeb/3251545 to your computer and use it in GitHub Desktop.
///////////////////////////////////////////////////////////////////////////////
// ATMOSPHERIC SCATTERING
// Viewing the sky from space.
//
// Based on OGRE Forum user HexiDave's Atmospheric Scattering demo
// http://www.ogre3d.org/forums/viewtopic.php?t=37072
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// VARIABLES FROM OGRE
///////////////////////////////////////////////////////////////////////////////
// automatic from OGRE
uniform vec4 g_lightPosition;
// manual from application
uniform float g_exposure;
// from OGRE material properties
uniform sampler2D s2Tex1;
///////////////////////////////////////////////////////////////////////////////
// VARYINGS AND UNIFORMS
///////////////////////////////////////////////////////////////////////////////
varying vec4 v_rayleighColour;
varying vec4 v_mieColour;
varying vec3 v_direction;
///////////////////////////////////////////////////////////////////////////////
// LOCAL VARIABLES
///////////////////////////////////////////////////////////////////////////////
const float fG = -0.990;
const float fG2 = fG * fG;
///////////////////////////////////////////////////////////////////////////////
// HELPERS
///////////////////////////////////////////////////////////////////////////////
// calculates the Mie phase function
float getMiePhase(float fCos, float fCos2, float g, float g2) {
return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
}
// calculates the Rayleigh phase function
float getRayleighPhase(float fCos2) {
return 0.75 * (2.0 + 0.5 * fCos2);
}
///////////////////////////////////////////////////////////////////////////////
// MAIN FUNCTION
///////////////////////////////////////////////////////////////////////////////
void main() {
float fCos = dot(-g_lightPosition.xyz, v_direction) / length(v_direction);
float fCos2 = fCos*fCos;
vec4 v4Diffuse = texture2D(s2Tex1, gl_TexCoord[0].st);
// HDR Lighting
// gl_FragColor = 1.0 - exp(-g_exposure * (v_rayleighColour + 0.25 * v_mieColour));
vec4 v4RayleighColour = v_rayleighColour * getRayleighPhase(fCos2);
// Don't use 0.0 or else inexplicable bugs occur
v4RayleighColour = clamp(v4RayleighColour, 0.000000001, 1.0);
vec4 v4MieColour = v_mieColour * getMiePhase(fCos, fCos2, fG, fG2);
v4MieColour = clamp(v4MieColour, 0.00000000001, 100000000.0);
gl_FragColor.rgb = v4RayleighColour.rgb + v4Diffuse.rgb * v4MieColour.rgb;
gl_FragColor.a = 1.0;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment