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