GameDev.netBezier Curves and Surfaces

Bezier Curves and Surfaces
## IntroductionIn this tutorial I will attempt to give you an understanding of a common way to make curved lines and surfaces. I will also give you some coding examples that will show you that it actually I will assume that you know how to use c++ in the coding examples. The coding examples also contain some OpenGL function calls, but they are commented and not too hard to understand. I will also assume that you know some basic math, but I'm starting off pretty easy by introducing parametric curves so you should be able to grasp it even if you don't have the greatest math knowledge. I have tested the coding examples, and I have looked over the text several times, so they should be fairly bug free. Still, if you find anything that looks like its wrong, let me know. You can find my email address at the bottom of the page. ## Parametric curvesYou are probably familiar with curves like
If we substitute
We now know that We can easily make a curve in 3D just by defining ## Bezier curves## Simple line
The simplest form is a straight line from a control point I will use I am introducing another variable, This parametric curve describes a line that goes from point
If you set Note that the reason this works is because ## Quadratic
Ok, now we have done lines, but what about curves? This:
Knowing that We now need three control points,
Still, if you set ## Cubic
We can also increase the number of control points using
Now we need four control points
It still works the same way as the previous ones, as You can easily use Personally I like the cubic ones best. You can easily make circles with them, and you can control the direction of the curve independently at each control point.
// Control points (substitute these values with your own if you like) double Ax = -2.0; double Ay = -1.0; double Az = 1.0; double Bx = -1.0; double By = 3.0; double Bz = 1.0; double Cx = 1.0; double Cy = -3.0; double Cz = -1.0; double Dx = 2.0; double Dy = 1.0; double Dz = -1.0; // Points on the curve double X; double Y; double Z; // Variable double a = 1.0; double b = 1.0 - a; // Tell OGL to start drawing a line strip glBegin(GL_LINE_STRIP); /* We will not actually draw a curve, but we will divide the curve into small points and draw a line between each point. If the points are close enough, it will appear as a curved line. 20 points are plenty, and since the variable goes from 1.0 to 0.0 we must change it by 1/20 = 0.05 each time */ for(int i = 0; i <= 20; i++) { // Get a point on the curve X = Ax*a*a*a + Bx*3*a*a*b + Cx*3*a*b*b + Dx*b*b*b; Y = Ay*a*a*a + By*3*a*a*b + Cy*3*a*b*b + Dy*b*b*b; Z = Az*a*a*a + Bz*3*a*a*b + Cz*3*a*b*b + Dz*b*b*b; // Draw the line from point to point (assuming OGL is set up properly) glVertex3d(X, Y, Z); // Change the variable a -= 0.05; b = 1.0 - a; } // Tell OGL to stop drawing the line strip glEnd(); /* Normally you will want to save the coordinates to an array for later use. And you will probably not need to calculate the curve each frame. This code demonstrates an easily understandable way to do it, not necessarily the most useful way. */ ## Curved Surfaces
If you think of a line as 1D, and a surface as 2D, and understand that a square can be defined as length·width, then you are not far from understanding that a curved bezier surface can be defined as a bezierCurve·bezierCurve. This can be done with any This defines our first cubic curve (leaving out the control points):
But now we need two, so here is the second one:
In the last one, The next step is to multiply them by each other:
This is still equal to one, since The cubic curves had four control points, so it makes sense that we get sixteen control points when we multiply two of them with each other. I'm calling the control points
The functions now have two variables: Now you can draw a curved surface, but to make it look good it needs proper lighting. To do that we need normal vectors for each point on the surface that we want to draw. To find the normal vectors we will go through four steps. First we need to find the derivative with respect to
If you are unfamiliar with derivation and think this seems very confusing then don't worry, this looks pretty confusing to anyone. The nice patterns we had have disappeared. Derivation is a complex theme so I will not explain it in depth here. Briefly though, the reason why we do this is that the derivative functions allow us to find tangent vectors on the surface, so later on we can use them to calculate the normal vectors. The second step is to find the derivative with respect to
Now we have a way to describe two tangent vectors on each point on the surface. This mean that we will be able to calculate the normal vector of any point on the surface. In general, if you have two vectors, This is how you cross two vectors
This is normally refered to as You might have guessed what the third step is. Since we have two tangent vectors on the surface (the derived functions:
Which is equivalent to:
The fourth step is to normalize the normal vectors (to normalize a vector means to make its length equal to one; in many cases it will be assumed that a normal vector has length equal to one). To normalize a vector we must first find the its length (L):
Then we divide it by its length to obtain a new vector (
// Sixteen control points (substitute these values with your own if you like) double Ax = -2.0; double Ay = 2.0; double Az = 1.0; double Bx = -1.0; double By = 3.0; double Bz = -1.0; double Cx = 1.0; double Cy = 3.0; double Cz = 1.0; double Dx = 2.0; double Dy = 2.0; double Dz = -1.0; double Ex = -1.5; double Ey = 1.0; double Ez = 1.0; double Fx = -0.5; double Fy = 1.5; double Fz = -1.0; double Gx = 1.5; double Gy = 1.5; double Gz = 1.0; double Hx = 2.5; double Hy = 1.0; double Hz = -1.0; double Ix = -2.5; double Iy = -1.0; double Iz = 1.0; double Jx = -1.5; double Jy = -0.5; double Jz = -1.0; double Kx = 0.5; double Ky = -0.5; double Kz = 1.0; double Lx = 1.5; double Ly = -1.0; double Lz = -1.0; double Mx = -2.0; double My = -2.0; double Mz = 1.0; double Nx = -1.0; double Ny = -1.0; double Nz = -1.0; double Ox = 1.0; double Oy = -1.0; double Oz = 1.0; double Px = 2.0; double Py = -2.0; double Pz = -1.0; /* We will not actually draw a curved surface, but we will divide the surface into small quads and draw them. If the quads are small enough, it will appear as a curved surface. We will use a variable, detail, to define how many quads to use. Since the variables goes from 1.0 to 0.0 we must change them by 1/detail from vertex to vertex. We will also store the vertices and the normal vectors in arrays and draw them in a separate loop */ // Detail of 10 mean that we will calculate 11·11 vertices int detail = 10; double change = 1.0 / (double)detail; // Vertices (maximum detail will now be 20·20 quads) double Xv[21][21]; double Yv[21][21]; double Zv[21][21]; // Normal vectors double Xn[21][21]; double Yn[21][21]; double Zn[21][21]; // Just making sure that the detail level is not set too high if(detail > 20) { detail = 20; } // Variables double a = 1.0; double b = 1.0 - a; double c = 1.0; double d = 1.0 - c; // Tangent vectors double Xta; double Yta; double Zta; double Xtc; double Ytc; double Ztc; /* Since we have two variables, we need two loops, we will change the a-variable from 1.0 to 0.0 by steps of 1/detail ( = change), and for each step we loop the c-variable from 1.0 to 0.0, thus creating a grid of points covering the surface. Note that we could have had separate detail levels for the a-variable and the c-variable if we wanted to */ for(int i = 0; i <= detail; i++) { for(int j = 0; j <= detail; j++) { // First get the vertices Xv[i][j] = Ax*a*a*a*c*c*c + Bx*3*a*a*a*c*c*d + Cx*3*a*a*a*c*d*d + Dx*a*a*a*d*d*d + Ex*3*a*a*b*c*c*c + Fx*9*a*a*b*c*c*d + Gx*9*a*a*b*c*d*d + Hx*3*a*a*b*d*d*d + Ix*3*a*b*b*c*c*c + Jx*9*a*b*b*c*c*d + Kx*9*a*b*b*c*d*d + Lx*3*a*b*b*d*d*d + Mx*b*b*b*c*c*c + Nx*3*b*b*b*c*c*d + Ox*3*b*b*b*c*d*d + Px*b*b*b*d*d*d; Yv[i][j] = Ay*a*a*a*c*c*c + By*3*a*a*a*c*c*d + Cy*3*a*a*a*c*d*d + Dy*a*a*a*d*d*d + Ey*3*a*a*b*c*c*c + Fy*9*a*a*b*c*c*d + Gy*9*a*a*b*c*d*d + Hy*3*a*a*b*d*d*d + Iy*3*a*b*b*c*c*c + Jy*9*a*b*b*c*c*d + Ky*9*a*b*b*c*d*d + Ly*3*a*b*b*d*d*d + My*b*b*b*c*c*c + Ny*3*b*b*b*c*c*d + Oy*3*b*b*b*c*d*d + Py*b*b*b*d*d*d; Zv[i][j] = Az*a*a*a*c*c*c + Bz*3*a*a*a*c*c*d + Cz*3*a*a*a*c*d*d + Dz*a*a*a*d*d*d + Ez*3*a*a*b*c*c*c + Fz*9*a*a*b*c*c*d + Gz*9*a*a*b*c*d*d + Hz*3*a*a*b*d*d*d + Iz*3*a*b*b*c*c*c + Jz*9*a*b*b*c*c*d + Kz*9*a*b*b*c*d*d + Lz*3*a*b*b*d*d*d + Mz*b*b*b*c*c*c + Nz*3*b*b*b*c*c*d + Oz*3*b*b*b*c*d*d + Pz*b*b*b*d*d*d; // Then use the derived functions to get the tangent vectors Xta = Ax*3*a*a*c*c*c + Bx*9*a*a*c*c*d + Cx*9*a*a*c*d*d + Dx*3*a*a*d*d*d + Ex*3*(2*a-3*a*a)*c*c*c + Fx*9*(2*a-3*a*a)*c*c*d + Gx*9*(2*a-3*a*a)*c*d*d + Hx*3*(2*a-3*a*a)*d*d*d + Ix*3*(1-4*a+3*a*a)*c*c*c + Jx*9*(1-4*a+3*a*a)*c*c*d + Kx*9*(1-4*a+3*a*a)*c*d*d + Lx*3*(1-4*a+3*a*a)*d*d*d + Mx*3*(2*a-1-a*a)*c*c*c + Nx*9*(2*a-1-a*a)*c*c*d + Ox*9*(2*a-1-a*a)*c*d*d + Px*3*(2*a-1-a*a)*d*d*d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*b*b*(2*c-3*c*c) + Ox*3*b*b*b*(1-4*c+3*c*c) + Px*3*b*b*b*(-1+2*c-c*c); Ytc = Ay*3*a*a*a*c*c + By*3*a*a*a*(2*c-3*c*c) + Cy*3*a*a*a*(1-4*c+3*c*c) + Dy*3*a*a*a*(-1+2*c-c*c) + Ey*9*a*a*b*c*c + Fy*9*a*a*b*(2*c-3*c*c) + Gy*9*a*a*b*(1-4*c+3*c*c) + Hy*9*a*a*b*(-1+2*c-c*c) + Iy*9*a*b*b*c*c + Jy*9*a*b*b*(2*c-3*c*c) + Ky*9*a*b*b*(1-4*c+3*c*c) + Ly*9*a*b*b*(-1+2*c-c*c) + My*3*b*b*b*c*c + Ny*3*b*b*b*(2*c-3*c*c) + Oy*3*b*b*b*(1-4*c+3*c*c) + Py*3*b*b*b*(-1+2*c-c*c); Ztc = Az*3*a*a*a*c*c + Bz*3*a*a*a*(2*c-3*c*c) + Cz*3*a*a*a*(1-4*c+3*c*c) + Dz*3*a*a*a*(-1+2*c-c*c) + Ez*9*a*a*b*c*c + Fz*9*a*a*b*(2*c-3*c*c) + Gz*9*a*a*b*(1-4*c+3*c*c) + Hz*9*a*a*b*(-1+2*c-c*c) + Iz*9*a*b*b*c*c + Jz*9*a*b*b*(2*c-3*c*c) + Kz*9*a*b*b*(1-4*c+3*c*c) + Lz*9*a*b*b*(-1+2*c-c*c) + Mz*3*b*b*b*c*c + Nz*3*b*b*b*(2*c-3*c*c) + Oz*3*b*b*b*(1-4*c+3*c*c) + Pz*3*b*b*b*(-1+2*c-c*c); // Cross the tangent vectors, put the result to the normal vector array // Note: I simplified -((Xta*Ztc)-(Xtc*Zta)) to (Xtc*Zta) - (Xta*Ztc) Xn[i][j] = (Yta*Ztc) - (Ytc*Zta); Yn[i][j] = (Xtc*Zta) - (Xta*Ztc); Zn[i][j] = (Xta*Ytc) - (Xtc*Yta); // Find length of normal vector double length = sqrt((Xn[i][j]*Xn[i][j])+(Yn[i][j] *Yn[i][j])+(Zn[i][j]*Zn[i][j])); // Normalize (and prevent divide by zero error) if(length > 0) { length = 1.0/length; Xn[i][j] *= length; Yn[i][j] *= length; Zn[i][j] *= length; } //change the c-variable within the inner loop c -= change; d = 1.0 - c; } //change the a-variable outside the inner loop a -= change; b = 1.0 - a; // Reset the c-variable to make it ready for the inner loop again c = 1.0; d = 1.0 - c; } /* Now we have two arrays, one with vertices, and one with normal vectors, drawing them is straightforward if you know how to use a graphics API. Following is one way to do it using openGL and triangle strips. (assuming GL_LIGHTING etc.. has been properly set up) */ for(int m = 0; m < detail; m++) { glBegin(GL_TRIANGLE_STRIP); for(int n = 0; n <= detail; n++) { glNormal3d(Xn[m][n],Yn[m][n],Zn[m][n]); glVertex3d(Xv[m][n],Yv[m][n],Zv[m][n]); // Note that I used real-less-than in the first loop, since I want to // access the m+1 entry in the array to properly draw the triangle strip glNormal3d(Xn[m+1][n],Yn[m+1][n],Zn[m+1][n]); glVertex3d(Xv[m+1][n],Yv[m+1][n],Zv[m+1][n]); } glEnd(); } ## CommentBezier curves do not necessarily have to be curves in 3D or 2D space, they might just as well be color curves (by substituting x,y,z coordinates with r,g,b values), curves in 4D space or anything else. Personally I find it useful to use a 2D bezier patch to calculate texture coordinates that fit on a 3D bezier surface. ## SourcesVarious web sites and my head ;) ## Questions, comments, report mistakes etc.My name: Jesper Tveit
For background info, try a google search for "Etienne Bézier" That's it! Thanks for your attention
© 1999-2011 Gamedev.net. All rights reserved. |