blob: 7741a75f855c6ddb36a40ade0bfb0c8fca448207 [file] [log] [blame]
Brian Paulfdd631a2002-04-22 16:03:37 +00001/* $Id: texcyl.c,v 1.6 2002/04/22 16:03:37 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
jtgafb833d1999-08-19 00:55:39 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <math.h>
16#include <GL/glut.h>
17
pescod1ff1f62000-12-24 22:53:54 +000018#include "readtex.c" /* I know, this is a hack. */
jtgafb833d1999-08-19 00:55:39 +000019
20#define TEXTURE_FILE "../images/reflect.rgb"
21
22#define LIT 1
23#define TEXTURED 2
24#define REFLECT 3
25#define ANIMATE 10
26#define POINT_FILTER 20
27#define LINEAR_FILTER 21
28#define QUIT 100
29
30static GLuint CylinderObj = 0;
31static GLboolean Animate = GL_TRUE;
32
33static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
34static GLfloat DXrot = 1.0, DYrot = 2.5;
35
Brian Paul4f664982000-09-29 23:09:39 +000036/* performance info */
37static GLint T0 = 0;
38static GLint Frames = 0;
39
jtgafb833d1999-08-19 00:55:39 +000040
41static void Idle( void )
42{
43 if (Animate) {
44 Xrot += DXrot;
45 Yrot += DYrot;
46 glutPostRedisplay();
47 }
48}
49
50
51static void Display( void )
52{
53 glClear( GL_COLOR_BUFFER_BIT );
54
55 glPushMatrix();
56 glRotatef(Xrot, 1.0, 0.0, 0.0);
57 glRotatef(Yrot, 0.0, 1.0, 0.0);
58 glRotatef(Zrot, 0.0, 0.0, 1.0);
59 glScalef(5.0, 5.0, 5.0);
60 glCallList(CylinderObj);
61
62 glPopMatrix();
63
64 glutSwapBuffers();
Brian Paul4f664982000-09-29 23:09:39 +000065
66 if (Animate) {
67 GLint t = glutGet(GLUT_ELAPSED_TIME);
68 Frames++;
69 if (t - T0 >= 5000) {
70 GLfloat seconds = (t - T0) / 1000.0;
71 GLfloat fps = Frames / seconds;
72 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
73 T0 = t;
74 Frames = 0;
75 }
76 }
jtgafb833d1999-08-19 00:55:39 +000077}
78
79
80static void Reshape( int width, int height )
81{
82 glViewport( 0, 0, width, height );
83 glMatrixMode( GL_PROJECTION );
84 glLoadIdentity();
85 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
86 glMatrixMode( GL_MODELVIEW );
87 glLoadIdentity();
88 glTranslatef( 0.0, 0.0, -70.0 );
89}
90
91
92static void SetMode(GLuint m)
93{
94 /* disable everything */
95 glDisable(GL_LIGHTING);
96 glDisable(GL_TEXTURE_2D);
97 glDisable(GL_TEXTURE_GEN_S);
98 glDisable(GL_TEXTURE_GEN_T);
99
100 /* enable what's needed */
101 if (m==LIT) {
102 glEnable(GL_LIGHTING);
103 }
104 else if (m==TEXTURED) {
105 glEnable(GL_TEXTURE_2D);
106 }
107 else if (m==REFLECT) {
108 glEnable(GL_TEXTURE_2D);
109 glEnable(GL_TEXTURE_GEN_S);
110 glEnable(GL_TEXTURE_GEN_T);
111 }
112}
113
114
115static void ModeMenu(int entry)
116{
117 if (entry==ANIMATE) {
118 Animate = !Animate;
119 }
120 else if (entry==POINT_FILTER) {
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
123 }
124 else if (entry==LINEAR_FILTER) {
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 }
128 else if (entry==QUIT) {
129 exit(0);
130 }
131 else {
132 SetMode(entry);
133 }
134 glutPostRedisplay();
135}
136
137
138static void Key( unsigned char key, int x, int y )
139{
140 (void) x;
141 (void) y;
142 switch (key) {
143 case 27:
144 exit(0);
145 break;
146 }
147 glutPostRedisplay();
148}
149
150
151static void SpecialKey( int key, int x, int y )
152{
153 float step = 3.0;
154 (void) x;
155 (void) y;
156
157 switch (key) {
158 case GLUT_KEY_UP:
159 Xrot += step;
160 break;
161 case GLUT_KEY_DOWN:
162 Xrot -= step;
163 break;
164 case GLUT_KEY_LEFT:
165 Yrot += step;
166 break;
167 case GLUT_KEY_RIGHT:
168 Yrot -= step;
169 break;
170 }
171 glutPostRedisplay();
172}
173
174
Brian Paulac126091999-10-21 16:39:06 +0000175static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000176{
177 GLUquadricObj *q = gluNewQuadric();
178 CylinderObj = glGenLists(1);
179 glNewList(CylinderObj, GL_COMPILE);
180
181 glTranslatef(0.0, 0.0, -1.0);
182
183 /* cylinder */
184 gluQuadricNormals(q, GL_SMOOTH);
185 gluQuadricTexture(q, GL_TRUE);
186 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
187
188 /* end cap */
189 glTranslatef(0.0, 0.0, 2.0);
190 gluDisk(q, 0.0, 0.6, 24, 1);
191
192 /* other end cap */
193 glTranslatef(0.0, 0.0, -2.0);
194 gluQuadricOrientation(q, GLU_INSIDE);
195 gluDisk(q, 0.0, 0.6, 24, 1);
196
197 glEndList();
198 gluDeleteQuadric(q);
199
200 /* lighting */
201 glEnable(GL_LIGHTING);
202 {
203 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
204 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
205 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
206 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
207 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
208 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
209 glEnable(GL_LIGHT0);
210 }
211
212 /* fitering = nearest, initially */
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
214 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
215
216 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
217 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
218
219 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
220 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
221
222 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
223 printf("Error: couldn't load texture image\n");
224 exit(1);
225 }
226
227 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
228
229 SetMode(LIT);
Brian Paulac126091999-10-21 16:39:06 +0000230
231 if (argc > 1 && strcmp(argv[1], "-info")==0) {
232 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
233 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
234 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
235 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
236 }
jtgafb833d1999-08-19 00:55:39 +0000237}
238
239
240int main( int argc, char *argv[] )
241{
242 glutInit( &argc, argv );
243 glutInitWindowSize( 400, 400 );
Brian Paul8afa9e52001-03-27 17:35:26 +0000244 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000245
246 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
247
248 glutCreateWindow(argv[0] );
249
Brian Paulac126091999-10-21 16:39:06 +0000250 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000251
252 glutReshapeFunc( Reshape );
253 glutKeyboardFunc( Key );
254 glutSpecialFunc( SpecialKey );
255 glutDisplayFunc( Display );
256 glutIdleFunc( Idle );
257
258 glutCreateMenu(ModeMenu);
259 glutAddMenuEntry("Lit", LIT);
260 glutAddMenuEntry("Textured", TEXTURED);
261 glutAddMenuEntry("Reflect", REFLECT);
262 glutAddMenuEntry("Point Filtered", POINT_FILTER);
263 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
264 glutAddMenuEntry("Toggle Animation", ANIMATE);
265 glutAddMenuEntry("Quit", QUIT);
266 glutAttachMenu(GLUT_RIGHT_BUTTON);
267
268 glutMainLoop();
269 return 0;
270}