[ Pobierz całość w formacie PDF ]
.11.467Figure 9.11: Two curves meeting with C0 continuityWe can fix this by achieving C1 continuity.In it, the second control point of the second curve is colinearwith the last two points of the previous curve, but not the same distance from the first control point thatthe third control point of the previous curve is.Curves with C1 continuity appear smooth, as shown inFigure 9.12.Figure 9.12: Two curves meeting with C1 continuityTo make our curves seem totally smooth, we must go for C2 continuity.To do this, the distance betweenthe third and fourth control points of one curve must be the same direction and same distance apart asthe first and second control points of the next one.This puts serious constraints on how we can modelour Bezier surfaces, however.The restrictions we have to impose give us an extremely fair, extremelysmooth looking joint connecting the two curve segments, as shown in Figure 9.13.468Figure 9.13: Two curves meeting with C2 continuityThe MathEveryone put on your math caps; here comes the fun part.We'll define Bezier curve of degree nparametrically, as a function of t.We can think of t being the time during the particles' travel.The tvariable varies from 0.0 to 1.0 for each Bezier curve.where Bi,n(t) is the Bernstein polynomial:The vector pi is control point i.Let's work out the equations for our cubic (n = 3) curves.To help with the flow of the derivation we'regoing to do, we'll expand each equation so it's in the form of ax3+bx2+cx+d.469Putting everything together, we get:The equation can be solved using vector mathematics, or we can extract the x, y, and z components ofeach control point and solve the curve position for that component independently of the other two.Some insight as to how this equation generates a curve for us comes when we graph the equation.Thegraph of the four Bernstein blending functions appears in Figure 9.14.470Figure 9.14: A graph of the four blending functionsNote that at t = 0 we are only influenced by the first control point (the Bernstein for the others evaluatesto 0).This agrees with the observation that at t = 0 our curve is sitting on top of the first control point.The same is true for the last point at t = 1.Also note that the second and third points never get tocontribute completely to the curve (their graphs never reach 1.0), which explains why the middle twocontrol points do not intersect (unless our control points are all collinear, of course).Finding the Basis MatrixThe equation presented above to find Bezier curve points is a bit clunky, and doesn't fit well into the 3Dframework we've set up thus far.Luckily, we can decompose the equation into matrix-vector math, aswe'll soon see.Let us consider each coordinate separately, performing the equations for x, y, and z separately.Sowhen we write p0, for example, we're referring to one particular coordinate of the first control vector.Ifwe think about the control points as a vector, we can rewrite the equation as a dot product of two 4Dvectors:Note that the equations for y and z are identical, just swapping the corresponding components.We'llexclude the component subscripts for the rest of the equations, but keep them in mind.Now, each termin the second vector is one of the Bernstein terms.Let's fill in their full forms that we figured out above.(I took the liberty of adding a few choice zero terms, to help the logic flow of where we're taking this.)Hmmm & well, this is interesting.We have a lot of like terms here.As it turns out, we can represent theright term as the result of the multiplication of a 4×4 matrix and the vector.471NoteIf you don't follow the jump, go to Chapter 5 to see how we multiply 4×4 and 1×4matrices together.Try working it out on paper so you see what is happening.If you've followed up to this point, pat yourself on the back.We just derived MB, the basis matrix forBezier curves:Now we're golden: We can find any point p(t) on a Bezier curve.For each component (x, y, and z), wemultiply together a vector of those components from the four control points, the vector , andthe basis matrix MB.We perform the 1D computation for all three axes independently.Calculating Bezier CurvesSo this begs the question of how we render our Bezier curves.Well, the way it's typically done isstepping across the curve a discrete amount of steps, calculating the point along the curve at that point,and then drawing a small line between each pair of lines.So our curves are not perfectly curvy (unlesswe calculate an infinite amount of points between t = 0 and t = 1, which is a bit on the impossible side).However, we're always bound to some resolution below which we don't really care about.In printing, it'sthe dots-per-inch of the printer, and in video it's the resolution of the monitor.So if we calculate thecurve such that each sub-line is less than one pixel long, it will appear exactly as the limit curve would,and has much more feasible memory and computational constraints.Here's the code to do it.Listing 9.3: The cSlowBezierIterator classmatrix4 cBezierPatch::m_basisMatrix = matrix4(-1.0f, 3.0f, -3.0f, 1.0f,3.0f, -6.0f, 3.0f, 0.0f,-3.0f, 3.0f, 0.0f, 0.0f,472 1.0f, 0.0f, 0.0f, 0
[ Pobierz całość w formacie PDF ]