The Skyquad

A skyquad is to a quad what a skybox is to a box. That is, a skyquad is a method of rendering a sky by sending just a single quad down the pipeline. The skyquad as a concept is useful not only in OpenGL, but also in ray tracing, where all we have is a bunch of rays passing through an image plane. In fact, I derived the skyquad from the more general ray equation of my path tracer, so I will start with that first.

The Ray Equation

In a ray tracer, we have a camera and a window into the world - the to-be-rendered-image - and the goal is to determine the ray passing through each of the pixels of the image. This is depicted in the following figure:

Ray equation

The problem of determining a ray through a given pixel can be summarised as follows, using the same notation as in the figure:



In addition, we will use the following conventions:

Note that $(x,y)$ need not correspond with the integer coordinates of a pixel; it can be any real point on the image plane.

Determining the distance to the image plane

The first step is determining the distance $d$ from the camera to the image plane. The following figure offers a simplified view of the problem for this particular purpose:

Image plane distance

From the figure, we can see that:

$$tan(\frac{fovy}{2}) = \frac{h/2}{d}$$

Solving for $d$ yields:

\begin{align} tan(\frac{fovy}{2}) &= \frac{h/2}{d} \\ & \Leftrightarrow \\\ d &= \frac{h/2}{tan(\frac{fovy}{2})} \\ &= \frac{h}{2}\;cot(\frac{fovy}{2}) \end{align}

Deriving the ray equation

Next, from the first figure, we can observe that the center of the image is determined by:

$$\vec{c} = \vec{o} + d\vec{f}$$

To reach out the target point $(x,y)$ from $\vec{c}$, we offset $\vec{c}$ by a delta. We push $\vec{c}$ $d_x$ units along the camera's right vector $\vec{r}$, and $d_y$ units along the camera's up vector $\vec{u}$:

$$[x,y] = \vec{c} + d_x\vec{r} + d_y\vec{u}$$

Deriving $d_x$ and $d_y$ is straightforward. When $x=0$, $d_x=-w/2$, and when $x=w$, $d_x=w/2$. The case for $d_y$ is similar. This results in:

$$d_x = x - \frac{w}{2}$$

$$d_y = y - \frac{h}{2}$$

The vector from the camera position $\vec{o}$ to the point $(x,y)$ on the image plane is then given by:

\begin{align} \vec{v} &= [x,y] − \vec{o} \\ &= \vec{c} + d_x\vec{r} + d_y\vec{u} - \vec{o} \\ &= \vec{o} + d\vec{f} + d_x\vec{r} + d_y\vec{u} - \vec{o} \\ &= d\vec{f} + d_x\vec{r} + d_y\vec{u} \\ &= d\vec{f} + (x - \frac{w}{2})\vec{r} + (y - \frac{h}{2})\vec{u} \end{align}

Finally, the ray equation is $r = \vec{o} + \lambda\vec{t}$, where $\vec{t}$ is conveniently defined as $\vec{v}$ normalised: $\vec{t} = \frac{\vec{v}}{||\vec{v}||}$.

The Skyquad in OpenGL

To draw the skyquad, we render a quad directly in NDC space [1], with $z=1$ to force it to appear in the background. For each pixel in the quad, we compute the ray originating from the camera and passing through that pixel. We then use the ray's direction to sample a cubemap texture holding the skybox data, and the fetched texel determines the colour value that we finally assign to that pixel.

To simplify our calculations, we compute the ray direction in NDC space and then apply the viewport's aspect ratio $r$ and the camera's rotation matrix $R$ to transform it into world space. NDC space has many interesting properties that make our life remarkably easier:

With the above properties in mind, the distance to the image plane $d$ becomes:

\begin{align} d &= \frac{h}{2}\;cot(\frac{fovy}{2}) \\ &= \frac{1}{2}\;cot(\frac{fovy}{2}) \end{align}

The ray direction vector in NDC space boils down to:

\begin{align} \vec{v}' &= d\vec{f} + x - \frac{w}{2}\vec{r} + (y - \frac{h}{2})\vec{u} \\ &= d[0,0,-1] + x - \frac{1}{2}[1,0,0] + (y - \frac{h}{2})[0,1,0] \\ &= [x - \frac{1}{2}, y - \frac{1}{2}, -d] \end{align}

Next, apply the viewport's aspect ratio $r$ to properly scale the ray:

$$v = v' * [r,1]$$

And finally, normalise the vector and apply the camera's rotation matrix $R$ to obtain the direction in world space:

$$\vec{t} = R \frac{\vec{v}}{||\vec{v}||}$$

To render the skyquad, we send a 2d quad with coordinates ranging from $(-1,-1)$ to $(1,1)$ down the pipeline with the shader program that follows. Note that we need not compute a ray per pixel; instead, we compute a ray for every vertex of the quad and let the pipeline interpolate them:

Vertex shader

uniform mat3 Rotation;
uniform float fovy;
uniform float aspect;

layout (location = 0) in vec2 Position;

out vec3 Ray;

vec3 skyRay (vec2 Texcoord)
    float d = 0.5 / tan(fovy/2.0);
    return normalize(vec3((Texcoord.x - 0.5) * aspect, Texcoord.y - 0.5, -d));

void main ()
    Ray = Rotation * skyRay(Position*0.5 + 0.5); // map from [-1,1] to [0,1]
    gl_Position = vec4(Position, 0.0, 1.0);

Fragment shader

uniform samplerCube tex;

in vec3 Ray;

layout (location = 0) out vec4 Colour;

void main ()
    vec3 R = normalize(Ray);
    Colour = vec4(pow(texture(tex, R).rgb, vec3(1.0/2.2)), 1.0);

Certainly a lot of maths for such a simple shader! Fortunately they did pay off, and we have a general solution that works for both ray tracing and OpenGL.

And as always, the mandatory screenshot:


[1] Technically it is clip space, but since we set $w=1$ in the shader then the perspective division leaves the coordinates unchanged.