blob: 845e49273fff25861ff65ee2c1f2806b9ea6611e [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>
24#include <GL/glut.h>
25
pescod1ff1f62000-12-24 22:53:54 +000026#include "readtex.c" /* I know, this is a hack. */
Brian Paulbb1119f1999-10-22 20:34:57 +000027
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 Paulcb40ebd2004-05-04 23:57:12 +000037/* for convolution */
38#define FILTER_SIZE 7
39
Brian Paulbb1119f1999-10-22 20:34:57 +000040static GLuint CylinderObj = 0;
41static GLuint TeapotObj = 0;
42static GLuint Object = 0;
43static GLboolean Animate = GL_TRUE;
44
45static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
46static GLfloat DXrot = 1.0, DYrot = 2.5;
47
48static GLfloat Black[4] = { 0, 0, 0, 0 };
49static GLfloat White[4] = { 1, 1, 1, 1 };
50static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */
Brian Paule8e20ae2000-08-29 21:17:38 +000051static GLfloat Shininess = 6;
Brian Paulbb1119f1999-10-22 20:34:57 +000052
53static GLuint BaseTexture, SpecularTexture;
54static GLboolean DoSpecTexture = GL_TRUE;
55
56/* performance info */
57static GLint T0 = 0;
58static GLint Frames = 0;
59
60
61
62
63static void Idle( void )
64{
65 if (Animate) {
66 Xrot += DXrot;
67 Yrot += DYrot;
68 glutPostRedisplay();
69 }
70}
71
72
73static 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 Paulbb1119f1999-10-22 20:34:57 +000087 glCallList(Object);
Brian Paulbb1119f1999-10-22 20:34:57 +000088
89 /* Second pass: specular lighting with reflection texture */
Brian Paul1b94df02002-11-28 15:51:55 +000090 glEnable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +000091 glBlendFunc(GL_ONE, GL_ONE); /* add */
92 glEnable(GL_BLEND);
Brian Paulbb1119f1999-10-22 20:34:57 +000093 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 Paul1b94df02002-11-28 15:51:55 +0000107 glDisable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000108
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
127static 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
141static 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
155static 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
176static 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
206static 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
230static void Init( int argc, char *argv[] )
231{
Brian Paulcb40ebd2004-05-04 23:57:12 +0000232 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 Paulbb1119f1999-10-22 20:34:57 +0000248 /* Cylinder object */
249 {
250 static GLfloat height = 100.0;
251 static GLfloat radius = 40.0;
Brian Paul9a19ccb1999-10-26 17:08:31 +0000252 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 Paulbb1119f1999-10-22 20:34:57 +0000255 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 Paul9a19ccb1999-10-26 17:08:31 +0000265 glMatrixMode(GL_TEXTURE);
266 glLoadIdentity();
Brian Paul02e8a032000-06-27 17:04:43 +0000267 /*glScalef(8.0, 4.0, 2.0);*/
Brian Paul9a19ccb1999-10-26 17:08:31 +0000268 glMatrixMode(GL_MODELVIEW);
269
Brian Paulbb1119f1999-10-22 20:34:57 +0000270 /* cylinder */
271 gluQuadricNormals(q, GL_SMOOTH);
272 gluQuadricTexture(q, GL_TRUE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000273 gluCylinder(q, radius, radius, height, slices, stacks);
Brian Paulbb1119f1999-10-22 20:34:57 +0000274
275 /* end cap */
Brian Paul9a19ccb1999-10-26 17:08:31 +0000276 glMatrixMode(GL_TEXTURE);
277 glLoadIdentity();
278 glScalef(3.0, 3.0, 1.0);
279 glMatrixMode(GL_MODELVIEW);
280
Brian Paulbb1119f1999-10-22 20:34:57 +0000281 glTranslatef(0.0, 0.0, height);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000282 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000283
284 /* other end cap */
285 glTranslatef(0.0, 0.0, -height);
286 gluQuadricOrientation(q, GLU_INSIDE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000287 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000288
289 glPopMatrix();
Brian Paul9a19ccb1999-10-26 17:08:31 +0000290
291 glMatrixMode(GL_TEXTURE);
292 glLoadIdentity();
293 glMatrixMode(GL_MODELVIEW);
294
Brian Paulbb1119f1999-10-22 20:34:57 +0000295 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 Paulcb40ebd2004-05-04 23:57:12 +0000346 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 Paulbb1119f1999-10-22 20:34:57 +0000391 }
392
393 /* misc */
394 glEnable(GL_CULL_FACE);
395 glEnable(GL_TEXTURE_2D);
396 glEnable(GL_DEPTH_TEST);
397 glEnable(GL_NORMALIZE);
398
Brian Paul1b94df02002-11-28 15:51:55 +0000399 glPolygonOffset( -1, -1 );
Brian Paulbb1119f1999-10-22 20:34:57 +0000400}
401
402
403int main( int argc, char *argv[] )
404{
405 glutInit( &argc, argv );
Keith Whitwell469d1b02004-01-28 10:07:48 +0000406 glutInitWindowPosition(0, 0);
Brian Paulbb1119f1999-10-22 20:34:57 +0000407 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}