Skip to content

Instantly share code, notes, and snippets.

@shirish47
Last active October 24, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirish47/d68f227e6d5c5b50931d to your computer and use it in GitHub Desktop.
Save shirish47/d68f227e6d5c5b50931d to your computer and use it in GitHub Desktop.
#version 150
uniform mat4 ciModelViewProjection;
uniform mat4 ciProjectionMatrix;
uniform mat4 ciModelViewMatrix;
in vec4 ciPosition;
uniform vec3 v3LightPosition; // The direction vector to the light source
uniform vec3 cameraPosition;
uniform vec3 v3InvWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue
//uniform float fCameraHeight; // The camera's current height
uniform float fCameraHeight2; // fCameraHeight^2
uniform float fOuterRadius; // The outer (atmosphere) radius
uniform float fOuterRadius2; // fOuterRadius^2
uniform float fInnerRadius; // The inner (planetary) radius
uniform float fInnerRadius2; // fInnerRadius^2
uniform float fKrESun; // Kr * ESun
uniform float fKmESun; // Km * ESun
uniform float fKr4PI; // Kr * 4 * PI
uniform float fKm4PI; // Km * 4 * PI
uniform float fScale; // 1 / (fOuterRadius - fInnerRadius)
uniform float fScaleDepth; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
uniform float fScaleOverScaleDepth; // fScale / fScaleDepth
const int nSamples = 3;
const float fSamples = 3.0;
out vec3 v3Direction;
out vec3 c0;
out vec3 c1;
out vec3 v3LightDirection;
float scale(float fCos)
{
float x = 1.0 - fCos;
return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
}
void main(void)
{
v3LightDirection=v3LightPosition/length(v3LightPosition);
// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
vec3 v3Ray = ciPosition.xyz - cameraPosition;
float fFar = length(v3Ray);
v3Ray /= fFar;
// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
float B = 2.0 * dot(cameraPosition, v3Ray);
float C = fCameraHeight2 - fOuterRadius2;
float fDet = max(0.0, B*B - 4.0 * C);
float fNear = 0.5 * (-B - sqrt(fDet));
// Calculate the ray's starting position, then calculate its scattering offset
vec3 v3Start = cameraPosition + v3Ray * fNear;
fFar -= fNear;
float fStartAngle = dot(v3Ray, v3Start) / fOuterRadius;
float fStartDepth = exp(-1.0 / fScaleDepth);
float fStartOffset = fStartDepth * scale(fStartAngle);
//c0 = vec3(1.0, 0, 0) * fStartAngle;
// Initialize the scattering loop variables
float fSampleLength = fFar / fSamples;
float fScaledLength = fSampleLength * fScale;
vec3 v3SampleRay = v3Ray * fSampleLength;
vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
//gl_FrontColor = vec4(0.0, 0.0, 0.0, 0.0);
// Now loop through the sample rays
vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
for(int i=0; i<nSamples; i++)
{
float fHeight = length(v3SamplePoint);
float fDepth = exp(fScaleOverScaleDepth *(fInnerRadius - fHeight));
float fLightAngle = dot(v3LightPosition, v3SamplePoint) / fHeight;
float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
float fScatter = (fStartOffset + fDepth * (scale(fLightAngle) - scale(fCameraAngle)));
vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
v3SamplePoint += v3SampleRay;
}
// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
gl_Position = ciModelViewProjection * ciPosition;
c0 = v3FrontColor * (v3InvWavelength * fKrESun);
c1 = v3FrontColor * fKmESun;
v3Direction = cameraPosition - ciPosition.xyz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment