What you’ll learn in this chapter:
For most applications that make use of 3D graphics, you’ll need smooth curves and surfaces. Making use of the techniques discussed elsewhere in this book, you could divide such a surface into many smaller quads or triangles, then calculate the normals at the various vertices, and apply lighting—producing what appears to be a very smooth and flowing surface. Or, with little more than basic algebra you could even write code that evaluates an equation for a surface and uses something like triangle strips or quads to generate a surface with either a fine or coarse visual resolution.
Suppose, however, you want to create a curve or surface and you don’t have an algebraic equation to start with. It’s far from a trivial task to figure it out in reverse, starting from what you visualize as the end result and working down to a second- or third-order polynomial. Taking a rigorous mathematical approach is time consuming and error prone, even with the aid of a computer. And forget about doing it in your head.
Recognizing this fundamental need in the art of computer-generated graphics, Pierre Bazier, an automobile designer for Renault in the 1970s, created a set of mathematical models that could represent curves and surfaces by specifying only a small set of control points. In addition to simplifying the representation of curved surfaces, the models facilitated interactive adjustments to the shape of the curve or surface..
Other types of curves and surfaces, and indeed a whole new vocabulary for computer-generated surfaces soon evolved. The mathematics behind this magic show are no more complex than the matrix manipulations in Chapter 7, and an intuitive understanding of these curves is easy to grasp. As we did in Chapter 7, we will take the approach that you can do a lot with these functions without a deep understanding of their mathematics.
A curve has a single starting point, a length, and an endpoint. It’s really just a line that squiggles about in 3D space. A surface, on the other hand, has width and length and thus a surface area. We’ll begin by showing you how to draw some smooth curves in 3D space, and then extend this to surfaces. But first let’s establish some common vocabulary and math fundamentals.
When you think of straight lines, you may think of this famous equation:
Y = mX + b
Here m equals the slope of the line, and b is the Y intercept of the line (the place where the line crosses the y-axis). This may take you back to your eighth-grade algebra class, where you also learned about the equations for parabolas, hyperbolas, exponential curves, and so on. All of these equations expressed Y (or X) in terms of some function of X (or Y).
Another way of expressing the equation for a curve or line is as a parametric equation. A parametric equation expresses both X and Y in terms of another variable that varies across some predefined range of values, that is not explicitly a part of the geometry of the curve. Sometimes in physics, for example, the X, Y, and Z coordinates of a particle may be in terms of some functions of time, where time is expressed in seconds. In the following, f(), g(), and h() are unique functions that vary with time (t):
X = f(t) Y = g(t) Z = h(t)
When we define a curve in OpenGL, we will also define it as a parametric equation. The parametric parameter of the curve, which we’ll call u, and its range of values will be the domain of that curve. Surfaces will be described using two parametric parameters: u and v. Figure 17-1 shows both a curve and a surface defined in terms of u and v domains. The important thing to realize here is that the parametric parameters (u and v) represent the extents of the equations that describe the curve; they do not reflect actual coordinate values.
Curves are represented by a number of control points that influence the shape of the curve. For the Bazier curves, the first and last control points are actually part of the curve. The other control points act as magnets, pulling the curve towards them. Figure 17-2 shows some examples of this concept, with varying numbers of control points.
The order of the curve is represented by the number of control points used to describe its shape. The degree is one less than the order of the curve. The mathematical meaning of these terms pertains to the parametric equations that exactly describe the curve, with the order being the number of coefficients, and the degree being the highest exponent of the parametric parameter. If you want to read more about the mathematical basis of Bazier curves, see Appendix B.
The curve in Figure 17-2(b) is called a quadratic curve (degree 2), and Figure 17-2(c) is called a cubic (degree 3). Cubic curves are the most typical. Theoretically, you could define a curve of any order, but higher-order curves start to oscillate uncontrollably and can vary wildly with the slightest change to the control points.