destroy window upon exit, remove CVS log comment
[mesa.git] / progs / util / glutskel.c
1 /**
2 * A skeleton/template GLUT program
3 *
4 * Written by Brian Paul and in the public domain.
5 */
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glut.h>
12
13 static int Win;
14 static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
15 static GLboolean Anim = GL_FALSE;
16
17
18 static void
19 Idle(void)
20 {
21 Xrot += 3.0;
22 Yrot += 4.0;
23 Zrot += 2.0;
24 glutPostRedisplay();
25 }
26
27
28 static void
29 Draw(void)
30 {
31 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
32
33 glPushMatrix();
34 glRotatef(Xrot, 1, 0, 0);
35 glRotatef(Yrot, 0, 1, 0);
36 glRotatef(Zrot, 0, 0, 1);
37
38 glutSolidCube(2.0);
39
40 glPopMatrix();
41
42 glutSwapBuffers();
43 }
44
45
46 static void
47 Reshape(int width, int height)
48 {
49 glViewport(0, 0, width, height);
50 glMatrixMode(GL_PROJECTION);
51 glLoadIdentity();
52 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
53 glMatrixMode(GL_MODELVIEW);
54 glLoadIdentity();
55 glTranslatef(0.0, 0.0, -15.0);
56 }
57
58
59 static void
60 Key(unsigned char key, int x, int y)
61 {
62 const GLfloat step = 3.0;
63 (void) x;
64 (void) y;
65 switch (key) {
66 case 'a':
67 Anim = !Anim;
68 if (Anim)
69 glutIdleFunc(Idle);
70 else
71 glutIdleFunc(NULL);
72 break;
73 case 'z':
74 Zrot -= step;
75 break;
76 case 'Z':
77 Zrot += step;
78 break;
79 case 27:
80 glutDestroyWindow(Win);
81 exit(0);
82 break;
83 }
84 glutPostRedisplay();
85 }
86
87
88 static void
89 SpecialKey(int key, int x, int y)
90 {
91 const GLfloat step = 3.0;
92 (void) x;
93 (void) y;
94 switch (key) {
95 case GLUT_KEY_UP:
96 Xrot -= step;
97 break;
98 case GLUT_KEY_DOWN:
99 Xrot += step;
100 break;
101 case GLUT_KEY_LEFT:
102 Yrot -= step;
103 break;
104 case GLUT_KEY_RIGHT:
105 Yrot += step;
106 break;
107 }
108 glutPostRedisplay();
109 }
110
111
112 static void
113 Init(void)
114 {
115 /* setup lighting, etc */
116 glEnable(GL_DEPTH_TEST);
117 glEnable(GL_LIGHTING);
118 glEnable(GL_LIGHT0);
119 }
120
121
122 int
123 main(int argc, char *argv[])
124 {
125 glutInit(&argc, argv);
126 glutInitWindowPosition(0, 0);
127 glutInitWindowSize(400, 400);
128 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
129 Win = glutCreateWindow(argv[0]);
130 glutReshapeFunc(Reshape);
131 glutKeyboardFunc(Key);
132 glutSpecialFunc(SpecialKey);
133 glutDisplayFunc(Draw);
134 if (Anim)
135 glutIdleFunc(Idle);
136 Init();
137 glutMainLoop();
138 return 0;
139 }