Friday, April 26, 2013

Atmospheric effects in openGL

And now for some programming...

Long story short:

Here is a technique for drawing an atmosphere, which is especially useful if you want something that is quick but you're not planning on the camera moving around. Enjoy.

Anyway, some considerable time ago I wrote about making the world in 7 days using openGL and java. Of course, in reality, I'd only synthesised a tiny region, not even the size of an English county... What I'd really love is to have a whole planet to look down on.

Actually, this is a much, much simpler task, so long as you don't want to visit anywhere on the surface, as from 300km up, everything looks flat anyway (approximately).

The real trick to making a planet look lovely is the atmosphere (and bump mapping*).

Lately I've been using libGDX for my graphical work (I'm a softy for portability, and when I adopted it, it was probably the most portable graphics library around), but hopefully what I attempt here will be fairly universal.

The basic layout for me is like this:

1)Earth = sphere with earth texture in centre of screen
2)Starmap = sphere centred on camera with star texture
3)Clouds = sphere surrounding earth with cloud texture
4)Atmosphere = cone extending from the horizon of sphere (1) to behind the camera

And now for a pretty diagram:

... What can I say... I'm a biologist.

Long story short, the key is to have the cone completely transparent, but draw it with fog. You end up with something that looks a bit like this:

Which is (I think) a reasonable approximation of Earth from ~300 km up. OK, so this is a bit hacky, but it is really quick and conceptually simple**.

If you want more detail, here are the components in more detail:


Since I personally never plan to leave orbit, instead of using a sphere, I've cheated and used a convex shape to reduce the number of triangles rendered. Thus, instead of having a sphere rotating, I've had to move the texture coordinates instead. But honestly, that is simple enough. It also means that I don't need to have the whole earth texture loaded into memory, I only have a thin strip for a texture that corresponds to the area over which the viewpoint travels, assuming that I always pass the same terrain***.

I use a night (city lights) texture too, which is only visible when the sun starts sinking below the horizon.


You can't see it so well in the screenshot, but at night I make the stars more vivid by increasing the brightness. The stars are another NASA freebie (I love NASA). There's not much else to say here


Just a png texture of clouds mapped onto another convex shape with a slightly different speed of rotation, so that the same area isn't always covered by the same clouds.


As mentioned earlier, the atmosphere is just a transparent cone that extends from behind the camera to the horizon. To make it appear as the atmosphere all you do is enable fog, then draw the cone. Finally, you draw the planet in front of all that.

The density of the atmosphere can be changed by modifying the opengl fog density/fog start parameters.

If I recall correctly, the draw order was important: stars, then atmosphere cone, then planet.

Of course there are major caveats with this method:
1) if the camera moves relative to the planet, the cone has to distort accordingly
2) if the camera moves, the fog density/fog start parameters must be adapted otherwise the atmosphere will appear to grow as you move away from the planet

Which is one reason why I haven't bothered allowing the camera to move! But it makes a lovely background ;)

*Sorry, I won't be discussing bump mapping as I don't use it yet. I have reasons for this, but I advise that you use bump mapping if you can, because it looks great! But anyway, bump mapping is a fairly standard practice nowadays (here's what google thinks is the best site on the matter, but because I'm too busy to see if it actually is, here is what it thinks is the second best site on the matter. In the very least I've used NeHe's site before, and liked it...)

**Bearing in mind this is all designed to run on mobile phones...

***Yes, I've cheated and have my viewpoint make the impossible orbit along a constant longitude in the Northern hemisphere. I should have put the orbit around the equator. Sorry about that. I like the Northern hemisphere.

No comments:

Post a Comment