Atmospheric Scattering

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.

  1. #1 by Daniel on March 8, 2010 - 6:50 am

    Hey i was wondering if you could go over how you managed to get the shader from Sean’s article on GPU Gems 2 to work as i am having some troubles with it… any chance of a tutorial or sample ???

    Thanks for your time.

    • #2 by Lintfordpickle on March 8, 2010 - 8:55 am

      Hi Daniel,

      As I mentioned in the article, I also had trouble getting good results from the scattering, but even now that it is working I’m relutant to do a tutorial on O’Neils scattering because it isn’t my own code and although I’m confident with the implmentation, I not confident I could explain it sufficiantly enough to do a tutorial.

      There is however a tutorial and sample code with O’Neils scattering which is working here by Stainless.

      If you want though, you can also send me an email with a screenshot, and I’ll try and help you out.

      johnhampson (at) britonia-game (dot) com

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: