gallium/draw: initial code to properly support llvm in the draw module
[mesa.git] / progs / windml / uglolympic.c
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 /*
26 * Nov 20, 1995 use stdlib's rand()/srand() instead of random()/srand48(), etc.
27 */
28
29 /*
30 * Modified by Stephane Raimbault to be able to run in VxWorks 07/18/01
31 *
32 * Modified by Li Wei(liwei@aiar.xjtu.edu.cn) to be able to run in Windows
33 * 6/13
34 *
35 * Modified by Brian Paul to compile with Windows OR Unix. 7/23/97
36 */
37
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <math.h>
43
44 #include <ugl/ugl.h>
45 #include <ugl/uglevent.h>
46 #include <ugl/uglinput.h>
47 #include <ugl/uglucode.h>
48
49 #include <GL/uglmesa.h>
50 #include <GL/glu.h>
51
52 #ifndef RAND_MAX
53 # define RAND_MAX 32767
54 #endif
55
56 #define XSIZE 100
57 #define YSIZE 75
58
59 #define RINGS 5
60 #define BLUERING 0
61 #define BLACKRING 1
62 #define REDRING 2
63 #define YELLOWRING 3
64 #define GREENRING 4
65
66 #define BACKGROUND 8
67
68 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
69 UGL_LOCAL UGL_EVENT_Q_ID qId;
70 UGL_LOCAL UGL_MESA_CONTEXT umc;
71 UGL_LOCAL volatile UGL_BOOL stopWex;
72
73 UGL_LOCAL int rgb;
74 UGL_LOCAL unsigned char rgb_colors[RINGS][3];
75 UGL_LOCAL int mapped_colors[RINGS];
76 UGL_LOCAL float dests[RINGS][3];
77 UGL_LOCAL float offsets[RINGS][3];
78 UGL_LOCAL float angs[RINGS];
79 UGL_LOCAL float rotAxis[RINGS][3];
80 UGL_LOCAL int iters[RINGS];
81 UGL_LOCAL GLuint theTorus;
82
83 enum {
84 COLOR_BLACK = 0,
85 COLOR_RED,
86 COLOR_GREEN,
87 COLOR_YELLOW,
88 COLOR_BLUE,
89 COLOR_MAGENTA,
90 COLOR_CYAN,
91 COLOR_WHITE
92 };
93
94 /*
95 UGL_LOCAL float RGBMap[9][3] = {
96 {0, 0, 0},
97 {1, 0, 0},
98 {0, 1, 0},
99 {1, 1, 0},
100 {0, 0, 1},
101 {1, 0, 1},
102 {0, 1, 1},
103 {1, 1, 1},
104 {0.5, 0.5, 0.5}
105 };
106
107 UGL_LOCAL void SetColor(int c)
108 {
109 (rgb) ? glColor3fv(RGBMap[c]): glIndexf(c);
110 }
111
112 UGL_LOCAL void InitMap(void)
113 {
114 int i;
115
116 if (rgb)
117 return;
118
119 for (i = 0; i < 9; i++)
120 uglMesaSetColor(i, RGBMap[i][0], RGBMap[i][1], RGBMap[i][2]);
121 }
122
123 UGL_LOCAL void SetFogRamp(int density, int startIndex)
124 {
125 int fogValues, colorValues;
126 int i, j, k;
127 float intensity;
128
129 fogValues = 1 << density;
130 colorValues = 1 << startIndex;
131 for (i = 0; i < colorValues; i++)
132 {
133 for (j = 0; j < fogValues; j++)
134 {
135 k = i * fogValues + j;
136 intensity = (i * fogValues + j * colorValues) / 255.0;
137 uglMesaSetColor(k, intensity, intensity, intensity);
138 }
139 }
140 }
141
142 UGL_LOCAL void SetGreyRamp(void)
143 {
144 int i;
145 float intensity;
146
147 for (i = 0; i < 255; i++)
148 {
149 intensity = i / 255.0;
150 uglMesaSetColor(i, intensity, intensity, intensity);
151 }
152 }
153 */
154
155 UGL_LOCAL void FillTorus(float rc, int numc, float rt, int numt)
156 {
157 int i, j, k;
158 double s, t;
159 double x, y, z;
160 double pi, twopi;
161
162 pi = 3.14159265358979323846;
163 twopi = 2 * pi;
164
165 for (i = 0; i < numc; i++)
166 {
167 glBegin(GL_QUAD_STRIP);
168 for (j = 0; j <= numt; j++)
169 {
170 for (k = 1; k >= 0; k--)
171 {
172 s = (i + k) % numc + 0.5;
173 t = j % numt;
174
175 x = cos(t*twopi/numt) * cos(s*twopi/numc);
176 y = sin(t*twopi/numt) * cos(s*twopi/numc);
177 z = sin(s*twopi/numc);
178 glNormal3f(x, y, z);
179
180 x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
181 y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
182 z = rc * sin(s*twopi/numc);
183 glVertex3f(x, y, z);
184 }
185 }
186 glEnd();
187 }
188 }
189
190 UGL_LOCAL float Clamp(int iters_left, float t)
191 {
192 if (iters_left < 3)
193 {
194 return 0.0;
195 }
196 return (iters_left-2)*t/iters_left;
197 }
198
199 UGL_LOCAL void drawGL(void)
200 {
201 int i, j;
202
203 for (i = 0; i < RINGS; i++)
204 {
205 if (iters[i]) {
206 for (j = 0; j < 3; j++)
207 {
208 offsets[i][j] = Clamp(iters[i], offsets[i][j]);
209 }
210 angs[i] = Clamp(iters[i], angs[i]);
211 iters[i]--;
212 }
213 }
214
215 glPushMatrix();
216
217 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
218 gluLookAt(0,0,10, 0,0,0, 0,1,0);
219
220 for (i = 0; i < RINGS; i++)
221 {
222 if (rgb)
223 {
224 glColor3ubv(rgb_colors[i]);
225 }
226 else
227 {
228 glIndexi(mapped_colors[i]);
229 }
230 glPushMatrix();
231 glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
232 dests[i][2]+offsets[i][2]);
233 glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
234 glCallList(theTorus);
235 glPopMatrix();
236 }
237
238 glPopMatrix();
239
240 glFlush();
241
242 uglMesaSwapBuffers();
243 }
244
245 UGL_LOCAL float MyRand(void)
246 {
247 return 10.0 * ( (float) rand() / (float) RAND_MAX - 0.5 );
248 }
249
250 UGL_LOCAL void ReInit(void)
251 {
252 int i;
253 float deviation;
254
255 deviation = MyRand() / 2;
256 deviation = deviation * deviation;
257 for (i = 0; i < RINGS; i++)
258 {
259 offsets[i][0] = MyRand();
260 offsets[i][1] = MyRand();
261 offsets[i][2] = MyRand();
262 angs[i] = 260.0 * MyRand();
263 rotAxis[i][0] = MyRand();
264 rotAxis[i][1] = MyRand();
265 rotAxis[i][2] = MyRand();
266 iters[i] = (deviation * MyRand() + 60.0);
267 }
268 }
269
270 UGL_LOCAL void initGL(void)
271 {
272 float base, height;
273 float aspect, x, y;
274 int i;
275
276 float top_y = 1.0;
277 float bottom_y = 0.0;
278 float top_z = 0.15;
279 float bottom_z = 0.69;
280 float spacing = 2.5;
281 static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
282 static float lmodel_twoside[] = {GL_FALSE};
283 static float lmodel_local[] = {GL_FALSE};
284 static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
285 static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
286 static float light0_position[] = {0.8660254, 0.5, 1, 0};
287 static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
288 static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
289 static float bevel_mat_shininess[] = {40.0};
290 static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
291 static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
292
293 ReInit();
294
295 for (i = 0; i < RINGS; i++)
296 {
297 rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
298 }
299 rgb_colors[BLUERING][2] = 255;
300 rgb_colors[REDRING][0] = 255;
301 rgb_colors[GREENRING][1] = 255;
302 rgb_colors[YELLOWRING][0] = 255;
303 rgb_colors[YELLOWRING][1] = 255;
304 mapped_colors[BLUERING] = COLOR_BLUE;
305 mapped_colors[REDRING] = COLOR_RED;
306 mapped_colors[GREENRING] = COLOR_GREEN;
307 mapped_colors[YELLOWRING] = COLOR_YELLOW;
308 mapped_colors[BLACKRING] = COLOR_BLACK;
309
310 dests[BLUERING][0] = -spacing;
311 dests[BLUERING][1] = top_y;
312 dests[BLUERING][2] = top_z;
313
314 dests[BLACKRING][0] = 0.0;
315 dests[BLACKRING][1] = top_y;
316 dests[BLACKRING][2] = top_z;
317
318 dests[REDRING][0] = spacing;
319 dests[REDRING][1] = top_y;
320 dests[REDRING][2] = top_z;
321
322 dests[YELLOWRING][0] = -spacing / 2.0;
323 dests[YELLOWRING][1] = bottom_y;
324 dests[YELLOWRING][2] = bottom_z;
325
326 dests[GREENRING][0] = spacing / 2.0;
327 dests[GREENRING][1] = bottom_y;
328 dests[GREENRING][2] = bottom_z;
329
330 base = 2.0;
331 height = 2.0;
332 theTorus = glGenLists(1);
333 glNewList(theTorus, GL_COMPILE);
334 FillTorus(0.1, 8, 1.0, 25);
335 glEndList();
336
337 x = (float)XSIZE;
338 y = (float)YSIZE;
339 aspect = x / y;
340 glEnable(GL_CULL_FACE);
341 glCullFace(GL_BACK);
342 glEnable(GL_DEPTH_TEST);
343 glClearDepth(1.0);
344
345 if (rgb)
346 {
347 glClearColor(0.5, 0.5, 0.5, 0.0);
348 glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
349 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
350 glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
351 glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
352 glEnable(GL_LIGHT0);
353
354 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
355 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
356 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
357 glEnable(GL_LIGHTING);
358
359 glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
360 glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
361 glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
362 glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
363
364 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
365 glEnable(GL_COLOR_MATERIAL);
366 glShadeModel(GL_SMOOTH);
367 }
368 else
369 {
370 glClearIndex(BACKGROUND);
371 glShadeModel(GL_FLAT);
372 }
373
374 glMatrixMode(GL_PROJECTION);
375 gluPerspective(45, 1.33, 0.1, 100.0);
376 glMatrixMode(GL_MODELVIEW);
377 }
378
379 UGL_LOCAL void echoUse(void)
380 {
381 printf("tOlympic keys:\n");
382 printf(" SPACE Reinitialize\n");
383 printf(" ESC Exit\n");
384 }
385
386 UGL_LOCAL void readKey (UGL_WCHAR key)
387 {
388 switch(key)
389 {
390 case UGL_UNI_SPACE:
391 ReInit();
392 break;
393 case UGL_UNI_ESCAPE:
394 stopWex = 1;
395 break;
396 }
397 }
398
399 UGL_LOCAL void loopEvent(void)
400 {
401 UGL_EVENT event;
402 UGL_INPUT_EVENT * pInputEvent;
403
404 UGL_FOREVER
405 {
406 if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
407 != UGL_STATUS_Q_EMPTY)
408 {
409 pInputEvent = (UGL_INPUT_EVENT *)&event;
410
411 if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
412 pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
413 readKey(pInputEvent->type.keyboard.key);
414 }
415
416 drawGL();
417 if (stopWex)
418 break;
419 }
420 }
421
422 void windMLOlympic (UGL_BOOL windMLMode);
423
424 void uglolympic (void)
425 {
426 taskSpawn("tOlympic", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLOlympic,
427 0,1,2,3,4,5,6,7,8,9);
428 }
429
430 void windMLOlympic(UGL_BOOL windMLMode)
431 {
432 UGL_INPUT_DEVICE_ID keyboardDevId;
433
434 uglInitialize();
435
436 uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
437
438 if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
439 (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
440 {
441 qId = uglEventQCreate (eventServiceId, 100);
442 }
443 else
444 {
445 eventServiceId = UGL_NULL;
446 }
447
448 if (windMLMode)
449 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
450 | UGL_MESA_WINDML_EXCLUSIVE, NULL);
451 else
452 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
453
454 if (umc == NULL)
455 {
456 uglDeinitialize();
457 return;
458 }
459
460 uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
461 UGL_MESA_FULLSCREEN_HEIGHT);
462
463 uglMesaGetIntegerv(UGL_MESA_RGB, &rgb);
464
465 initGL();
466
467 echoUse();
468
469 stopWex = 0;
470 loopEvent();
471
472 if (eventServiceId != UGL_NULL)
473 uglEventQDestroy (eventServiceId, qId);
474
475 uglMesaDestroyContext();
476 uglDeinitialize();
477
478 return;
479 }