The problem I'm facing right now is that is that the sampled color is supposed to be 'v3FrontColor += v3Attenuate * fScaledLength * depth;' Currently I omit the scaling factor depth * fScaledLength. When I include these scalings everything will be pitch-black. The code of the my current implementations is http://ul.to/3f8jtkux:
uniform mat4 matView;
uniform vec4 view_position;
uniform vec3 v3LightPos;
uniform float fSize;
const int nSamples = 3;
const float fSamples = (float)(nSamples);
const vec3 Wavelength = vec3(0.650,0.570,0.475);
const vec3 v3InvWavelength = 1.0f / vec3( Wavelength.x * Wavelength.x * Wavelength.x * Wavelength.x,
Wavelength.y * Wavelength.y * Wavelength.y * Wavelength.y,
Wavelength.z * Wavelength.z * Wavelength.z * Wavelength.z);
const float fInnerRadius = /*10*/ 6371;
const float fOuterRadius = /*fInnerRadius * 1.025*/ 6530.28f;
const float fInnerRadius2 = fInnerRadius * fInnerRadius;
const float fOuterRadius2 = fOuterRadius * fOuterRadius;
const float fScale = 1.0 / (fOuterRadius - fInnerRadius);
const float fScaleDepth = 0.25;
const float fScaleOverScaleDepth = fScale / fScaleDepth;
const float fm_ESun = 15.0;
const float fm_Kr = 0.0025;
const float fm_Km = 0.0010;
const float fKrESun = fm_Kr * fm_ESun;
const float fKmESun = fm_Km * fm_ESun;
const float fKr4PI = fm_Kr * 4 * 3.141592653;
const float fKm4PI = fm_Km * 4 * 3.141592653;
varying vec3 v3Direction;
varying vec4 c0, c1;
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 )
{
// 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 v3CameraPos = vec3(0.0, fInnerRadius + (( fOuterRadius - fInnerRadius ) * fSize ), 0.0);
float fCameraHeight = length(v3CameraPos);
float fCameraHeight2 = fCameraHeight * fCameraHeight;
vec3 v3Pos = (gl_Vertex.xyz * fOuterRadius);
vec3 v3Ray = v3Pos - v3CameraPos;
float fFar = length(v3Ray);
v3Ray /= fFar;
// Calculate the ray's starting position, then calculate its scattering offset
vec3 v3Start = v3CameraPos;
float fStartAngle = dot(v3Start,v3Ray) / length(v3Start);
float fDepth = exp(fScaleOverScaleDepth * (-fInnerRadius + fCameraHeight));
float fStartOffset = fDepth*scale(fStartAngle);
// Initialize the scattering loop variables
float fSampleLength = fFar / fSamples;
float fScaledLength = fSampleLength * fScale;
vec3 v3SampleRay = v3Ray * fSampleLength;
vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
vec3 v3v3LightPosN = normalize(v3LightPos.xyz);
// Now loop through the sample rays
vec3 v3FrontColor = vec3(0.0, 0.0, 0.0), v3Attenuate;
for(int i=0; i<1; i++)
{
float height = length (v3SamplePoint);
float depth = exp(fScaleOverScaleDepth * (fInnerRadius - height));
float lightAngle = dot(v3v3LightPosN, v3SamplePoint) / height;
float cameraAngle = dot(v3Ray, v3SamplePoint) / height;
float scatter = ( fStartOffset + depth * (scale (lightAngle) - scale (cameraAngle)));
v3Attenuate = exp(-scatter * (v3InvWavelength.xyz * fKr4PI + fKm4PI));
v3FrontColor += v3Attenuate /** fScaledLength * depth /** * */;
v3SamplePoint += v3SampleRay;
}
// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
vec4 newPos = vec4( (gl_Vertex.xyz + view_position.xyz), 1.0);
gl_Position = gl_ModelViewProjectionMatrix * vec4(newPos.xyz, 1.0);
gl_Position.z = gl_Position.w * 0.99999;
c0 = vec4(v3FrontColor * (v3InvWavelength * fKrESun), 1.0); ;
c1 = vec4(v3FrontColor * fKmESun, 1.0);
v3Direction = v3CameraPos - v3Pos;
}
I would appreciate it if somebody could show me what I'm doing wrong, at this point everything starts to look like each-other.