146 II Rendering Techniques
10.2 Rendering Terrains with OpenGL GPU
The goal of this section is to present a heightmap-based, fully tessellated terrain ren-
dering implementation, upon which the LOD solutions will grow (see Figure 10.1).
We assume that the heightmap is a regular grid, represented by a greyscale image
loaded as a texture. However , the terrain size is not limited by the texture ’s size, as
height values between texels can be sampled. The GPU has dedicated hardware for
sampling, such as GLSL texture* functions, according to the sampler or texture
sampler state. The terrain size, in terms of grid points, is, therefore, theoretically
unlimited. To avoid the almost ﬂatness of the regions represented by the sampled
points, noise-based approaches can be used to provide high-frequency detail.
The terrain size, in terms of physical units, can be further parameterized by deﬁn-
ing a grid spacing; in other words, the number of units between two consecutive
points in the ﬁnal grid.
To render the terrain, we use the new primitive introduced with tessellation,
the patch. A patch can cover as many grid points as the maximum tessellation
levels permitted by hardware, a square grid of 64 quads in the current OpenGL 4.0
hardware.Thisdeﬁnesapatchas65× 65 vertices, as the edges of patches are shared
between adjacent patches. To render a terrain, we deﬁne a grid of such patches. As
an example, to render a terrain of 8K × 8K points, a grid of 128 × 128 patches
is required. Other patch sizes are possible, but the reported tests (shown later in
Figure 10.9), show a per formance penalty when using smaller patches.
Since the terrain grid is a highly regular structure, only one vertex to deﬁne a
patch is needed (e.g., the lower left corner). The regular nature of the terrain’s grid
allows the developer to compute all other patch elements based solely on this vertex.
The ﬁnal vertex positions, texture coordinates, and normals will be computed in the
The patch positions are deﬁned as if the terrain were to be drawn in a normalized
square, ranging from 0 to 1. A translation and scale operation will be applied in the
tessellation evaluation shader to place the terrain where n eeded.
Figure 10.1. Full tessellation (left); high LOD (middle); low LOD (right).