jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | /* |
| 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 Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 21 | * 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 26 | * Brian Paul (August 2005 - added multi window feature) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 29 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 30 | #include <assert.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 31 | #include <math.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include "GL/glut.h" |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 35 | #include "showbuffer.h" |
| 36 | #include "readtex.h" |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | #define DEG2RAD (3.14159/180.0) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 40 | #define TABLE_TEXTURE "../images/tile.rgb" |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 41 | #define MAX_OBJECTS 2 |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 42 | #define INIT_WIDTH 400 |
| 43 | #define INIT_HEIGHT 300 |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 44 | |
| 45 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 46 | struct 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 Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 53 | GLenum drawBuffer; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 54 | GLuint table_list; |
| 55 | GLuint objects_list[MAX_OBJECTS]; |
| 56 | double t0; |
| 57 | struct window *next; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | static struct window *FirstWindow = NULL; |
| 62 | |
| 63 | |
| 64 | static void |
| 65 | CreateWindow(void); |
| 66 | |
| 67 | |
| 68 | static struct window * |
| 69 | CurrentWindow(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 | |
| 81 | static GLboolean |
| 82 | AnyAnimating(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 | |
| 93 | static void |
| 94 | KillWindow(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 | |
| 115 | static void |
| 116 | KillAllWindows(void) |
| 117 | { |
| 118 | while (FirstWindow) |
| 119 | KillWindow(FirstWindow); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | static GLuint |
| 124 | MakeTable(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 125 | { |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 128 | GLuint table_list; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 129 | |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 135 | /*glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 136 | 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 154 | return table_list; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 158 | static void |
| 159 | MakeObjects(GLuint *objects_list) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 160 | { |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 187 | static void |
| 188 | InitWindow(struct window *w) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 189 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 190 | GLint imgWidth, imgHeight; |
| 191 | GLenum imgFormat; |
| 192 | GLubyte *image = NULL; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 193 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 194 | w->table_list = MakeTable(); |
| 195 | MakeObjects(w->objects_list); |
| 196 | |
| 197 | image = LoadRGBImage( TABLE_TEXTURE, &imgWidth, &imgHeight, &imgFormat ); |
| 198 | if (!image) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 199 | printf("Couldn't read %s\n", TABLE_TEXTURE); |
| 200 | exit(0); |
| 201 | } |
| 202 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 203 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight, |
| 204 | imgFormat, GL_UNSIGNED_BYTE, image); |
| 205 | free(image); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 206 | |
| 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 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 211 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 212 | glShadeModel( GL_FLAT ); |
| 213 | |
| 214 | glEnable( GL_LIGHT0 ); |
| 215 | glEnable( GL_LIGHTING ); |
| 216 | |
Brian Paul | 73ccfa0 | 2001-04-25 15:51:32 +0000 | [diff] [blame] | 217 | glClearColor( 0.5, 0.5, 0.9, 0.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 218 | |
| 219 | glEnable( GL_NORMALIZE ); |
| 220 | } |
| 221 | |
| 222 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 223 | static void |
| 224 | Reshape(int width, int height) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 225 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 226 | struct window *w = CurrentWindow(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 227 | GLfloat yAspect = 2.5; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 228 | GLfloat xAspect = yAspect * (float) width / (float) height; |
| 229 | w->width = width; |
| 230 | w->height = height; |
| 231 | glViewport(0, 0, width, height); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 232 | glMatrixMode(GL_PROJECTION); |
| 233 | glLoadIdentity(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 234 | glFrustum( -xAspect, xAspect, -yAspect, yAspect, 10.0, 30.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 235 | glMatrixMode(GL_MODELVIEW); |
| 236 | glLoadIdentity(); |
| 237 | } |
| 238 | |
| 239 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 240 | static void |
| 241 | DrawObjects(struct window *w, GLfloat eyex, GLfloat eyey, GLfloat eyez) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 242 | { |
| 243 | (void) eyex; |
| 244 | (void) eyey; |
| 245 | (void) eyez; |
| 246 | #ifndef USE_ZBUFFER |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 247 | if (eyex<0.5) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 248 | #endif |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 249 | 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(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 263 | #ifndef USE_ZBUFFER |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 264 | } |
| 265 | else { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 266 | 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(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 273 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 274 | 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 Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 280 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 281 | #endif |
| 282 | } |
| 283 | |
| 284 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 285 | static void |
| 286 | DrawTable(struct window *w) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 287 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 288 | glCallList(w->table_list); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 292 | static void |
| 293 | DrawWindow(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 294 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 295 | struct window *w = CurrentWindow(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 296 | static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 297 | GLfloat dist = 20.0; |
| 298 | GLfloat eyex, eyey, eyez; |
| 299 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 300 | glDrawBuffer(w->drawBuffer); |
| 301 | glReadBuffer(w->drawBuffer); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 302 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 303 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 304 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 305 | 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); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 308 | |
| 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 */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 316 | glDisable( GL_DEPTH_TEST ); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 317 | glEnable( GL_STENCIL_TEST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 318 | glStencilFunc( GL_ALWAYS, 1, 0xffffffff ); |
| 319 | glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE ); |
| 320 | glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 321 | DrawTable(w); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 322 | glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); |
| 323 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 324 | glEnable( GL_DEPTH_TEST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 325 | |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 338 | DrawObjects(w, eyex, eyey, eyez); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 339 | 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 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 350 | glEnable( GL_TEXTURE_2D ); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 351 | DrawTable(w); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 352 | glDisable( GL_TEXTURE_2D ); |
| 353 | glDisable( GL_BLEND ); |
| 354 | |
| 355 | /* view from top */ |
| 356 | glPushMatrix(); |
| 357 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 358 | DrawObjects(w, eyex, eyey, eyez); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 359 | |
| 360 | glPopMatrix(); |
| 361 | |
| 362 | glPopMatrix(); |
| 363 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 364 | if (w->showBuffer == GL_DEPTH) { |
| 365 | ShowDepthBuffer(w->width, w->height, 1.0, 0.0); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 366 | } |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 367 | else if (w->showBuffer == GL_STENCIL) { |
| 368 | ShowStencilBuffer(w->width, w->height, 255.0, 0.0); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 369 | } |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 370 | else if (w->showBuffer == GL_ALPHA) { |
| 371 | ShowAlphaBuffer(w->width, w->height); |
Brian Paul | 73ccfa0 | 2001-04-25 15:51:32 +0000 | [diff] [blame] | 372 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 373 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 374 | if (w->drawBuffer == GL_BACK) |
| 375 | glutSwapBuffers(); |
| 376 | else |
| 377 | glFinish(); |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 378 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 379 | /* calc/show frame rate */ |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 380 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 381 | static GLint t0 = 0; |
| 382 | static GLint frames = 0; |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 383 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 384 | 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 Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 393 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 394 | |
| 395 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 396 | static void |
| 397 | Idle(void) |
Brian Paul | 2b2e621 | 2001-01-23 23:43:53 +0000 | [diff] [blame] | 398 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 399 | 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 Paul | 2b2e621 | 2001-01-23 23:43:53 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 418 | static void |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 419 | UpdateIdleFunc(void) |
| 420 | { |
| 421 | if (AnyAnimating()) |
| 422 | glutIdleFunc(Idle); |
| 423 | else |
| 424 | glutIdleFunc(NULL); |
| 425 | } |
| 426 | |
| 427 | static void |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 428 | Key(unsigned char key, int x, int y) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 429 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 430 | struct window *w = CurrentWindow(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 431 | (void) x; |
| 432 | (void) y; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 433 | |
| 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 Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 451 | 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 458 | case ' ': |
| 459 | w->anim = !w->anim; |
| 460 | w->t0 = -1; |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 461 | UpdateIdleFunc(); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 462 | glutPostRedisplay(); |
| 463 | break; |
| 464 | case 'n': |
| 465 | CreateWindow(); |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 466 | UpdateIdleFunc(); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 467 | break; |
| 468 | case 'k': |
| 469 | KillWindow(w); |
| 470 | if (FirstWindow == NULL) |
| 471 | exit(0); |
| 472 | break; |
| 473 | case 27: |
| 474 | KillAllWindows(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 475 | exit(0); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 476 | break; |
| 477 | default: |
| 478 | ; |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 479 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 483 | static void |
| 484 | SpecialKey(int key, int x, int y) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 485 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 486 | struct window *w = CurrentWindow(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 487 | (void) x; |
| 488 | (void) y; |
| 489 | switch (key) { |
| 490 | case GLUT_KEY_UP: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 491 | w->xrot += 3.0; |
| 492 | if (w->xrot > 85) |
| 493 | w->xrot = 85; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 494 | break; |
| 495 | case GLUT_KEY_DOWN: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 496 | w->xrot -= 3.0; |
| 497 | if (w->xrot < 5) |
| 498 | w->xrot = 5; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 499 | break; |
| 500 | case GLUT_KEY_LEFT: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 501 | w->yrot += 3.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 502 | break; |
| 503 | case GLUT_KEY_RIGHT: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 504 | w->yrot -= 3.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | glutPostRedisplay(); |
| 508 | } |
| 509 | |
| 510 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 511 | static void |
| 512 | CreateWindow(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 513 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 514 | 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 Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 529 | w->drawBuffer = GL_BACK; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 530 | |
| 531 | InitWindow(w); |
| 532 | |
| 533 | glutReshapeFunc(Reshape); |
| 534 | glutDisplayFunc(DrawWindow); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 535 | glutKeyboardFunc(Key); |
| 536 | glutSpecialFunc(SpecialKey); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 537 | |
| 538 | /* insert at head of list */ |
| 539 | w->next = FirstWindow; |
| 540 | FirstWindow = w; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | static void |
| 545 | Usage(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 Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame^] | 552 | printf(" f - toggle rendering to front/back color buffer\n"); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 553 | 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 | |
| 560 | int |
| 561 | main(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(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 569 | glutMainLoop(); |
| 570 | return 0; |
| 571 | } |