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