fcdsdc

1 #include<GL/glut.h>
2
3 static GLfloat spin = 0.0;
4
5 void init(void)
6 {
7 glClearColor(0.0, 0.0, 0.0, 0.0);
8 glShadeModel(GL_FLAT);
9 }
10
11 void display(void)
12 {
13 glClear(GL_COLOR_BUFFER_BIT);
14 glPushMatrix();
15 glRotatef(spin, 0.0, 0.0, 1.0);
16 glColor3f(0.2, 0.5, 0.9);
17 glRectf(-30.0, -30.0, 30.0, 30.0);
18 glPopMatrix();
19 glutSwapBuffers();
20 }
21
22 void spinDisplay(void)
23 {
24 spin += 2.0;
25 if(spin > 360.0)
26 spin -= 360.0;
27 glutPostRedisplay();
28 }
29
30 void reshape(int w, int h)
31 {
32 glViewport(0, 0, (GLsizei)w, (GLsizei)h);
33 glMatrixMode(GL_PROJECTION);
34 glLoadIdentity();
35 glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
36 glMatrixMode(GL_MODELVIEW);
37 glLoadIdentity();
38 }
39
40 void mouse(int button, int state, int x, int y)
41 {
42 switch(button)
43 {
44 case GLUT_LEFT_BUTTON:
45 if(state == GLUT_DOWN)
46 glutIdleFunc(spinDisplay);
47 break;
48
49 case GLUT_MIDDLE_BUTTON:
50 if(state == GLUT_DOWN)
51 glutIdleFunc(NULL);
52 break;
53
54 default:
55 break;
56 }
57 }
58
59 int main(int argc, char **argv)
60 {
61 glutInit(&argc, argv);
62 /*
63 *启用双缓冲模式以得到平滑的动画效果
64 *可以改用单缓冲模式(GLUT_SINGLE)对比下效果
65 */
66 glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB);
67 glutInitWindowSize(350, 350);
68 glutInitWindowPosition(50, 50);
69 glutCreateWindow(argv[0]);
70 init();
71 glutDisplayFunc(display);
72 /*
73 * 窗口大小发生改变时候触发
74 * glutReshapeFunc(void(*func)(int w, int h))
75 */
76 glutReshapeFunc(reshape);
77 /*
78 *按下鼠标按钮,并移动鼠标时触发
79 */
80 glutMotionFunc(reshape);
81 glutMouseFunc(mouse);
82 glutMainLoop();
83
84 return 0;
85 }

posted @ 2011-05-31 11:06  zendPger  Views(161)  Comments(0Edit  收藏  举报