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