我正在尝试根据鼠标输入坐标绘制一条线。
但是,以下代码编译时,不会从鼠标输入中显示一行。
int coord_1[2]; //(x,y) of first point for line
int coord_2[2]; //(x,y) of second point for line
bool ready_1=false;
bool ready_2=false;
void drawLine()
{
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(coord_1[0], coord_1[1]);
glVertex2f(coord_2[0], coord_2[1]);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(ready_1 && ready_2)
{
drawLine();
ready_1 = ready_2 = false;
}
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) //button pressed down
{
//only do something on release
}
if (state == GLUT_UP) //released button
{
cout << "right button up" << endl;
}
break;
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) //button pressed down
{
coord_1[0]=x;
coord_2[1]=y;
cout << "first pos" << endl;
ready_1=true;
}
if (state == GLUT_UP) //released button
{
coord_2[0]=x;
coord_2[1]=y;
cout << "second pos" << endl;
ready_2=true;
}
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA|GLUT_MULTISAMPLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(512,512);
glutCreateWindow("Asn 1");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(processNormalKeys);
// GLUT main loop
glutMainLoop();
return(0);
}
正确方向的任何帮助或指示?
答案 0 :(得分:0)
你可能画了一条线,但是既然你之后重新设置了绘图条件,那么你的线会在下一个绘制步骤中被隐藏(这可能非常快)。
删除ready_1 = ready_2 = false;
,该行应保持可见。
相反,您可以在收到左键按下事件时设置ready_2 = false
。