st/mesa: fix incorrect RowStride computation
[mesa.git] / progs / samples / olympic.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 Li Wei(liwei@aiar.xjtu.edu.cn) to be able to run in Windows
31 * 6/13
32 *
33 * Modified by Brian Paul to compile with Windows OR Unix. 7/23/97
34 */
35
36
37 #define _HPUX_SOURCE
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <math.h>
43 #include <GL/glut.h>
44
45 #ifndef RAND_MAX
46 # define RAND_MAX 32767
47 #endif
48
49
50 #define XSIZE 100
51 #define YSIZE 75
52
53 #define RINGS 5
54 #define BLUERING 0
55 #define BLACKRING 1
56 #define REDRING 2
57 #define YELLOWRING 3
58 #define GREENRING 4
59
60 #define BACKGROUND 8
61
62
63 GLenum rgb, doubleBuffer;
64
65 #include "tkmap.c"
66
67 unsigned char rgb_colors[RINGS][3];
68 int mapped_colors[RINGS];
69 float dests[RINGS][3];
70 float offsets[RINGS][3];
71 float angs[RINGS];
72 float rotAxis[RINGS][3];
73 int iters[RINGS];
74 GLuint theTorus;
75
76
77 static void FillTorus(float rc, int numc, float rt, int numt)
78 {
79 int i, j, k;
80 double s, t;
81 double x, y, z;
82 double pi, twopi;
83
84 pi = 3.14159265358979323846;
85 twopi = 2 * pi;
86
87 for (i = 0; i < numc; i++) {
88 glBegin(GL_QUAD_STRIP);
89 for (j = 0; j <= numt; j++) {
90 for (k = 1; k >= 0; k--) {
91 s = (i + k) % numc + 0.5;
92 t = j % numt;
93
94 x = cos(t*twopi/numt) * cos(s*twopi/numc);
95 y = sin(t*twopi/numt) * cos(s*twopi/numc);
96 z = sin(s*twopi/numc);
97 glNormal3f(x, y, z);
98
99 x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
100 y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
101 z = rc * sin(s*twopi/numc);
102 glVertex3f(x, y, z);
103 }
104 }
105 glEnd();
106 }
107 }
108
109 static float Clamp(int iters_left, float t)
110 {
111 if (iters_left < 3) {
112 return 0.0;
113 }
114 return (iters_left-2)*t/iters_left;
115 }
116
117 static void DrawScene(void)
118 {
119 int i, j;
120 GLboolean goIdle;
121 static double t0 = -1.;
122 double t, dt;
123 t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
124 if (t0 < 0.)
125 t0 = t;
126 dt = t - t0;
127
128 if (dt < 1./30.)
129 return;
130
131 t0 = t;
132
133 goIdle = GL_TRUE;
134 for (i = 0; i < RINGS; i++) {
135 if (iters[i]) {
136 for (j = 0; j < 3; j++) {
137 offsets[i][j] = Clamp(iters[i], offsets[i][j]);
138 }
139 angs[i] = Clamp(iters[i], angs[i]);
140 iters[i]--;
141 goIdle = GL_FALSE;
142 }
143 }
144 if (goIdle) {
145 glutIdleFunc(NULL);
146 }
147
148 glPushMatrix();
149
150 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
151 gluLookAt(0,0,10, 0,0,0, 0,1,0);
152
153 for (i = 0; i < RINGS; i++) {
154 if (rgb) {
155 glColor3ubv(rgb_colors[i]);
156 } else {
157 glIndexi(mapped_colors[i]);
158 }
159 glPushMatrix();
160 glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
161 dests[i][2]+offsets[i][2]);
162 glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
163 glCallList(theTorus);
164 glPopMatrix();
165 }
166
167 glPopMatrix();
168
169 glFlush();
170 if (doubleBuffer) {
171 glutSwapBuffers();
172 }
173 }
174
175 static float MyRand(void)
176 {
177 return 10.0 * ( (float) rand() / (float) RAND_MAX - 0.5 );
178 }
179
180 #if !defined(GLUTCALLBACK)
181 #define GLUTCALLBACK
182 #endif
183
184 static void GLUTCALLBACK glut_post_redisplay_p(void)
185 {
186 glutPostRedisplay();
187 }
188
189 static void ReInit(void)
190 {
191 int i;
192 float deviation;
193
194 deviation = MyRand() / 2;
195 deviation = deviation * deviation;
196 for (i = 0; i < RINGS; i++) {
197 offsets[i][0] = MyRand();
198 offsets[i][1] = MyRand();
199 offsets[i][2] = MyRand();
200 angs[i] = 260.0 * MyRand();
201 rotAxis[i][0] = MyRand();
202 rotAxis[i][1] = MyRand();
203 rotAxis[i][2] = MyRand();
204 iters[i] = (deviation * MyRand() + 60.0);
205 }
206 glutIdleFunc(glut_post_redisplay_p);
207 }
208
209 static void Init(void)
210 {
211 float base, height;
212 float aspect, x, y;
213 int i;
214
215 float top_y = 1.0;
216 float bottom_y = 0.0;
217 float top_z = 0.15;
218 float bottom_z = 0.69;
219 float spacing = 2.5;
220 static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
221 static float lmodel_twoside[] = {GL_FALSE};
222 static float lmodel_local[] = {GL_FALSE};
223 static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
224 static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
225 static float light0_position[] = {0.8660254, 0.5, 1, 0};
226 static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
227 static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
228 static float bevel_mat_shininess[] = {40.0};
229 static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
230 static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
231
232 srand( (unsigned int) glutGet(GLUT_ELAPSED_TIME) );
233
234 ReInit();
235 for (i = 0; i < RINGS; i++) {
236 rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
237 }
238 rgb_colors[BLUERING][2] = 255;
239 rgb_colors[REDRING][0] = 255;
240 rgb_colors[GREENRING][1] = 255;
241 rgb_colors[YELLOWRING][0] = 255;
242 rgb_colors[YELLOWRING][1] = 255;
243 mapped_colors[BLUERING] = COLOR_BLUE;
244 mapped_colors[REDRING] = COLOR_RED;
245 mapped_colors[GREENRING] = COLOR_GREEN;
246 mapped_colors[YELLOWRING] = COLOR_YELLOW;
247 mapped_colors[BLACKRING] = COLOR_BLACK;
248
249 dests[BLUERING][0] = -spacing;
250 dests[BLUERING][1] = top_y;
251 dests[BLUERING][2] = top_z;
252
253 dests[BLACKRING][0] = 0.0;
254 dests[BLACKRING][1] = top_y;
255 dests[BLACKRING][2] = top_z;
256
257 dests[REDRING][0] = spacing;
258 dests[REDRING][1] = top_y;
259 dests[REDRING][2] = top_z;
260
261 dests[YELLOWRING][0] = -spacing / 2.0;
262 dests[YELLOWRING][1] = bottom_y;
263 dests[YELLOWRING][2] = bottom_z;
264
265 dests[GREENRING][0] = spacing / 2.0;
266 dests[GREENRING][1] = bottom_y;
267 dests[GREENRING][2] = bottom_z;
268
269 base = 2.0;
270 height = 2.0;
271 theTorus = glGenLists(1);
272 glNewList(theTorus, GL_COMPILE);
273 FillTorus(0.1, 8, 1.0, 25);
274 glEndList();
275
276 x = (float)XSIZE;
277 y = (float)YSIZE;
278 aspect = x / y;
279 glEnable(GL_CULL_FACE);
280 glCullFace(GL_BACK);
281 glEnable(GL_DEPTH_TEST);
282 glClearDepth(1.0);
283
284 if (rgb) {
285 glClearColor(0.5, 0.5, 0.5, 0.0);
286 glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
287 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
288 glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
289 glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
290 glEnable(GL_LIGHT0);
291
292 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
293 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
294 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
295 glEnable(GL_LIGHTING);
296
297 glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
298 glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
299 glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
300 glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
301
302 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
303 glEnable(GL_COLOR_MATERIAL);
304 glShadeModel(GL_SMOOTH);
305 } else {
306 glClearIndex(BACKGROUND);
307 glShadeModel(GL_FLAT);
308 }
309
310 glMatrixMode(GL_PROJECTION);
311 gluPerspective(45, 1.33, 0.1, 100.0);
312 glMatrixMode(GL_MODELVIEW);
313 }
314
315 static void Reshape(int width, int height)
316 {
317
318 glViewport(0, 0, width, height);
319 }
320
321 static void Key(unsigned char key, int x, int y)
322 {
323
324 switch (key) {
325 case 27:
326 exit(1);
327 case 32:
328 ReInit();
329 break;
330 }
331 }
332
333 static GLenum Args(int argc, char **argv)
334 {
335 GLint i;
336
337 rgb = GL_TRUE;
338 doubleBuffer = GL_TRUE;
339
340 for (i = 1; i < argc; i++) {
341 if (strcmp(argv[i], "-ci") == 0) {
342 rgb = GL_FALSE;
343 } else if (strcmp(argv[i], "-rgb") == 0) {
344 rgb = GL_TRUE;
345 } else if (strcmp(argv[i], "-sb") == 0) {
346 doubleBuffer = GL_FALSE;
347 } else if (strcmp(argv[i], "-db") == 0) {
348 doubleBuffer = GL_TRUE;
349 } else {
350 printf("%s (Bad option).\n", argv[i]);
351 return GL_FALSE;
352 }
353 }
354 return GL_TRUE;
355 }
356
357 int main(int argc, char **argv)
358 {
359 GLenum type;
360
361 glutInit(&argc, argv);
362
363 if (Args(argc, argv) == GL_FALSE) {
364 exit(1);
365 }
366
367 glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 300);
368
369 type = GLUT_DEPTH;
370 type |= (rgb) ? GLUT_RGB : GLUT_INDEX;
371 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
372 glutInitDisplayMode(type);
373
374 if (glutCreateWindow("Olympic") == GL_FALSE) {
375 exit(1);
376 }
377
378 InitMap();
379
380 Init();
381
382 glutReshapeFunc(Reshape);
383 glutKeyboardFunc(Key);
384 glutDisplayFunc(DrawScene);
385 glutIdleFunc(glut_post_redisplay_p);
386
387 glutMainLoop();
388 return 0;
389 }