3 * Textured cylinder demo: lighting, texturing, reflection mapping.
5 * Command line options:
6 * -info print GL implementation information
9 * Brian Paul May 1997 This program is in the public domain.
20 #define TEXTURE_FILE "../images/reflect.rgb"
26 #define POINT_FILTER 20
27 #define LINEAR_FILTER 21
30 static GLint Win
= -1;
32 static GLuint CylinderObj
= 0;
33 static GLboolean Animate
= GL_TRUE
;
35 static GLfloat Xrot
= 0.0, Yrot
= 0.0, Zrot
= 0.0;
36 static GLfloat DXrot
= 50.0, DYrot
= 125.0;
38 /* performance info */
40 static GLint Frames
= 0;
43 static void Idle( void )
45 static double t0
= -1.;
46 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
60 static void Display( void )
62 glClear( GL_COLOR_BUFFER_BIT
);
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
);
76 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
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
);
90 static void Reshape( int width
, int height
)
92 glViewport( 0, 0, width
, height
);
93 glMatrixMode( GL_PROJECTION
);
95 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
96 glMatrixMode( GL_MODELVIEW
);
98 glTranslatef( 0.0, 0.0, -70.0 );
102 static void SetMode(GLuint m
)
104 /* disable everything */
105 glDisable(GL_LIGHTING
);
106 glDisable(GL_TEXTURE_2D
);
107 glDisable(GL_TEXTURE_GEN_S
);
108 glDisable(GL_TEXTURE_GEN_T
);
110 /* enable what's needed */
112 glEnable(GL_LIGHTING
);
114 else if (m
==TEXTURED
) {
115 glEnable(GL_TEXTURE_2D
);
117 else if (m
==REFLECT
) {
118 glEnable(GL_TEXTURE_2D
);
119 glEnable(GL_TEXTURE_GEN_S
);
120 glEnable(GL_TEXTURE_GEN_T
);
125 static void ModeMenu(int entry
)
127 if (entry
==ANIMATE
) {
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
);
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
);
142 else if (entry
==QUIT
) {
152 static void Key( unsigned char key
, int x
, int y
)
165 glutDestroyWindow(Win
);
173 static void SpecialKey( int key
, int x
, int y
)
197 static void Init( int argc
, char *argv
[] )
199 GLUquadricObj
*q
= gluNewQuadric();
200 CylinderObj
= glGenLists(1);
201 glNewList(CylinderObj
, GL_COMPILE
);
203 glTranslatef(0.0, 0.0, -1.0);
206 gluQuadricNormals(q
, GL_SMOOTH
);
207 gluQuadricTexture(q
, GL_TRUE
);
208 gluCylinder(q
, 0.6, 0.6, 2.0, 24, 1);
211 glTranslatef(0.0, 0.0, 2.0);
212 gluDisk(q
, 0.0, 0.6, 24, 1);
215 glTranslatef(0.0, 0.0, -2.0);
216 gluQuadricOrientation(q
, GLU_INSIDE
);
217 gluDisk(q
, 0.0, 0.6, 24, 1);
223 glEnable(GL_LIGHTING
);
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
);
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
);
238 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
239 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
241 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
242 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
244 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
245 printf("Error: couldn't load texture image\n");
249 glEnable(GL_CULL_FACE
); /* don't need Z testing for convex objects */
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
));
262 int main( int argc
, char *argv
[] )
264 glutInitWindowSize( 400, 400 );
265 glutInit( &argc
, argv
);
266 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
268 Win
= glutCreateWindow(argv
[0] );
272 glutReshapeFunc( Reshape
);
273 glutKeyboardFunc( Key
);
274 glutSpecialFunc( SpecialKey
);
275 glutDisplayFunc( Display
);
276 glutIdleFunc( Idle
);
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
);