DirectX 8 Graphics introduced programmable vertex shaders, which allows the vertex processing of Direct3D to be replaced by application specific code. This code, known as a vertex shader, performs the transformation and lighting of vertices, determining their final position, color parameters, and texture coordinates.
In this article, we will examine a novel method for rendering landscapes, which uses a vertex shader to perform real-time modeling of bicubic patches in the 3D pipeline.
Splines are a mathematical means of representing a curve, by specifying a series of points at intervals along the curve and defining a function that allows additional points within an interval to be calculated. There are various functions available to approximate a curve, but in this article we will focus on the Catmull-Rom spline.
The points that define the curve are known as "Control Points". One of the features of the Catmull-Rom spline is that the specified curve will pass through all of the control points - this is not true of all types of splines.
This formula gives Catmull-Rom spline the following characteristics:
While a spline segment is defined using four control points, a spline may have any number of additional control points. This results in a continuous chain of segments, each defined by the two control points that form the endpoints of the segments, plus an additional control point on either side of the endpoints. Thus for a given segment with endpoints Pn and Pn+1, the segment would be calculated using [Pn-1, Pn, Pn+1, Pn+2].
Because a segment requires control points to the outside of the segment endpoints, the segments at the extreme ends of the spline cannot be calculated. Thus, for a spline with control points 1 through N, the minimum segment that can be formulated is P2<->P3, and the maximum segment is PN-2<->PN-1. Thus, to define S segments, S+3 control points are required.
To express this algebraically, given function f(t,PA,PB,PC,PD) which calculates a point in a spline segment, then the calculation of a point (u,v) on a surface from control points P1...P16 would be:
f( v, f(u,P1,P2,P3,P4), f(u,P5,P6,P7,P8), f(u,P9,P10,P11,P12),
Figure 5, below, illustrates the complete flow of data that occurs when calculating a point in a patch.
The processing cost of that operation is quite high to be performed every frame - even given the simplified form presented in Equation 2, the operation would require around 330 floating point operations for each generated 3D vertex. Because of this, landscape meshes are typically created at load time. In a large scene, memory footprint can become an issue, so it is may be necessary to store a local area and re-generate portions of a landscape on the fly as the viewer moves into them. Doing so while maintaining a steady frame rate can require a bit of finesses, so as not to suffer periodic frame rate drops due to processing overhead.
Downloadable demo for this article
Visitors Since 1/1/2000: