I am creating a Bezier curve tool in OpenGL and every time my program draws something in motion with cursor coordinates I see the previously drawn lines on the screen for a second or two before they disappear as shown below:
This is how it should look:
Below are my mouse motion functions and render function. I am using a double buffer and swap buffers each frame.
void passiveMotion(int x, int y)
{
Point Cursor;
Cursor.setxy(x, SCREEN_HEIGHT - y);
if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
{
drawLine(pointsVector[points], Cursor);
}
glutSwapBuffers();
}
void motion(int x, int y)
{
Point Cursor;
Cursor.setxy(x, SCREEN_HEIGHT - y);
drawLine(pointsVector[points], Cursor);
Point reverseCursor;
reverseCursor.x = (2 * pointsVector[points].x) - Cursor.x;
reverseCursor.y = (2 * pointsVector[points].y) - Cursor.y;
drawLine(pointsVector[points], reverseCursor);
vector<Point> finalPoints = final4Points();
Point p1;
p1.setxy(pointsVector[0].x, pointsVector[0].y);
if ((finalPoints[1].x != NULL) && (finalPoints[1].y != NULL))
{
// draw bezier curve
for (float i = 0; i < 1; i += 0.01)
{
// The Green Lines
int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
int xb = getPt(finalPoints[1].x, reverseCursor.x, i);
int yb = getPt(finalPoints[1].y, reverseCursor.y, i);
int xc = getPt(reverseCursor.x, finalPoints[3].x, i);
int yc = getPt(reverseCursor.y, finalPoints[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
p1 = p2;
}
}
glutSwapBuffers();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 1; i < points; i++)
{
// draw main line & dot
glColor3f(255, 0, 0);
drawDot(pointsVector[i]);
glColor3f(0, 0, 0);
drawLine(pointsVector[i], pointsVector[i + 1]);
// draw tangent lines and tangent dots
glColor3f(0, 255, 0);
drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
glColor3f(0, 0, 255);
drawDot(controlPoints[i + 1]);
drawDot(reverseControlPoints[i + 1]);
vector<Point> finalPoints = final4Points();
Point p1;
p1.setxy(pointsVector[0].x, pointsVector[0].y);
if((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
{
// draw bezier curve
for (float i = 0; i < 1; i += 0.01)
{
// The Green Lines
int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
p1 = p2;
}
}
}
glutSwapBuffers();
}
glClear()
? I will look into implementing those suggestions, thank you! $\endgroup$