Subscribe to The Gamer's Algorithms to Goove By

## The Skybox

So we've basically covered how to make a functional terrain, but just turning the background blue may not make for a very convincing "sky". This next algorithm is probably the most simple, but is so commonly used in 3D games that it may be one of the more important ones.

Basically, a skybox is just a big cube that is textured with a sky picture and displayed inside out. Since you, represented by the camera, are inside the box, it must be textured inside out. This means the face normals of the box must point inwards. The face normals are just the direction each of the 6 faces of the cube face. The box needs to be big enough to make the sky appear to be off in the infinate distance.

Really, the only "catch" to this is that the box must always be centered on the camera. And if the camera changes orientation, the skybox must not. So to recap, the skybox changes positions every time the camera does (to keep the skybox centered on the camera) and the skybox never changes orientation even if the camera does. If the sky were to roll and pitch with the camera, you would not be able to tell the camera was actually moving unless there were other objects and even then it would look "wrong". If it were not for these rules, I probably couldn't even get away with calling this an "algorithm" because its so simple.

You can make this box any way you like. You can go into a modeling program like Blender, 3D Studio Max, or Maya and just make a 1 by 1 by 1 cube, turn it inside out (with the facing of the normals), and texture it. Then you can scale it to any size you want in your code, as long as it's large enough to appear infinately far away.

You can also do it procedurally by making 6 square quads, texture them facing inward, and positioning them to form a cube. You probably want to do this as a static vertex buffer on the graphics card since the skybox is probably never going to change. The big difference here is that you might be assigning one texture to each of the 6 faces, whereas doing a textured cube you might combine all 6 faces of the cube into one UV map texture.

I've found that you usually don't need a very high resolution texture for a skybox. Part of the algorithm is that the skybox is always centered at the camera, and so when the camera moves, the skybox goes with it. That means the skybox can never be reached by the camera and it is thus infinately far away.

You might be worried that you can see the seams of the box edges with this method. When a skybox texture is properly created, you would be surprised at how hard it is to tell that its a box. Generally, if you can tell that its a box, the texture was done wrong. There are programs out there that will help you create skybox textures as well as skybox textures that you can download off the Internet for free or for a fee.

http://virtuallyprog...nt/Content.html

Generally, skybox cubes are UV unwrapped to a single texture in a "t" shape. This shape can be in any of the four cardinal directions. The UV texture has to match the UV mapping of the cube model.

Keeping the skybox centered is simple. You just set the skybox's position equal to the camera position every time the camera is moved. Sometimes, I also find it helpful to offset the skybox vertically to get it to look right on the horizon. Just make sure not to rotate the skybox when the camera rotates. And that's all there is to it.

Now, I should also point out that you don't actually have to use a box for a "skybox". You can use sky spheres, sky planes, or any other shape. With a sky plane you probably want to curve the "plane" down below the horizon, so that the camera can't see the edges. Regardless, of which you choose, you never want to allow the user to get to a point where they can see off the end of the terrain and see what's below. You can simply reposition the player when they move close to an edge. Or you can place physical objects in their way. A common trick in video games has always been to use unscalable walls and unscalable mountains.

You might use a sky hemisphere. If you take half a sphere and stretch it out over the terrain, it can do a very good job as a skybox. And you may be able to animate the texture on it to move the sun across the sky and even transition into a night time texture. But I would recommend just using a plain ol' skybox for the first year or so; for most beginner to intermediate projects a skybox will work just fine. But if you do go with another shape, the algorithm is the same: just keep it centered on the camera as the camera moves and don't rotate it with the camera.

### 0 Comments On This Entry

There are no Trackbacks for this entry

S M T W T F S
12345
6789101112
13141516171819
202122 23 242526
27282930