# Making a Bezier curve oscillate

This topic is 2565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How might one go about making a Bezier curve oscillate? I've been trying to do this all day today by changing the control points back and forth on each pass through the drawScene function but to no avail. Here is the relevant code I've written:

//The control points
GLfloat ctrlPoints[4][3] = {{-4.0, 0.0f, 0.0f}, {-6.0f, 4.0f, 0.0f},
{6.0f, -4.0f, 0.0f}, {4.0f, 0.0f, 0.0f}};

//The flag for oscillating the points
int flag = 0;

//Your standard resize function, make note of the call to gluOrtho2D
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f);

glMatrixMode(GL_MODELVIEW);
}

void drawScene()
{
//Clear the screen and load the identity matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

//Lets get this party started by setting up the Bezier
glMap1f(GL_MAP1_VERTEX_3, 0.0f, 100.0f, 3, 4, &ctrlPoints[0][0]);

//Enable the evaluator
glEnable(GL_MAP1_VERTEX_3);

//Make the curve red
glColor3f(1.0f, 0.0f, 0.0f);

//Begin drawing the curve with a line strip connecting the points
glBegin(GL_LINE_STRIP);

//Loop through the u range and produce the points
for(int i = 0;i <= 100; i++)
glEvalCoord1f((GLfloat) i);

//End the drawing
glEnd();

//And disable the evaluator
glDisable(GL_MAP1_VERTEX_3);

//Now swap buffers
glutSwapBuffers();

//If the flag is 0 and the second control y-point is greater than the y-point
//at which it bottoms out then keep decrementing the second y-point and keep
//incrementing the third y-point
if(flag == 0 && ctrlPoints[1][1] >= -4.0f)
{
ctrlPoints[1][1] = ctrlPoints[1][1] - (GLfloat)0.1f;
ctrlPoints[2][1] = ctrlPoints[2][1] + (GLfloat)0.1f;
}
else if(flag == 0 && ctrlPoints[1][1] < -4.0f)//Otherwise flip the flag
flag = 1;

//If flag is 1 and the second y-point is less than its top y-point
//then do the opposite as when the flag was 0
if(flag == 1 && ctrlPoints[1][1] <= 4.0f)
{
ctrlPoints[1][1] = ctrlPoints[1][1] + (GLfloat)0.1f;
ctrlPoints[2][1] = ctrlPoints[2][1] - (GLfloat)0.1f;
}
else if(flag == 1 && ctrlPoints[1][1] > 4.0f)
flag = 0;
}

Any help would be appreciated.

##### Share on other sites
How does you curve change? Bezier curve is sensitive to the change of each control point. In another word, if you change one of control points, your whole Bezier curve would change. Try use B-spline curve. The control point of b-spline curve can affect the curve more precisely, the change of one of control points will only cause the change of part of the curve around that control point.

don't know if I get your point, just about what I think.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001962
×