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