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(); |
Brian Paul | 1ad3b7e | 2005-11-19 23:09:14 +0000 | [diff] [blame^] | 184 | |
| 185 | gluDeleteQuadric(q); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 189 | static void |
| 190 | InitWindow(struct window *w) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 191 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 192 | GLint imgWidth, imgHeight; |
| 193 | GLenum imgFormat; |
| 194 | GLubyte *image = NULL; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 195 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 196 | w->table_list = MakeTable(); |
| 197 | MakeObjects(w->objects_list); |
| 198 | |
| 199 | image = LoadRGBImage( TABLE_TEXTURE, &imgWidth, &imgHeight, &imgFormat ); |
| 200 | if (!image) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 201 | printf("Couldn't read %s\n", TABLE_TEXTURE); |
| 202 | exit(0); |
| 203 | } |
| 204 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 205 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight, |
| 206 | imgFormat, GL_UNSIGNED_BYTE, image); |
| 207 | free(image); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 208 | |
| 209 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
| 210 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
| 211 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 212 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 213 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 214 | glShadeModel( GL_FLAT ); |
| 215 | |
| 216 | glEnable( GL_LIGHT0 ); |
| 217 | glEnable( GL_LIGHTING ); |
| 218 | |
Brian Paul | 73ccfa0 | 2001-04-25 15:51:32 +0000 | [diff] [blame] | 219 | glClearColor( 0.5, 0.5, 0.9, 0.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 220 | |
| 221 | glEnable( GL_NORMALIZE ); |
| 222 | } |
| 223 | |
| 224 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 225 | static void |
| 226 | Reshape(int width, int height) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 227 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 228 | struct window *w = CurrentWindow(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 229 | GLfloat yAspect = 2.5; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 230 | GLfloat xAspect = yAspect * (float) width / (float) height; |
| 231 | w->width = width; |
| 232 | w->height = height; |
| 233 | glViewport(0, 0, width, height); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 234 | glMatrixMode(GL_PROJECTION); |
| 235 | glLoadIdentity(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 236 | glFrustum( -xAspect, xAspect, -yAspect, yAspect, 10.0, 30.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 237 | glMatrixMode(GL_MODELVIEW); |
| 238 | glLoadIdentity(); |
| 239 | } |
| 240 | |
| 241 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 242 | static void |
| 243 | DrawObjects(struct window *w, GLfloat eyex, GLfloat eyey, GLfloat eyez) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 244 | { |
| 245 | (void) eyex; |
| 246 | (void) eyey; |
| 247 | (void) eyez; |
| 248 | #ifndef USE_ZBUFFER |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 249 | if (eyex<0.5) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 250 | #endif |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 251 | glPushMatrix(); |
| 252 | glTranslatef( 1.0, 1.5, 0.0 ); |
| 253 | glRotatef( w->spin, 1.0, 0.5, 0.0 ); |
| 254 | glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 ); |
| 255 | glCallList( w->objects_list[0] ); |
| 256 | glPopMatrix(); |
| 257 | |
| 258 | glPushMatrix(); |
| 259 | glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*w->spin) ), 0.0 ); |
| 260 | glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 ); |
| 261 | glRotatef( w->spin, 1.0, 0.5, 0.0 ); |
| 262 | glScalef( 0.5, 0.5, 0.5 ); |
| 263 | glCallList( w->objects_list[1] ); |
| 264 | glPopMatrix(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 265 | #ifndef USE_ZBUFFER |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 266 | } |
| 267 | else { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 268 | glPushMatrix(); |
| 269 | glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*w->spin) ), 0.0 ); |
| 270 | glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 ); |
| 271 | glRotatef( w->spin, 1.0, 0.5, 0.0 ); |
| 272 | glScalef( 0.5, 0.5, 0.5 ); |
| 273 | glCallList( w->objects_list[1] ); |
| 274 | glPopMatrix(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 275 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 276 | glPushMatrix(); |
| 277 | glTranslatef( 1.0, 1.5, 0.0 ); |
| 278 | glRotatef( w->spin, 1.0, 0.5, 0.0 ); |
| 279 | glRotatef( 0.5*w->spin, 0.0, 0.5, 1.0 ); |
| 280 | glCallList( w->objects_list[0] ); |
| 281 | glPopMatrix(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 282 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 283 | #endif |
| 284 | } |
| 285 | |
| 286 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 287 | static void |
| 288 | DrawTable(struct window *w) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 289 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 290 | glCallList(w->table_list); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 294 | static void |
| 295 | DrawWindow(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 296 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 297 | struct window *w = CurrentWindow(); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 298 | static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 299 | GLfloat dist = 20.0; |
| 300 | GLfloat eyex, eyey, eyez; |
| 301 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 302 | glDrawBuffer(w->drawBuffer); |
| 303 | glReadBuffer(w->drawBuffer); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 304 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 305 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 306 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 307 | eyex = dist * cos(w->yrot * DEG2RAD) * cos(w->xrot * DEG2RAD); |
| 308 | eyez = dist * sin(w->yrot * DEG2RAD) * cos(w->xrot * DEG2RAD); |
| 309 | eyey = dist * sin(w->xrot * DEG2RAD); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 310 | |
| 311 | /* view from top */ |
| 312 | glPushMatrix(); |
| 313 | gluLookAt( eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); |
| 314 | |
| 315 | glLightfv( GL_LIGHT0, GL_POSITION, light_pos ); |
| 316 | |
| 317 | /* draw table into stencil planes */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 318 | glDisable( GL_DEPTH_TEST ); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 319 | glEnable( GL_STENCIL_TEST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 320 | glStencilFunc( GL_ALWAYS, 1, 0xffffffff ); |
| 321 | glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE ); |
| 322 | glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 323 | DrawTable(w); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 324 | glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); |
| 325 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 326 | glEnable( GL_DEPTH_TEST ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 327 | |
| 328 | /* render view from below (reflected viewport) */ |
| 329 | /* only draw where stencil==1 */ |
| 330 | if (eyey>0.0) { |
| 331 | glPushMatrix(); |
| 332 | |
| 333 | glStencilFunc( GL_EQUAL, 1, 0xffffffff ); /* draw if ==1 */ |
| 334 | glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP ); |
| 335 | glScalef( 1.0, -1.0, 1.0 ); |
| 336 | |
| 337 | /* Reposition light in reflected space. */ |
| 338 | glLightfv(GL_LIGHT0, GL_POSITION, light_pos); |
| 339 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 340 | DrawObjects(w, eyex, eyey, eyez); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 341 | glPopMatrix(); |
| 342 | |
| 343 | /* Restore light's original unreflected position. */ |
| 344 | glLightfv(GL_LIGHT0, GL_POSITION, light_pos); |
| 345 | } |
| 346 | |
| 347 | glDisable( GL_STENCIL_TEST ); |
| 348 | |
| 349 | glEnable( GL_BLEND ); |
| 350 | glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); |
| 351 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 352 | glEnable( GL_TEXTURE_2D ); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 353 | DrawTable(w); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 354 | glDisable( GL_TEXTURE_2D ); |
| 355 | glDisable( GL_BLEND ); |
| 356 | |
| 357 | /* view from top */ |
| 358 | glPushMatrix(); |
| 359 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 360 | DrawObjects(w, eyex, eyey, eyez); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 361 | |
| 362 | glPopMatrix(); |
| 363 | |
| 364 | glPopMatrix(); |
| 365 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 366 | if (w->showBuffer == GL_DEPTH) { |
| 367 | ShowDepthBuffer(w->width, w->height, 1.0, 0.0); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 368 | } |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 369 | else if (w->showBuffer == GL_STENCIL) { |
| 370 | ShowStencilBuffer(w->width, w->height, 255.0, 0.0); |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 371 | } |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 372 | else if (w->showBuffer == GL_ALPHA) { |
| 373 | ShowAlphaBuffer(w->width, w->height); |
Brian Paul | 73ccfa0 | 2001-04-25 15:51:32 +0000 | [diff] [blame] | 374 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 375 | |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 376 | if (w->drawBuffer == GL_BACK) |
| 377 | glutSwapBuffers(); |
| 378 | else |
| 379 | glFinish(); |
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 | /* calc/show frame rate */ |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 382 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 383 | static GLint t0 = 0; |
| 384 | static GLint frames = 0; |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 385 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 386 | frames++; |
| 387 | if (t - t0 >= 5000) { |
| 388 | GLfloat seconds = (t - t0) / 1000.0; |
| 389 | GLfloat fps = frames / seconds; |
| 390 | printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps); |
| 391 | t0 = t; |
| 392 | frames = 0; |
Brian Paul | cefc42f | 2000-09-15 16:43:57 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 395 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 396 | |
| 397 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 398 | static void |
| 399 | Idle(void) |
Brian Paul | 2b2e621 | 2001-01-23 23:43:53 +0000 | [diff] [blame] | 400 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 401 | double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; |
| 402 | struct window *w; |
| 403 | for (w = FirstWindow; w; w = w->next) { |
| 404 | if (w->anim) { |
| 405 | double dt; |
| 406 | if (w->t0 < 0.0) |
| 407 | w->t0 = t; |
| 408 | dt = t - w->t0; |
| 409 | w->t0 = t; |
| 410 | w->spin += 60.0 * dt; |
| 411 | w->yrot += 90.0 * dt; |
| 412 | assert(w->id); |
| 413 | glutSetWindow(w->id); |
| 414 | glutPostRedisplay(); |
| 415 | } |
| 416 | } |
Brian Paul | 2b2e621 | 2001-01-23 23:43:53 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 420 | static void |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 421 | UpdateIdleFunc(void) |
| 422 | { |
| 423 | if (AnyAnimating()) |
| 424 | glutIdleFunc(Idle); |
| 425 | else |
| 426 | glutIdleFunc(NULL); |
| 427 | } |
| 428 | |
| 429 | static void |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 430 | Key(unsigned char key, int x, int y) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 431 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 432 | struct window *w = CurrentWindow(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 433 | (void) x; |
| 434 | (void) y; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 435 | |
| 436 | switch (key) { |
| 437 | case 'd': |
| 438 | w->showBuffer = GL_DEPTH; |
| 439 | glutPostRedisplay(); |
| 440 | break; |
| 441 | case 's': |
| 442 | w->showBuffer = GL_STENCIL; |
| 443 | glutPostRedisplay(); |
| 444 | break; |
| 445 | case 'a': |
| 446 | w->showBuffer = GL_ALPHA; |
| 447 | glutPostRedisplay(); |
| 448 | break; |
| 449 | case 'c': |
| 450 | w->showBuffer = GL_NONE; |
| 451 | glutPostRedisplay(); |
| 452 | break; |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 453 | case 'f': |
| 454 | if (w->drawBuffer == GL_FRONT) |
| 455 | w->drawBuffer = GL_BACK; |
| 456 | else |
| 457 | w->drawBuffer = GL_FRONT; |
| 458 | glutPostRedisplay(); |
| 459 | break; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 460 | case ' ': |
| 461 | w->anim = !w->anim; |
| 462 | w->t0 = -1; |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 463 | UpdateIdleFunc(); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 464 | glutPostRedisplay(); |
| 465 | break; |
| 466 | case 'n': |
| 467 | CreateWindow(); |
Brian Paul | 8e247d5 | 2005-08-25 17:46:04 +0000 | [diff] [blame] | 468 | UpdateIdleFunc(); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 469 | break; |
| 470 | case 'k': |
| 471 | KillWindow(w); |
| 472 | if (FirstWindow == NULL) |
| 473 | exit(0); |
| 474 | break; |
| 475 | case 27: |
| 476 | KillAllWindows(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 477 | exit(0); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 478 | break; |
| 479 | default: |
| 480 | ; |
Brian Paul | 7a6bb1b | 2000-04-12 01:08:30 +0000 | [diff] [blame] | 481 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 485 | static void |
| 486 | SpecialKey(int key, int x, int y) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 487 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 488 | struct window *w = CurrentWindow(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 489 | (void) x; |
| 490 | (void) y; |
| 491 | switch (key) { |
| 492 | case GLUT_KEY_UP: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 493 | w->xrot += 3.0; |
| 494 | if (w->xrot > 85) |
| 495 | w->xrot = 85; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 496 | break; |
| 497 | case GLUT_KEY_DOWN: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 498 | w->xrot -= 3.0; |
| 499 | if (w->xrot < 5) |
| 500 | w->xrot = 5; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 501 | break; |
| 502 | case GLUT_KEY_LEFT: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 503 | w->yrot += 3.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 504 | break; |
| 505 | case GLUT_KEY_RIGHT: |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 506 | w->yrot -= 3.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | glutPostRedisplay(); |
| 510 | } |
| 511 | |
| 512 | |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 513 | static void |
| 514 | CreateWindow(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 515 | { |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 516 | char title[1000]; |
| 517 | struct window *w = (struct window *) calloc(1, sizeof(struct window)); |
| 518 | |
| 519 | glutInitWindowSize(INIT_WIDTH, INIT_HEIGHT); |
| 520 | w->id = glutCreateWindow("foo"); |
| 521 | sprintf(title, "reflect window %d", w->id); |
| 522 | glutSetWindowTitle(title); |
| 523 | assert(w->id); |
| 524 | w->width = INIT_WIDTH; |
| 525 | w->height = INIT_HEIGHT; |
| 526 | w->anim = GL_TRUE; |
| 527 | w->xrot = 30.0; |
| 528 | w->yrot = 50.0; |
| 529 | w->spin = 0.0; |
| 530 | w->showBuffer = GL_NONE; |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 531 | w->drawBuffer = GL_BACK; |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 532 | |
| 533 | InitWindow(w); |
| 534 | |
| 535 | glutReshapeFunc(Reshape); |
| 536 | glutDisplayFunc(DrawWindow); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 537 | glutKeyboardFunc(Key); |
| 538 | glutSpecialFunc(SpecialKey); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 539 | |
| 540 | /* insert at head of list */ |
| 541 | w->next = FirstWindow; |
| 542 | FirstWindow = w; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | static void |
| 547 | Usage(void) |
| 548 | { |
| 549 | printf("Keys:\n"); |
| 550 | printf(" a - show alpha buffer\n"); |
| 551 | printf(" d - show depth buffer\n"); |
| 552 | printf(" s - show stencil buffer\n"); |
| 553 | printf(" c - show color buffer\n"); |
Brian Paul | d578373 | 2005-08-31 16:42:59 +0000 | [diff] [blame] | 554 | printf(" f - toggle rendering to front/back color buffer\n"); |
Brian Paul | 5d7c486 | 2005-08-24 21:32:02 +0000 | [diff] [blame] | 555 | printf(" n - create new window\n"); |
| 556 | printf(" k - kill window\n"); |
| 557 | printf(" SPACE - toggle animation\n"); |
| 558 | printf(" ARROWS - rotate scene\n"); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | int |
| 563 | main(int argc, char *argv[]) |
| 564 | { |
| 565 | glutInit(&argc, argv); |
| 566 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | |
| 567 | GLUT_STENCIL | GLUT_ALPHA); |
| 568 | CreateWindow(); |
| 569 | glutIdleFunc(Idle); |
| 570 | Usage(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 571 | glutMainLoop(); |
| 572 | return 0; |
| 573 | } |