【发布时间】:2015-11-06 17:08:27
【问题描述】:
我试图在我的 OpenGL 和 C++ 程序中用鼠标绘制多个线段。现在我可以画一个,一旦我开始画另一个,前一个就消失了。
以下是我与鼠标绘图相关的代码。关于如何绘制多条线的任何建议?
LineSegment seg;
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { // if left button is clicked, that is the start point
seg.x1 = seg.x2 = x;
seg.y1 = seg.y2 = y;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { // once they lift the mouse, thats the finish point
seg.x2 = x;
seg.y2 = y;
}
}
void motion(int x, int y) {
seg.x2 = x;
seg.y2 = y;
glutPostRedisplay(); // refresh the screen showing the motion of the line
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glBegin(GL_LINES); // draw lines
glVertex2f(seg.x1, seg.y1);
glVertex2f(seg.x2, seg.y2);
glEnd();
glutSwapBuffers();
}
【问题讨论】: