blob: 4e7adc8d48fa94cc63dd4292d5e58fb6d95889e1 [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>
Brian Paul92eddb02005-01-09 17:37:50 +000024#include <string.h>
Brian Paulbb1119f1999-10-22 20:34:57 +000025#include <GL/glut.h>
26
Brian Paul92eddb02005-01-09 17:37:50 +000027#include "readtex.h"
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
Brian Paulcb40ebd2004-05-04 23:57:12 +000038/* for convolution */
39#define FILTER_SIZE 7
40
Brian Paulbb1119f1999-10-22 20:34:57 +000041static GLuint CylinderObj = 0;
42static GLuint TeapotObj = 0;
43static GLuint Object = 0;
44static GLboolean Animate = GL_TRUE;
45
46static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
Brian Paul92eddb02005-01-09 17:37:50 +000047static GLfloat DXrot = 20.0, DYrot = 50.;
Brian Paulbb1119f1999-10-22 20:34:57 +000048
49static GLfloat Black[4] = { 0, 0, 0, 0 };
50static GLfloat White[4] = { 1, 1, 1, 1 };
51static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */
Brian Paule8e20ae2000-08-29 21:17:38 +000052static GLfloat Shininess = 6;
Brian Paulbb1119f1999-10-22 20:34:57 +000053
54static GLuint BaseTexture, SpecularTexture;
55static GLboolean DoSpecTexture = GL_TRUE;
56
57/* performance info */
58static GLint T0 = 0;
59static GLint Frames = 0;
60
61
Brian Paulbb1119f1999-10-22 20:34:57 +000062static void Idle( void )
63{
Brian Paul92eddb02005-01-09 17:37:50 +000064 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
Brian Paulbb1119f1999-10-22 20:34:57 +000071 if (Animate) {
Brian Paul92eddb02005-01-09 17:37:50 +000072 Xrot += DXrot*dt;
73 Yrot += DYrot*dt;
Brian Paulbb1119f1999-10-22 20:34:57 +000074 glutPostRedisplay();
75 }
76}
77
78
79static 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);
Brian Paulbb1119f1999-10-22 20:34:57 +000093 glCallList(Object);
Brian Paulbb1119f1999-10-22 20:34:57 +000094
95 /* Second pass: specular lighting with reflection texture */
Brian Paul1b94df02002-11-28 15:51:55 +000096 glEnable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +000097 glBlendFunc(GL_ONE, GL_ONE); /* add */
98 glEnable(GL_BLEND);
Brian Paulbb1119f1999-10-22 20:34:57 +000099 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);
Brian Paul1b94df02002-11-28 15:51:55 +0000113 glDisable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000114
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
133static 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
147static 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
161static 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
182static 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
212static 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
236static void Init( int argc, char *argv[] )
237{
Brian Paulcb40ebd2004-05-04 23:57:12 +0000238 GLboolean convolve = GL_FALSE;
239 int i;
240
241 for (i = 1; i < argc; i++) {
242 if (strcmp(argv[i], "-info")==0) {
243 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
244 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
245 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
246 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
247 }
248 else if (strcmp(argv[i], "-c")==0) {
249 convolve = GL_TRUE;
250 }
251 }
252
253
Brian Paulbb1119f1999-10-22 20:34:57 +0000254 /* Cylinder object */
255 {
256 static GLfloat height = 100.0;
257 static GLfloat radius = 40.0;
Brian Paul9a19ccb1999-10-26 17:08:31 +0000258 static GLint slices = 24; /* pie slices around Z axis */
259 static GLint stacks = 10; /* subdivisions along length of cylinder */
260 static GLint rings = 4; /* rings in the end disks */
Brian Paulbb1119f1999-10-22 20:34:57 +0000261 GLUquadricObj *q = gluNewQuadric();
262 assert(q);
263 gluQuadricTexture(q, GL_TRUE);
264
265 CylinderObj = glGenLists(1);
266 glNewList(CylinderObj, GL_COMPILE);
267
268 glPushMatrix();
269 glTranslatef(0.0, 0.0, -0.5 * height);
270
Brian Paul9a19ccb1999-10-26 17:08:31 +0000271 glMatrixMode(GL_TEXTURE);
272 glLoadIdentity();
Brian Paul02e8a032000-06-27 17:04:43 +0000273 /*glScalef(8.0, 4.0, 2.0);*/
Brian Paul9a19ccb1999-10-26 17:08:31 +0000274 glMatrixMode(GL_MODELVIEW);
275
Brian Paulbb1119f1999-10-22 20:34:57 +0000276 /* cylinder */
277 gluQuadricNormals(q, GL_SMOOTH);
278 gluQuadricTexture(q, GL_TRUE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000279 gluCylinder(q, radius, radius, height, slices, stacks);
Brian Paulbb1119f1999-10-22 20:34:57 +0000280
281 /* end cap */
Brian Paul9a19ccb1999-10-26 17:08:31 +0000282 glMatrixMode(GL_TEXTURE);
283 glLoadIdentity();
284 glScalef(3.0, 3.0, 1.0);
285 glMatrixMode(GL_MODELVIEW);
286
Brian Paulbb1119f1999-10-22 20:34:57 +0000287 glTranslatef(0.0, 0.0, height);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000288 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000289
290 /* other end cap */
291 glTranslatef(0.0, 0.0, -height);
292 gluQuadricOrientation(q, GLU_INSIDE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000293 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000294
295 glPopMatrix();
Brian Paul9a19ccb1999-10-26 17:08:31 +0000296
297 glMatrixMode(GL_TEXTURE);
298 glLoadIdentity();
299 glMatrixMode(GL_MODELVIEW);
300
Brian Paulbb1119f1999-10-22 20:34:57 +0000301 glEndList();
302 gluDeleteQuadric(q);
303 }
304
305 /* Teapot */
306 {
307 TeapotObj = glGenLists(1);
308 glNewList(TeapotObj, GL_COMPILE);
309
310 glFrontFace(GL_CW);
311 glutSolidTeapot(40.0);
312 glFrontFace(GL_CCW);
313
314 glEndList();
315 }
316
317 /* show cylinder by default */
318 Object = CylinderObj;
319
320
321 /* lighting */
322 glEnable(GL_LIGHTING);
323 {
324 GLfloat pos[4] = { 3, 3, 3, 1 };
325 glLightfv(GL_LIGHT0, GL_AMBIENT, Black);
326 glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
327 glLightfv(GL_LIGHT0, GL_SPECULAR, White);
328 glLightfv(GL_LIGHT0, GL_POSITION, pos);
329 glEnable(GL_LIGHT0);
330 glMaterialfv(GL_FRONT, GL_AMBIENT, Black);
331 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
332 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
333 }
334
335 /* Base texture */
336 glGenTextures(1, &BaseTexture);
337 glBindTexture(GL_TEXTURE_2D, BaseTexture);
338 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
339 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
340 if (!LoadRGBMipmaps(BASE_TEXTURE_FILE, GL_RGB)) {
341 printf("Error: couldn't load texture image file %s\n", BASE_TEXTURE_FILE);
342 exit(1);
343 }
344
345 /* Specular texture */
346 glGenTextures(1, &SpecularTexture);
347 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
350 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
351 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
Brian Paulcb40ebd2004-05-04 23:57:12 +0000352 if (convolve) {
353 /* use convolution to blur the texture to simulate a dull finish
354 * on the object.
355 */
356 GLubyte *img;
357 GLenum format;
358 GLint w, h;
359 GLfloat filter[FILTER_SIZE][FILTER_SIZE][4];
360
361 for (h = 0; h < FILTER_SIZE; h++) {
362 for (w = 0; w < FILTER_SIZE; w++) {
363 const GLfloat k = 1.0 / (FILTER_SIZE * FILTER_SIZE);
364 filter[h][w][0] = k;
365 filter[h][w][1] = k;
366 filter[h][w][2] = k;
367 filter[h][w][3] = k;
368 }
369 }
370
371 glEnable(GL_CONVOLUTION_2D);
372 glConvolutionParameteri(GL_CONVOLUTION_2D,
373 GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
374 glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_RGBA,
375 FILTER_SIZE, FILTER_SIZE,
376 GL_RGBA, GL_FLOAT, filter);
377
378 img = LoadRGBImage(SPECULAR_TEXTURE_FILE, &w, &h, &format);
379 if (!img) {
380 printf("Error: couldn't load texture image file %s\n",
381 SPECULAR_TEXTURE_FILE);
382 exit(1);
383 }
384
Brian Paulcb40ebd2004-05-04 23:57:12 +0000385 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
386 format, GL_UNSIGNED_BYTE, img);
387 free(img);
388 }
389 else {
390 /* regular path */
391 if (!LoadRGBMipmaps(SPECULAR_TEXTURE_FILE, GL_RGB)) {
392 printf("Error: couldn't load texture image file %s\n",
393 SPECULAR_TEXTURE_FILE);
394 exit(1);
395 }
Brian Paulbb1119f1999-10-22 20:34:57 +0000396 }
397
398 /* misc */
399 glEnable(GL_CULL_FACE);
400 glEnable(GL_TEXTURE_2D);
401 glEnable(GL_DEPTH_TEST);
402 glEnable(GL_NORMALIZE);
403
Brian Paul1b94df02002-11-28 15:51:55 +0000404 glPolygonOffset( -1, -1 );
Brian Paulbb1119f1999-10-22 20:34:57 +0000405}
406
407
408int main( int argc, char *argv[] )
409{
410 glutInit( &argc, argv );
Keith Whitwell469d1b02004-01-28 10:07:48 +0000411 glutInitWindowPosition(0, 0);
Brian Paulbb1119f1999-10-22 20:34:57 +0000412 glutInitWindowSize( 500, 500 );
413
414 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
415
416 glutCreateWindow(argv[0] );
417
418 Init(argc, argv);
419
420 glutReshapeFunc( Reshape );
421 glutKeyboardFunc( Key );
422 glutSpecialFunc( SpecialKey );
423 glutDisplayFunc( Display );
424 glutIdleFunc( Idle );
425
426 glutCreateMenu(ModeMenu);
427 glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE);
428 glutAddMenuEntry("Toggle Object", OBJECT);
429 glutAddMenuEntry("Toggle Animate", ANIMATE);
430 glutAddMenuEntry("Quit", QUIT);
431 glutAttachMenu(GLUT_RIGHT_BUTTON);
432
433 glutMainLoop();
434 return 0;
435}