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