Sign in to follow this  

Making a Bezier curve oscillate

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

If you intended to correct an error in the post then please contact us.

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);
glLoadIdentity();
gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

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);
glLoadIdentity();

//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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this