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