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