r600: fix copy/paste typo
[mesa.git] / progs / windml / ugltexcyl.c
1 /*
2 * Textured cylinder demo: lighting, texturing, reflection mapping.
3 *
4 * Brian Paul May 1997 This program is in the public domain.
5 *
6 * Conversion to UGL/Mesa by Stephane Raimbault
7 */
8
9 /*
10 * Revision 1.2 2001/09/10 19:21:13 brianp
11 * WindML updates (Stephane Raimbault)
12 *
13 * Revision 1.1 2001/08/20 16:07:11 brianp
14 * WindML driver (Stephane Raimbault)
15 *
16 * Revision 1.5 2001/03/27 17:35:26 brianp
17 * set initial window pos
18 *
19 * Revision 1.4 2000/12/24 22:53:54 pesco
20 * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
21 * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
22 * Essentially the same.
23 * Program files updated to include "readtex.c", not "../util/readtex.c".
24 * * demos/reflect.c: Likewise for "showbuffer.c".
25 *
26 *
27 * * Makefile.am (EXTRA_DIST): Added top-level regular files.
28 *
29 * * include/GL/Makefile.am (INC_X11): Added glxext.h.
30 *
31 *
32 * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
33 * Mesa GGI headers in dist even if HAVE_GGI is not given.
34 *
35 * * configure.in: Look for GLUT and demo source dirs in $srcdir.
36 *
37 * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
38 * More source list updates in various Makefile.am's.
39 *
40 * * Makefile.am (dist-hook): Remove CVS directory from distribution.
41 * (DIST_SUBDIRS): List all possible subdirs here.
42 * (SUBDIRS): Only list subdirs selected for build again.
43 * The above two applied to all subdir Makefile.am's also.
44 *
45 * Revision 1.3 2000/09/29 23:09:39 brianp
46 * added fps output
47 *
48 * Revision 1.2 1999/10/21 16:39:06 brianp
49 * added -info command line option
50 *
51 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
52 * Imported sources
53 *
54 * Revision 3.3 1999/03/28 18:24:37 brianp
55 * minor clean-up
56 *
57 * Revision 3.2 1998/11/05 04:34:04 brianp
58 * moved image files to ../images/ directory
59 *
60 * Revision 3.1 1998/06/23 03:16:51 brianp
61 * added Point/Linear sampling menu items
62 *
63 * Revision 3.0 1998/02/14 18:42:29 brianp
64 * initial rev
65 *
66 */
67
68
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <math.h>
72 #include <tickLib.h>
73
74 #include <ugl/ugl.h>
75 #include <ugl/uglucode.h>
76 #include <ugl/uglevent.h>
77 #include <ugl/uglinput.h>
78
79 #include <GL/uglmesa.h>
80 #include <GL/glu.h>
81
82 #include "../util/readtex.h"
83
84 #define TEXTURE_FILE "Mesa/images/reflect.rgb"
85
86 #define LIT 1
87 #define TEXTURED 2
88 #define REFLECT 3
89 #define ANIMATE 10
90 #define POINT_FILTER 20
91 #define LINEAR_FILTER 21
92 #define QUIT 100
93 #define COUNT_FRAMES
94
95 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
96 UGL_LOCAL UGL_EVENT_Q_ID qId;
97 UGL_LOCAL volatile UGL_BOOL stopWex;
98 UGL_LOCAL UGL_MESA_CONTEXT umc;
99
100 UGL_LOCAL GLuint CylinderObj;
101 UGL_LOCAL GLboolean Animate;
102 UGL_LOCAL GLboolean linearFilter;
103
104 UGL_LOCAL GLfloat Xrot, Yrot, Zrot;
105 UGL_LOCAL GLfloat DXrot, DYrot;
106
107 UGL_LOCAL GLuint limit;
108 UGL_LOCAL GLuint count;
109 UGL_LOCAL GLuint tickStart, tickStop, tickBySec;
110
111 UGL_LOCAL void cleanUp (void);
112
113 UGL_LOCAL void drawGL(void)
114 {
115 #ifdef COUNT_FRAMES
116 int time;
117 #endif
118
119 glClear( GL_COLOR_BUFFER_BIT );
120
121 glPushMatrix();
122 glRotatef(Xrot, 1.0, 0.0, 0.0);
123 glRotatef(Yrot, 0.0, 1.0, 0.0);
124 glRotatef(Zrot, 0.0, 0.0, 1.0);
125 glScalef(5.0, 5.0, 5.0);
126 glCallList(CylinderObj);
127
128 glPopMatrix();
129
130 uglMesaSwapBuffers();
131
132 if (Animate)
133 {
134 Xrot += DXrot;
135 Yrot += DYrot;
136 }
137
138 #ifdef COUNT_FRAMES
139 if (count > limit)
140 {
141 tickStop = tickGet ();
142 time = (tickStop-tickStart)/tickBySec;
143 printf (" %i fps\n", count/time);
144 tickStart = tickStop;
145 count = 0;
146 }
147 else
148 count++;
149 #endif
150
151 }
152
153 UGL_LOCAL void echoUse(void)
154 {
155 printf("Keys:\n");
156 printf(" Up/Down Rotate on Y\n");
157 printf(" Left/Right Rotate on X\n");
158 printf(" a Toggle animation\n");
159 printf(" f Toggle point/linear filtered\n");
160 printf(" l Lit\n");
161 printf(" t Textured\n");
162 printf(" r Reflect\n");
163 printf(" ESC Exit\n");
164 }
165
166 UGL_LOCAL void readKey(UGL_WCHAR key)
167 {
168 float step = 3.0;
169 switch (key)
170 {
171 case 'a':
172 Animate = !Animate;
173 break;
174 case 'f':
175 if(linearFilter)
176 {
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
178 GL_NEAREST);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
180 GL_NEAREST);
181 }
182 else
183 {
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
185 GL_LINEAR);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
187 GL_LINEAR);
188 }
189 linearFilter = !linearFilter;
190 break;
191 case 'l':
192 glEnable(GL_LIGHTING);
193 glDisable(GL_TEXTURE_2D);
194 glDisable(GL_TEXTURE_GEN_S);
195 glDisable(GL_TEXTURE_GEN_T);
196 break;
197 case 't':
198 glDisable(GL_LIGHTING);
199 glEnable(GL_TEXTURE_2D);
200 glDisable(GL_TEXTURE_GEN_S);
201 glDisable(GL_TEXTURE_GEN_T);
202 break;
203 case 'r':
204 glDisable(GL_LIGHTING);
205 glEnable(GL_TEXTURE_2D);
206 glEnable(GL_TEXTURE_GEN_S);
207 glEnable(GL_TEXTURE_GEN_T);
208 break;
209 case UGL_UNI_UP_ARROW:
210 Xrot += step;
211 break;
212 case UGL_UNI_DOWN_ARROW:
213 Xrot -= step;
214 break;
215 case UGL_UNI_LEFT_ARROW:
216 Yrot += step;
217 break;
218 case UGL_UNI_RIGHT_ARROW:
219 Yrot -= step;
220 break;
221 case UGL_UNI_ESCAPE:
222 stopWex = UGL_TRUE;
223 break;
224 }
225 }
226
227 UGL_LOCAL void loopEvent(void)
228 {
229 UGL_EVENT event;
230 UGL_INPUT_EVENT * pInputEvent;
231
232 UGL_FOREVER
233 {
234 if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
235 != UGL_STATUS_Q_EMPTY)
236 {
237 pInputEvent = (UGL_INPUT_EVENT *)&event;
238
239 if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
240 pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
241 readKey(pInputEvent->type.keyboard.key);
242 }
243
244 drawGL();
245 if (stopWex)
246 break;
247 }
248 }
249
250 UGL_LOCAL void initGL(void)
251 {
252 GLUquadricObj *q = gluNewQuadric();
253 CylinderObj = glGenLists(1);
254 glNewList(CylinderObj, GL_COMPILE);
255
256 glTranslatef(0.0, 0.0, -1.0);
257
258 /* cylinder */
259 gluQuadricNormals(q, GL_SMOOTH);
260 gluQuadricTexture(q, GL_TRUE);
261 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
262
263 /* end cap */
264 glTranslatef(0.0, 0.0, 2.0);
265 gluDisk(q, 0.0, 0.6, 24, 1);
266
267 /* other end cap */
268 glTranslatef(0.0, 0.0, -2.0);
269 gluQuadricOrientation(q, GLU_INSIDE);
270 gluDisk(q, 0.0, 0.6, 24, 1);
271
272 glEndList();
273 gluDeleteQuadric(q);
274
275 /* lighting */
276 glEnable(GL_LIGHTING);
277 {
278 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
279 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
280 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
281 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
282 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
283 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
284 glEnable(GL_LIGHT0);
285 }
286
287 /* fitering = nearest, initially */
288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
290
291 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
292 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
293
294 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
295 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
296
297 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB))
298 {
299 printf("Error: couldn't load texture image\n");
300 cleanUp();
301 exit(1);
302 }
303
304 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
305
306 glEnable(GL_LIGHTING);
307
308 glMatrixMode( GL_PROJECTION );
309 glLoadIdentity();
310 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
311 glMatrixMode( GL_MODELVIEW );
312 glLoadIdentity();
313 glTranslatef( 0.0, 0.0, -70.0 );
314
315 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
316 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
317 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
318 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
319
320 #ifdef COUNT_FRAMES
321 tickStart = tickGet ();
322 tickBySec = sysClkRateGet ();
323 #endif
324
325 }
326
327 UGL_LOCAL void cleanUp (void)
328 {
329 uglEventQDestroy (eventServiceId, qId);
330
331 uglMesaDestroyContext();
332 uglDeinitialize ();
333 }
334
335 void windMLTexCyl (UGL_BOOL windMLMode);
336
337 void ugltexcyl (void)
338 {
339 taskSpawn ("tTexCyl", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCyl,
340 UGL_FALSE,1,2,3,4,5,6,7,8,9);
341 }
342
343 void windMLTexCyl (UGL_BOOL windMLMode)
344 {
345 UGL_INPUT_DEVICE_ID keyboardDevId;
346 GLsizei displayWidth, displayHeight;
347 GLsizei x, y, w, h;
348
349 CylinderObj = 0;
350 Animate = GL_TRUE;
351 linearFilter = GL_FALSE;
352 Xrot = 0.0;
353 Yrot = 0.0;
354 Zrot = 0.0;
355 DXrot = 1.0;
356 DYrot = 2.5;
357 limit = 100;
358 count = 1;
359
360 uglInitialize ();
361
362 uglDriverFind (UGL_KEYBOARD_TYPE, 0,
363 (UGL_UINT32 *)&keyboardDevId);
364
365 uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
366
367 qId = uglEventQCreate (eventServiceId, 100);
368
369 /* Double buffering */
370 if (windMLMode)
371 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
372 | UGL_MESA_WINDML_EXCLUSIVE, NULL);
373 else
374 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
375
376 if (umc == NULL)
377 {
378 uglDeinitialize ();
379 return;
380 }
381
382 uglMesaMakeCurrentContext (umc, 0, 0, 1, 1);
383
384 uglMesaGetIntegerv(UGL_MESA_DISPLAY_WIDTH, &displayWidth);
385 uglMesaGetIntegerv(UGL_MESA_DISPLAY_HEIGHT, &displayHeight);
386
387 h = (displayHeight*3)/4;
388 w = h;
389 x = (displayWidth-w)/2;
390 y = (displayHeight-h)/2;
391
392 uglMesaMoveToWindow(x, y);
393 uglMesaResizeToWindow(w, h);
394
395 initGL ();
396
397 echoUse();
398
399 stopWex = UGL_FALSE;
400 loopEvent();
401
402 cleanUp();
403
404 return;
405 }
406