Reflecting and refracting the world!


A skybox is simply a cube made up of six images that mimic an enviroment. Each side of the cube is essentially a 2d texture; however, the skybox itself will be stored as just one texture. By wrapping the enire world with a texture, the viewer is able to move the camera around 360 degrees and view the entire scene. Models can still be placed throughout the world and the skybox will simply be a backdrop.

The idea behind this is to place the camera at the center of the cube. As the camera moves the cube moves along with it, this way the viewer gets a sense of an 'infinte' world by never being able to reach the horizon. In order to create this endless world we need a texture that is created in a way such that if it is cut and folded properly it creates a box where the contents along the edges of the internal faces are perfectly aligned with one another and create a sense of continuity for someone who is located inside the box

Example of a skybox texture:

In order to sample from a cubemap we have to provide a 3d direction vector which points from the center of the cube to the point on the texture that we want to sample. The best thing about cubmaps and why they're so ideal for skyboxes is that the vertex positions of a cube can also be used as the direction vectors for sampling the cubemap. This way we dont know need any texture coordinates and we can just create a cube model from a load of vertex positions. These vertex positions will then simply double up as the direction vectors to sample from a cubemap in the fragment shader.

Example of using texture coordinates as direction vectors:


One of the more common enviroment mapping techniques is reflection. Reflection can simply be thought as a property of an object that copies its surrounding environment based on the viewers angle.

The principles behind reflections can be straight-forward. In general terms, our goal is to calculate a reflection vector that is used to sample a specific part of a cubemap based on the camers position. In order to do this we must first calculate the object's normal vector based on the view direction vector. Using GLSL's reflection function we are able to calculate the reflection vector. This reflection vector is then used as a direction vector to sample the cubemap which in turn returns a color value of the enviroment.


Another popular form of environment mapping is refraction. Refraction is simple the change in direction of light due to the material the light flows through. This change in direction is governed by Snell's Law.

In order to calculate the refracton vector we again need to calculate the normal and view vectors first. We can then use GLSL's refract function, which takes in the normal vector, view vector, and a ratio. The third parameter being the ratio between the both material's refractive indices. Some common indices being:

Material Refractive Index
Air 1.00
Water 1.33
Glass 1.52
Diamond 2.42


External Resources

Simple OpenGl Image Library

Cube Maps: Sky Boxes and Environment Mapping

Skybox Textures