我想用鼠标交互在openGL Glut中绘制一个多边形,每次左键单击都将是一个顶点,并且将在每个顶点之间绘制一条线。单击鼠标右键时,多边形将关闭,从最后一个顶点到第一个顶点绘制一条线。我想出了这个,但它似乎不起作用。
void draw_polygon(int button, int state, int x, int y) {
bool right_pushed = 0;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
p1.x = x;
p1.y = 480 - y;
//if right is clicked draw a line to here
first.x = x;
first.y = 480 - y;
}
while (right_pushed == false) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
p2.x = x;
p2.y = 480 - y;
}
GLfloat dx = p2.x …Run Code Online (Sandbox Code Playgroud)