1
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <GL/glut.h>
6
7
8 void display(void);
9 void idle(void);
10 void reshape(int w, int h);
11 void keyboard(unsigned char key, int x, int y);
12
13 void draw_cube(void);
14 void game_mode(char *mode);
15 void dump_game_mode(void);
16 void init(void);
17 void on_exit(void);
18
19 float spin = 0;
20
21
22 void
draw_cube(void)23 draw_cube(void)
24 {
25 glLoadIdentity();
26
27 /* A step backward, then spin the cube */
28 glTranslatef(0, 0, -5);
29 glRotatef(spin, 0, 0, 1);
30 glRotatef(spin, 1, 0.6, 0);
31
32 /* We tell we want to draw quads */
33 glBegin (GL_QUADS);
34
35 /* Every four calls to glVertex, a quad is drawn */
36 glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
37 glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
38 glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
39 glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
40
41 glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
42 glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
43 glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
44 glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
45
46 glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
47 glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
48 glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
49 glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
50
51 glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
52 glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
53 glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
54 glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
55
56 glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
57 glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
58 glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
59 glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
60
61 glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
62 glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
63 glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
64 glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
65
66 glEnd();
67 }
68
69
70 void
display(void)71 display(void)
72 {
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
75 draw_cube();
76
77 glutSwapBuffers();
78 }
79
80
81 void
idle(void)82 idle(void)
83 {
84 spin += 0.5;
85 if (spin > 360.0)
86 spin = 0.0;
87
88 glutPostRedisplay();
89 }
90
91
92 void
reshape(int w,int h)93 reshape(int w, int h)
94 {
95 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
96 // Setup the view of the cube.
97 glMatrixMode(GL_PROJECTION);
98 glLoadIdentity();
99 gluPerspective( /* field of view in degree */ 40.0,
100 /* aspect ratio */ 1.0,
101 /* Z near */ 1.0, /* Z far */ 10.0);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
104 gluLookAt(0.0, 0.0, 5.0, // eye is at (0,0,5)
105 0.0, 0.0, 0.0, // center is at (0,0,0)
106 0.0, 1.0, 0.); // up is in positive Y direction
107
108 // Adjust cube position to be asthetic angle.
109 glTranslatef(0.0, 0.0, -1.0);
110 glRotatef(60, 1.0, 0.0, 0.0);
111 glRotatef(-20, 0.0, 0.0, 1.0);
112 }
113
114
115 void
keyboard(unsigned char key,int x,int y)116 keyboard(unsigned char key, int x, int y)
117 {
118 switch(key) {
119 case 27:
120 exit(0);
121 break;
122
123 case 'g':
124 case 'G':
125 dump_game_mode();
126 break;
127
128 case 'e':
129 case 'E': {
130 char mode[255];
131
132 printf("Game mode string? ");
133 gets(mode);
134 if (!strlen(mode))
135 break;
136
137 game_mode(mode);
138 if (glutGameModeGet(GLUT_GAME_MODE_DISPLAY_CHANGED))
139 init();
140 break;
141 }
142
143 case 'l':
144 case 'L': {
145 // return to default window
146 glutLeaveGameMode();
147 if (!glutGetWindow())
148 // exit if none
149 exit(0);
150 break;
151 }
152 }
153 }
154
155
156 void
init(void)157 init(void)
158 {
159 glutIdleFunc(idle);
160 glutDisplayFunc(display);
161 glutReshapeFunc(reshape);
162 glutKeyboardFunc(keyboard);
163
164 glClearColor(0, 0, 0, 0);
165 glEnable(GL_DEPTH_TEST);
166 }
167
168
169 void
on_exit(void)170 on_exit(void)
171 {
172 printf("Exit.\n");
173 }
174
175
176 void
game_mode(char * mode)177 game_mode(char *mode)
178 {
179 glutGameModeString(mode);
180
181 printf("glutGameModeString(\"%s\"): ", mode);
182 if (!glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
183 printf("*not* possible!\n");
184 return;
185 }
186
187 printf("possible.\nglutEnterGameMode()\n");
188 glutEnterGameMode();
189
190 printf("glutGameModeGet(GLUT_GAME_MODE_DISPLAY_CHANGED) = %d\n",
191 glutGameModeGet(GLUT_GAME_MODE_DISPLAY_CHANGED));
192 }
193
194
195 void
dump_game_mode()196 dump_game_mode()
197 {
198 printf("glutGameModeGet():\n");
199
200 # define DUMP(pname) \
201 printf("\t" #pname " = %d\n", glutGameModeGet(pname));
202
203 DUMP(GLUT_GAME_MODE_ACTIVE);
204 DUMP(GLUT_GAME_MODE_POSSIBLE);
205 DUMP(GLUT_GAME_MODE_WIDTH);
206 DUMP(GLUT_GAME_MODE_HEIGHT);
207 DUMP(GLUT_GAME_MODE_PIXEL_DEPTH);
208 DUMP(GLUT_GAME_MODE_REFRESH_RATE);
209 DUMP(GLUT_GAME_MODE_DISPLAY_CHANGED);
210
211 # undef DUMP
212
213 printf("\n");
214 }
215
216 int
main(int argc,char ** argv)217 main(int argc, char **argv)
218 {
219 glutInit(&argc, argv);
220 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
221
222 if (argc > 1)
223 game_mode(argv[1]);
224
225 if (!glutGameModeGet(GLUT_GAME_MODE_ACTIVE)) {
226 printf("Using windowed mode.\n");
227 glutCreateWindow(argv[0]);
228 }
229
230 init();
231
232 atexit(on_exit);
233 glutMainLoop();
234
235 return 0;
236 }
237