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