Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
Brian Paul | 20cdbc0 | 1999-10-23 08:12:23 +0000 | [diff] [blame] | 3 | * Specular reflection demo. The specular highlight is modulated by |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 4 | * a sphere-mapped texture. The result is a high-gloss surface. |
| 5 | * NOTE: you really need hardware acceleration for this. |
Brian Paul | 20cdbc0 | 1999-10-23 08:12:23 +0000 | [diff] [blame] | 6 | * Also note, this technique can't be implemented with multi-texture |
| 7 | * and separate specular color interpolation because there's no way |
| 8 | * to indicate that the second texture unit (the reflection map) |
| 9 | * should modulate the specular color and not the base color. |
| 10 | * A future multi-texture extension could fix that. |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 11 | * |
| 12 | * Command line options: |
| 13 | * -info print GL implementation information |
| 14 | * |
| 15 | * |
| 16 | * Brian Paul October 22, 1999 This program is in the public domain. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <math.h> |
| 24 | #include <GL/glut.h> |
| 25 | |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame] | 26 | #include "readtex.c" /* I know, this is a hack. */ |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 27 | |
| 28 | #define SPECULAR_TEXTURE_FILE "../images/reflect.rgb" |
| 29 | #define BASE_TEXTURE_FILE "../images/tile.rgb" |
| 30 | |
| 31 | /* Menu items */ |
| 32 | #define DO_SPEC_TEXTURE 1 |
| 33 | #define OBJECT 2 |
| 34 | #define ANIMATE 3 |
| 35 | #define QUIT 100 |
| 36 | |
Brian Paul | cb40ebd | 2004-05-04 23:57:12 +0000 | [diff] [blame^] | 37 | /* for convolution */ |
| 38 | #define FILTER_SIZE 7 |
| 39 | |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 40 | static GLuint CylinderObj = 0; |
| 41 | static GLuint TeapotObj = 0; |
| 42 | static GLuint Object = 0; |
| 43 | static GLboolean Animate = GL_TRUE; |
| 44 | |
| 45 | static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0; |
| 46 | static GLfloat DXrot = 1.0, DYrot = 2.5; |
| 47 | |
| 48 | static GLfloat Black[4] = { 0, 0, 0, 0 }; |
| 49 | static GLfloat White[4] = { 1, 1, 1, 1 }; |
| 50 | static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */ |
Brian Paul | e8e20ae | 2000-08-29 21:17:38 +0000 | [diff] [blame] | 51 | static GLfloat Shininess = 6; |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 52 | |
| 53 | static GLuint BaseTexture, SpecularTexture; |
| 54 | static GLboolean DoSpecTexture = GL_TRUE; |
| 55 | |
| 56 | /* performance info */ |
| 57 | static GLint T0 = 0; |
| 58 | static GLint Frames = 0; |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | static void Idle( void ) |
| 64 | { |
| 65 | if (Animate) { |
| 66 | Xrot += DXrot; |
| 67 | Yrot += DYrot; |
| 68 | glutPostRedisplay(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void Display( void ) |
| 74 | { |
| 75 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 76 | |
| 77 | glPushMatrix(); |
| 78 | glRotatef(Xrot, 1.0, 0.0, 0.0); |
| 79 | glRotatef(Yrot, 0.0, 1.0, 0.0); |
| 80 | glRotatef(Zrot, 0.0, 0.0, 1.0); |
| 81 | |
| 82 | /* First pass: diffuse lighting with base texture */ |
| 83 | glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse); |
| 84 | glMaterialfv(GL_FRONT, GL_SPECULAR, Black); |
| 85 | glEnable(GL_TEXTURE_2D); |
| 86 | glBindTexture(GL_TEXTURE_2D, BaseTexture); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 87 | glCallList(Object); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 88 | |
| 89 | /* Second pass: specular lighting with reflection texture */ |
Brian Paul | 1b94df0 | 2002-11-28 15:51:55 +0000 | [diff] [blame] | 90 | glEnable(GL_POLYGON_OFFSET_FILL); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 91 | glBlendFunc(GL_ONE, GL_ONE); /* add */ |
| 92 | glEnable(GL_BLEND); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 93 | glMaterialfv(GL_FRONT, GL_DIFFUSE, Black); |
| 94 | glMaterialfv(GL_FRONT, GL_SPECULAR, White); |
| 95 | if (DoSpecTexture) { |
| 96 | glBindTexture(GL_TEXTURE_2D, SpecularTexture); |
| 97 | glEnable(GL_TEXTURE_GEN_S); |
| 98 | glEnable(GL_TEXTURE_GEN_T); |
| 99 | } |
| 100 | else { |
| 101 | glDisable(GL_TEXTURE_2D); |
| 102 | } |
| 103 | glCallList(Object); |
| 104 | glDisable(GL_TEXTURE_GEN_S); |
| 105 | glDisable(GL_TEXTURE_GEN_T); |
| 106 | glDisable(GL_BLEND); |
Brian Paul | 1b94df0 | 2002-11-28 15:51:55 +0000 | [diff] [blame] | 107 | glDisable(GL_POLYGON_OFFSET_FILL); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 108 | |
| 109 | glPopMatrix(); |
| 110 | |
| 111 | glutSwapBuffers(); |
| 112 | |
| 113 | if (Animate) { |
| 114 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
| 115 | Frames++; |
| 116 | if (t - T0 >= 5000) { |
| 117 | GLfloat seconds = (t - T0) / 1000.0; |
| 118 | GLfloat fps = Frames / seconds; |
| 119 | printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps); |
| 120 | T0 = t; |
| 121 | Frames = 0; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void Reshape( int width, int height ) |
| 128 | { |
| 129 | GLfloat h = 30.0; |
| 130 | GLfloat w = h * width / height; |
| 131 | glViewport( 0, 0, width, height ); |
| 132 | glMatrixMode( GL_PROJECTION ); |
| 133 | glLoadIdentity(); |
| 134 | glFrustum( -w, w, -h, h, 150.0, 500.0 ); |
| 135 | glMatrixMode( GL_MODELVIEW ); |
| 136 | glLoadIdentity(); |
| 137 | glTranslatef( 0.0, 0.0, -380.0 ); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | static void ToggleAnimate(void) |
| 142 | { |
| 143 | Animate = !Animate; |
| 144 | if (Animate) { |
| 145 | glutIdleFunc( Idle ); |
| 146 | T0 = glutGet(GLUT_ELAPSED_TIME); |
| 147 | Frames = 0; |
| 148 | } |
| 149 | else { |
| 150 | glutIdleFunc( NULL ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | static void ModeMenu(int entry) |
| 156 | { |
| 157 | if (entry==ANIMATE) { |
| 158 | ToggleAnimate(); |
| 159 | } |
| 160 | else if (entry==DO_SPEC_TEXTURE) { |
| 161 | DoSpecTexture = !DoSpecTexture; |
| 162 | } |
| 163 | else if (entry==OBJECT) { |
| 164 | if (Object == TeapotObj) |
| 165 | Object = CylinderObj; |
| 166 | else |
| 167 | Object = TeapotObj; |
| 168 | } |
| 169 | else if (entry==QUIT) { |
| 170 | exit(0); |
| 171 | } |
| 172 | glutPostRedisplay(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static void Key( unsigned char key, int x, int y ) |
| 177 | { |
| 178 | (void) x; |
| 179 | (void) y; |
| 180 | switch (key) { |
| 181 | case 's': |
| 182 | Shininess--; |
| 183 | if (Shininess < 0.0) |
| 184 | Shininess = 0.0; |
| 185 | glMaterialf(GL_FRONT, GL_SHININESS, Shininess); |
| 186 | printf("Shininess = %g\n", Shininess); |
| 187 | break; |
| 188 | case 'S': |
| 189 | Shininess++; |
| 190 | if (Shininess > 128.0) |
| 191 | Shininess = 128.0; |
| 192 | glMaterialf(GL_FRONT, GL_SHININESS, Shininess); |
| 193 | printf("Shininess = %g\n", Shininess); |
| 194 | break; |
| 195 | case ' ': |
| 196 | ToggleAnimate(); |
| 197 | break; |
| 198 | case 27: |
| 199 | exit(0); |
| 200 | break; |
| 201 | } |
| 202 | glutPostRedisplay(); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | static void SpecialKey( int key, int x, int y ) |
| 207 | { |
| 208 | float step = 3.0; |
| 209 | (void) x; |
| 210 | (void) y; |
| 211 | |
| 212 | switch (key) { |
| 213 | case GLUT_KEY_UP: |
| 214 | Xrot += step; |
| 215 | break; |
| 216 | case GLUT_KEY_DOWN: |
| 217 | Xrot -= step; |
| 218 | break; |
| 219 | case GLUT_KEY_LEFT: |
| 220 | Yrot += step; |
| 221 | break; |
| 222 | case GLUT_KEY_RIGHT: |
| 223 | Yrot -= step; |
| 224 | break; |
| 225 | } |
| 226 | glutPostRedisplay(); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | static void Init( int argc, char *argv[] ) |
| 231 | { |
Brian Paul | cb40ebd | 2004-05-04 23:57:12 +0000 | [diff] [blame^] | 232 | GLboolean convolve = GL_FALSE; |
| 233 | int i; |
| 234 | |
| 235 | for (i = 1; i < argc; i++) { |
| 236 | if (strcmp(argv[i], "-info")==0) { |
| 237 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 238 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 239 | printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
| 240 | printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 241 | } |
| 242 | else if (strcmp(argv[i], "-c")==0) { |
| 243 | convolve = GL_TRUE; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 248 | /* Cylinder object */ |
| 249 | { |
| 250 | static GLfloat height = 100.0; |
| 251 | static GLfloat radius = 40.0; |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 252 | static GLint slices = 24; /* pie slices around Z axis */ |
| 253 | static GLint stacks = 10; /* subdivisions along length of cylinder */ |
| 254 | static GLint rings = 4; /* rings in the end disks */ |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 255 | GLUquadricObj *q = gluNewQuadric(); |
| 256 | assert(q); |
| 257 | gluQuadricTexture(q, GL_TRUE); |
| 258 | |
| 259 | CylinderObj = glGenLists(1); |
| 260 | glNewList(CylinderObj, GL_COMPILE); |
| 261 | |
| 262 | glPushMatrix(); |
| 263 | glTranslatef(0.0, 0.0, -0.5 * height); |
| 264 | |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 265 | glMatrixMode(GL_TEXTURE); |
| 266 | glLoadIdentity(); |
Brian Paul | 02e8a03 | 2000-06-27 17:04:43 +0000 | [diff] [blame] | 267 | /*glScalef(8.0, 4.0, 2.0);*/ |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 268 | glMatrixMode(GL_MODELVIEW); |
| 269 | |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 270 | /* cylinder */ |
| 271 | gluQuadricNormals(q, GL_SMOOTH); |
| 272 | gluQuadricTexture(q, GL_TRUE); |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 273 | gluCylinder(q, radius, radius, height, slices, stacks); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 274 | |
| 275 | /* end cap */ |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 276 | glMatrixMode(GL_TEXTURE); |
| 277 | glLoadIdentity(); |
| 278 | glScalef(3.0, 3.0, 1.0); |
| 279 | glMatrixMode(GL_MODELVIEW); |
| 280 | |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 281 | glTranslatef(0.0, 0.0, height); |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 282 | gluDisk(q, 0.0, radius, slices, rings); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 283 | |
| 284 | /* other end cap */ |
| 285 | glTranslatef(0.0, 0.0, -height); |
| 286 | gluQuadricOrientation(q, GLU_INSIDE); |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 287 | gluDisk(q, 0.0, radius, slices, rings); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 288 | |
| 289 | glPopMatrix(); |
Brian Paul | 9a19ccb | 1999-10-26 17:08:31 +0000 | [diff] [blame] | 290 | |
| 291 | glMatrixMode(GL_TEXTURE); |
| 292 | glLoadIdentity(); |
| 293 | glMatrixMode(GL_MODELVIEW); |
| 294 | |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 295 | glEndList(); |
| 296 | gluDeleteQuadric(q); |
| 297 | } |
| 298 | |
| 299 | /* Teapot */ |
| 300 | { |
| 301 | TeapotObj = glGenLists(1); |
| 302 | glNewList(TeapotObj, GL_COMPILE); |
| 303 | |
| 304 | glFrontFace(GL_CW); |
| 305 | glutSolidTeapot(40.0); |
| 306 | glFrontFace(GL_CCW); |
| 307 | |
| 308 | glEndList(); |
| 309 | } |
| 310 | |
| 311 | /* show cylinder by default */ |
| 312 | Object = CylinderObj; |
| 313 | |
| 314 | |
| 315 | /* lighting */ |
| 316 | glEnable(GL_LIGHTING); |
| 317 | { |
| 318 | GLfloat pos[4] = { 3, 3, 3, 1 }; |
| 319 | glLightfv(GL_LIGHT0, GL_AMBIENT, Black); |
| 320 | glLightfv(GL_LIGHT0, GL_DIFFUSE, White); |
| 321 | glLightfv(GL_LIGHT0, GL_SPECULAR, White); |
| 322 | glLightfv(GL_LIGHT0, GL_POSITION, pos); |
| 323 | glEnable(GL_LIGHT0); |
| 324 | glMaterialfv(GL_FRONT, GL_AMBIENT, Black); |
| 325 | glMaterialf(GL_FRONT, GL_SHININESS, Shininess); |
| 326 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); |
| 327 | } |
| 328 | |
| 329 | /* Base texture */ |
| 330 | glGenTextures(1, &BaseTexture); |
| 331 | glBindTexture(GL_TEXTURE_2D, BaseTexture); |
| 332 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 333 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 334 | if (!LoadRGBMipmaps(BASE_TEXTURE_FILE, GL_RGB)) { |
| 335 | printf("Error: couldn't load texture image file %s\n", BASE_TEXTURE_FILE); |
| 336 | exit(1); |
| 337 | } |
| 338 | |
| 339 | /* Specular texture */ |
| 340 | glGenTextures(1, &SpecularTexture); |
| 341 | glBindTexture(GL_TEXTURE_2D, SpecularTexture); |
| 342 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 343 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 344 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 345 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
Brian Paul | cb40ebd | 2004-05-04 23:57:12 +0000 | [diff] [blame^] | 346 | if (convolve) { |
| 347 | /* use convolution to blur the texture to simulate a dull finish |
| 348 | * on the object. |
| 349 | */ |
| 350 | GLubyte *img; |
| 351 | GLenum format; |
| 352 | GLint w, h; |
| 353 | GLfloat filter[FILTER_SIZE][FILTER_SIZE][4]; |
| 354 | |
| 355 | for (h = 0; h < FILTER_SIZE; h++) { |
| 356 | for (w = 0; w < FILTER_SIZE; w++) { |
| 357 | const GLfloat k = 1.0 / (FILTER_SIZE * FILTER_SIZE); |
| 358 | filter[h][w][0] = k; |
| 359 | filter[h][w][1] = k; |
| 360 | filter[h][w][2] = k; |
| 361 | filter[h][w][3] = k; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | glEnable(GL_CONVOLUTION_2D); |
| 366 | glConvolutionParameteri(GL_CONVOLUTION_2D, |
| 367 | GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); |
| 368 | glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_RGBA, |
| 369 | FILTER_SIZE, FILTER_SIZE, |
| 370 | GL_RGBA, GL_FLOAT, filter); |
| 371 | |
| 372 | img = LoadRGBImage(SPECULAR_TEXTURE_FILE, &w, &h, &format); |
| 373 | if (!img) { |
| 374 | printf("Error: couldn't load texture image file %s\n", |
| 375 | SPECULAR_TEXTURE_FILE); |
| 376 | exit(1); |
| 377 | } |
| 378 | |
| 379 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); |
| 380 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, |
| 381 | format, GL_UNSIGNED_BYTE, img); |
| 382 | free(img); |
| 383 | } |
| 384 | else { |
| 385 | /* regular path */ |
| 386 | if (!LoadRGBMipmaps(SPECULAR_TEXTURE_FILE, GL_RGB)) { |
| 387 | printf("Error: couldn't load texture image file %s\n", |
| 388 | SPECULAR_TEXTURE_FILE); |
| 389 | exit(1); |
| 390 | } |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* misc */ |
| 394 | glEnable(GL_CULL_FACE); |
| 395 | glEnable(GL_TEXTURE_2D); |
| 396 | glEnable(GL_DEPTH_TEST); |
| 397 | glEnable(GL_NORMALIZE); |
| 398 | |
Brian Paul | 1b94df0 | 2002-11-28 15:51:55 +0000 | [diff] [blame] | 399 | glPolygonOffset( -1, -1 ); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
| 403 | int main( int argc, char *argv[] ) |
| 404 | { |
| 405 | glutInit( &argc, argv ); |
Keith Whitwell | 469d1b0 | 2004-01-28 10:07:48 +0000 | [diff] [blame] | 406 | glutInitWindowPosition(0, 0); |
Brian Paul | bb1119f | 1999-10-22 20:34:57 +0000 | [diff] [blame] | 407 | glutInitWindowSize( 500, 500 ); |
| 408 | |
| 409 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); |
| 410 | |
| 411 | glutCreateWindow(argv[0] ); |
| 412 | |
| 413 | Init(argc, argv); |
| 414 | |
| 415 | glutReshapeFunc( Reshape ); |
| 416 | glutKeyboardFunc( Key ); |
| 417 | glutSpecialFunc( SpecialKey ); |
| 418 | glutDisplayFunc( Display ); |
| 419 | glutIdleFunc( Idle ); |
| 420 | |
| 421 | glutCreateMenu(ModeMenu); |
| 422 | glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE); |
| 423 | glutAddMenuEntry("Toggle Object", OBJECT); |
| 424 | glutAddMenuEntry("Toggle Animate", ANIMATE); |
| 425 | glutAddMenuEntry("Quit", QUIT); |
| 426 | glutAttachMenu(GLUT_RIGHT_BUTTON); |
| 427 | |
| 428 | glutMainLoop(); |
| 429 | return 0; |
| 430 | } |