As you may have noticed this will be the first entry of two. I have implemented Sean O’Neils atmospheric Scattering as described in GPU Gems 2, and in the next few articles I hope to outline how I went about implementing it.
Sean O’Neils implementation is split into several parts, these are GroundFromAtmosphere, AtmosphereFromAtmosphere and SpaceFromAtmosphere as well as GroundFromSpace, AtmosphereFromSpace and SpaceFromSpace.
You can read Sean’s article on GPU Gems2 on the NVidia wesite, where they also provide full source code (although the source is for OpenGL and GLSL).
I’ve actually had quite a bit of trouble implementing this, partially because the variables and input constants are so ‘fiddly’, and also because of the maths involved, but I’m quite pleased with the results.
I would definately recommend setting up the variables so they can be changed at run-time. Another thing to note is that the radius of the atmosphere has to be 102.5% times larger than the radius of the planet (m_fRadius * 1.025f).
I have found looking around various forums that everybody seems to NEED to use different settings in order to get the scattering working correctly, which seems a bit odd to me. So, for your convience, here are the scattering settings which I am currently sending to the effect files:
m_fWavelengths = new float[3]; m_fWavelengths[0] = 0.640f; // 640 nm for red m_fWavelengths[1] = 0.560f; // 560 nm for green m_fWavelengths[2] = 0.455f; // 455 nm for blue m_fWavelengths[0] = (float)Math.Pow(m_fWavelengths[0], 4.0f); m_fWavelengths[1] = (float)Math.Pow(m_fWavelengths[1], 4.0f); m_fWavelengths[2] = (float)Math.Pow(m_fWavelengths[2], 4.0f); // Rayleigh scattering constant m_fKr = 0.0025f; m_fKr4PI = m_fKr * 4 * (float)Math.PI; // Mie scattering constant m_fKm = 0.0010f; m_fKm4PI = m_fKm * 4 * (float)Math.PI; m_fSunBrightness = 16.0f; // Set the brightness of the sun. m_fScaleDepth = 0.25f; m_fg = -0.8f;
Hopefully after reading the article on GPU Gems, the above variables will be self explanatory (yeah right!).
I’ve yet to find something that looks good from both Space and from within the atmosphere (currently when within the atmosphere either the sky is too dark or the ground is :s), I think this problem may be related to my heightmap information, but I’ll have to look further into it. but at least playing with the variables is quite fun; and produces some interesting atmospheres.