blob: 0bec0663bc2835d9162110e0c569f4d7253dad54 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/*
2 * Demo of a reflective, texture-mapped surface with OpenGL.
3 * Brian Paul August 14, 1995 This file is in the public domain.
4 *
5 * Hardware texture mapping is highly recommended!
6 *
7 * The basic steps are:
8 * 1. Render the reflective object (a polygon) from the normal viewpoint,
9 * setting the stencil planes = 1.
10 * 2. Render the scene from a special viewpoint: the viewpoint which
11 * is on the opposite side of the reflective plane. Only draw where
12 * stencil = 1. This draws the objects in the reflective surface.
13 * 3. Render the scene from the original viewpoint. This draws the
14 * objects in the normal fashion. Use blending when drawing
15 * the reflective, textured surface.
16 *
17 * This is a very crude demo. It could be much better.
18 */
19
20/*
Brian Paul7a6bb1b2000-04-12 01:08:30 +000021 * Authors:
22 * Brian Paul
23 * Dirk Reiners (reiners@igd.fhg.de) made some modifications to this code.
24 * Mark Kilgard (April 1997)
25 * Brian Paul (April 2000 - added keyboard d/s options)
Brian Paul5d7c4862005-08-24 21:32:02 +000026 * Brian Paul (August 2005 - added multi window feature)
jtgafb833d1999-08-19 00:55:39 +000027 */
28
jtgafb833d1999-08-19 00:55:39 +000029
Brian Paul5d7c4862005-08-24 21:32:02 +000030#include <assert.h>
jtgafb833d1999-08-19 00:55:39 +000031#include <math.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include "GL/glut.h"
Brian Paul92eddb02005-01-09 17:37:50 +000035#include "showbuffer.h"
36#include "readtex.h"
jtgafb833d1999-08-19 00:55:39 +000037
38
39#define DEG2RAD (3.14159/180.0)
jtgafb833d1999-08-19 00:55:39 +000040#define TABLE_TEXTURE "../images/tile.rgb"
jtgafb833d1999-08-19 00:55:39 +000041#define MAX_OBJECTS 2
Brian Paul5d7c4862005-08-24 21:32:02 +000042#define INIT_WIDTH 400
43#define INIT_HEIGHT 300
jtgafb833d1999-08-19 00:55:39 +000044
Karl Schultzb6875312006-03-29 03:42:32 +000045#ifdef _WIN32
46#undef CreateWindowA
47#endif
jtgafb833d1999-08-19 00:55:39 +000048
Brian Paul5d7c4862005-08-24 21:32:02 +000049struct window {
50 int id; /* returned by glutCreateWindow() */
51 int width, height;
52 GLboolean anim;
53 GLfloat xrot, yrot;
54 GLfloat spin;
55 GLenum showBuffer;
Brian Pauld5783732005-08-31 16:42:59 +000056 GLenum drawBuffer;
Brian Paul5d7c4862005-08-24 21:32:02 +000057 GLuint table_list;
58 GLuint objects_list[MAX_OBJECTS];
59 double t0;
60 struct window *next;
61};
62
63
64static struct window *FirstWindow = NULL;
65
66
67static void
68CreateWindow(void);
69
70
71static struct window *
72CurrentWindow(void)
73{
74 int id = glutGetWindow();
75 struct window *w;
76 for (w = FirstWindow; w; w = w->next) {
77 if (w->id == id)
78 return w;
79 }
80 return NULL;
81}
82
83
84static GLboolean
85AnyAnimating(void)
86{
87 struct window *w;
88 for (w = FirstWindow; w; w = w->next) {
89 if (w->anim)
90 return 1;
91 }
92 return 0;
93}
94
95
96static void
97KillWindow(struct window *w)
98{
99 struct window *win, *prev = NULL;
100 for (win = FirstWindow; win; win = win->next) {
101 if (win == w) {
102 if (prev) {
103 prev->next = win->next;
104 }
105 else {
106 FirstWindow = win->next;
107 }
108 glutDestroyWindow(win->id);
109 win->next = NULL;
110 free(win);
111 return;
112 }
113 prev = win;
114 }
115}
116
117
118static void
119KillAllWindows(void)
120{
121 while (FirstWindow)
122 KillWindow(FirstWindow);
123}
124
125
126static GLuint
127MakeTable(void)
jtgafb833d1999-08-19 00:55:39 +0000128{
129 static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 };
130 static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
Brian Paul5d7c4862005-08-24 21:32:02 +0000131 GLuint table_list;
jtgafb833d1999-08-19 00:55:39 +0000132
133 table_list = glGenLists(1);
134 glNewList( table_list, GL_COMPILE );
135
136 /* load table's texture */
137 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat );
Brian Paul5d7c4862005-08-24 21:32:02 +0000138 /*glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/
jtgafb833d1999-08-19 00:55:39 +0000139 glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat );
140 glMaterialfv( GL_FRONT, GL_AMBIENT, gray );
141
142 /* draw textured square for the table */
143 glPushMatrix();
144 glScalef( 4.0, 4.0, 4.0 );
145 glBegin( GL_POLYGON );
146 glNormal3f( 0.0, 1.0, 0.0 );
147 glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 0.0, 1.0 );
148 glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 0.0, 1.0 );
149 glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 0.0, -1.0 );
150 glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 0.0, -1.0 );
151 glEnd();
152 glPopMatrix();
153
154 glDisable( GL_TEXTURE_2D );
155
156 glEndList();
Brian Paul5d7c4862005-08-24 21:32:02 +0000157 return table_list;
jtgafb833d1999-08-19 00:55:39 +0000158}
159
160
Brian Paul5d7c4862005-08-24 21:32:02 +0000161static void
162MakeObjects(GLuint *objects_list)
jtgafb833d1999-08-19 00:55:39 +0000163{
164 GLUquadricObj *q;
165
166 static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
167 static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 };
168 static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
169
170 q = gluNewQuadric();
171 gluQuadricDrawStyle( q, GLU_FILL );
172 gluQuadricNormals( q, GLU_SMOOTH );
173
174 objects_list[0] = glGenLists(1);
175 glNewList( objects_list[0], GL_COMPILE );
176 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
177 glMaterialfv( GL_FRONT, GL_EMISSION, black );
178 gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 );
179 glEndList();
180
181 objects_list[1] = glGenLists(1);
182 glNewList( objects_list[1], GL_COMPILE );
183 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
184 glMaterialfv( GL_FRONT, GL_EMISSION, black );
185 gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 );
186 glEndList();
Brian Paul1ad3b7e2005-11-19 23:09:14 +0000187
188 gluDeleteQuadric(q);
jtgafb833d1999-08-19 00:55:39 +0000189}
190
191
Brian Paul5d7c4862005-08-24 21:32:02 +0000192static void
193InitWindow(struct window *w)
jtgafb833d1999-08-19 00:55:39 +0000194{
Brian Paul5d7c4862005-08-24 21:32:02 +0000195 GLint imgWidth, imgHeight;
196 GLenum imgFormat;
197 GLubyte *image = NULL;
jtgafb833d1999-08-19 00:55:39 +0000198
Brian Paul5d7c4862005-08-24 21:32:02 +0000199 w->table_list = MakeTable();
200 MakeObjects(w->objects_list);
201
202 image = LoadRGBImage( TABLE_TEXTURE, &imgWidth, &imgHeight, &imgFormat );
203 if (!image) {
jtgafb833d1999-08-19 00:55:39 +0000204 printf("Couldn't read %s\n", TABLE_TEXTURE);
205 exit(0);
206 }
207
Brian Paul5d7c4862005-08-24 21:32:02 +0000208 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight,
209 imgFormat, GL_UNSIGNED_BYTE, image);
210 free(image);
jtgafb833d1999-08-19 00:55:39 +0000211
212 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
213 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
214 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
215 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
jtgafb833d1999-08-19 00:55:39 +0000216
jtgafb833d1999-08-19 00:55:39 +0000217 glShadeModel( GL_FLAT );
218
219 glEnable( GL_LIGHT0 );
220 glEnable( GL_LIGHTING );
221
Brian Paul73ccfa02001-04-25 15:51:32 +0000222 glClearColor( 0.5, 0.5, 0.9, 0.0 );
jtgafb833d1999-08-19 00:55:39 +0000223
224 glEnable( GL_NORMALIZE );
225}
226
227
Brian Paul5d7c4862005-08-24 21:32:02 +0000228static void
229Reshape(int width, int height)
jtgafb833d1999-08-19 00:55:39 +0000230{
Brian Paul5d7c4862005-08-24 21:32:02 +0000231 struct window *w = CurrentWindow();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000232 GLfloat yAspect = 2.5;
Brian Paul5d7c4862005-08-24 21:32:02 +0000233 GLfloat xAspect = yAspect * (float) width / (float) height;
234 w->width = width;
235 w->height = height;
236 glViewport(0, 0, width, height);
jtgafb833d1999-08-19 00:55:39 +0000237 glMatrixMode(GL_PROJECTION);
238 glLoadIdentity();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000239 glFrustum( -xAspect, xAspect, -yAspect, yAspect, 10.0, 30.0 );
jtgafb833d1999-08-19 00:55:39 +0000240 glMatrixMode(GL_MODELVIEW);
241 glLoadIdentity();
242}
243
244
Brian Paul5d7c4862005-08-24 21:32:02 +0000245static void
246DrawObjects(struct window *w, GLfloat eyex, GLfloat eyey, GLfloat eyez)
jtgafb833d1999-08-19 00:55:39 +0000247{
248 (void) eyex;
249 (void) eyey;
250 (void) eyez;
251#ifndef USE_ZBUFFER
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000252 if (eyex<0.5) {
jtgafb833d1999-08-19 00:55:39 +0000253#endif
Brian Paul5d7c4862005-08-24 21:32:02 +0000254 glPushMatrix();
255 glTranslatef( 1.0, 1.5, 0.0 );
256 glRotatef( w->spin, 1.0, 0.5, 0.0 );
257 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
258 glCallList( w->objects_list[0] );
259 glPopMatrix();
260
261 glPushMatrix();
262 glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*w->spin) ), 0.0 );
263 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
264 glRotatef( w->spin, 1.0, 0.5, 0.0 );
265 glScalef( 0.5, 0.5, 0.5 );
266 glCallList( w->objects_list[1] );
267 glPopMatrix();
jtgafb833d1999-08-19 00:55:39 +0000268#ifndef USE_ZBUFFER
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000269 }
270 else {
Brian Paul5d7c4862005-08-24 21:32:02 +0000271 glPushMatrix();
272 glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*w->spin) ), 0.0 );
273 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
274 glRotatef( w->spin, 1.0, 0.5, 0.0 );
275 glScalef( 0.5, 0.5, 0.5 );
276 glCallList( w->objects_list[1] );
277 glPopMatrix();
jtgafb833d1999-08-19 00:55:39 +0000278
Brian Paul5d7c4862005-08-24 21:32:02 +0000279 glPushMatrix();
280 glTranslatef( 1.0, 1.5, 0.0 );
281 glRotatef( w->spin, 1.0, 0.5, 0.0 );
282 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
283 glCallList( w->objects_list[0] );
284 glPopMatrix();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000285 }
jtgafb833d1999-08-19 00:55:39 +0000286#endif
287}
288
289
Brian Paul5d7c4862005-08-24 21:32:02 +0000290static void
291DrawTable(struct window *w)
jtgafb833d1999-08-19 00:55:39 +0000292{
Brian Paul5d7c4862005-08-24 21:32:02 +0000293 glCallList(w->table_list);
jtgafb833d1999-08-19 00:55:39 +0000294}
295
296
Brian Paul5d7c4862005-08-24 21:32:02 +0000297static void
298DrawWindow(void)
jtgafb833d1999-08-19 00:55:39 +0000299{
Brian Paul5d7c4862005-08-24 21:32:02 +0000300 struct window *w = CurrentWindow();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000301 static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 };
jtgafb833d1999-08-19 00:55:39 +0000302 GLfloat dist = 20.0;
303 GLfloat eyex, eyey, eyez;
304
Brian Paul693a66a2006-03-30 14:24:58 +0000305 if (w->drawBuffer == GL_NONE) {
306 glDrawBuffer(GL_BACK);
307 glReadBuffer(GL_BACK);
308 }
309 else {
310 glDrawBuffer(w->drawBuffer);
311 glReadBuffer(w->drawBuffer);
312 }
jtgafb833d1999-08-19 00:55:39 +0000313
Brian Pauld5783732005-08-31 16:42:59 +0000314 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
jtgafb833d1999-08-19 00:55:39 +0000315
Brian Paul693a66a2006-03-30 14:24:58 +0000316 if (w->drawBuffer == GL_NONE) {
317 glDrawBuffer(GL_NONE);
318 }
319
Brian Paul5d7c4862005-08-24 21:32:02 +0000320 eyex = dist * cos(w->yrot * DEG2RAD) * cos(w->xrot * DEG2RAD);
321 eyez = dist * sin(w->yrot * DEG2RAD) * cos(w->xrot * DEG2RAD);
322 eyey = dist * sin(w->xrot * DEG2RAD);
jtgafb833d1999-08-19 00:55:39 +0000323
324 /* view from top */
325 glPushMatrix();
326 gluLookAt( eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
327
328 glLightfv( GL_LIGHT0, GL_POSITION, light_pos );
329
330 /* draw table into stencil planes */
jtgafb833d1999-08-19 00:55:39 +0000331 glDisable( GL_DEPTH_TEST );
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000332 glEnable( GL_STENCIL_TEST );
jtgafb833d1999-08-19 00:55:39 +0000333 glStencilFunc( GL_ALWAYS, 1, 0xffffffff );
334 glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
335 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
Brian Paul5d7c4862005-08-24 21:32:02 +0000336 DrawTable(w);
jtgafb833d1999-08-19 00:55:39 +0000337 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
338
jtgafb833d1999-08-19 00:55:39 +0000339 glEnable( GL_DEPTH_TEST );
jtgafb833d1999-08-19 00:55:39 +0000340
341 /* render view from below (reflected viewport) */
342 /* only draw where stencil==1 */
343 if (eyey>0.0) {
344 glPushMatrix();
345
346 glStencilFunc( GL_EQUAL, 1, 0xffffffff ); /* draw if ==1 */
347 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
348 glScalef( 1.0, -1.0, 1.0 );
349
350 /* Reposition light in reflected space. */
351 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
352
Brian Paul5d7c4862005-08-24 21:32:02 +0000353 DrawObjects(w, eyex, eyey, eyez);
jtgafb833d1999-08-19 00:55:39 +0000354 glPopMatrix();
355
356 /* Restore light's original unreflected position. */
357 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
358 }
359
360 glDisable( GL_STENCIL_TEST );
361
362 glEnable( GL_BLEND );
363 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
364
jtgafb833d1999-08-19 00:55:39 +0000365 glEnable( GL_TEXTURE_2D );
Brian Paul5d7c4862005-08-24 21:32:02 +0000366 DrawTable(w);
jtgafb833d1999-08-19 00:55:39 +0000367 glDisable( GL_TEXTURE_2D );
368 glDisable( GL_BLEND );
369
370 /* view from top */
371 glPushMatrix();
372
Brian Paul5d7c4862005-08-24 21:32:02 +0000373 DrawObjects(w, eyex, eyey, eyez);
jtgafb833d1999-08-19 00:55:39 +0000374
375 glPopMatrix();
376
377 glPopMatrix();
378
Brian Paul5d7c4862005-08-24 21:32:02 +0000379 if (w->showBuffer == GL_DEPTH) {
380 ShowDepthBuffer(w->width, w->height, 1.0, 0.0);
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000381 }
Brian Paul5d7c4862005-08-24 21:32:02 +0000382 else if (w->showBuffer == GL_STENCIL) {
383 ShowStencilBuffer(w->width, w->height, 255.0, 0.0);
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000384 }
Brian Paul5d7c4862005-08-24 21:32:02 +0000385 else if (w->showBuffer == GL_ALPHA) {
386 ShowAlphaBuffer(w->width, w->height);
Brian Paul73ccfa02001-04-25 15:51:32 +0000387 }
jtgafb833d1999-08-19 00:55:39 +0000388
Brian Paul693a66a2006-03-30 14:24:58 +0000389 if (w->drawBuffer == GL_FRONT)
Brian Pauld5783732005-08-31 16:42:59 +0000390 glFinish();
Brian Paul693a66a2006-03-30 14:24:58 +0000391 else
392 glutSwapBuffers();
Brian Paulcefc42f2000-09-15 16:43:57 +0000393
Brian Paul5d7c4862005-08-24 21:32:02 +0000394 /* calc/show frame rate */
Brian Paulcefc42f2000-09-15 16:43:57 +0000395 {
Brian Paul5d7c4862005-08-24 21:32:02 +0000396 static GLint t0 = 0;
397 static GLint frames = 0;
Brian Paulcefc42f2000-09-15 16:43:57 +0000398 GLint t = glutGet(GLUT_ELAPSED_TIME);
Brian Paul5d7c4862005-08-24 21:32:02 +0000399 frames++;
400 if (t - t0 >= 5000) {
401 GLfloat seconds = (t - t0) / 1000.0;
402 GLfloat fps = frames / seconds;
403 printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps);
404 t0 = t;
405 frames = 0;
Brian Paulcefc42f2000-09-15 16:43:57 +0000406 }
407 }
jtgafb833d1999-08-19 00:55:39 +0000408}
jtgafb833d1999-08-19 00:55:39 +0000409
410
Brian Paul5d7c4862005-08-24 21:32:02 +0000411static void
412Idle(void)
Brian Paul2b2e6212001-01-23 23:43:53 +0000413{
Brian Paul5d7c4862005-08-24 21:32:02 +0000414 double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
415 struct window *w;
416 for (w = FirstWindow; w; w = w->next) {
417 if (w->anim) {
418 double dt;
419 if (w->t0 < 0.0)
420 w->t0 = t;
421 dt = t - w->t0;
422 w->t0 = t;
423 w->spin += 60.0 * dt;
424 w->yrot += 90.0 * dt;
425 assert(w->id);
426 glutSetWindow(w->id);
427 glutPostRedisplay();
428 }
429 }
Brian Paul2b2e6212001-01-23 23:43:53 +0000430}
431
432
Brian Paul5d7c4862005-08-24 21:32:02 +0000433static void
Brian Paul8e247d52005-08-25 17:46:04 +0000434UpdateIdleFunc(void)
435{
436 if (AnyAnimating())
437 glutIdleFunc(Idle);
438 else
439 glutIdleFunc(NULL);
440}
441
442static void
Brian Paul5d7c4862005-08-24 21:32:02 +0000443Key(unsigned char key, int x, int y)
jtgafb833d1999-08-19 00:55:39 +0000444{
Brian Paul5d7c4862005-08-24 21:32:02 +0000445 struct window *w = CurrentWindow();
jtgafb833d1999-08-19 00:55:39 +0000446 (void) x;
447 (void) y;
Brian Paul5d7c4862005-08-24 21:32:02 +0000448
449 switch (key) {
450 case 'd':
451 w->showBuffer = GL_DEPTH;
452 glutPostRedisplay();
453 break;
454 case 's':
455 w->showBuffer = GL_STENCIL;
456 glutPostRedisplay();
457 break;
458 case 'a':
459 w->showBuffer = GL_ALPHA;
460 glutPostRedisplay();
461 break;
462 case 'c':
463 w->showBuffer = GL_NONE;
464 glutPostRedisplay();
465 break;
Brian Pauld5783732005-08-31 16:42:59 +0000466 case 'f':
467 if (w->drawBuffer == GL_FRONT)
468 w->drawBuffer = GL_BACK;
469 else
470 w->drawBuffer = GL_FRONT;
471 glutPostRedisplay();
472 break;
Brian Paul693a66a2006-03-30 14:24:58 +0000473 case '0':
474 w->drawBuffer = GL_NONE;
475 glutPostRedisplay();
476 break;
Brian Paul5d7c4862005-08-24 21:32:02 +0000477 case ' ':
478 w->anim = !w->anim;
479 w->t0 = -1;
Brian Paul8e247d52005-08-25 17:46:04 +0000480 UpdateIdleFunc();
Brian Paul5d7c4862005-08-24 21:32:02 +0000481 glutPostRedisplay();
482 break;
483 case 'n':
484 CreateWindow();
Brian Paul8e247d52005-08-25 17:46:04 +0000485 UpdateIdleFunc();
Brian Paul5d7c4862005-08-24 21:32:02 +0000486 break;
487 case 'k':
488 KillWindow(w);
489 if (FirstWindow == NULL)
490 exit(0);
491 break;
492 case 27:
493 KillAllWindows();
jtgafb833d1999-08-19 00:55:39 +0000494 exit(0);
Brian Paul5d7c4862005-08-24 21:32:02 +0000495 break;
496 default:
497 ;
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000498 }
jtgafb833d1999-08-19 00:55:39 +0000499}
500
501
Brian Paul5d7c4862005-08-24 21:32:02 +0000502static void
503SpecialKey(int key, int x, int y)
jtgafb833d1999-08-19 00:55:39 +0000504{
Brian Paul5d7c4862005-08-24 21:32:02 +0000505 struct window *w = CurrentWindow();
jtgafb833d1999-08-19 00:55:39 +0000506 (void) x;
507 (void) y;
508 switch (key) {
509 case GLUT_KEY_UP:
Brian Paul5d7c4862005-08-24 21:32:02 +0000510 w->xrot += 3.0;
511 if (w->xrot > 85)
512 w->xrot = 85;
jtgafb833d1999-08-19 00:55:39 +0000513 break;
514 case GLUT_KEY_DOWN:
Brian Paul5d7c4862005-08-24 21:32:02 +0000515 w->xrot -= 3.0;
516 if (w->xrot < 5)
517 w->xrot = 5;
jtgafb833d1999-08-19 00:55:39 +0000518 break;
519 case GLUT_KEY_LEFT:
Brian Paul5d7c4862005-08-24 21:32:02 +0000520 w->yrot += 3.0;
jtgafb833d1999-08-19 00:55:39 +0000521 break;
522 case GLUT_KEY_RIGHT:
Brian Paul5d7c4862005-08-24 21:32:02 +0000523 w->yrot -= 3.0;
jtgafb833d1999-08-19 00:55:39 +0000524 break;
525 }
526 glutPostRedisplay();
527}
528
529
Brian Paul5d7c4862005-08-24 21:32:02 +0000530static void
531CreateWindow(void)
jtgafb833d1999-08-19 00:55:39 +0000532{
Brian Paul5d7c4862005-08-24 21:32:02 +0000533 char title[1000];
534 struct window *w = (struct window *) calloc(1, sizeof(struct window));
535
536 glutInitWindowSize(INIT_WIDTH, INIT_HEIGHT);
537 w->id = glutCreateWindow("foo");
538 sprintf(title, "reflect window %d", w->id);
539 glutSetWindowTitle(title);
540 assert(w->id);
541 w->width = INIT_WIDTH;
542 w->height = INIT_HEIGHT;
543 w->anim = GL_TRUE;
544 w->xrot = 30.0;
545 w->yrot = 50.0;
546 w->spin = 0.0;
547 w->showBuffer = GL_NONE;
Brian Pauld5783732005-08-31 16:42:59 +0000548 w->drawBuffer = GL_BACK;
Brian Paul5d7c4862005-08-24 21:32:02 +0000549
550 InitWindow(w);
551
552 glutReshapeFunc(Reshape);
553 glutDisplayFunc(DrawWindow);
jtgafb833d1999-08-19 00:55:39 +0000554 glutKeyboardFunc(Key);
555 glutSpecialFunc(SpecialKey);
Brian Paul5d7c4862005-08-24 21:32:02 +0000556
557 /* insert at head of list */
558 w->next = FirstWindow;
559 FirstWindow = w;
560}
561
562
563static void
564Usage(void)
565{
566 printf("Keys:\n");
567 printf(" a - show alpha buffer\n");
568 printf(" d - show depth buffer\n");
569 printf(" s - show stencil buffer\n");
570 printf(" c - show color buffer\n");
Brian Pauld5783732005-08-31 16:42:59 +0000571 printf(" f - toggle rendering to front/back color buffer\n");
Brian Paul5d7c4862005-08-24 21:32:02 +0000572 printf(" n - create new window\n");
573 printf(" k - kill window\n");
574 printf(" SPACE - toggle animation\n");
575 printf(" ARROWS - rotate scene\n");
576}
577
578
579int
580main(int argc, char *argv[])
581{
582 glutInit(&argc, argv);
583 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH |
584 GLUT_STENCIL | GLUT_ALPHA);
585 CreateWindow();
586 glutIdleFunc(Idle);
587 Usage();
jtgafb833d1999-08-19 00:55:39 +0000588 glutMainLoop();
589 return 0;
590}