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