blob: 0b2e7426d53369eb3fd9879ad3932bc17b94284a [file] [log] [blame]
Brian Paulbb1119f1999-10-22 20:34:57 +00001
2/*
Brian Paul20cdbc01999-10-23 08:12:23 +00003 * Specular reflection demo. The specular highlight is modulated by
Brian Paulbb1119f1999-10-22 20:34:57 +00004 * a sphere-mapped texture. The result is a high-gloss surface.
5 * NOTE: you really need hardware acceleration for this.
Brian Paul20cdbc01999-10-23 08:12:23 +00006 * 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 Paulbb1119f1999-10-22 20:34:57 +000011 *
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 Paul92eddb02005-01-09 17:37:50 +000024#include <string.h>
Brian Paulbb1119f1999-10-22 20:34:57 +000025#include <GL/glut.h>
26
Brian Paul92eddb02005-01-09 17:37:50 +000027#include "readtex.h"
Brian Paulbb1119f1999-10-22 20:34:57 +000028
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
Brian Paulcb40ebd2004-05-04 23:57:12 +000038/* for convolution */
39#define FILTER_SIZE 7
40
Brian Paul68ad7ca2006-04-11 23:41:40 +000041static GLint WinWidth = 500, WinHeight = 500;
Brian Paulbb1119f1999-10-22 20:34:57 +000042static GLuint CylinderObj = 0;
43static GLuint TeapotObj = 0;
44static GLuint Object = 0;
45static GLboolean Animate = GL_TRUE;
46
47static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
Brian Paul92eddb02005-01-09 17:37:50 +000048static GLfloat DXrot = 20.0, DYrot = 50.;
Brian Paulbb1119f1999-10-22 20:34:57 +000049
50static GLfloat Black[4] = { 0, 0, 0, 0 };
51static GLfloat White[4] = { 1, 1, 1, 1 };
52static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */
Brian Paule8e20ae2000-08-29 21:17:38 +000053static GLfloat Shininess = 6;
Brian Paulbb1119f1999-10-22 20:34:57 +000054
55static GLuint BaseTexture, SpecularTexture;
56static GLboolean DoSpecTexture = GL_TRUE;
57
Brian Paul68ad7ca2006-04-11 23:41:40 +000058static GLboolean ButtonDown = GL_FALSE;
59static GLint ButtonX, ButtonY;
60static GLfloat Xrot0, Yrot0;
61
62
Brian Paulbb1119f1999-10-22 20:34:57 +000063/* performance info */
64static GLint T0 = 0;
65static GLint Frames = 0;
66
67
Brian Paulbb1119f1999-10-22 20:34:57 +000068static void Idle( void )
69{
Brian Paul92eddb02005-01-09 17:37:50 +000070 static double t0 = -1.;
71 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
72 if (t0 < 0.0)
73 t0 = t;
74 dt = t - t0;
75 t0 = t;
76
Brian Paulbb1119f1999-10-22 20:34:57 +000077 if (Animate) {
Brian Paul92eddb02005-01-09 17:37:50 +000078 Xrot += DXrot*dt;
79 Yrot += DYrot*dt;
Brian Paulbb1119f1999-10-22 20:34:57 +000080 glutPostRedisplay();
81 }
82}
83
84
85static void Display( void )
86{
87 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
88
89 glPushMatrix();
90 glRotatef(Xrot, 1.0, 0.0, 0.0);
91 glRotatef(Yrot, 0.0, 1.0, 0.0);
92 glRotatef(Zrot, 0.0, 0.0, 1.0);
93
94 /* First pass: diffuse lighting with base texture */
95 glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
96 glMaterialfv(GL_FRONT, GL_SPECULAR, Black);
97 glEnable(GL_TEXTURE_2D);
98 glBindTexture(GL_TEXTURE_2D, BaseTexture);
Brian Paulbb1119f1999-10-22 20:34:57 +000099 glCallList(Object);
Brian Paulbb1119f1999-10-22 20:34:57 +0000100
101 /* Second pass: specular lighting with reflection texture */
Brian Paul1b94df02002-11-28 15:51:55 +0000102 glEnable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000103 glBlendFunc(GL_ONE, GL_ONE); /* add */
104 glEnable(GL_BLEND);
Brian Paulbb1119f1999-10-22 20:34:57 +0000105 glMaterialfv(GL_FRONT, GL_DIFFUSE, Black);
106 glMaterialfv(GL_FRONT, GL_SPECULAR, White);
107 if (DoSpecTexture) {
108 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
109 glEnable(GL_TEXTURE_GEN_S);
110 glEnable(GL_TEXTURE_GEN_T);
111 }
112 else {
113 glDisable(GL_TEXTURE_2D);
114 }
115 glCallList(Object);
116 glDisable(GL_TEXTURE_GEN_S);
117 glDisable(GL_TEXTURE_GEN_T);
118 glDisable(GL_BLEND);
Brian Paul1b94df02002-11-28 15:51:55 +0000119 glDisable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000120
121 glPopMatrix();
122
123 glutSwapBuffers();
124
125 if (Animate) {
126 GLint t = glutGet(GLUT_ELAPSED_TIME);
127 Frames++;
128 if (t - T0 >= 5000) {
129 GLfloat seconds = (t - T0) / 1000.0;
130 GLfloat fps = Frames / seconds;
131 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
132 T0 = t;
133 Frames = 0;
134 }
135 }
136}
137
138
139static void Reshape( int width, int height )
140{
141 GLfloat h = 30.0;
142 GLfloat w = h * width / height;
Brian Paul68ad7ca2006-04-11 23:41:40 +0000143 WinWidth = width;
144 WinHeight = height;
Brian Paulbb1119f1999-10-22 20:34:57 +0000145 glViewport( 0, 0, width, height );
146 glMatrixMode( GL_PROJECTION );
147 glLoadIdentity();
148 glFrustum( -w, w, -h, h, 150.0, 500.0 );
149 glMatrixMode( GL_MODELVIEW );
150 glLoadIdentity();
151 glTranslatef( 0.0, 0.0, -380.0 );
152}
153
154
155static void ToggleAnimate(void)
156{
157 Animate = !Animate;
158 if (Animate) {
159 glutIdleFunc( Idle );
160 T0 = glutGet(GLUT_ELAPSED_TIME);
161 Frames = 0;
162 }
163 else {
164 glutIdleFunc( NULL );
165 }
166}
167
168
169static void ModeMenu(int entry)
170{
171 if (entry==ANIMATE) {
172 ToggleAnimate();
173 }
174 else if (entry==DO_SPEC_TEXTURE) {
175 DoSpecTexture = !DoSpecTexture;
176 }
177 else if (entry==OBJECT) {
178 if (Object == TeapotObj)
179 Object = CylinderObj;
180 else
181 Object = TeapotObj;
182 }
183 else if (entry==QUIT) {
184 exit(0);
185 }
186 glutPostRedisplay();
187}
188
189
190static void Key( unsigned char key, int x, int y )
191{
192 (void) x;
193 (void) y;
194 switch (key) {
195 case 's':
196 Shininess--;
197 if (Shininess < 0.0)
198 Shininess = 0.0;
199 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
200 printf("Shininess = %g\n", Shininess);
201 break;
202 case 'S':
203 Shininess++;
204 if (Shininess > 128.0)
205 Shininess = 128.0;
206 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
207 printf("Shininess = %g\n", Shininess);
208 break;
Brian Paul68ad7ca2006-04-11 23:41:40 +0000209 case 'a':
Brian Paulbb1119f1999-10-22 20:34:57 +0000210 case ' ':
211 ToggleAnimate();
212 break;
213 case 27:
214 exit(0);
215 break;
216 }
217 glutPostRedisplay();
218}
219
220
221static void SpecialKey( int key, int x, int y )
222{
223 float step = 3.0;
224 (void) x;
225 (void) y;
226
227 switch (key) {
228 case GLUT_KEY_UP:
229 Xrot += step;
230 break;
231 case GLUT_KEY_DOWN:
232 Xrot -= step;
233 break;
234 case GLUT_KEY_LEFT:
235 Yrot += step;
236 break;
237 case GLUT_KEY_RIGHT:
238 Yrot -= step;
239 break;
240 }
241 glutPostRedisplay();
242}
243
244
Brian Paul68ad7ca2006-04-11 23:41:40 +0000245static void
246MouseMotion(int x, int y)
247{
248 const float k = 300.0;
249 if (ButtonDown) {
250 float dx = x - ButtonX;
251 float dy = y - ButtonY;
252 Xrot = Xrot0 + k * dy / WinWidth;
253 Yrot = Yrot0 + k * dx / WinHeight;
254 glutPostRedisplay();
255 }
256}
257
258
259static void
260MouseButton(int button, int state, int x, int y)
261{
262 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
263 ButtonDown = GL_TRUE;
264 ButtonX = x;
265 ButtonY = y;
266 Xrot0 = Xrot;
267 Yrot0 = Yrot;
268 }
269 else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
270 ButtonDown = GL_FALSE;
271 }
272}
273
274
Brian Paulbb1119f1999-10-22 20:34:57 +0000275static void Init( int argc, char *argv[] )
276{
Brian Paulcb40ebd2004-05-04 23:57:12 +0000277 GLboolean convolve = GL_FALSE;
Brian Paul197d7252006-03-30 14:25:54 +0000278 GLboolean fullscreen = GL_FALSE;
Brian Paulcb40ebd2004-05-04 23:57:12 +0000279 int i;
280
281 for (i = 1; i < argc; i++) {
282 if (strcmp(argv[i], "-info")==0) {
283 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
284 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
285 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
286 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
287 }
288 else if (strcmp(argv[i], "-c")==0) {
289 convolve = GL_TRUE;
290 }
Brian Paul197d7252006-03-30 14:25:54 +0000291 else if (strcmp(argv[i], "-f")==0) {
292 fullscreen = GL_TRUE;
293 }
Brian Paulcb40ebd2004-05-04 23:57:12 +0000294 }
295
Brian Paul197d7252006-03-30 14:25:54 +0000296 if (fullscreen)
297 glutFullScreen();
Brian Paulcb40ebd2004-05-04 23:57:12 +0000298
Brian Paulbb1119f1999-10-22 20:34:57 +0000299 /* Cylinder object */
300 {
301 static GLfloat height = 100.0;
302 static GLfloat radius = 40.0;
Brian Paul9a19ccb1999-10-26 17:08:31 +0000303 static GLint slices = 24; /* pie slices around Z axis */
304 static GLint stacks = 10; /* subdivisions along length of cylinder */
305 static GLint rings = 4; /* rings in the end disks */
Brian Paulbb1119f1999-10-22 20:34:57 +0000306 GLUquadricObj *q = gluNewQuadric();
307 assert(q);
308 gluQuadricTexture(q, GL_TRUE);
309
310 CylinderObj = glGenLists(1);
311 glNewList(CylinderObj, GL_COMPILE);
312
313 glPushMatrix();
314 glTranslatef(0.0, 0.0, -0.5 * height);
315
Brian Paul9a19ccb1999-10-26 17:08:31 +0000316 glMatrixMode(GL_TEXTURE);
317 glLoadIdentity();
Brian Paul02e8a032000-06-27 17:04:43 +0000318 /*glScalef(8.0, 4.0, 2.0);*/
Brian Paul9a19ccb1999-10-26 17:08:31 +0000319 glMatrixMode(GL_MODELVIEW);
320
Brian Paulbb1119f1999-10-22 20:34:57 +0000321 /* cylinder */
322 gluQuadricNormals(q, GL_SMOOTH);
323 gluQuadricTexture(q, GL_TRUE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000324 gluCylinder(q, radius, radius, height, slices, stacks);
Brian Paulbb1119f1999-10-22 20:34:57 +0000325
326 /* end cap */
Brian Paul9a19ccb1999-10-26 17:08:31 +0000327 glMatrixMode(GL_TEXTURE);
328 glLoadIdentity();
329 glScalef(3.0, 3.0, 1.0);
330 glMatrixMode(GL_MODELVIEW);
331
Brian Paulbb1119f1999-10-22 20:34:57 +0000332 glTranslatef(0.0, 0.0, height);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000333 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000334
335 /* other end cap */
336 glTranslatef(0.0, 0.0, -height);
337 gluQuadricOrientation(q, GLU_INSIDE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000338 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000339
340 glPopMatrix();
Brian Paul9a19ccb1999-10-26 17:08:31 +0000341
342 glMatrixMode(GL_TEXTURE);
343 glLoadIdentity();
344 glMatrixMode(GL_MODELVIEW);
345
Brian Paulbb1119f1999-10-22 20:34:57 +0000346 glEndList();
347 gluDeleteQuadric(q);
348 }
349
350 /* Teapot */
351 {
352 TeapotObj = glGenLists(1);
353 glNewList(TeapotObj, GL_COMPILE);
354
355 glFrontFace(GL_CW);
356 glutSolidTeapot(40.0);
357 glFrontFace(GL_CCW);
358
359 glEndList();
360 }
361
362 /* show cylinder by default */
363 Object = CylinderObj;
364
365
366 /* lighting */
367 glEnable(GL_LIGHTING);
368 {
369 GLfloat pos[4] = { 3, 3, 3, 1 };
370 glLightfv(GL_LIGHT0, GL_AMBIENT, Black);
371 glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
372 glLightfv(GL_LIGHT0, GL_SPECULAR, White);
373 glLightfv(GL_LIGHT0, GL_POSITION, pos);
374 glEnable(GL_LIGHT0);
375 glMaterialfv(GL_FRONT, GL_AMBIENT, Black);
376 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
377 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
378 }
379
380 /* Base texture */
381 glGenTextures(1, &BaseTexture);
382 glBindTexture(GL_TEXTURE_2D, BaseTexture);
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
385 if (!LoadRGBMipmaps(BASE_TEXTURE_FILE, GL_RGB)) {
386 printf("Error: couldn't load texture image file %s\n", BASE_TEXTURE_FILE);
387 exit(1);
388 }
389
390 /* Specular texture */
391 glGenTextures(1, &SpecularTexture);
392 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
393 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
394 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
395 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
396 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
Brian Paulcb40ebd2004-05-04 23:57:12 +0000397 if (convolve) {
398 /* use convolution to blur the texture to simulate a dull finish
399 * on the object.
400 */
401 GLubyte *img;
402 GLenum format;
403 GLint w, h;
404 GLfloat filter[FILTER_SIZE][FILTER_SIZE][4];
405
406 for (h = 0; h < FILTER_SIZE; h++) {
407 for (w = 0; w < FILTER_SIZE; w++) {
408 const GLfloat k = 1.0 / (FILTER_SIZE * FILTER_SIZE);
409 filter[h][w][0] = k;
410 filter[h][w][1] = k;
411 filter[h][w][2] = k;
412 filter[h][w][3] = k;
413 }
414 }
415
416 glEnable(GL_CONVOLUTION_2D);
417 glConvolutionParameteri(GL_CONVOLUTION_2D,
418 GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
419 glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_RGBA,
420 FILTER_SIZE, FILTER_SIZE,
421 GL_RGBA, GL_FLOAT, filter);
422
423 img = LoadRGBImage(SPECULAR_TEXTURE_FILE, &w, &h, &format);
424 if (!img) {
425 printf("Error: couldn't load texture image file %s\n",
426 SPECULAR_TEXTURE_FILE);
427 exit(1);
428 }
429
Brian Paulcb40ebd2004-05-04 23:57:12 +0000430 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
431 format, GL_UNSIGNED_BYTE, img);
432 free(img);
433 }
434 else {
435 /* regular path */
436 if (!LoadRGBMipmaps(SPECULAR_TEXTURE_FILE, GL_RGB)) {
437 printf("Error: couldn't load texture image file %s\n",
438 SPECULAR_TEXTURE_FILE);
439 exit(1);
440 }
Brian Paulbb1119f1999-10-22 20:34:57 +0000441 }
442
443 /* misc */
444 glEnable(GL_CULL_FACE);
445 glEnable(GL_TEXTURE_2D);
446 glEnable(GL_DEPTH_TEST);
447 glEnable(GL_NORMALIZE);
448
Brian Paul1b94df02002-11-28 15:51:55 +0000449 glPolygonOffset( -1, -1 );
Brian Paulbb1119f1999-10-22 20:34:57 +0000450}
451
452
453int main( int argc, char *argv[] )
454{
455 glutInit( &argc, argv );
Keith Whitwell469d1b02004-01-28 10:07:48 +0000456 glutInitWindowPosition(0, 0);
Brian Paul68ad7ca2006-04-11 23:41:40 +0000457 glutInitWindowSize(WinWidth, WinHeight);
Brian Paulbb1119f1999-10-22 20:34:57 +0000458
459 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
460
461 glutCreateWindow(argv[0] );
462
463 Init(argc, argv);
464
465 glutReshapeFunc( Reshape );
466 glutKeyboardFunc( Key );
467 glutSpecialFunc( SpecialKey );
468 glutDisplayFunc( Display );
Brian Paul68ad7ca2006-04-11 23:41:40 +0000469 glutMotionFunc(MouseMotion);
470 glutMouseFunc(MouseButton);
Brian Paul197d7252006-03-30 14:25:54 +0000471 if (Animate)
472 glutIdleFunc( Idle );
Brian Paulbb1119f1999-10-22 20:34:57 +0000473
474 glutCreateMenu(ModeMenu);
475 glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE);
476 glutAddMenuEntry("Toggle Object", OBJECT);
477 glutAddMenuEntry("Toggle Animate", ANIMATE);
478 glutAddMenuEntry("Quit", QUIT);
479 glutAttachMenu(GLUT_RIGHT_BUTTON);
480
481 glutMainLoop();
482 return 0;
483}