Tutorial: Texturing and Skybox

Simple Texture Viewer

Example output of the texture viewer program

The simple texture viewer in this tutorial uses the stb image loader - which is a pure header file "library" (like the STL in C++).

There are other simple image loading libraries including SOIL and SOIL2 library (Simple OpenGL Image Library) SOIL2. Both are layered over stb.

Loading a Texture

Texture Mapping

Skybox

A skybox is a use of texture mapping to create a background for a scene, as in the above screenshot of an 'Island Defence' shooting game, where the cloudy sky in the distance is in fact texture mapped quads.

A skybox is a set of six textures which form a cube at distance 1 around the camera. They form a background image with an “infinite” depth. The textures may be separate or all six may be packed into the one texture with varying layouts. They are created with a perspective distortion so that they may be drawn directly as a cube with the camera in the exact center (it is incorrect to move the camera from the center).

It is not possible to physically draw the skybox at infinity. The standard projection matrix will clip geometry outside the near/far planes. Attempting to increase the near/far range will result in depth fighting artifacts. Instead, draw the skybox first without depth testing enabled. Now the box will not affect the depth testing of any following geometry.

Incorrect application of the textures of a skybox can cause a seam at the edges. One possible solution is to offset the coordinates at the edge to half a texel (0.5/width, 0.5/height). Another solution is to provide the correct arguments to glTexParameteri(...) before rendering the texture, with regards to texture clamping. Here are the skybox textures for the above example. Feel free to use a different set of images if you prefer.