Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / progs / demos / texcyl.c
1
2 /*
3 * Textured cylinder demo: lighting, texturing, reflection mapping.
4 *
5 * Command line options:
6 * -info print GL implementation information
7 *
8 *
9 * Brian Paul May 1997 This program is in the public domain.
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include <string.h>
16 #include <GL/glut.h>
17
18 #include "readtex.h"
19
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
30 static GLint Win = -1;
31
32 static GLuint CylinderObj = 0;
33 static GLboolean Animate = GL_TRUE;
34
35 static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
36 static GLfloat DXrot = 50.0, DYrot = 125.0;
37
38 /* performance info */
39 static GLint T0 = 0;
40 static GLint Frames = 0;
41
42
43 static void Idle( void )
44 {
45 static double t0 = -1.;
46 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
47 if (t0 < 0.0)
48 t0 = t;
49 dt = t - t0;
50 t0 = t;
51
52 if (Animate) {
53 Xrot += DXrot * dt;
54 Yrot += DYrot * dt;
55 glutPostRedisplay();
56 }
57 }
58
59
60 static void Display( void )
61 {
62 glClear( GL_COLOR_BUFFER_BIT );
63
64 glPushMatrix();
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);
70
71 glPopMatrix();
72
73 glutSwapBuffers();
74
75 if (Animate) {
76 GLint t = glutGet(GLUT_ELAPSED_TIME);
77 Frames++;
78 if (t - T0 >= 5000) {
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);
82 fflush(stdout);
83 T0 = t;
84 Frames = 0;
85 }
86 }
87 }
88
89
90 static void Reshape( int width, int height )
91 {
92 glViewport( 0, 0, width, height );
93 glMatrixMode( GL_PROJECTION );
94 glLoadIdentity();
95 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
96 glMatrixMode( GL_MODELVIEW );
97 glLoadIdentity();
98 glTranslatef( 0.0, 0.0, -70.0 );
99 }
100
101
102 static void SetMode(GLuint m)
103 {
104 /* disable everything */
105 glDisable(GL_LIGHTING);
106 glDisable(GL_TEXTURE_2D);
107 glDisable(GL_TEXTURE_GEN_S);
108 glDisable(GL_TEXTURE_GEN_T);
109
110 /* enable what's needed */
111 if (m==LIT) {
112 glEnable(GL_LIGHTING);
113 }
114 else if (m==TEXTURED) {
115 glEnable(GL_TEXTURE_2D);
116 }
117 else if (m==REFLECT) {
118 glEnable(GL_TEXTURE_2D);
119 glEnable(GL_TEXTURE_GEN_S);
120 glEnable(GL_TEXTURE_GEN_T);
121 }
122 }
123
124
125 static void ModeMenu(int entry)
126 {
127 if (entry==ANIMATE) {
128 Animate = !Animate;
129 if (Animate)
130 glutIdleFunc(Idle);
131 else
132 glutIdleFunc(NULL);
133 }
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);
137 }
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);
141 }
142 else if (entry==QUIT) {
143 exit(0);
144 }
145 else {
146 SetMode(entry);
147 }
148 glutPostRedisplay();
149 }
150
151
152 static void Key( unsigned char key, int x, int y )
153 {
154 (void) x;
155 (void) y;
156 switch (key) {
157 case ' ':
158 Animate = !Animate;
159 if (Animate)
160 glutIdleFunc(Idle);
161 else
162 glutIdleFunc(NULL);
163 break;
164 case 27:
165 glutDestroyWindow(Win);
166 exit(0);
167 break;
168 }
169 glutPostRedisplay();
170 }
171
172
173 static void SpecialKey( int key, int x, int y )
174 {
175 float step = 3.0;
176 (void) x;
177 (void) y;
178
179 switch (key) {
180 case GLUT_KEY_UP:
181 Xrot += step;
182 break;
183 case GLUT_KEY_DOWN:
184 Xrot -= step;
185 break;
186 case GLUT_KEY_LEFT:
187 Yrot += step;
188 break;
189 case GLUT_KEY_RIGHT:
190 Yrot -= step;
191 break;
192 }
193 glutPostRedisplay();
194 }
195
196
197 static void Init( int argc, char *argv[] )
198 {
199 GLUquadricObj *q = gluNewQuadric();
200 CylinderObj = glGenLists(1);
201 glNewList(CylinderObj, GL_COMPILE);
202
203 glTranslatef(0.0, 0.0, -1.0);
204
205 /* cylinder */
206 gluQuadricNormals(q, GL_SMOOTH);
207 gluQuadricTexture(q, GL_TRUE);
208 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
209
210 /* end cap */
211 glTranslatef(0.0, 0.0, 2.0);
212 gluDisk(q, 0.0, 0.6, 24, 1);
213
214 /* other end cap */
215 glTranslatef(0.0, 0.0, -2.0);
216 gluQuadricOrientation(q, GLU_INSIDE);
217 gluDisk(q, 0.0, 0.6, 24, 1);
218
219 glEndList();
220 gluDeleteQuadric(q);
221
222 /* lighting */
223 glEnable(GL_LIGHTING);
224 {
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);
231 glEnable(GL_LIGHT0);
232 }
233
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);
237
238 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
239 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
240
241 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
242 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
243
244 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
245 printf("Error: couldn't load texture image\n");
246 exit(1);
247 }
248
249 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
250
251 SetMode(LIT);
252
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));
258 }
259 }
260
261
262 int main( int argc, char *argv[] )
263 {
264 glutInit( &argc, argv );
265 glutInitWindowSize( 400, 400 );
266 glutInitWindowPosition( 0, 0 );
267
268 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
269
270 Win = glutCreateWindow(argv[0] );
271
272 Init(argc, argv);
273
274 glutReshapeFunc( Reshape );
275 glutKeyboardFunc( Key );
276 glutSpecialFunc( SpecialKey );
277 glutDisplayFunc( Display );
278 glutIdleFunc( Idle );
279
280 glutCreateMenu(ModeMenu);
281 glutAddMenuEntry("Lit", LIT);
282 glutAddMenuEntry("Textured", TEXTURED);
283 glutAddMenuEntry("Reflect", REFLECT);
284 glutAddMenuEntry("Point Filtered", POINT_FILTER);
285 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
286 glutAddMenuEntry("Toggle Animation", ANIMATE);
287 glutAddMenuEntry("Quit", QUIT);
288 glutAttachMenu(GLUT_RIGHT_BUTTON);
289
290 glutMainLoop();
291 return 0;
292 }