Merge commit 'origin/gallium-master-merge'
[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 T0 = t;
83 Frames = 0;
84 }
85 }
86 }
87
88
89 static void Reshape( int width, int height )
90 {
91 glViewport( 0, 0, width, height );
92 glMatrixMode( GL_PROJECTION );
93 glLoadIdentity();
94 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
95 glMatrixMode( GL_MODELVIEW );
96 glLoadIdentity();
97 glTranslatef( 0.0, 0.0, -70.0 );
98 }
99
100
101 static void SetMode(GLuint m)
102 {
103 /* disable everything */
104 glDisable(GL_LIGHTING);
105 glDisable(GL_TEXTURE_2D);
106 glDisable(GL_TEXTURE_GEN_S);
107 glDisable(GL_TEXTURE_GEN_T);
108
109 /* enable what's needed */
110 if (m==LIT) {
111 glEnable(GL_LIGHTING);
112 }
113 else if (m==TEXTURED) {
114 glEnable(GL_TEXTURE_2D);
115 }
116 else if (m==REFLECT) {
117 glEnable(GL_TEXTURE_2D);
118 glEnable(GL_TEXTURE_GEN_S);
119 glEnable(GL_TEXTURE_GEN_T);
120 }
121 }
122
123
124 static void ModeMenu(int entry)
125 {
126 if (entry==ANIMATE) {
127 Animate = !Animate;
128 if (Animate)
129 glutIdleFunc(Idle);
130 else
131 glutIdleFunc(NULL);
132 }
133 else if (entry==POINT_FILTER) {
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
136 }
137 else if (entry==LINEAR_FILTER) {
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
140 }
141 else if (entry==QUIT) {
142 exit(0);
143 }
144 else {
145 SetMode(entry);
146 }
147 glutPostRedisplay();
148 }
149
150
151 static void Key( unsigned char key, int x, int y )
152 {
153 (void) x;
154 (void) y;
155 switch (key) {
156 case ' ':
157 Animate = !Animate;
158 if (Animate)
159 glutIdleFunc(Idle);
160 else
161 glutIdleFunc(NULL);
162 break;
163 case 27:
164 glutDestroyWindow(Win);
165 exit(0);
166 break;
167 }
168 glutPostRedisplay();
169 }
170
171
172 static void SpecialKey( int key, int x, int y )
173 {
174 float step = 3.0;
175 (void) x;
176 (void) y;
177
178 switch (key) {
179 case GLUT_KEY_UP:
180 Xrot += step;
181 break;
182 case GLUT_KEY_DOWN:
183 Xrot -= step;
184 break;
185 case GLUT_KEY_LEFT:
186 Yrot += step;
187 break;
188 case GLUT_KEY_RIGHT:
189 Yrot -= step;
190 break;
191 }
192 glutPostRedisplay();
193 }
194
195
196 static void Init( int argc, char *argv[] )
197 {
198 GLUquadricObj *q = gluNewQuadric();
199 CylinderObj = glGenLists(1);
200 glNewList(CylinderObj, GL_COMPILE);
201
202 glTranslatef(0.0, 0.0, -1.0);
203
204 /* cylinder */
205 gluQuadricNormals(q, GL_SMOOTH);
206 gluQuadricTexture(q, GL_TRUE);
207 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
208
209 /* end cap */
210 glTranslatef(0.0, 0.0, 2.0);
211 gluDisk(q, 0.0, 0.6, 24, 1);
212
213 /* other end cap */
214 glTranslatef(0.0, 0.0, -2.0);
215 gluQuadricOrientation(q, GLU_INSIDE);
216 gluDisk(q, 0.0, 0.6, 24, 1);
217
218 glEndList();
219 gluDeleteQuadric(q);
220
221 /* lighting */
222 glEnable(GL_LIGHTING);
223 {
224 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
225 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
226 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
227 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
228 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
229 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
230 glEnable(GL_LIGHT0);
231 }
232
233 /* fitering = nearest, initially */
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
236
237 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
238 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
239
240 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
241 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
242
243 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
244 printf("Error: couldn't load texture image\n");
245 exit(1);
246 }
247
248 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
249
250 SetMode(LIT);
251
252 if (argc > 1 && strcmp(argv[1], "-info")==0) {
253 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
254 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
255 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
256 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
257 }
258 }
259
260
261 int main( int argc, char *argv[] )
262 {
263 glutInit( &argc, argv );
264 glutInitWindowSize( 400, 400 );
265 glutInitWindowPosition( 0, 0 );
266
267 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
268
269 Win = glutCreateWindow(argv[0] );
270
271 Init(argc, argv);
272
273 glutReshapeFunc( Reshape );
274 glutKeyboardFunc( Key );
275 glutSpecialFunc( SpecialKey );
276 glutDisplayFunc( Display );
277 glutIdleFunc( Idle );
278
279 glutCreateMenu(ModeMenu);
280 glutAddMenuEntry("Lit", LIT);
281 glutAddMenuEntry("Textured", TEXTURED);
282 glutAddMenuEntry("Reflect", REFLECT);
283 glutAddMenuEntry("Point Filtered", POINT_FILTER);
284 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
285 glutAddMenuEntry("Toggle Animation", ANIMATE);
286 glutAddMenuEntry("Quit", QUIT);
287 glutAttachMenu(GLUT_RIGHT_BUTTON);
288
289 glutMainLoop();
290 return 0;
291 }