blob: 9f9703ac6ca5d6827abf1d6673d2d91434b521c0 [file] [log] [blame]
Brian Paul1b94df02002-11-28 15:51:55 +00001/* $Id: gloss.c,v 1.7 2002/11/28 15:51:55 brianp Exp $ */
Brian Paulbb1119f1999-10-22 20:34:57 +00002
3/*
Brian Paul20cdbc01999-10-23 08:12:23 +00004 * Specular reflection demo. The specular highlight is modulated by
Brian Paulbb1119f1999-10-22 20:34:57 +00005 * a sphere-mapped texture. The result is a high-gloss surface.
6 * NOTE: you really need hardware acceleration for this.
Brian Paul20cdbc01999-10-23 08:12:23 +00007 * Also note, this technique can't be implemented with multi-texture
8 * and separate specular color interpolation because there's no way
9 * to indicate that the second texture unit (the reflection map)
10 * should modulate the specular color and not the base color.
11 * A future multi-texture extension could fix that.
Brian Paulbb1119f1999-10-22 20:34:57 +000012 *
13 * Command line options:
14 * -info print GL implementation information
15 *
16 *
17 * Brian Paul October 22, 1999 This program is in the public domain.
18 */
19
20
21#include <assert.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <math.h>
25#include <GL/glut.h>
26
pescod1ff1f62000-12-24 22:53:54 +000027#include "readtex.c" /* I know, this is a hack. */
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
38static GLuint CylinderObj = 0;
39static GLuint TeapotObj = 0;
40static GLuint Object = 0;
41static GLboolean Animate = GL_TRUE;
42
43static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
44static GLfloat DXrot = 1.0, DYrot = 2.5;
45
46static GLfloat Black[4] = { 0, 0, 0, 0 };
47static GLfloat White[4] = { 1, 1, 1, 1 };
48static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */
Brian Paule8e20ae2000-08-29 21:17:38 +000049static GLfloat Shininess = 6;
Brian Paulbb1119f1999-10-22 20:34:57 +000050
51static GLuint BaseTexture, SpecularTexture;
52static GLboolean DoSpecTexture = GL_TRUE;
53
54/* performance info */
55static GLint T0 = 0;
56static GLint Frames = 0;
57
58
59
60
61static void Idle( void )
62{
63 if (Animate) {
64 Xrot += DXrot;
65 Yrot += DYrot;
66 glutPostRedisplay();
67 }
68}
69
70
71static void Display( void )
72{
73 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
74
75 glPushMatrix();
76 glRotatef(Xrot, 1.0, 0.0, 0.0);
77 glRotatef(Yrot, 0.0, 1.0, 0.0);
78 glRotatef(Zrot, 0.0, 0.0, 1.0);
79
80 /* First pass: diffuse lighting with base texture */
81 glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
82 glMaterialfv(GL_FRONT, GL_SPECULAR, Black);
83 glEnable(GL_TEXTURE_2D);
84 glBindTexture(GL_TEXTURE_2D, BaseTexture);
Brian Paulbb1119f1999-10-22 20:34:57 +000085 glCallList(Object);
Brian Paulbb1119f1999-10-22 20:34:57 +000086
87 /* Second pass: specular lighting with reflection texture */
Brian Paul1b94df02002-11-28 15:51:55 +000088 glEnable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +000089 glBlendFunc(GL_ONE, GL_ONE); /* add */
90 glEnable(GL_BLEND);
Brian Paulbb1119f1999-10-22 20:34:57 +000091 glMaterialfv(GL_FRONT, GL_DIFFUSE, Black);
92 glMaterialfv(GL_FRONT, GL_SPECULAR, White);
93 if (DoSpecTexture) {
94 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
95 glEnable(GL_TEXTURE_GEN_S);
96 glEnable(GL_TEXTURE_GEN_T);
97 }
98 else {
99 glDisable(GL_TEXTURE_2D);
100 }
101 glCallList(Object);
102 glDisable(GL_TEXTURE_GEN_S);
103 glDisable(GL_TEXTURE_GEN_T);
104 glDisable(GL_BLEND);
Brian Paul1b94df02002-11-28 15:51:55 +0000105 glDisable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000106
107 glPopMatrix();
108
109 glutSwapBuffers();
110
111 if (Animate) {
112 GLint t = glutGet(GLUT_ELAPSED_TIME);
113 Frames++;
114 if (t - T0 >= 5000) {
115 GLfloat seconds = (t - T0) / 1000.0;
116 GLfloat fps = Frames / seconds;
117 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
118 T0 = t;
119 Frames = 0;
120 }
121 }
122}
123
124
125static void Reshape( int width, int height )
126{
127 GLfloat h = 30.0;
128 GLfloat w = h * width / height;
129 glViewport( 0, 0, width, height );
130 glMatrixMode( GL_PROJECTION );
131 glLoadIdentity();
132 glFrustum( -w, w, -h, h, 150.0, 500.0 );
133 glMatrixMode( GL_MODELVIEW );
134 glLoadIdentity();
135 glTranslatef( 0.0, 0.0, -380.0 );
136}
137
138
139static void ToggleAnimate(void)
140{
141 Animate = !Animate;
142 if (Animate) {
143 glutIdleFunc( Idle );
144 T0 = glutGet(GLUT_ELAPSED_TIME);
145 Frames = 0;
146 }
147 else {
148 glutIdleFunc( NULL );
149 }
150}
151
152
153static void ModeMenu(int entry)
154{
155 if (entry==ANIMATE) {
156 ToggleAnimate();
157 }
158 else if (entry==DO_SPEC_TEXTURE) {
159 DoSpecTexture = !DoSpecTexture;
160 }
161 else if (entry==OBJECT) {
162 if (Object == TeapotObj)
163 Object = CylinderObj;
164 else
165 Object = TeapotObj;
166 }
167 else if (entry==QUIT) {
168 exit(0);
169 }
170 glutPostRedisplay();
171}
172
173
174static void Key( unsigned char key, int x, int y )
175{
176 (void) x;
177 (void) y;
178 switch (key) {
179 case 's':
180 Shininess--;
181 if (Shininess < 0.0)
182 Shininess = 0.0;
183 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
184 printf("Shininess = %g\n", Shininess);
185 break;
186 case 'S':
187 Shininess++;
188 if (Shininess > 128.0)
189 Shininess = 128.0;
190 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
191 printf("Shininess = %g\n", Shininess);
192 break;
193 case ' ':
194 ToggleAnimate();
195 break;
196 case 27:
197 exit(0);
198 break;
199 }
200 glutPostRedisplay();
201}
202
203
204static void SpecialKey( int key, int x, int y )
205{
206 float step = 3.0;
207 (void) x;
208 (void) y;
209
210 switch (key) {
211 case GLUT_KEY_UP:
212 Xrot += step;
213 break;
214 case GLUT_KEY_DOWN:
215 Xrot -= step;
216 break;
217 case GLUT_KEY_LEFT:
218 Yrot += step;
219 break;
220 case GLUT_KEY_RIGHT:
221 Yrot -= step;
222 break;
223 }
224 glutPostRedisplay();
225}
226
227
228static void Init( int argc, char *argv[] )
229{
230 /* Cylinder object */
231 {
232 static GLfloat height = 100.0;
233 static GLfloat radius = 40.0;
Brian Paul9a19ccb1999-10-26 17:08:31 +0000234 static GLint slices = 24; /* pie slices around Z axis */
235 static GLint stacks = 10; /* subdivisions along length of cylinder */
236 static GLint rings = 4; /* rings in the end disks */
Brian Paulbb1119f1999-10-22 20:34:57 +0000237 GLUquadricObj *q = gluNewQuadric();
238 assert(q);
239 gluQuadricTexture(q, GL_TRUE);
240
241 CylinderObj = glGenLists(1);
242 glNewList(CylinderObj, GL_COMPILE);
243
244 glPushMatrix();
245 glTranslatef(0.0, 0.0, -0.5 * height);
246
Brian Paul9a19ccb1999-10-26 17:08:31 +0000247 glMatrixMode(GL_TEXTURE);
248 glLoadIdentity();
Brian Paul02e8a032000-06-27 17:04:43 +0000249 /*glScalef(8.0, 4.0, 2.0);*/
Brian Paul9a19ccb1999-10-26 17:08:31 +0000250 glMatrixMode(GL_MODELVIEW);
251
Brian Paulbb1119f1999-10-22 20:34:57 +0000252 /* cylinder */
253 gluQuadricNormals(q, GL_SMOOTH);
254 gluQuadricTexture(q, GL_TRUE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000255 gluCylinder(q, radius, radius, height, slices, stacks);
Brian Paulbb1119f1999-10-22 20:34:57 +0000256
257 /* end cap */
Brian Paul9a19ccb1999-10-26 17:08:31 +0000258 glMatrixMode(GL_TEXTURE);
259 glLoadIdentity();
260 glScalef(3.0, 3.0, 1.0);
261 glMatrixMode(GL_MODELVIEW);
262
Brian Paulbb1119f1999-10-22 20:34:57 +0000263 glTranslatef(0.0, 0.0, height);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000264 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000265
266 /* other end cap */
267 glTranslatef(0.0, 0.0, -height);
268 gluQuadricOrientation(q, GLU_INSIDE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000269 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000270
271 glPopMatrix();
Brian Paul9a19ccb1999-10-26 17:08:31 +0000272
273 glMatrixMode(GL_TEXTURE);
274 glLoadIdentity();
275 glMatrixMode(GL_MODELVIEW);
276
Brian Paulbb1119f1999-10-22 20:34:57 +0000277 glEndList();
278 gluDeleteQuadric(q);
279 }
280
281 /* Teapot */
282 {
283 TeapotObj = glGenLists(1);
284 glNewList(TeapotObj, GL_COMPILE);
285
286 glFrontFace(GL_CW);
287 glutSolidTeapot(40.0);
288 glFrontFace(GL_CCW);
289
290 glEndList();
291 }
292
293 /* show cylinder by default */
294 Object = CylinderObj;
295
296
297 /* lighting */
298 glEnable(GL_LIGHTING);
299 {
300 GLfloat pos[4] = { 3, 3, 3, 1 };
301 glLightfv(GL_LIGHT0, GL_AMBIENT, Black);
302 glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
303 glLightfv(GL_LIGHT0, GL_SPECULAR, White);
304 glLightfv(GL_LIGHT0, GL_POSITION, pos);
305 glEnable(GL_LIGHT0);
306 glMaterialfv(GL_FRONT, GL_AMBIENT, Black);
307 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
308 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
309 }
310
311 /* Base texture */
312 glGenTextures(1, &BaseTexture);
313 glBindTexture(GL_TEXTURE_2D, BaseTexture);
314 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
315 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
316 if (!LoadRGBMipmaps(BASE_TEXTURE_FILE, GL_RGB)) {
317 printf("Error: couldn't load texture image file %s\n", BASE_TEXTURE_FILE);
318 exit(1);
319 }
320
321 /* Specular texture */
322 glGenTextures(1, &SpecularTexture);
323 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Brian Paul02e8a032000-06-27 17:04:43 +0000326#if 1
Brian Paulbb1119f1999-10-22 20:34:57 +0000327 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
328 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
Brian Paul02e8a032000-06-27 17:04:43 +0000329#else
330 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
331 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
332 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
333#endif
Brian Paulbb1119f1999-10-22 20:34:57 +0000334 if (!LoadRGBMipmaps(SPECULAR_TEXTURE_FILE, GL_RGB)) {
335 printf("Error: couldn't load texture image file %s\n", SPECULAR_TEXTURE_FILE);
336 exit(1);
337 }
338
339 /* misc */
340 glEnable(GL_CULL_FACE);
341 glEnable(GL_TEXTURE_2D);
342 glEnable(GL_DEPTH_TEST);
343 glEnable(GL_NORMALIZE);
344
Brian Paul1b94df02002-11-28 15:51:55 +0000345 glPolygonOffset( -1, -1 );
346
Brian Paulbb1119f1999-10-22 20:34:57 +0000347 if (argc > 1 && strcmp(argv[1], "-info")==0) {
348 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
349 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
350 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
351 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
352 }
353}
354
355
356int main( int argc, char *argv[] )
357{
358 glutInit( &argc, argv );
359 glutInitWindowSize( 500, 500 );
360
361 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
362
363 glutCreateWindow(argv[0] );
364
365 Init(argc, argv);
366
367 glutReshapeFunc( Reshape );
368 glutKeyboardFunc( Key );
369 glutSpecialFunc( SpecialKey );
370 glutDisplayFunc( Display );
371 glutIdleFunc( Idle );
372
373 glutCreateMenu(ModeMenu);
374 glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE);
375 glutAddMenuEntry("Toggle Object", OBJECT);
376 glutAddMenuEntry("Toggle Animate", ANIMATE);
377 glutAddMenuEntry("Quit", QUIT);
378 glutAttachMenu(GLUT_RIGHT_BUTTON);
379
380 glutMainLoop();
381 return 0;
382}