blob: b2126e32678416c86d239151316f564120d48d18 [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>
José Fonseca9aa73cf2009-02-01 12:00:07 +000025#include <GL/glew.h>
Brian Paulbb1119f1999-10-22 20:34:57 +000026#include <GL/glut.h>
27
Brian Paul92eddb02005-01-09 17:37:50 +000028#include "readtex.h"
Brian Paul4e061782006-06-26 23:00:15 +000029#include "trackball.h"
30
Brian Paulbb1119f1999-10-22 20:34:57 +000031
32#define SPECULAR_TEXTURE_FILE "../images/reflect.rgb"
33#define BASE_TEXTURE_FILE "../images/tile.rgb"
34
35/* Menu items */
36#define DO_SPEC_TEXTURE 1
37#define OBJECT 2
38#define ANIMATE 3
39#define QUIT 100
40
Brian Paulcb40ebd2004-05-04 23:57:12 +000041/* for convolution */
42#define FILTER_SIZE 7
43
Brian Paul68ad7ca2006-04-11 23:41:40 +000044static GLint WinWidth = 500, WinHeight = 500;
Brian Paulbb1119f1999-10-22 20:34:57 +000045static GLuint CylinderObj = 0;
46static GLuint TeapotObj = 0;
47static GLuint Object = 0;
48static GLboolean Animate = GL_TRUE;
49
Brian Paul4e061782006-06-26 23:00:15 +000050static float CurQuat[4] = { 0, 0, 0, 1 };
Brian Paulbb1119f1999-10-22 20:34:57 +000051
52static GLfloat Black[4] = { 0, 0, 0, 0 };
53static GLfloat White[4] = { 1, 1, 1, 1 };
54static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */
Brian Paule8e20ae2000-08-29 21:17:38 +000055static GLfloat Shininess = 6;
Brian Paulbb1119f1999-10-22 20:34:57 +000056
57static GLuint BaseTexture, SpecularTexture;
58static GLboolean DoSpecTexture = GL_TRUE;
59
Brian Paul68ad7ca2006-04-11 23:41:40 +000060static GLboolean ButtonDown = GL_FALSE;
61static GLint ButtonX, ButtonY;
Brian Paul68ad7ca2006-04-11 23:41:40 +000062
63
Brian Paulbb1119f1999-10-22 20:34:57 +000064/* performance info */
65static GLint T0 = 0;
66static GLint Frames = 0;
67
68
Brian Paulbb1119f1999-10-22 20:34:57 +000069static void Idle( void )
70{
Brian Paul4e061782006-06-26 23:00:15 +000071 static const float yAxis[3] = {0, 1, 0};
Brian Paul92eddb02005-01-09 17:37:50 +000072 static double t0 = -1.;
Brian Paul4e061782006-06-26 23:00:15 +000073 float quat[4];
Brian Paul92eddb02005-01-09 17:37:50 +000074 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
75 if (t0 < 0.0)
76 t0 = t;
77 dt = t - t0;
78 t0 = t;
79
Brian Paul4e061782006-06-26 23:00:15 +000080 axis_to_quat(yAxis, 2.0 * dt, quat);
81 add_quats(quat, CurQuat, CurQuat);
82
83 glutPostRedisplay();
Brian Paulbb1119f1999-10-22 20:34:57 +000084}
85
86
87static void Display( void )
88{
Brian Paul4e061782006-06-26 23:00:15 +000089 GLfloat rot[4][4];
90
Brian Paulbb1119f1999-10-22 20:34:57 +000091 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
92
93 glPushMatrix();
Brian Paul4e061782006-06-26 23:00:15 +000094 build_rotmatrix(rot, CurQuat);
95 glMultMatrixf(&rot[0][0]);
Brian Paulbb1119f1999-10-22 20:34:57 +000096
97 /* First pass: diffuse lighting with base texture */
98 glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
99 glMaterialfv(GL_FRONT, GL_SPECULAR, Black);
100 glEnable(GL_TEXTURE_2D);
101 glBindTexture(GL_TEXTURE_2D, BaseTexture);
Brian Paulbb1119f1999-10-22 20:34:57 +0000102 glCallList(Object);
Brian Paulbb1119f1999-10-22 20:34:57 +0000103
104 /* Second pass: specular lighting with reflection texture */
Brian Paul1b94df02002-11-28 15:51:55 +0000105 glEnable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000106 glBlendFunc(GL_ONE, GL_ONE); /* add */
107 glEnable(GL_BLEND);
Brian Paulbb1119f1999-10-22 20:34:57 +0000108 glMaterialfv(GL_FRONT, GL_DIFFUSE, Black);
109 glMaterialfv(GL_FRONT, GL_SPECULAR, White);
110 if (DoSpecTexture) {
111 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
112 glEnable(GL_TEXTURE_GEN_S);
113 glEnable(GL_TEXTURE_GEN_T);
114 }
115 else {
116 glDisable(GL_TEXTURE_2D);
117 }
118 glCallList(Object);
119 glDisable(GL_TEXTURE_GEN_S);
120 glDisable(GL_TEXTURE_GEN_T);
121 glDisable(GL_BLEND);
Brian Paul1b94df02002-11-28 15:51:55 +0000122 glDisable(GL_POLYGON_OFFSET_FILL);
Brian Paulbb1119f1999-10-22 20:34:57 +0000123
124 glPopMatrix();
125
126 glutSwapBuffers();
127
128 if (Animate) {
129 GLint t = glutGet(GLUT_ELAPSED_TIME);
130 Frames++;
131 if (t - T0 >= 5000) {
132 GLfloat seconds = (t - T0) / 1000.0;
133 GLfloat fps = Frames / seconds;
134 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
135 T0 = t;
136 Frames = 0;
137 }
138 }
139}
140
141
142static void Reshape( int width, int height )
143{
144 GLfloat h = 30.0;
145 GLfloat w = h * width / height;
Brian Paul68ad7ca2006-04-11 23:41:40 +0000146 WinWidth = width;
147 WinHeight = height;
Brian Paulbb1119f1999-10-22 20:34:57 +0000148 glViewport( 0, 0, width, height );
149 glMatrixMode( GL_PROJECTION );
150 glLoadIdentity();
151 glFrustum( -w, w, -h, h, 150.0, 500.0 );
152 glMatrixMode( GL_MODELVIEW );
153 glLoadIdentity();
154 glTranslatef( 0.0, 0.0, -380.0 );
155}
156
157
158static void ToggleAnimate(void)
159{
160 Animate = !Animate;
161 if (Animate) {
162 glutIdleFunc( Idle );
163 T0 = glutGet(GLUT_ELAPSED_TIME);
164 Frames = 0;
165 }
166 else {
167 glutIdleFunc( NULL );
168 }
169}
170
171
172static void ModeMenu(int entry)
173{
174 if (entry==ANIMATE) {
175 ToggleAnimate();
176 }
177 else if (entry==DO_SPEC_TEXTURE) {
178 DoSpecTexture = !DoSpecTexture;
179 }
180 else if (entry==OBJECT) {
181 if (Object == TeapotObj)
182 Object = CylinderObj;
183 else
184 Object = TeapotObj;
185 }
186 else if (entry==QUIT) {
187 exit(0);
188 }
189 glutPostRedisplay();
190}
191
192
193static void Key( unsigned char key, int x, int y )
194{
195 (void) x;
196 (void) y;
197 switch (key) {
198 case 's':
199 Shininess--;
200 if (Shininess < 0.0)
201 Shininess = 0.0;
202 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
203 printf("Shininess = %g\n", Shininess);
204 break;
205 case 'S':
206 Shininess++;
207 if (Shininess > 128.0)
208 Shininess = 128.0;
209 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
210 printf("Shininess = %g\n", Shininess);
211 break;
Brian Paul68ad7ca2006-04-11 23:41:40 +0000212 case 'a':
Brian Paulbb1119f1999-10-22 20:34:57 +0000213 case ' ':
214 ToggleAnimate();
215 break;
216 case 27:
217 exit(0);
218 break;
219 }
220 glutPostRedisplay();
221}
222
223
Brian Paul68ad7ca2006-04-11 23:41:40 +0000224static void
225MouseMotion(int x, int y)
226{
Brian Paul68ad7ca2006-04-11 23:41:40 +0000227 if (ButtonDown) {
Brian Paul4e061782006-06-26 23:00:15 +0000228 float x0 = (2.0 * ButtonX - WinWidth) / WinWidth;
229 float y0 = (WinHeight - 2.0 * ButtonY) / WinHeight;
230 float x1 = (2.0 * x - WinWidth) / WinWidth;
231 float y1 = (WinHeight - 2.0 * y) / WinHeight;
232 float q[4];
233
234 trackball(q, x0, y0, x1, y1);
235 ButtonX = x;
236 ButtonY = y;
237 add_quats(q, CurQuat, CurQuat);
238
Brian Paul68ad7ca2006-04-11 23:41:40 +0000239 glutPostRedisplay();
240 }
241}
242
243
244static void
245MouseButton(int button, int state, int x, int y)
246{
247 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
248 ButtonDown = GL_TRUE;
249 ButtonX = x;
250 ButtonY = y;
Brian Paul68ad7ca2006-04-11 23:41:40 +0000251 }
252 else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
253 ButtonDown = GL_FALSE;
254 }
255}
256
257
Brian Paulbb1119f1999-10-22 20:34:57 +0000258static void Init( int argc, char *argv[] )
259{
Brian Paulcb40ebd2004-05-04 23:57:12 +0000260 GLboolean convolve = GL_FALSE;
Brian Paul197d7252006-03-30 14:25:54 +0000261 GLboolean fullscreen = GL_FALSE;
Brian Paulcb40ebd2004-05-04 23:57:12 +0000262 int i;
263
264 for (i = 1; i < argc; i++) {
265 if (strcmp(argv[i], "-info")==0) {
266 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
267 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
268 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
269 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
270 }
271 else if (strcmp(argv[i], "-c")==0) {
272 convolve = GL_TRUE;
273 }
Brian Paul197d7252006-03-30 14:25:54 +0000274 else if (strcmp(argv[i], "-f")==0) {
275 fullscreen = GL_TRUE;
276 }
Brian Paulcb40ebd2004-05-04 23:57:12 +0000277 }
278
Brian Paul197d7252006-03-30 14:25:54 +0000279 if (fullscreen)
280 glutFullScreen();
Brian Paulcb40ebd2004-05-04 23:57:12 +0000281
Brian Paulbb1119f1999-10-22 20:34:57 +0000282 /* Cylinder object */
283 {
284 static GLfloat height = 100.0;
285 static GLfloat radius = 40.0;
Brian Paul9a19ccb1999-10-26 17:08:31 +0000286 static GLint slices = 24; /* pie slices around Z axis */
287 static GLint stacks = 10; /* subdivisions along length of cylinder */
288 static GLint rings = 4; /* rings in the end disks */
Brian Paulbb1119f1999-10-22 20:34:57 +0000289 GLUquadricObj *q = gluNewQuadric();
290 assert(q);
291 gluQuadricTexture(q, GL_TRUE);
292
293 CylinderObj = glGenLists(1);
294 glNewList(CylinderObj, GL_COMPILE);
295
296 glPushMatrix();
297 glTranslatef(0.0, 0.0, -0.5 * height);
298
Brian Paul9a19ccb1999-10-26 17:08:31 +0000299 glMatrixMode(GL_TEXTURE);
300 glLoadIdentity();
Brian Paul02e8a032000-06-27 17:04:43 +0000301 /*glScalef(8.0, 4.0, 2.0);*/
Brian Paul9a19ccb1999-10-26 17:08:31 +0000302 glMatrixMode(GL_MODELVIEW);
303
Brian Paulbb1119f1999-10-22 20:34:57 +0000304 /* cylinder */
305 gluQuadricNormals(q, GL_SMOOTH);
306 gluQuadricTexture(q, GL_TRUE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000307 gluCylinder(q, radius, radius, height, slices, stacks);
Brian Paulbb1119f1999-10-22 20:34:57 +0000308
309 /* end cap */
Brian Paul9a19ccb1999-10-26 17:08:31 +0000310 glMatrixMode(GL_TEXTURE);
311 glLoadIdentity();
312 glScalef(3.0, 3.0, 1.0);
313 glMatrixMode(GL_MODELVIEW);
314
Brian Paulbb1119f1999-10-22 20:34:57 +0000315 glTranslatef(0.0, 0.0, height);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000316 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000317
318 /* other end cap */
319 glTranslatef(0.0, 0.0, -height);
320 gluQuadricOrientation(q, GLU_INSIDE);
Brian Paul9a19ccb1999-10-26 17:08:31 +0000321 gluDisk(q, 0.0, radius, slices, rings);
Brian Paulbb1119f1999-10-22 20:34:57 +0000322
323 glPopMatrix();
Brian Paul9a19ccb1999-10-26 17:08:31 +0000324
325 glMatrixMode(GL_TEXTURE);
326 glLoadIdentity();
327 glMatrixMode(GL_MODELVIEW);
328
Brian Paulbb1119f1999-10-22 20:34:57 +0000329 glEndList();
330 gluDeleteQuadric(q);
331 }
332
333 /* Teapot */
334 {
335 TeapotObj = glGenLists(1);
336 glNewList(TeapotObj, GL_COMPILE);
337
338 glFrontFace(GL_CW);
339 glutSolidTeapot(40.0);
340 glFrontFace(GL_CCW);
341
342 glEndList();
343 }
344
345 /* show cylinder by default */
346 Object = CylinderObj;
347
348
349 /* lighting */
350 glEnable(GL_LIGHTING);
351 {
352 GLfloat pos[4] = { 3, 3, 3, 1 };
353 glLightfv(GL_LIGHT0, GL_AMBIENT, Black);
354 glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
355 glLightfv(GL_LIGHT0, GL_SPECULAR, White);
356 glLightfv(GL_LIGHT0, GL_POSITION, pos);
357 glEnable(GL_LIGHT0);
358 glMaterialfv(GL_FRONT, GL_AMBIENT, Black);
359 glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
360 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
361 }
362
363 /* Base texture */
364 glGenTextures(1, &BaseTexture);
365 glBindTexture(GL_TEXTURE_2D, BaseTexture);
366 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
367 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
368 if (!LoadRGBMipmaps(BASE_TEXTURE_FILE, GL_RGB)) {
369 printf("Error: couldn't load texture image file %s\n", BASE_TEXTURE_FILE);
370 exit(1);
371 }
372
373 /* Specular texture */
374 glGenTextures(1, &SpecularTexture);
375 glBindTexture(GL_TEXTURE_2D, SpecularTexture);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
377 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
378 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
379 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
Brian Paulcb40ebd2004-05-04 23:57:12 +0000380 if (convolve) {
381 /* use convolution to blur the texture to simulate a dull finish
382 * on the object.
383 */
384 GLubyte *img;
385 GLenum format;
386 GLint w, h;
387 GLfloat filter[FILTER_SIZE][FILTER_SIZE][4];
388
389 for (h = 0; h < FILTER_SIZE; h++) {
390 for (w = 0; w < FILTER_SIZE; w++) {
391 const GLfloat k = 1.0 / (FILTER_SIZE * FILTER_SIZE);
392 filter[h][w][0] = k;
393 filter[h][w][1] = k;
394 filter[h][w][2] = k;
395 filter[h][w][3] = k;
396 }
397 }
398
399 glEnable(GL_CONVOLUTION_2D);
400 glConvolutionParameteri(GL_CONVOLUTION_2D,
401 GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
402 glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_RGBA,
403 FILTER_SIZE, FILTER_SIZE,
404 GL_RGBA, GL_FLOAT, filter);
405
406 img = LoadRGBImage(SPECULAR_TEXTURE_FILE, &w, &h, &format);
407 if (!img) {
408 printf("Error: couldn't load texture image file %s\n",
409 SPECULAR_TEXTURE_FILE);
410 exit(1);
411 }
412
Brian Paulcb40ebd2004-05-04 23:57:12 +0000413 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
414 format, GL_UNSIGNED_BYTE, img);
415 free(img);
416 }
417 else {
418 /* regular path */
419 if (!LoadRGBMipmaps(SPECULAR_TEXTURE_FILE, GL_RGB)) {
420 printf("Error: couldn't load texture image file %s\n",
421 SPECULAR_TEXTURE_FILE);
422 exit(1);
423 }
Brian Paulbb1119f1999-10-22 20:34:57 +0000424 }
425
426 /* misc */
427 glEnable(GL_CULL_FACE);
428 glEnable(GL_TEXTURE_2D);
429 glEnable(GL_DEPTH_TEST);
430 glEnable(GL_NORMALIZE);
431
Brian Paul1b94df02002-11-28 15:51:55 +0000432 glPolygonOffset( -1, -1 );
Brian Paulbb1119f1999-10-22 20:34:57 +0000433}
434
435
436int main( int argc, char *argv[] )
437{
438 glutInit( &argc, argv );
Brian Paul68ad7ca2006-04-11 23:41:40 +0000439 glutInitWindowSize(WinWidth, WinHeight);
Brian Paulbb1119f1999-10-22 20:34:57 +0000440 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
Brian Paulbb1119f1999-10-22 20:34:57 +0000441 glutCreateWindow(argv[0] );
José Fonseca9aa73cf2009-02-01 12:00:07 +0000442 glewInit();
Brian Paulbb1119f1999-10-22 20:34:57 +0000443 glutReshapeFunc( Reshape );
444 glutKeyboardFunc( Key );
Brian Paulbb1119f1999-10-22 20:34:57 +0000445 glutDisplayFunc( Display );
Brian Paul68ad7ca2006-04-11 23:41:40 +0000446 glutMotionFunc(MouseMotion);
447 glutMouseFunc(MouseButton);
Brian Paul197d7252006-03-30 14:25:54 +0000448 if (Animate)
449 glutIdleFunc( Idle );
Brian Paulbb1119f1999-10-22 20:34:57 +0000450
451 glutCreateMenu(ModeMenu);
452 glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE);
453 glutAddMenuEntry("Toggle Object", OBJECT);
454 glutAddMenuEntry("Toggle Animate", ANIMATE);
455 glutAddMenuEntry("Quit", QUIT);
456 glutAttachMenu(GLUT_RIGHT_BUTTON);
457
Brian Paul4e061782006-06-26 23:00:15 +0000458 Init(argc, argv);
459
Brian Paulbb1119f1999-10-22 20:34:57 +0000460 glutMainLoop();
461 return 0;
462}