Brian Paul | 8afa9e5 | 2001-03-27 17:35:26 +0000 | [diff] [blame] | 1 | /* $Id: texcyl.c,v 1.5 2001/03/27 17:35:26 brianp Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Textured cylinder demo: lighting, texturing, reflection mapping. |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 5 | * |
| 6 | * Command line options: |
| 7 | * -info print GL implementation information |
| 8 | * |
| 9 | * |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 10 | * Brian Paul May 1997 This program is in the public domain. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * $Log: texcyl.c,v $ |
Brian Paul | 8afa9e5 | 2001-03-27 17:35:26 +0000 | [diff] [blame] | 15 | * Revision 1.5 2001/03/27 17:35:26 brianp |
| 16 | * set initial window pos |
| 17 | * |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame] | 18 | * Revision 1.4 2000/12/24 22:53:54 pesco |
| 19 | * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util. |
| 20 | * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus: |
| 21 | * Essentially the same. |
| 22 | * Program files updated to include "readtex.c", not "../util/readtex.c". |
| 23 | * * demos/reflect.c: Likewise for "showbuffer.c". |
| 24 | * |
| 25 | * |
| 26 | * * Makefile.am (EXTRA_DIST): Added top-level regular files. |
| 27 | * |
| 28 | * * include/GL/Makefile.am (INC_X11): Added glxext.h. |
| 29 | * |
| 30 | * |
| 31 | * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include |
| 32 | * Mesa GGI headers in dist even if HAVE_GGI is not given. |
| 33 | * |
| 34 | * * configure.in: Look for GLUT and demo source dirs in $srcdir. |
| 35 | * |
| 36 | * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch]. |
| 37 | * More source list updates in various Makefile.am's. |
| 38 | * |
| 39 | * * Makefile.am (dist-hook): Remove CVS directory from distribution. |
| 40 | * (DIST_SUBDIRS): List all possible subdirs here. |
| 41 | * (SUBDIRS): Only list subdirs selected for build again. |
| 42 | * The above two applied to all subdir Makefile.am's also. |
| 43 | * |
Brian Paul | 4f66498 | 2000-09-29 23:09:39 +0000 | [diff] [blame] | 44 | * Revision 1.3 2000/09/29 23:09:39 brianp |
| 45 | * added fps output |
| 46 | * |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 47 | * Revision 1.2 1999/10/21 16:39:06 brianp |
| 48 | * added -info command line option |
| 49 | * |
| 50 | * Revision 1.1.1.1 1999/08/19 00:55:40 jtg |
| 51 | * Imported sources |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 52 | * |
| 53 | * Revision 3.3 1999/03/28 18:24:37 brianp |
| 54 | * minor clean-up |
| 55 | * |
| 56 | * Revision 3.2 1998/11/05 04:34:04 brianp |
| 57 | * moved image files to ../images/ directory |
| 58 | * |
| 59 | * Revision 3.1 1998/06/23 03:16:51 brianp |
| 60 | * added Point/Linear sampling menu items |
| 61 | * |
| 62 | * Revision 3.0 1998/02/14 18:42:29 brianp |
| 63 | * initial rev |
| 64 | * |
| 65 | */ |
| 66 | |
| 67 | |
| 68 | #include <stdio.h> |
| 69 | #include <stdlib.h> |
| 70 | #include <math.h> |
| 71 | #include <GL/glut.h> |
| 72 | |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame] | 73 | #include "readtex.c" /* I know, this is a hack. */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 74 | |
| 75 | #define TEXTURE_FILE "../images/reflect.rgb" |
| 76 | |
| 77 | #define LIT 1 |
| 78 | #define TEXTURED 2 |
| 79 | #define REFLECT 3 |
| 80 | #define ANIMATE 10 |
| 81 | #define POINT_FILTER 20 |
| 82 | #define LINEAR_FILTER 21 |
| 83 | #define QUIT 100 |
| 84 | |
| 85 | static GLuint CylinderObj = 0; |
| 86 | static GLboolean Animate = GL_TRUE; |
| 87 | |
| 88 | static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0; |
| 89 | static GLfloat DXrot = 1.0, DYrot = 2.5; |
| 90 | |
Brian Paul | 4f66498 | 2000-09-29 23:09:39 +0000 | [diff] [blame] | 91 | /* performance info */ |
| 92 | static GLint T0 = 0; |
| 93 | static GLint Frames = 0; |
| 94 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 95 | |
| 96 | static void Idle( void ) |
| 97 | { |
| 98 | if (Animate) { |
| 99 | Xrot += DXrot; |
| 100 | Yrot += DYrot; |
| 101 | glutPostRedisplay(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void Display( void ) |
| 107 | { |
| 108 | glClear( GL_COLOR_BUFFER_BIT ); |
| 109 | |
| 110 | glPushMatrix(); |
| 111 | glRotatef(Xrot, 1.0, 0.0, 0.0); |
| 112 | glRotatef(Yrot, 0.0, 1.0, 0.0); |
| 113 | glRotatef(Zrot, 0.0, 0.0, 1.0); |
| 114 | glScalef(5.0, 5.0, 5.0); |
| 115 | glCallList(CylinderObj); |
| 116 | |
| 117 | glPopMatrix(); |
| 118 | |
| 119 | glutSwapBuffers(); |
Brian Paul | 4f66498 | 2000-09-29 23:09:39 +0000 | [diff] [blame] | 120 | |
| 121 | if (Animate) { |
| 122 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
| 123 | Frames++; |
| 124 | if (t - T0 >= 5000) { |
| 125 | GLfloat seconds = (t - T0) / 1000.0; |
| 126 | GLfloat fps = Frames / seconds; |
| 127 | printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps); |
| 128 | T0 = t; |
| 129 | Frames = 0; |
| 130 | } |
| 131 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | static void Reshape( int width, int height ) |
| 136 | { |
| 137 | glViewport( 0, 0, width, height ); |
| 138 | glMatrixMode( GL_PROJECTION ); |
| 139 | glLoadIdentity(); |
| 140 | glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); |
| 141 | glMatrixMode( GL_MODELVIEW ); |
| 142 | glLoadIdentity(); |
| 143 | glTranslatef( 0.0, 0.0, -70.0 ); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | static void SetMode(GLuint m) |
| 148 | { |
| 149 | /* disable everything */ |
| 150 | glDisable(GL_LIGHTING); |
| 151 | glDisable(GL_TEXTURE_2D); |
| 152 | glDisable(GL_TEXTURE_GEN_S); |
| 153 | glDisable(GL_TEXTURE_GEN_T); |
| 154 | |
| 155 | /* enable what's needed */ |
| 156 | if (m==LIT) { |
| 157 | glEnable(GL_LIGHTING); |
| 158 | } |
| 159 | else if (m==TEXTURED) { |
| 160 | glEnable(GL_TEXTURE_2D); |
| 161 | } |
| 162 | else if (m==REFLECT) { |
| 163 | glEnable(GL_TEXTURE_2D); |
| 164 | glEnable(GL_TEXTURE_GEN_S); |
| 165 | glEnable(GL_TEXTURE_GEN_T); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static void ModeMenu(int entry) |
| 171 | { |
| 172 | if (entry==ANIMATE) { |
| 173 | Animate = !Animate; |
| 174 | } |
| 175 | else if (entry==POINT_FILTER) { |
| 176 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 177 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 178 | } |
| 179 | else if (entry==LINEAR_FILTER) { |
| 180 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 181 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 182 | } |
| 183 | else if (entry==QUIT) { |
| 184 | exit(0); |
| 185 | } |
| 186 | else { |
| 187 | SetMode(entry); |
| 188 | } |
| 189 | glutPostRedisplay(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | static void Key( unsigned char key, int x, int y ) |
| 194 | { |
| 195 | (void) x; |
| 196 | (void) y; |
| 197 | switch (key) { |
| 198 | case 27: |
| 199 | exit(0); |
| 200 | break; |
| 201 | } |
| 202 | glutPostRedisplay(); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | static void SpecialKey( int key, int x, int y ) |
| 207 | { |
| 208 | float step = 3.0; |
| 209 | (void) x; |
| 210 | (void) y; |
| 211 | |
| 212 | switch (key) { |
| 213 | case GLUT_KEY_UP: |
| 214 | Xrot += step; |
| 215 | break; |
| 216 | case GLUT_KEY_DOWN: |
| 217 | Xrot -= step; |
| 218 | break; |
| 219 | case GLUT_KEY_LEFT: |
| 220 | Yrot += step; |
| 221 | break; |
| 222 | case GLUT_KEY_RIGHT: |
| 223 | Yrot -= step; |
| 224 | break; |
| 225 | } |
| 226 | glutPostRedisplay(); |
| 227 | } |
| 228 | |
| 229 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 230 | static void Init( int argc, char *argv[] ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 231 | { |
| 232 | GLUquadricObj *q = gluNewQuadric(); |
| 233 | CylinderObj = glGenLists(1); |
| 234 | glNewList(CylinderObj, GL_COMPILE); |
| 235 | |
| 236 | glTranslatef(0.0, 0.0, -1.0); |
| 237 | |
| 238 | /* cylinder */ |
| 239 | gluQuadricNormals(q, GL_SMOOTH); |
| 240 | gluQuadricTexture(q, GL_TRUE); |
| 241 | gluCylinder(q, 0.6, 0.6, 2.0, 24, 1); |
| 242 | |
| 243 | /* end cap */ |
| 244 | glTranslatef(0.0, 0.0, 2.0); |
| 245 | gluDisk(q, 0.0, 0.6, 24, 1); |
| 246 | |
| 247 | /* other end cap */ |
| 248 | glTranslatef(0.0, 0.0, -2.0); |
| 249 | gluQuadricOrientation(q, GLU_INSIDE); |
| 250 | gluDisk(q, 0.0, 0.6, 24, 1); |
| 251 | |
| 252 | glEndList(); |
| 253 | gluDeleteQuadric(q); |
| 254 | |
| 255 | /* lighting */ |
| 256 | glEnable(GL_LIGHTING); |
| 257 | { |
| 258 | GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0}; |
| 259 | GLfloat white[4] = {1.0, 1.0, 1.0, 1.0}; |
| 260 | GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 }; |
| 261 | glMaterialfv(GL_FRONT, GL_DIFFUSE, teal); |
| 262 | glLightfv(GL_LIGHT0, GL_AMBIENT, gray); |
| 263 | glLightfv(GL_LIGHT0, GL_DIFFUSE, white); |
| 264 | glEnable(GL_LIGHT0); |
| 265 | } |
| 266 | |
| 267 | /* fitering = nearest, initially */ |
| 268 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 269 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 270 | |
| 271 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
| 272 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); |
| 273 | |
| 274 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 275 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 276 | |
| 277 | if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { |
| 278 | printf("Error: couldn't load texture image\n"); |
| 279 | exit(1); |
| 280 | } |
| 281 | |
| 282 | glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */ |
| 283 | |
| 284 | SetMode(LIT); |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 285 | |
| 286 | if (argc > 1 && strcmp(argv[1], "-info")==0) { |
| 287 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 288 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 289 | printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
| 290 | printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 291 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
| 295 | int main( int argc, char *argv[] ) |
| 296 | { |
| 297 | glutInit( &argc, argv ); |
| 298 | glutInitWindowSize( 400, 400 ); |
Brian Paul | 8afa9e5 | 2001-03-27 17:35:26 +0000 | [diff] [blame] | 299 | glutInitWindowPosition( 0, 0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 300 | |
| 301 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 302 | |
| 303 | glutCreateWindow(argv[0] ); |
| 304 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 305 | Init(argc, argv); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 306 | |
| 307 | glutReshapeFunc( Reshape ); |
| 308 | glutKeyboardFunc( Key ); |
| 309 | glutSpecialFunc( SpecialKey ); |
| 310 | glutDisplayFunc( Display ); |
| 311 | glutIdleFunc( Idle ); |
| 312 | |
| 313 | glutCreateMenu(ModeMenu); |
| 314 | glutAddMenuEntry("Lit", LIT); |
| 315 | glutAddMenuEntry("Textured", TEXTURED); |
| 316 | glutAddMenuEntry("Reflect", REFLECT); |
| 317 | glutAddMenuEntry("Point Filtered", POINT_FILTER); |
| 318 | glutAddMenuEntry("Linear Filtered", LINEAR_FILTER); |
| 319 | glutAddMenuEntry("Toggle Animation", ANIMATE); |
| 320 | glutAddMenuEntry("Quit", QUIT); |
| 321 | glutAttachMenu(GLUT_RIGHT_BUTTON); |
| 322 | |
| 323 | glutMainLoop(); |
| 324 | return 0; |
| 325 | } |