因此,我试图在OpenGL中在单击的点之间绘制线条。如果我按下左键单击,该图形将显示在屏幕上,但是如果释放左键单击,该图形将消失:
struct Vector {
float x,y;
Vector(float x = 0,float y = 0) : x(x),y(y) {}
} last_mouse_pos;
void onInitialization() {
glClearColor(0,0); // A hatterszin beallitasa.
glClear(GL_COLOR_BUFFER_BIT); // A kepernyo torlese,az uj hatterszinnel.
}
void onDisplay() {
glutSwapBuffers();
}
Vector convertToNdc(float x,float y) {
Vector ret;
ret.x = (x - kScreenWidth / 2) / (kScreenWidth / 2);
ret.y = (kScreenHeight / 2 - y) / (kScreenHeight / 2);
return ret;
}
int i = 0;
void onmouse(int button,int state,int x,int y) {
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
glClear(GL_COLOR_BUFFER_BIT);
glutPostRedisplay();
}
else if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
i++;
if (i == 1) last_mouse_pos = convertToNdc(x,y);
if (i > 1) {
Vector pos = convertToNdc(x,y);
glBegin(GL_LInes);
glVertex2f(last_mouse_pos.x,last_mouse_pos.y);
glVertex2f(pos.x,pos.y);
glEnd();
glutPostRedisplay();
last_mouse_pos = pos;
}
}
}
}
所以我得了2分,如果我一直按住鼠标左键,它将划出一条线;如果松开它,屏幕将变黑。如果我单击其他位置,则现在有2行,但是仅当我按左键单击时才行。如果我松开,所有的东西都会再次变黑。