GDC 2001: Implementing Multi-colored Volumetric Fog Without Using up Texture Stages

The Fog Volumes

In order to have volumetric fog, we'll need to somehow define the volumes of space where the fog will be confined. There are many different volumes to choose from, but in this talk we will concentrate on three basic volumes that are simple but very powerful: the half-space, the sphere and the ellipsoid, and also in the classic distance fog. Other volumes (cylinders, cones, convex hulls, etc…) can easily be used, provided that we research the appropriate math.

We will be measuring the distance that a ray of light traverses the different fog volumes, so we will concentrate in the mathematical formulas for the intersection between a ray and the volumes. We will define the ray of light in reverse: originating at the observer point (O) and extending in the direction of the geometry being rendered (D). We will use the parametric formula of a straight line:

X = O + D * t

Where X is a point on the line and t is the parameter, which is 0 at the observer and 1 at the object being rendered. The value of the parameter t for the intersection points is the result we will want. We will assume that any volumes used are non concave, so that only one segment of the ray will traverse through each of them. We will call t1 to the parameter for the point where the ray enters the fog volume and t2 to the parameter for the point where the ray exits. Note that the values calculated can be outside of the range (0,1), in which case they will be clamped to that range later on in the actual algorithm.

The Classic Distance Fog

The points of t1 and t2 are easily calculated from minFog and maxFog as:

t1 = minFog / magnitude(D)
t2 = maxFog / magnitude(D)

See Figure 1 for reference.

The Half-Space

In 3D, an infinite plane divides the space into two half-spaces. The half space is defined mathematically by a vector normal to the plane (N) and a point contained in the plane (P). Its general formula is:

(X – P) * N > 0

Therefore, its intersection with the ray of light is (see Figure 3):

t > ((P–O)·N) / (D·N) (if D · N > 0)
t < ((P–O)·N) / (D·N) (if D · N < 0)

So the half-plane of fog will extend towards infinity in the view direction in the first case, and backwards behind the observer in the second case. In the fog calculations, we will be using two points, which we will call t1 and t2, which represent respectively the point of entry into the fog and the point of exit. Therefore, we will want to make t1 = t and t2 = infinity, or t1 = -infinity and t2 = t, as appropriate depending on the sign of D · N.

If D · N is 0, then the ray doesn’t intersect the volume boundary. In this case, if (P – O) · N < 0, then the whole ray is inside of the volume, while if (P – O) · N > 0, then the ray is outside of the volume.

Figure 3. The half-space volume.

The Sphere

A sphere in a 3D space is mathematically defined as its center point (C) and its radius (r). Its general formula is:

(X – C)² < r²

Therefore, its intersections with the ray of light is (see Figure 3):

t1 = {-(O–C)·D - sqrt( ²- D²*[(O-C)² - r²] )} / D²
t2 = {-(O–C)·D + sqrt(² - D²*[(O-C)² - r²] )} / D²

These formulas will always result in t1 <= t2, so the sphere of fog will lie between t1 and t2.

If [(O – C) · D]²- D²· [(O - C)² - R²] is less than or equal to 0, then the ray doesn’t intersect the volume.

Figure 4. The sphere volume.

The Ellipsoid

Both the half-plane and the sphere are simple volumes that can be manipulated analytically without problem. But the ellipsoid is more complicated, so we will use a little trick that simplifies the calculations significantly.

An ellipsoid can be seen as a unit sphere that has been scaled, stretched, rotated and translated in space. Now, scale, stretch, rotation and translation are all linear transformations that can be specified and manipulated using 4x4 matrices. The key here is that the resulting 4x4 matrix defines a linear transformation that is reversible, so we can always compute the inverse matrix (E), which can be used to transform the ray into the space where the ellipsoid becomes the unit sphere. Not only that, the t1 and t2 values calculated in that space are valid also in the world or camera spaces, where the ray was defined.

Transforming the ray is easy. It consists on transforming O and D into two new vectors Oe and De:

Oe = O * E
D
e = D * E

So then we can use the formulas we calculated previously for the sphere, only this time we know that C = 0 and r = 1:

t1 = {-De·Oe - sqrt( (Oe·De)²- De²*(Oe² – 1) )} / De²
t2 = {-De·Oe + sqrt( (Oe·De)² - De²*(Oe² – 1) )} / De²

Again, we know that t1 <= t2, so the ellipsoid of fog will lie between t1 and t2.

Also, if (Oe·De)²- De²*(Oe² – 1) is less than or equal to 0, then the ray doesn’t intersect the volume.

Figure 5. Ellipsoid fog volume.

________________________________________________________

Volumetric Fog Raycasting