blob: 9cd71d1c3db5f9e89b7b8bf2744aa61d6470f885 [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
45
Brian Paul5d7c4862005-08-24 21:32:02 +000046struct window {
47 int id; /* returned by glutCreateWindow() */
48 int width, height;
49 GLboolean anim;
50 GLfloat xrot, yrot;
51 GLfloat spin;
52 GLenum showBuffer;
Brian Pauld5783732005-08-31 16:42:59 +000053 GLenum drawBuffer;
Brian Paul5d7c4862005-08-24 21:32:02 +000054 GLuint table_list;
55 GLuint objects_list[MAX_OBJECTS];
56 double t0;
57 struct window *next;
58};
59
60
61static struct window *FirstWindow = NULL;
62
63
64static void
65CreateWindow(void);
66
67
68static struct window *
69CurrentWindow(void)
70{
71 int id = glutGetWindow();
72 struct window *w;
73 for (w = FirstWindow; w; w = w->next) {
74 if (w->id == id)
75 return w;
76 }
77 return NULL;
78}
79
80
81static GLboolean
82AnyAnimating(void)
83{
84 struct window *w;
85 for (w = FirstWindow; w; w = w->next) {
86 if (w->anim)
87 return 1;
88 }
89 return 0;
90}
91
92
93static void
94KillWindow(struct window *w)
95{
96 struct window *win, *prev = NULL;
97 for (win = FirstWindow; win; win = win->next) {
98 if (win == w) {
99 if (prev) {
100 prev->next = win->next;
101 }
102 else {
103 FirstWindow = win->next;
104 }
105 glutDestroyWindow(win->id);
106 win->next = NULL;
107 free(win);
108 return;
109 }
110 prev = win;
111 }
112}
113
114
115static void
116KillAllWindows(void)
117{
118 while (FirstWindow)
119 KillWindow(FirstWindow);
120}
121
122
123static GLuint
124MakeTable(void)
jtgafb833d1999-08-19 00:55:39 +0000125{
126 static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 };
127 static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
Brian Paul5d7c4862005-08-24 21:32:02 +0000128 GLuint table_list;
jtgafb833d1999-08-19 00:55:39 +0000129
130 table_list = glGenLists(1);
131 glNewList( table_list, GL_COMPILE );
132
133 /* load table's texture */
134 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat );
Brian Paul5d7c4862005-08-24 21:32:02 +0000135 /*glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/
jtgafb833d1999-08-19 00:55:39 +0000136 glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat );
137 glMaterialfv( GL_FRONT, GL_AMBIENT, gray );
138
139 /* draw textured square for the table */
140 glPushMatrix();
141 glScalef( 4.0, 4.0, 4.0 );
142 glBegin( GL_POLYGON );
143 glNormal3f( 0.0, 1.0, 0.0 );
144 glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 0.0, 1.0 );
145 glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 0.0, 1.0 );
146 glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 0.0, -1.0 );
147 glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 0.0, -1.0 );
148 glEnd();
149 glPopMatrix();
150
151 glDisable( GL_TEXTURE_2D );
152
153 glEndList();
Brian Paul5d7c4862005-08-24 21:32:02 +0000154 return table_list;
jtgafb833d1999-08-19 00:55:39 +0000155}
156
157
Brian Paul5d7c4862005-08-24 21:32:02 +0000158static void
159MakeObjects(GLuint *objects_list)
jtgafb833d1999-08-19 00:55:39 +0000160{
161 GLUquadricObj *q;
162
163 static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
164 static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 };
165 static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
166
167 q = gluNewQuadric();
168 gluQuadricDrawStyle( q, GLU_FILL );
169 gluQuadricNormals( q, GLU_SMOOTH );
170
171 objects_list[0] = glGenLists(1);
172 glNewList( objects_list[0], GL_COMPILE );
173 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
174 glMaterialfv( GL_FRONT, GL_EMISSION, black );
175 gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 );
176 glEndList();
177
178 objects_list[1] = glGenLists(1);
179 glNewList( objects_list[1], GL_COMPILE );
180 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
181 glMaterialfv( GL_FRONT, GL_EMISSION, black );
182 gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 );
183 glEndList();
184}
185
186
Brian Paul5d7c4862005-08-24 21:32:02 +0000187static void
188InitWindow(struct window *w)
jtgafb833d1999-08-19 00:55:39 +0000189{
Brian Paul5d7c4862005-08-24 21:32:02 +0000190 GLint imgWidth, imgHeight;
191 GLenum imgFormat;
192 GLubyte *image = NULL;
jtgafb833d1999-08-19 00:55:39 +0000193
Brian Paul5d7c4862005-08-24 21:32:02 +0000194 w->table_list = MakeTable();
195 MakeObjects(w->objects_list);
196
197 image = LoadRGBImage( TABLE_TEXTURE, &imgWidth, &imgHeight, &imgFormat );
198 if (!image) {
jtgafb833d1999-08-19 00:55:39 +0000199 printf("Couldn't read %s\n", TABLE_TEXTURE);
200 exit(0);
201 }
202
Brian Paul5d7c4862005-08-24 21:32:02 +0000203 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight,
204 imgFormat, GL_UNSIGNED_BYTE, image);
205 free(image);
jtgafb833d1999-08-19 00:55:39 +0000206
207 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
208 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
209 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
210 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
jtgafb833d1999-08-19 00:55:39 +0000211
jtgafb833d1999-08-19 00:55:39 +0000212 glShadeModel( GL_FLAT );
213
214 glEnable( GL_LIGHT0 );
215 glEnable( GL_LIGHTING );
216
Brian Paul73ccfa02001-04-25 15:51:32 +0000217 glClearColor( 0.5, 0.5, 0.9, 0.0 );
jtgafb833d1999-08-19 00:55:39 +0000218
219 glEnable( GL_NORMALIZE );
220}
221
222
Brian Paul5d7c4862005-08-24 21:32:02 +0000223static void
224Reshape(int width, int height)
jtgafb833d1999-08-19 00:55:39 +0000225{
Brian Paul5d7c4862005-08-24 21:32:02 +0000226 struct window *w = CurrentWindow();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000227 GLfloat yAspect = 2.5;
Brian Paul5d7c4862005-08-24 21:32:02 +0000228 GLfloat xAspect = yAspect * (float) width / (float) height;
229 w->width = width;
230 w->height = height;
231 glViewport(0, 0, width, height);
jtgafb833d1999-08-19 00:55:39 +0000232 glMatrixMode(GL_PROJECTION);
233 glLoadIdentity();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000234 glFrustum( -xAspect, xAspect, -yAspect, yAspect, 10.0, 30.0 );
jtgafb833d1999-08-19 00:55:39 +0000235 glMatrixMode(GL_MODELVIEW);
236 glLoadIdentity();
237}
238
239
Brian Paul5d7c4862005-08-24 21:32:02 +0000240static void
241DrawObjects(struct window *w, GLfloat eyex, GLfloat eyey, GLfloat eyez)
jtgafb833d1999-08-19 00:55:39 +0000242{
243 (void) eyex;
244 (void) eyey;
245 (void) eyez;
246#ifndef USE_ZBUFFER
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000247 if (eyex<0.5) {
jtgafb833d1999-08-19 00:55:39 +0000248#endif
Brian Paul5d7c4862005-08-24 21:32:02 +0000249 glPushMatrix();
250 glTranslatef( 1.0, 1.5, 0.0 );
251 glRotatef( w->spin, 1.0, 0.5, 0.0 );
252 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
253 glCallList( w->objects_list[0] );
254 glPopMatrix();
255
256 glPushMatrix();
257 glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*w->spin) ), 0.0 );
258 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
259 glRotatef( w->spin, 1.0, 0.5, 0.0 );
260 glScalef( 0.5, 0.5, 0.5 );
261 glCallList( w->objects_list[1] );
262 glPopMatrix();
jtgafb833d1999-08-19 00:55:39 +0000263#ifndef USE_ZBUFFER
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000264 }
265 else {
Brian Paul5d7c4862005-08-24 21:32:02 +0000266 glPushMatrix();
267 glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*w->spin) ), 0.0 );
268 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
269 glRotatef( w->spin, 1.0, 0.5, 0.0 );
270 glScalef( 0.5, 0.5, 0.5 );
271 glCallList( w->objects_list[1] );
272 glPopMatrix();
jtgafb833d1999-08-19 00:55:39 +0000273
Brian Paul5d7c4862005-08-24 21:32:02 +0000274 glPushMatrix();
275 glTranslatef( 1.0, 1.5, 0.0 );
276 glRotatef( w->spin, 1.0, 0.5, 0.0 );
277 glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 );
278 glCallList( w->objects_list[0] );
279 glPopMatrix();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000280 }
jtgafb833d1999-08-19 00:55:39 +0000281#endif
282}
283
284
Brian Paul5d7c4862005-08-24 21:32:02 +0000285static void
286DrawTable(struct window *w)
jtgafb833d1999-08-19 00:55:39 +0000287{
Brian Paul5d7c4862005-08-24 21:32:02 +0000288 glCallList(w->table_list);
jtgafb833d1999-08-19 00:55:39 +0000289}
290
291
Brian Paul5d7c4862005-08-24 21:32:02 +0000292static void
293DrawWindow(void)
jtgafb833d1999-08-19 00:55:39 +0000294{
Brian Paul5d7c4862005-08-24 21:32:02 +0000295 struct window *w = CurrentWindow();
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000296 static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 };
jtgafb833d1999-08-19 00:55:39 +0000297 GLfloat dist = 20.0;
298 GLfloat eyex, eyey, eyez;
299
Brian Pauld5783732005-08-31 16:42:59 +0000300 glDrawBuffer(w->drawBuffer);
301 glReadBuffer(w->drawBuffer);
jtgafb833d1999-08-19 00:55:39 +0000302
Brian Pauld5783732005-08-31 16:42:59 +0000303 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
jtgafb833d1999-08-19 00:55:39 +0000304
Brian Paul5d7c4862005-08-24 21:32:02 +0000305 eyex = dist * cos(w->yrot * DEG2RAD) * cos(w->xrot * DEG2RAD);
306 eyez = dist * sin(w->yrot * DEG2RAD) * cos(w->xrot * DEG2RAD);
307 eyey = dist * sin(w->xrot * DEG2RAD);
jtgafb833d1999-08-19 00:55:39 +0000308
309 /* view from top */
310 glPushMatrix();
311 gluLookAt( eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
312
313 glLightfv( GL_LIGHT0, GL_POSITION, light_pos );
314
315 /* draw table into stencil planes */
jtgafb833d1999-08-19 00:55:39 +0000316 glDisable( GL_DEPTH_TEST );
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000317 glEnable( GL_STENCIL_TEST );
jtgafb833d1999-08-19 00:55:39 +0000318 glStencilFunc( GL_ALWAYS, 1, 0xffffffff );
319 glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
320 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
Brian Paul5d7c4862005-08-24 21:32:02 +0000321 DrawTable(w);
jtgafb833d1999-08-19 00:55:39 +0000322 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
323
jtgafb833d1999-08-19 00:55:39 +0000324 glEnable( GL_DEPTH_TEST );
jtgafb833d1999-08-19 00:55:39 +0000325
326 /* render view from below (reflected viewport) */
327 /* only draw where stencil==1 */
328 if (eyey>0.0) {
329 glPushMatrix();
330
331 glStencilFunc( GL_EQUAL, 1, 0xffffffff ); /* draw if ==1 */
332 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
333 glScalef( 1.0, -1.0, 1.0 );
334
335 /* Reposition light in reflected space. */
336 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
337
Brian Paul5d7c4862005-08-24 21:32:02 +0000338 DrawObjects(w, eyex, eyey, eyez);
jtgafb833d1999-08-19 00:55:39 +0000339 glPopMatrix();
340
341 /* Restore light's original unreflected position. */
342 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
343 }
344
345 glDisable( GL_STENCIL_TEST );
346
347 glEnable( GL_BLEND );
348 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
349
jtgafb833d1999-08-19 00:55:39 +0000350 glEnable( GL_TEXTURE_2D );
Brian Paul5d7c4862005-08-24 21:32:02 +0000351 DrawTable(w);
jtgafb833d1999-08-19 00:55:39 +0000352 glDisable( GL_TEXTURE_2D );
353 glDisable( GL_BLEND );
354
355 /* view from top */
356 glPushMatrix();
357
Brian Paul5d7c4862005-08-24 21:32:02 +0000358 DrawObjects(w, eyex, eyey, eyez);
jtgafb833d1999-08-19 00:55:39 +0000359
360 glPopMatrix();
361
362 glPopMatrix();
363
Brian Paul5d7c4862005-08-24 21:32:02 +0000364 if (w->showBuffer == GL_DEPTH) {
365 ShowDepthBuffer(w->width, w->height, 1.0, 0.0);
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000366 }
Brian Paul5d7c4862005-08-24 21:32:02 +0000367 else if (w->showBuffer == GL_STENCIL) {
368 ShowStencilBuffer(w->width, w->height, 255.0, 0.0);
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000369 }
Brian Paul5d7c4862005-08-24 21:32:02 +0000370 else if (w->showBuffer == GL_ALPHA) {
371 ShowAlphaBuffer(w->width, w->height);
Brian Paul73ccfa02001-04-25 15:51:32 +0000372 }
jtgafb833d1999-08-19 00:55:39 +0000373
Brian Pauld5783732005-08-31 16:42:59 +0000374 if (w->drawBuffer == GL_BACK)
375 glutSwapBuffers();
376 else
377 glFinish();
Brian Paulcefc42f2000-09-15 16:43:57 +0000378
Brian Paul5d7c4862005-08-24 21:32:02 +0000379 /* calc/show frame rate */
Brian Paulcefc42f2000-09-15 16:43:57 +0000380 {
Brian Paul5d7c4862005-08-24 21:32:02 +0000381 static GLint t0 = 0;
382 static GLint frames = 0;
Brian Paulcefc42f2000-09-15 16:43:57 +0000383 GLint t = glutGet(GLUT_ELAPSED_TIME);
Brian Paul5d7c4862005-08-24 21:32:02 +0000384 frames++;
385 if (t - t0 >= 5000) {
386 GLfloat seconds = (t - t0) / 1000.0;
387 GLfloat fps = frames / seconds;
388 printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps);
389 t0 = t;
390 frames = 0;
Brian Paulcefc42f2000-09-15 16:43:57 +0000391 }
392 }
jtgafb833d1999-08-19 00:55:39 +0000393}
jtgafb833d1999-08-19 00:55:39 +0000394
395
Brian Paul5d7c4862005-08-24 21:32:02 +0000396static void
397Idle(void)
Brian Paul2b2e6212001-01-23 23:43:53 +0000398{
Brian Paul5d7c4862005-08-24 21:32:02 +0000399 double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
400 struct window *w;
401 for (w = FirstWindow; w; w = w->next) {
402 if (w->anim) {
403 double dt;
404 if (w->t0 < 0.0)
405 w->t0 = t;
406 dt = t - w->t0;
407 w->t0 = t;
408 w->spin += 60.0 * dt;
409 w->yrot += 90.0 * dt;
410 assert(w->id);
411 glutSetWindow(w->id);
412 glutPostRedisplay();
413 }
414 }
Brian Paul2b2e6212001-01-23 23:43:53 +0000415}
416
417
Brian Paul5d7c4862005-08-24 21:32:02 +0000418static void
Brian Paul8e247d52005-08-25 17:46:04 +0000419UpdateIdleFunc(void)
420{
421 if (AnyAnimating())
422 glutIdleFunc(Idle);
423 else
424 glutIdleFunc(NULL);
425}
426
427static void
Brian Paul5d7c4862005-08-24 21:32:02 +0000428Key(unsigned char key, int x, int y)
jtgafb833d1999-08-19 00:55:39 +0000429{
Brian Paul5d7c4862005-08-24 21:32:02 +0000430 struct window *w = CurrentWindow();
jtgafb833d1999-08-19 00:55:39 +0000431 (void) x;
432 (void) y;
Brian Paul5d7c4862005-08-24 21:32:02 +0000433
434 switch (key) {
435 case 'd':
436 w->showBuffer = GL_DEPTH;
437 glutPostRedisplay();
438 break;
439 case 's':
440 w->showBuffer = GL_STENCIL;
441 glutPostRedisplay();
442 break;
443 case 'a':
444 w->showBuffer = GL_ALPHA;
445 glutPostRedisplay();
446 break;
447 case 'c':
448 w->showBuffer = GL_NONE;
449 glutPostRedisplay();
450 break;
Brian Pauld5783732005-08-31 16:42:59 +0000451 case 'f':
452 if (w->drawBuffer == GL_FRONT)
453 w->drawBuffer = GL_BACK;
454 else
455 w->drawBuffer = GL_FRONT;
456 glutPostRedisplay();
457 break;
Brian Paul5d7c4862005-08-24 21:32:02 +0000458 case ' ':
459 w->anim = !w->anim;
460 w->t0 = -1;
Brian Paul8e247d52005-08-25 17:46:04 +0000461 UpdateIdleFunc();
Brian Paul5d7c4862005-08-24 21:32:02 +0000462 glutPostRedisplay();
463 break;
464 case 'n':
465 CreateWindow();
Brian Paul8e247d52005-08-25 17:46:04 +0000466 UpdateIdleFunc();
Brian Paul5d7c4862005-08-24 21:32:02 +0000467 break;
468 case 'k':
469 KillWindow(w);
470 if (FirstWindow == NULL)
471 exit(0);
472 break;
473 case 27:
474 KillAllWindows();
jtgafb833d1999-08-19 00:55:39 +0000475 exit(0);
Brian Paul5d7c4862005-08-24 21:32:02 +0000476 break;
477 default:
478 ;
Brian Paul7a6bb1b2000-04-12 01:08:30 +0000479 }
jtgafb833d1999-08-19 00:55:39 +0000480}
481
482
Brian Paul5d7c4862005-08-24 21:32:02 +0000483static void
484SpecialKey(int key, int x, int y)
jtgafb833d1999-08-19 00:55:39 +0000485{
Brian Paul5d7c4862005-08-24 21:32:02 +0000486 struct window *w = CurrentWindow();
jtgafb833d1999-08-19 00:55:39 +0000487 (void) x;
488 (void) y;
489 switch (key) {
490 case GLUT_KEY_UP:
Brian Paul5d7c4862005-08-24 21:32:02 +0000491 w->xrot += 3.0;
492 if (w->xrot > 85)
493 w->xrot = 85;
jtgafb833d1999-08-19 00:55:39 +0000494 break;
495 case GLUT_KEY_DOWN:
Brian Paul5d7c4862005-08-24 21:32:02 +0000496 w->xrot -= 3.0;
497 if (w->xrot < 5)
498 w->xrot = 5;
jtgafb833d1999-08-19 00:55:39 +0000499 break;
500 case GLUT_KEY_LEFT:
Brian Paul5d7c4862005-08-24 21:32:02 +0000501 w->yrot += 3.0;
jtgafb833d1999-08-19 00:55:39 +0000502 break;
503 case GLUT_KEY_RIGHT:
Brian Paul5d7c4862005-08-24 21:32:02 +0000504 w->yrot -= 3.0;
jtgafb833d1999-08-19 00:55:39 +0000505 break;
506 }
507 glutPostRedisplay();
508}
509
510
Brian Paul5d7c4862005-08-24 21:32:02 +0000511static void
512CreateWindow(void)
jtgafb833d1999-08-19 00:55:39 +0000513{
Brian Paul5d7c4862005-08-24 21:32:02 +0000514 char title[1000];
515 struct window *w = (struct window *) calloc(1, sizeof(struct window));
516
517 glutInitWindowSize(INIT_WIDTH, INIT_HEIGHT);
518 w->id = glutCreateWindow("foo");
519 sprintf(title, "reflect window %d", w->id);
520 glutSetWindowTitle(title);
521 assert(w->id);
522 w->width = INIT_WIDTH;
523 w->height = INIT_HEIGHT;
524 w->anim = GL_TRUE;
525 w->xrot = 30.0;
526 w->yrot = 50.0;
527 w->spin = 0.0;
528 w->showBuffer = GL_NONE;
Brian Pauld5783732005-08-31 16:42:59 +0000529 w->drawBuffer = GL_BACK;
Brian Paul5d7c4862005-08-24 21:32:02 +0000530
531 InitWindow(w);
532
533 glutReshapeFunc(Reshape);
534 glutDisplayFunc(DrawWindow);
jtgafb833d1999-08-19 00:55:39 +0000535 glutKeyboardFunc(Key);
536 glutSpecialFunc(SpecialKey);
Brian Paul5d7c4862005-08-24 21:32:02 +0000537
538 /* insert at head of list */
539 w->next = FirstWindow;
540 FirstWindow = w;
541}
542
543
544static void
545Usage(void)
546{
547 printf("Keys:\n");
548 printf(" a - show alpha buffer\n");
549 printf(" d - show depth buffer\n");
550 printf(" s - show stencil buffer\n");
551 printf(" c - show color buffer\n");
Brian Pauld5783732005-08-31 16:42:59 +0000552 printf(" f - toggle rendering to front/back color buffer\n");
Brian Paul5d7c4862005-08-24 21:32:02 +0000553 printf(" n - create new window\n");
554 printf(" k - kill window\n");
555 printf(" SPACE - toggle animation\n");
556 printf(" ARROWS - rotate scene\n");
557}
558
559
560int
561main(int argc, char *argv[])
562{
563 glutInit(&argc, argv);
564 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH |
565 GLUT_STENCIL | GLUT_ALPHA);
566 CreateWindow();
567 glutIdleFunc(Idle);
568 Usage();
jtgafb833d1999-08-19 00:55:39 +0000569 glutMainLoop();
570 return 0;
571}