使用了OpenGL自帶的glut庫來做窗口,使用了
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
myMouse函數
myMouse
1 void myMouse(int button,int state,int x,int y)
2 {
3 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
4 {
5 if(count<3)
6 {
7 drawDot(x,screenHeight-y);
8 pt[count].x = x;
9 pt[count].y = screenHeight - y;
10 count++;
11 glFlush();
12 }
13 else
14 {
15 count = 0;
16 drawDot(x,screenHeight-y);
17 pt[count].x = x;
18 pt[count].y = screenHeight - y;
19 count++;
20 glFlush();
21 }
22 }
23 else if (button ==GLUT_LEFT_BUTTON && state == GLUT_UP)
24 {
25 if (count==3)
26 {
27
28 glFlush();
29 }
30
31 }
32 }
其中的drawdot是畫點函數。此時glut窗口上會繪制出點,但如果把這里的drawdot去掉,加在myDisplay函數中,卻不繪制點,奇怪!必須刷新下,才繪制點。
pt[3], count都是全局靜態變量。
myDisplay函數
myDisplay
1 void myDisplay(void)
2 {
3 glClear(GL_COLOR_BUFFER_BIT);
4
5 if (count!=0)
6 {
7 for (int i=0;i<count;i++)
8 {
9 drawDot(pt[i].x,pt[i].y);
10 }
11 }
12
13 glFlush();
14 }

myMouse