Community

Upcoming Events

Unite 2010

11/10 - 11/12 @ Montréal, Canada

GDC China

12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010

12/24 - 12/27

GDC 2011

2/28 - 3/4 @ San Francisco, CA

More events...

11/10 - 11/12 @ Montréal, Canada

GDC China

12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010

12/24 - 12/27

GDC 2011

2/28 - 3/4 @ San Francisco, CA

More events...

Quick Stats

64 people currently visiting GDNet.

2406 articles in the reference section.

Help us fight cancer!

Join SETI Team GDNet!

2406 articles in the reference section.

Help us fight cancer!

Join SETI Team GDNet!

## 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. */ |