blob: 80f9e4672ea7be4598ff244f7e373df989cc7fa2 [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;
Brian Paul197d7252006-03-30 14:25:54 +0000239 GLboolean fullscreen = GL_FALSE;
Brian Paulcb40ebd2004-05-04 23:57:12 +0000240 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 }
Brian Paul197d7252006-03-30 14:25:54 +0000252 else if (strcmp(argv[i], "-f")==0) {
253 fullscreen = GL_TRUE;
254 }
Brian Paulcb40ebd2004-05-04 23:57:12 +0000255 }
256
Brian Paul197d7252006-03-30 14:25:54 +0000257 if (fullscreen)
258 glutFullScreen();
Brian Paulcb40ebd2004-05-04 23:57:12 +0000259
Brian Paulbb1119f1999-10-22 20:34:57 +0000260 /* Cylinder object */
261 {
262 static GLfloat height = 100.0;
263 static GLfloat radius = 40.0;
Brian Paul9a19ccb1999-10-26 17:08:31 +0000264 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 */
Brian Paulbb1119f1999-10-22 20:34:57 +0000267 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
Brian Paul9a19ccb1999-10-26 17:08:31 +0000277 glMatrixMode(GL_TEXTURE);
278 glLoadIdentity();
Brian Paul02e8a032000-06-27 17:04:43 +0000279 /*glScalef(8.0, 4.0, 2.0);*/
Brian Paul9a19ccb1999-10-26 17:08:31 +0000280 glMatrixMode(GL_MODELVIEW);
281
Brian Paulbb1119f1999-10-22 20:34:57 +0000282 /* cylinder */
283 gluQuadricNormals(q, GL_SMOOTH);
284 gluQuadricTexture(q, GL_TRUE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000285 gluCylinder(q, radius, radius, height, slices, stacks);
Brian Paulbb1119f1999-10-22 20:34:57 +0000286
287 /* end cap */
Brian Paul9a19ccb1999-10-26 17:08:31 +0000288 glMatrixMode(GL_TEXTURE);
289 glLoadIdentity();
290 glScalef(3.0, 3.0, 1.0);
291 glMatrixMode(GL_MODELVIEW);
292
Brian Paulbb1119f1999-10-22 20:34:57 +0000293 glTranslatef(0.0, 0.0, height);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000294 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000295
296 /* other end cap */
297 glTranslatef(0.0, 0.0, -height);
298 gluQuadricOrientation(q, GLU_INSIDE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000299 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000300
301 glPopMatrix();
Brian Paul9a19ccb1999-10-26 17:08:31 +0000302
303 glMatrixMode(GL_TEXTURE);
304 glLoadIdentity();
305 glMatrixMode(GL_MODELVIEW);
306
Brian Paulbb1119f1999-10-22 20:34:57 +0000307 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);
Brian Paulcb40ebd2004-05-04 23:57:12 +0000358 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
Brian Paulcb40ebd2004-05-04 23:57:12 +0000391 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 }
Brian Paulbb1119f1999-10-22 20:34:57 +0000402 }
403
404 /* misc */
405 glEnable(GL_CULL_FACE);
406 glEnable(GL_TEXTURE_2D);
407 glEnable(GL_DEPTH_TEST);
408 glEnable(GL_NORMALIZE);
409
Brian Paul1b94df02002-11-28 15:51:55 +0000410 glPolygonOffset( -1, -1 );
Brian Paulbb1119f1999-10-22 20:34:57 +0000411}
412
413
414int main( int argc, char *argv[] )
415{
416 glutInit( &argc, argv );
Keith Whitwell469d1b02004-01-28 10:07:48 +0000417 glutInitWindowPosition(0, 0);
Brian Paulbb1119f1999-10-22 20:34:57 +0000418 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 );
Brian Paul197d7252006-03-30 14:25:54 +0000430 if (Animate)
431 glutIdleFunc( Idle );
Brian Paulbb1119f1999-10-22 20:34:57 +0000432
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}