I decided to have a try at writing a sky scattering shader, but as with everything, it hasn't gone perfectly.

The main issues I'm having, are that the light direction doesn't seem to have much effect on sky colour. Also, the lighting/colouring that there is, seems to be duplicated on the opposite side of the skysphere for some reason.

The shaders (GLSL):

[vertex]

Code:

uniform vec3 eyePosition;
const vec3 lightDir = vec3(1, -1, 1);
const vec3 Br = vec3(6.95e-6, 1.18e-5, 2.44e-5);
const vec3 Bm = vec3(4e-7, 6e-7, 2.4e-6);
const vec3 Brm = Br+Bm;
const vec4 Esun = vec4(1.0, 1.0, 1.0, 15.0);
const float g = 0.0;
float log2_e = 1.0/log(2.0);
const float e = 2.71828;
const float PI = 3.141592654;
varying vec3 scattering;
varying vec3 extinction;
varying vec3 normal;
varying vec3 light;
vec3 Fex(float s)
{
vec3 ex = exp(Brm * -s);
return ex;
}
vec3 rayleigh(float theta)
{
float pi316 = 3.0/(16.0*PI);
float phase = 1.0 + (theta*theta);
vec3 ret = pi316*phase*Br;
return ret;
}
vec3 mie(float theta)
{
float pi14 = 1.0/(4.0*PI);
float g1 = (1.0-g)*(1.0-g);
float g2 = 1.0+(g*g);
float g3 = 2.0*g;
float phase = g1 / pow(g2 - g3*theta, 1.5);
vec3 ret = pi14*phase*Bm;
return ret;
}
vec3 inScatter(float s, float theta)
{
vec3 num = rayleigh(theta) + mie(theta);
num *= Esun.w;
num *= 1.0-Fex(s);
vec3 ret = num/Brm;
return ret;
}
void main()
{
vec4 pos = gl_ModelViewProjectionMatrix*gl_Vertex;
light = normalize(lightDir);
normal = normalize(gl_Normal.xyz);
vec3 wDir = normalize(pos.xyz - eyePosition);
float dist = pos.z;
float cosTheta = dot(normal, light);
scattering = inScatter(dist, cosTheta);
extinction = Fex(dist);
light = lightDir;
gl_Position = pos;
}

[fragment]

Code:

varying vec3 scattering;
varying vec3 extinction;
varying vec3 normal;
varying vec3 light;
void main()
{
float Idiff = dot(normalize(normal), normalize(light));
vec4 surfaceColour = vec4(0.0, 0.0, 0.0, 1.0); //colourless atmosphere
vec4 final = (surfaceColour*vec4(extinction, 1.0)) + vec4(scattering, 1.0);
gl_FragColor = final;
}

I can't see anything wrong with the calculations, by comparing with what I've found in papers and other sources.

I'm not quite sure what angle I'm supposed to be using for cosTheta though. The angle between the view vector and the light vector? Or between the vertex normal and the light vector?

And what's an appropriate value for "g" in mie()?