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 | |
Karl Schultz | b687531 | 2006-03-29 03:42:32 +0000 | [diff] [blame] | 45 | #ifdef _WIN32 |
| 46 | #undef CreateWindowA |
| 47 | #endif |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 48 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 49 | struct 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 Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 56 | GLenum drawBuffer; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 57 | GLuint table_list; |
| 58 | GLuint objects_list[MAX_OBJECTS]; |
| 59 | double t0; |
| 60 | struct window *next; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | static struct window *FirstWindow = NULL; |
| 65 | |
| 66 | |
| 67 | static void |
| 68 | CreateWindow(void); |
| 69 | |
| 70 | |
| 71 | static struct window * |
| 72 | CurrentWindow(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 | |
| 84 | static GLboolean |
| 85 | AnyAnimating(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 | |
| 96 | static void |
| 97 | KillWindow(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 | |
| 118 | static void |
| 119 | KillAllWindows(void) |
| 120 | { |
| 121 | while (FirstWindow) |
| 122 | KillWindow(FirstWindow); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static GLuint |
| 127 | MakeTable(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 128 | { |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 131 | GLuint table_list; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 132 | |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 138 | /*glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 139 | 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 157 | return table_list; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 161 | static void |
| 162 | MakeObjects(GLuint *objects_list) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 163 | { |
| 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 Paul | 1ad3b7e | 2005-11-19 23:09:14 +0000 | [diff] [blame] | 187 | |
| 188 | gluDeleteQuadric(q); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 192 | static void |
| 193 | InitWindow(struct window *w) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 194 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 195 | GLint imgWidth, imgHeight; |
| 196 | GLenum imgFormat; |
| 197 | GLubyte *image = NULL; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 198 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 199 | w->table_list = MakeTable(); |
| 200 | MakeObjects(w->objects_list); |
| 201 | |
| 202 | image = LoadRGBImage( TABLE_TEXTURE, &imgWidth, &imgHeight, &imgFormat ); |
| 203 | if (!image) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 204 | printf("Couldn't read %s\n", TABLE_TEXTURE); |
| 205 | exit(0); |
| 206 | } |
| 207 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 208 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight, |
| 209 | imgFormat, GL_UNSIGNED_BYTE, image); |
| 210 | free(image); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 211 | |
| 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 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 216 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 217 | glShadeModel( GL_FLAT ); |
| 218 | |
| 219 | glEnable( GL_LIGHT0 ); |
| 220 | glEnable( GL_LIGHTING ); |
| 221 | |
Brian Paul | 73ccfa0 | 2001-04-25 15:51:32 +0000 | [diff] [blame] | 222 | glClearColor( 0.5, 0.5, 0.9, 0.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 223 | |
| 224 | glEnable( GL_NORMALIZE ); |
| 225 | } |
| 226 | |
| 227 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 228 | static void |
| 229 | Reshape(int width, int height) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 230 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 231 | struct window *w = CurrentWindow(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 232 | GLfloat yAspect = 2.5; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 233 | GLfloat xAspect = yAspect * (float) width / (float) height; |
| 234 | w->width = width; |
| 235 | w->height = height; |
| 236 | glViewport(0, 0, width, height); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 237 | glMatrixMode(GL_PROJECTION); |
| 238 | glLoadIdentity(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 239 | glFrustum( -xAspect, xAspect, -yAspect, yAspect, 10.0, 30.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 240 | glMatrixMode(GL_MODELVIEW); |
| 241 | glLoadIdentity(); |
| 242 | } |
| 243 | |
| 244 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 245 | static void |
| 246 | DrawObjects(struct window *w, GLfloat eyex, GLfloat eyey, GLfloat eyez) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 247 | { |
| 248 | (void) eyex; |
| 249 | (void) eyey; |
| 250 | (void) eyez; |
| 251 | #ifndef USE_ZBUFFER |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 252 | if (eyex<0.5) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 253 | #endif |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 254 | 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(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 268 | #ifndef USE_ZBUFFER |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 269 | } |
| 270 | else { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 271 | 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(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 278 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 279 | 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 Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 285 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 286 | #endif |
| 287 | } |
| 288 | |
| 289 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 290 | static void |
| 291 | DrawTable(struct window *w) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 292 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 293 | glCallList(w->table_list); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 297 | static void |
| 298 | DrawWindow(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 299 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 300 | struct window *w = CurrentWindow(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 301 | static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 302 | GLfloat dist = 20.0; |
| 303 | GLfloat eyex, eyey, eyez; |
| 304 | |
Brian Paul | 693a66a | 2006-03-30 14:24:58 +0000 | [diff] [blame] | 305 | 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 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 313 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 314 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 315 | |
Brian Paul | 693a66a | 2006-03-30 14:24:58 +0000 | [diff] [blame] | 316 | if (w->drawBuffer == GL_NONE) { |
| 317 | glDrawBuffer(GL_NONE); |
| 318 | } |
| 319 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 320 | 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); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 323 | |
| 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 */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 331 | glDisable( GL_DEPTH_TEST ); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 332 | glEnable( GL_STENCIL_TEST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 333 | glStencilFunc( GL_ALWAYS, 1, 0xffffffff ); |
| 334 | glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE ); |
| 335 | glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 336 | DrawTable(w); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 337 | glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); |
| 338 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 339 | glEnable( GL_DEPTH_TEST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 340 | |
| 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 Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 353 | DrawObjects(w, eyex, eyey, eyez); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 354 | 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 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 365 | glEnable( GL_TEXTURE_2D ); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 366 | DrawTable(w); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 367 | glDisable( GL_TEXTURE_2D ); |
| 368 | glDisable( GL_BLEND ); |
| 369 | |
| 370 | /* view from top */ |
| 371 | glPushMatrix(); |
| 372 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 373 | DrawObjects(w, eyex, eyey, eyez); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 374 | |
| 375 | glPopMatrix(); |
| 376 | |
| 377 | glPopMatrix(); |
| 378 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 379 | if (w->showBuffer == GL_DEPTH) { |
| 380 | ShowDepthBuffer(w->width, w->height, 1.0, 0.0); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 381 | } |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 382 | else if (w->showBuffer == GL_STENCIL) { |
| 383 | ShowStencilBuffer(w->width, w->height, 255.0, 0.0); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 384 | } |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 385 | else if (w->showBuffer == GL_ALPHA) { |
| 386 | ShowAlphaBuffer(w->width, w->height); |
Brian Paul | 73ccfa0 | 2001-04-25 15:51:32 +0000 | [diff] [blame] | 387 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 388 | |
Brian Paul | 693a66a | 2006-03-30 14:24:58 +0000 | [diff] [blame] | 389 | if (w->drawBuffer == GL_FRONT) |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 390 | glFinish(); |
Brian Paul | 693a66a | 2006-03-30 14:24:58 +0000 | [diff] [blame] | 391 | else |
| 392 | glutSwapBuffers(); |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 393 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 394 | /* calc/show frame rate */ |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 395 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 396 | static GLint t0 = 0; |
| 397 | static GLint frames = 0; |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 398 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 399 | 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); |
Keith Whitwell | e6479c6 | 2009-02-24 12:02:24 +0000 | [diff] [blame] | 404 | fflush(stdout); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 405 | t0 = t; |
| 406 | frames = 0; |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 409 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 410 | |
| 411 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 412 | static void |
| 413 | Idle(void) |
Brian Paul | 2b2e621 | 2001-01-23 23:43:53 +0000 | [diff] [blame] | 414 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 415 | double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; |
| 416 | struct window *w; |
| 417 | for (w = FirstWindow; w; w = w->next) { |
| 418 | if (w->anim) { |
| 419 | double dt; |
| 420 | if (w->t0 < 0.0) |
| 421 | w->t0 = t; |
| 422 | dt = t - w->t0; |
| 423 | w->t0 = t; |
| 424 | w->spin += 60.0 * dt; |
| 425 | w->yrot += 90.0 * dt; |
| 426 | assert(w->id); |
| 427 | glutSetWindow(w->id); |
| 428 | glutPostRedisplay(); |
| 429 | } |
| 430 | } |
Brian Paul | 2b2e621 | 2001-01-23 23:43:53 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 434 | static void |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 435 | UpdateIdleFunc(void) |
| 436 | { |
| 437 | if (AnyAnimating()) |
| 438 | glutIdleFunc(Idle); |
| 439 | else |
| 440 | glutIdleFunc(NULL); |
| 441 | } |
| 442 | |
| 443 | static void |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 444 | Key(unsigned char key, int x, int y) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 445 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 446 | struct window *w = CurrentWindow(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 447 | (void) x; |
| 448 | (void) y; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 449 | |
| 450 | switch (key) { |
| 451 | case 'd': |
| 452 | w->showBuffer = GL_DEPTH; |
| 453 | glutPostRedisplay(); |
| 454 | break; |
| 455 | case 's': |
| 456 | w->showBuffer = GL_STENCIL; |
| 457 | glutPostRedisplay(); |
| 458 | break; |
| 459 | case 'a': |
| 460 | w->showBuffer = GL_ALPHA; |
| 461 | glutPostRedisplay(); |
| 462 | break; |
| 463 | case 'c': |
| 464 | w->showBuffer = GL_NONE; |
| 465 | glutPostRedisplay(); |
| 466 | break; |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 467 | case 'f': |
| 468 | if (w->drawBuffer == GL_FRONT) |
| 469 | w->drawBuffer = GL_BACK; |
| 470 | else |
| 471 | w->drawBuffer = GL_FRONT; |
| 472 | glutPostRedisplay(); |
| 473 | break; |
Brian Paul | 693a66a | 2006-03-30 14:24:58 +0000 | [diff] [blame] | 474 | case '0': |
| 475 | w->drawBuffer = GL_NONE; |
| 476 | glutPostRedisplay(); |
| 477 | break; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 478 | case ' ': |
| 479 | w->anim = !w->anim; |
| 480 | w->t0 = -1; |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 481 | UpdateIdleFunc(); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 482 | glutPostRedisplay(); |
| 483 | break; |
| 484 | case 'n': |
| 485 | CreateWindow(); |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 486 | UpdateIdleFunc(); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 487 | break; |
| 488 | case 'k': |
| 489 | KillWindow(w); |
| 490 | if (FirstWindow == NULL) |
| 491 | exit(0); |
| 492 | break; |
| 493 | case 27: |
| 494 | KillAllWindows(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 495 | exit(0); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 496 | break; |
| 497 | default: |
| 498 | ; |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 499 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 503 | static void |
| 504 | SpecialKey(int key, int x, int y) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 505 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 506 | struct window *w = CurrentWindow(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 507 | (void) x; |
| 508 | (void) y; |
| 509 | switch (key) { |
| 510 | case GLUT_KEY_UP: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 511 | w->xrot += 3.0; |
| 512 | if (w->xrot > 85) |
| 513 | w->xrot = 85; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 514 | break; |
| 515 | case GLUT_KEY_DOWN: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 516 | w->xrot -= 3.0; |
| 517 | if (w->xrot < 5) |
| 518 | w->xrot = 5; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 519 | break; |
| 520 | case GLUT_KEY_LEFT: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 521 | w->yrot += 3.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 522 | break; |
| 523 | case GLUT_KEY_RIGHT: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 524 | w->yrot -= 3.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | glutPostRedisplay(); |
| 528 | } |
| 529 | |
| 530 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 531 | static void |
| 532 | CreateWindow(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 533 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 534 | char title[1000]; |
| 535 | struct window *w = (struct window *) calloc(1, sizeof(struct window)); |
| 536 | |
| 537 | glutInitWindowSize(INIT_WIDTH, INIT_HEIGHT); |
| 538 | w->id = glutCreateWindow("foo"); |
| 539 | sprintf(title, "reflect window %d", w->id); |
| 540 | glutSetWindowTitle(title); |
| 541 | assert(w->id); |
| 542 | w->width = INIT_WIDTH; |
| 543 | w->height = INIT_HEIGHT; |
| 544 | w->anim = GL_TRUE; |
| 545 | w->xrot = 30.0; |
| 546 | w->yrot = 50.0; |
| 547 | w->spin = 0.0; |
| 548 | w->showBuffer = GL_NONE; |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 549 | w->drawBuffer = GL_BACK; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 550 | |
| 551 | InitWindow(w); |
| 552 | |
| 553 | glutReshapeFunc(Reshape); |
| 554 | glutDisplayFunc(DrawWindow); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 555 | glutKeyboardFunc(Key); |
| 556 | glutSpecialFunc(SpecialKey); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 557 | |
| 558 | /* insert at head of list */ |
| 559 | w->next = FirstWindow; |
| 560 | FirstWindow = w; |
| 561 | } |
| 562 | |
| 563 | |
| 564 | static void |
| 565 | Usage(void) |
| 566 | { |
| 567 | printf("Keys:\n"); |
| 568 | printf(" a - show alpha buffer\n"); |
| 569 | printf(" d - show depth buffer\n"); |
| 570 | printf(" s - show stencil buffer\n"); |
| 571 | printf(" c - show color buffer\n"); |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 572 | printf(" f - toggle rendering to front/back color buffer\n"); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 573 | printf(" n - create new window\n"); |
| 574 | printf(" k - kill window\n"); |
| 575 | printf(" SPACE - toggle animation\n"); |
| 576 | printf(" ARROWS - rotate scene\n"); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | int |
| 581 | main(int argc, char *argv[]) |
| 582 | { |
| 583 | glutInit(&argc, argv); |
| 584 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | |
| 585 | GLUT_STENCIL | GLUT_ALPHA); |
| 586 | CreateWindow(); |
| 587 | glutIdleFunc(Idle); |
| 588 | Usage(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 589 | glutMainLoop(); |
| 590 | return 0; |
| 591 | } |