Chapter 24: Advanced OpenGL

Skinned Meshes, Terrain, Skyboxes

Shadow Mapping

Deferred Shading

Ambient Occlusion

Physically-Based Rendering

Particle Systems

Collision Detection

Skyboxes

With a skybox there are two goals:

  1. Make the skybox appear behind everything (if the sky is “in front” of something then there is a problem…)
  2. Make the skybox appear to not move (you can’t get “closer” to the sky)

In practice both of these objectives can be accomplished by simply making the skybox very large, but there are a couple of problems with that.

The “proper” way to make a skybox is by:

  1. Using a trick in the vertex shader to make the skybox always behind everything
  2. Make the skybox move along with the camera so it is always around the camera

With both of these accomplished, the skybox can be quite small, e.g. a single unit cube.

How do we do these things?

First, make this the last line in your vertex shader:

gl_Position.z = gl_Position.w;

or

gl_Position = gl_Position.xyww;

This makes it so that the ndc position of the vertex is 1 in Z, meaning it is as far away from the camera as possible. This is because of the perspective/homogenous divide done by OpenGL to get coordinates from clip space into ndc.

Next, either set the skybox position to the camera position before rendering, or (sneaky) extract only the rotational portion (discarding the translation portion) of the view matrix and using that instead of the usual view matrix when rendering the skybox.

This line can be used to create a view matrix without translation components, in a vertex shader:

mat4 ViewRotation = mat4(mat3(View));