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 GLuint CylinderObj
= 0;
31 static GLboolean Animate
= GL_TRUE
;
33 static GLfloat Xrot
= 0.0, Yrot
= 0.0, Zrot
= 0.0;
34 static GLfloat DXrot
= 50.0, DYrot
= 125.0;
36 /* performance info */
38 static GLint Frames
= 0;
41 static void Idle( void )
43 static double t0
= -1.;
44 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
58 static void Display( void )
60 glClear( GL_COLOR_BUFFER_BIT
);
63 glRotatef(Xrot
, 1.0, 0.0, 0.0);
64 glRotatef(Yrot
, 0.0, 1.0, 0.0);
65 glRotatef(Zrot
, 0.0, 0.0, 1.0);
66 glScalef(5.0, 5.0, 5.0);
67 glCallList(CylinderObj
);
74 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
77 GLfloat seconds
= (t
- T0
) / 1000.0;
78 GLfloat fps
= Frames
/ seconds
;
79 printf("%d frames in %g seconds = %g FPS\n", Frames
, seconds
, fps
);
87 static void Reshape( int width
, int height
)
89 glViewport( 0, 0, width
, height
);
90 glMatrixMode( GL_PROJECTION
);
92 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
93 glMatrixMode( GL_MODELVIEW
);
95 glTranslatef( 0.0, 0.0, -70.0 );
99 static void SetMode(GLuint m
)
101 /* disable everything */
102 glDisable(GL_LIGHTING
);
103 glDisable(GL_TEXTURE_2D
);
104 glDisable(GL_TEXTURE_GEN_S
);
105 glDisable(GL_TEXTURE_GEN_T
);
107 /* enable what's needed */
109 glEnable(GL_LIGHTING
);
111 else if (m
==TEXTURED
) {
112 glEnable(GL_TEXTURE_2D
);
114 else if (m
==REFLECT
) {
115 glEnable(GL_TEXTURE_2D
);
116 glEnable(GL_TEXTURE_GEN_S
);
117 glEnable(GL_TEXTURE_GEN_T
);
122 static void ModeMenu(int entry
)
124 if (entry
==ANIMATE
) {
131 else if (entry
==POINT_FILTER
) {
132 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
133 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
135 else if (entry
==LINEAR_FILTER
) {
136 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
137 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
139 else if (entry
==QUIT
) {
149 static void Key( unsigned char key
, int x
, int y
)
169 static void SpecialKey( int key
, int x
, int y
)
193 static void Init( int argc
, char *argv
[] )
195 GLUquadricObj
*q
= gluNewQuadric();
196 CylinderObj
= glGenLists(1);
197 glNewList(CylinderObj
, GL_COMPILE
);
199 glTranslatef(0.0, 0.0, -1.0);
202 gluQuadricNormals(q
, GL_SMOOTH
);
203 gluQuadricTexture(q
, GL_TRUE
);
204 gluCylinder(q
, 0.6, 0.6, 2.0, 24, 1);
207 glTranslatef(0.0, 0.0, 2.0);
208 gluDisk(q
, 0.0, 0.6, 24, 1);
211 glTranslatef(0.0, 0.0, -2.0);
212 gluQuadricOrientation(q
, GLU_INSIDE
);
213 gluDisk(q
, 0.0, 0.6, 24, 1);
219 glEnable(GL_LIGHTING
);
221 GLfloat gray
[4] = {0.2, 0.2, 0.2, 1.0};
222 GLfloat white
[4] = {1.0, 1.0, 1.0, 1.0};
223 GLfloat teal
[4] = { 0.0, 1.0, 0.8, 1.0 };
224 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, teal
);
225 glLightfv(GL_LIGHT0
, GL_AMBIENT
, gray
);
226 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, white
);
230 /* fitering = nearest, initially */
231 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
232 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
234 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
235 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
237 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
238 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
240 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
241 printf("Error: couldn't load texture image\n");
245 glEnable(GL_CULL_FACE
); /* don't need Z testing for convex objects */
249 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
250 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
251 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
252 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
253 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
258 int main( int argc
, char *argv
[] )
260 glutInit( &argc
, argv
);
261 glutInitWindowSize( 400, 400 );
262 glutInitWindowPosition( 0, 0 );
264 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
266 glutCreateWindow(argv
[0] );
270 glutReshapeFunc( Reshape
);
271 glutKeyboardFunc( Key
);
272 glutSpecialFunc( SpecialKey
);
273 glutDisplayFunc( Display
);
274 glutIdleFunc( Idle
);
276 glutCreateMenu(ModeMenu
);
277 glutAddMenuEntry("Lit", LIT
);
278 glutAddMenuEntry("Textured", TEXTURED
);
279 glutAddMenuEntry("Reflect", REFLECT
);
280 glutAddMenuEntry("Point Filtered", POINT_FILTER
);
281 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER
);
282 glutAddMenuEntry("Toggle Animation", ANIMATE
);
283 glutAddMenuEntry("Quit", QUIT
);
284 glutAttachMenu(GLUT_RIGHT_BUTTON
);