jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Textured cylinder demo: lighting, texturing, reflection mapping. |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 4 | * |
| 5 | * Command line options: |
| 6 | * -info print GL implementation information |
| 7 | * |
| 8 | * |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 9 | * Brian Paul May 1997 This program is in the public domain. |
| 10 | */ |
| 11 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <math.h> |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 15 | #include <string.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 16 | #include <GL/glut.h> |
| 17 | |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 18 | #include "readtex.h" |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 19 | |
| 20 | #define TEXTURE_FILE "../images/reflect.rgb" |
| 21 | |
| 22 | #define LIT 1 |
| 23 | #define TEXTURED 2 |
| 24 | #define REFLECT 3 |
| 25 | #define ANIMATE 10 |
| 26 | #define POINT_FILTER 20 |
| 27 | #define LINEAR_FILTER 21 |
| 28 | #define QUIT 100 |
| 29 | |
Brian | 8bef18f | 2008-04-01 14:56:10 -0600 | [diff] [blame] | 30 | static GLint Win = -1; |
| 31 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 32 | static GLuint CylinderObj = 0; |
| 33 | static GLboolean Animate = GL_TRUE; |
| 34 | |
| 35 | static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0; |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 36 | static GLfloat DXrot = 50.0, DYrot = 125.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 37 | |
Brian Paul | 4f66498 | 2000-09-29 23:09:39 +0000 | [diff] [blame] | 38 | /* performance info */ |
| 39 | static GLint T0 = 0; |
| 40 | static GLint Frames = 0; |
| 41 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 42 | |
| 43 | static void Idle( void ) |
| 44 | { |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 45 | static double t0 = -1.; |
| 46 | double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; |
| 47 | if (t0 < 0.0) |
| 48 | t0 = t; |
| 49 | dt = t - t0; |
| 50 | t0 = t; |
| 51 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 52 | if (Animate) { |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 53 | Xrot += DXrot * dt; |
| 54 | Yrot += DYrot * dt; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 55 | glutPostRedisplay(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void Display( void ) |
| 61 | { |
| 62 | glClear( GL_COLOR_BUFFER_BIT ); |
| 63 | |
| 64 | glPushMatrix(); |
| 65 | glRotatef(Xrot, 1.0, 0.0, 0.0); |
| 66 | glRotatef(Yrot, 0.0, 1.0, 0.0); |
| 67 | glRotatef(Zrot, 0.0, 0.0, 1.0); |
| 68 | glScalef(5.0, 5.0, 5.0); |
| 69 | glCallList(CylinderObj); |
| 70 | |
| 71 | glPopMatrix(); |
| 72 | |
| 73 | glutSwapBuffers(); |
Brian Paul | 4f66498 | 2000-09-29 23:09:39 +0000 | [diff] [blame] | 74 | |
| 75 | if (Animate) { |
| 76 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
| 77 | Frames++; |
| 78 | if (t - T0 >= 5000) { |
| 79 | GLfloat seconds = (t - T0) / 1000.0; |
| 80 | GLfloat fps = Frames / seconds; |
| 81 | printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps); |
Keith Whitwell | e6479c6 | 2009-02-24 12:02:24 +0000 | [diff] [blame] | 82 | fflush(stdout); |
Brian Paul | 4f66498 | 2000-09-29 23:09:39 +0000 | [diff] [blame] | 83 | T0 = t; |
| 84 | Frames = 0; |
| 85 | } |
| 86 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | static void Reshape( int width, int height ) |
| 91 | { |
| 92 | glViewport( 0, 0, width, height ); |
| 93 | glMatrixMode( GL_PROJECTION ); |
| 94 | glLoadIdentity(); |
| 95 | glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); |
| 96 | glMatrixMode( GL_MODELVIEW ); |
| 97 | glLoadIdentity(); |
| 98 | glTranslatef( 0.0, 0.0, -70.0 ); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static void SetMode(GLuint m) |
| 103 | { |
| 104 | /* disable everything */ |
| 105 | glDisable(GL_LIGHTING); |
| 106 | glDisable(GL_TEXTURE_2D); |
| 107 | glDisable(GL_TEXTURE_GEN_S); |
| 108 | glDisable(GL_TEXTURE_GEN_T); |
| 109 | |
| 110 | /* enable what's needed */ |
| 111 | if (m==LIT) { |
| 112 | glEnable(GL_LIGHTING); |
| 113 | } |
| 114 | else if (m==TEXTURED) { |
| 115 | glEnable(GL_TEXTURE_2D); |
| 116 | } |
| 117 | else if (m==REFLECT) { |
| 118 | glEnable(GL_TEXTURE_2D); |
| 119 | glEnable(GL_TEXTURE_GEN_S); |
| 120 | glEnable(GL_TEXTURE_GEN_T); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static void ModeMenu(int entry) |
| 126 | { |
| 127 | if (entry==ANIMATE) { |
| 128 | Animate = !Animate; |
Brian Paul | 0b12650 | 2003-08-28 03:07:49 +0000 | [diff] [blame] | 129 | if (Animate) |
| 130 | glutIdleFunc(Idle); |
| 131 | else |
| 132 | glutIdleFunc(NULL); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 133 | } |
| 134 | else if (entry==POINT_FILTER) { |
| 135 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 136 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 137 | } |
| 138 | else if (entry==LINEAR_FILTER) { |
| 139 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 140 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 141 | } |
| 142 | else if (entry==QUIT) { |
| 143 | exit(0); |
| 144 | } |
| 145 | else { |
| 146 | SetMode(entry); |
| 147 | } |
| 148 | glutPostRedisplay(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static void Key( unsigned char key, int x, int y ) |
| 153 | { |
| 154 | (void) x; |
| 155 | (void) y; |
| 156 | switch (key) { |
Brian Paul | 0b12650 | 2003-08-28 03:07:49 +0000 | [diff] [blame] | 157 | case ' ': |
| 158 | Animate = !Animate; |
| 159 | if (Animate) |
| 160 | glutIdleFunc(Idle); |
| 161 | else |
| 162 | glutIdleFunc(NULL); |
| 163 | break; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 164 | case 27: |
Brian | 8bef18f | 2008-04-01 14:56:10 -0600 | [diff] [blame] | 165 | glutDestroyWindow(Win); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 166 | exit(0); |
| 167 | break; |
| 168 | } |
| 169 | glutPostRedisplay(); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static void SpecialKey( int key, int x, int y ) |
| 174 | { |
| 175 | float step = 3.0; |
| 176 | (void) x; |
| 177 | (void) y; |
| 178 | |
| 179 | switch (key) { |
| 180 | case GLUT_KEY_UP: |
| 181 | Xrot += step; |
| 182 | break; |
| 183 | case GLUT_KEY_DOWN: |
| 184 | Xrot -= step; |
| 185 | break; |
| 186 | case GLUT_KEY_LEFT: |
| 187 | Yrot += step; |
| 188 | break; |
| 189 | case GLUT_KEY_RIGHT: |
| 190 | Yrot -= step; |
| 191 | break; |
| 192 | } |
| 193 | glutPostRedisplay(); |
| 194 | } |
| 195 | |
| 196 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 197 | static void Init( int argc, char *argv[] ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 198 | { |
| 199 | GLUquadricObj *q = gluNewQuadric(); |
| 200 | CylinderObj = glGenLists(1); |
| 201 | glNewList(CylinderObj, GL_COMPILE); |
| 202 | |
| 203 | glTranslatef(0.0, 0.0, -1.0); |
| 204 | |
| 205 | /* cylinder */ |
| 206 | gluQuadricNormals(q, GL_SMOOTH); |
| 207 | gluQuadricTexture(q, GL_TRUE); |
| 208 | gluCylinder(q, 0.6, 0.6, 2.0, 24, 1); |
| 209 | |
| 210 | /* end cap */ |
| 211 | glTranslatef(0.0, 0.0, 2.0); |
| 212 | gluDisk(q, 0.0, 0.6, 24, 1); |
| 213 | |
| 214 | /* other end cap */ |
| 215 | glTranslatef(0.0, 0.0, -2.0); |
| 216 | gluQuadricOrientation(q, GLU_INSIDE); |
| 217 | gluDisk(q, 0.0, 0.6, 24, 1); |
| 218 | |
| 219 | glEndList(); |
| 220 | gluDeleteQuadric(q); |
| 221 | |
| 222 | /* lighting */ |
| 223 | glEnable(GL_LIGHTING); |
| 224 | { |
| 225 | GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0}; |
| 226 | GLfloat white[4] = {1.0, 1.0, 1.0, 1.0}; |
| 227 | GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 }; |
| 228 | glMaterialfv(GL_FRONT, GL_DIFFUSE, teal); |
| 229 | glLightfv(GL_LIGHT0, GL_AMBIENT, gray); |
| 230 | glLightfv(GL_LIGHT0, GL_DIFFUSE, white); |
| 231 | glEnable(GL_LIGHT0); |
| 232 | } |
| 233 | |
| 234 | /* fitering = nearest, initially */ |
| 235 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 236 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 237 | |
| 238 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
| 239 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); |
| 240 | |
| 241 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 242 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 243 | |
| 244 | if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { |
| 245 | printf("Error: couldn't load texture image\n"); |
| 246 | exit(1); |
| 247 | } |
| 248 | |
| 249 | glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */ |
| 250 | |
| 251 | SetMode(LIT); |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 252 | |
| 253 | if (argc > 1 && strcmp(argv[1], "-info")==0) { |
| 254 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 255 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 256 | printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
| 257 | printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 258 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | int main( int argc, char *argv[] ) |
| 263 | { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 264 | glutInitWindowSize( 400, 400 ); |
Brian Paul | 263f432 | 2009-12-18 08:12:55 -0700 | [diff] [blame] | 265 | glutInit( &argc, argv ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 266 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 267 | |
Brian | 8bef18f | 2008-04-01 14:56:10 -0600 | [diff] [blame] | 268 | Win = glutCreateWindow(argv[0] ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 269 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 270 | Init(argc, argv); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 271 | |
| 272 | glutReshapeFunc( Reshape ); |
| 273 | glutKeyboardFunc( Key ); |
| 274 | glutSpecialFunc( SpecialKey ); |
| 275 | glutDisplayFunc( Display ); |
| 276 | glutIdleFunc( Idle ); |
| 277 | |
| 278 | glutCreateMenu(ModeMenu); |
| 279 | glutAddMenuEntry("Lit", LIT); |
| 280 | glutAddMenuEntry("Textured", TEXTURED); |
| 281 | glutAddMenuEntry("Reflect", REFLECT); |
| 282 | glutAddMenuEntry("Point Filtered", POINT_FILTER); |
| 283 | glutAddMenuEntry("Linear Filtered", LINEAR_FILTER); |
| 284 | glutAddMenuEntry("Toggle Animation", ANIMATE); |
| 285 | glutAddMenuEntry("Quit", QUIT); |
| 286 | glutAttachMenu(GLUT_RIGHT_BUTTON); |
| 287 | |
| 288 | glutMainLoop(); |
| 289 | return 0; |
| 290 | } |