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