http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / hacks / glx / superquadrics.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* superquadrics --- 3D mathematical shapes */
3
4 #if 0
5 static const char sccsid[] = "@(#)superquadrics.c       4.07 97/11/24 xlockmore";
6 #endif
7
8 /*-
9  * Permission to use, copy, modify, and distribute this software and its
10  * documentation for any purpose and without fee is hereby granted,
11  * provided that the above copyright notice appear in all copies and that
12  * both that copyright notice and this permission notice appear in
13  * supporting documentation.
14  *
15  * This file is provided AS IS with no warranties of any kind.  The author
16  * shall have no liability with respect to the infringement of copyrights,
17  * trade secrets or any patents by this file or any part thereof.  In no
18  * event will the author be liable for any lost revenue or profits or
19  * other special, indirect and consequential damages.
20  *
21  * Superquadrics were invented by Dr. Alan Barr of Caltech University.
22  * They were first published in "Computer Graphics and Applications",
23  * volume 1, number 1, 1981, in the article "Superquadrics and Angle-
24  * Preserving Transformations."  Dr. Barr based the Superquadrics on
25  * Piet Hein's "super ellipses."  Super ellipses are like 2D ellipses,
26  * except that the formula includes an exponent, raising its X and Y
27  * values to a (fractional) power, and allowing them to gradually
28  * change from round to square edges.  Superquadrics extend this
29  * idea into 3 dimensions, using two exponents to modify a
30  * quadric surface in a similar fashion.
31  *
32  * Revision History:
33  * 30-Mar-97: Turned into a module for xlockmore 4.02 alpha.  The code
34  *    is almost unrecognizable now from the first revision, except for
35  *    a few remaining two-letter variable names.  I still don't have
36  *    the normal vectors working right (I wrote the buggy normal vector
37  *    code myself, can you tell?)
38  * 07-Jan-97: A legend reborn; Superquadrics make an appearance as a
39  *    real OpenGL program written in C.  I can even render them with
40  *    proper lighting and specular highlights.  Gee, they look almost
41  *    as good now as the original color plates of them that my uncle
42  *    showed me as a child in 1981.  I don't know what computer hardware
43  *    he was using at the time, but it's taken a couple decades for the
44  *    PC clone hardware to catch up to it.
45  * 05-Jan-97: After almost a decade, Superquadrics had almost faded away
46  *    into the myths and folklore of all the things my brother and I played
47  *    with on computers when we were kids with too much time on our hands.
48  *    I had since gotten involved in Unix, OpenGL, and other things.
49  *    A sudden flash of inspiration caused me to dig out the old Pascal
50  *    source code, run it through p2c, and start ripping away the old
51  *    wireframe rendering code, to be replaced by OpenGL.
52  * Late 1989 or early 1990:  Around this time I did the Turbo Pascal
53  *    port of the Superquadrics.  Unfortunately, many of the original variable
54  *    names remained the same from the C= 64 original.  This was unfortunate
55  *    because BASIC on the c64 only allowed 2-letter, global variable names.
56  *    But the speed improvement over BASIC was very impressive at the time.
57  * Thanksgiving, 1987: Written.  My uncle Al, who invented Superquadrics some
58  *    years earlier, came to visit us.  I was a high school kid at the time,
59  *    with nothing more than a Commodore 64.  Somehow we wrote this program,
60  *    (he did the math obviously, I just coded it into BASIC for the c64).
61  *    Yeah, 320x200 resolution, colorless white wireframe, and half an hour
62  *    rendering time per superquadric.  PLOT x,y.  THOSE were the days.
63  *    In the following years I would port Superquadrics to AppleBASIC,
64  *    AmigaBASIC, and then Turbo Pascal for IBM clones.  5 minutes on a 286!
65  *    Talk about fast rendering!  But these days, when my Pentium 166 runs
66  *    the same program, the superquadric will already be waiting on the
67  *    screen before my monitor can change frequency from text to graphics
68  *    mode.  Can't time the number of minutes that way!  Darn ;)
69  *
70  * Ed Mackey
71  */
72
73 #ifdef STANDALONE
74 # define DEFAULTS       "*delay:                40000   \n"                     \
75                                         "*count:                25      \n"                     \
76                                         "*cycles:               40      \n"                     \
77                                         "*showFPS:      False   \n"                     \
78                                         "*wireframe:    False   \n"
79
80 # define superquadrics_handle_event 0
81 # include "xlockmore.h"                         /* from the xscreensaver distribution */
82 #else  /* !STANDALONE */
83 # include "xlock.h"                                     /* from the xlockmore distribution */
84 #endif /* !STANDALONE */
85
86 #ifdef USE_GL
87
88 /*-
89  * Note for low-CPU-speed machines:  If your frame rate is so low that
90  * attempts at animation appear futile, try using "-cycles 1", which puts
91  * Superquadrics into kind of a slide-show mode.  It will still use up
92  * all of your CPU power, but it may look nicer.
93  */
94
95 #define DEF_SPINSPEED  "5.0"
96
97 static float spinspeed;
98
99 static XrmOptionDescRec opts[] =
100 {
101   {"-spinspeed", ".superquadrics.spinspeed", XrmoptionSepArg, 0}
102 };
103 static argtype vars[] =
104 {
105   {&spinspeed, "spinspeed", "Spinspeed", DEF_SPINSPEED, t_Float}
106 };
107 static OptionStruct desc[] =
108 {
109         {"-spinspeed num", "speed of rotation, in degrees per frame"}
110 };
111
112 ENTRYPOINT ModeSpecOpt superquadrics_opts =
113 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
114
115 #ifdef USE_MODULES
116 ModStruct   superquadrics_description =
117 {"superquadrics", "init_superquadrics", "draw_superquadrics", "release_superquadrics",
118  "refresh_superquadrics", "init_superquadrics", NULL, &superquadrics_opts,
119  1000, 25, 40, 1, 4, 1.0, "",
120  "Shows 3D mathematical shapes", 0, NULL};
121
122 #endif
123
124 #define MaxRes          50
125 #define MinRes          5
126
127 typedef double dimi[MaxRes + 1];
128
129 typedef struct {
130         double      xExponent, yExponent;
131         GLfloat     r[4], g[4], b[4];
132         long        Mode;
133         int         rotx, rotz;
134 } state;
135
136 typedef struct {
137         GLXContext *glx_context;
138         int         dist, wireframe, flatshade, shownorms, maxcount, maxwait;
139         int         counter, viewcount, viewwait, mono;
140         GLfloat     curmat[4][4], rotx, roty, rotz, spinspeed;
141         /* In dimi: the first letter stands for cosine/sine, the second
142          *          stands for North, South, East, or West.  I think.
143          */
144         dimi        cs, se, sw, sn, ss, ce, cw, cn, Prevxx, Prevyy, Prevzz,
145                     Prevxn, Prevyn, Prevzn;
146         double      xExponent, yExponent, Mode;
147         int         resolution;
148         state       now, later;
149
150     int         pats[4][4];
151         int             cullmode;
152
153 } superquadricsstruct;
154
155 static superquadricsstruct *superquadrics = NULL;
156
157 #define CLIP_NORMALS 10000.0
158
159 static void ReshapeSuperquadrics(int w, int h);
160
161 static int
162 myrand(int range)
163 {
164         return ((int) (((float) range) * LRAND() / (MAXRAND)));
165 }
166
167 static float
168 myrandreal(void)
169 {
170         return (LRAND() / (MAXRAND));
171 }
172
173 /* Some old, old, OLD code follows.  Ahh this takes me back..... */
174
175 /* Output from p2c, the Pascal-to-C translator */
176 /* From input file "squad.pas" */
177
178 static double
179 XtoY(double x, double y)
180 {
181         double      z, a;
182
183         /* This is NOT your typical raise-X-to-the-Y-power function.  Do not attempt
184          * to replace this with a standard exponent function.  If you must, just
185          * replace the "a = exp(y * log(z));" line with something faster.
186          */
187
188         z = fabs(x);
189         if (z < 1e-20) {
190                 a = 0.0;
191                 return a;
192         }
193         a = exp(y * log(z));
194         if (a > CLIP_NORMALS)
195                 a = CLIP_NORMALS;
196         if (x < 0)
197                 a = -a;
198         return a;
199 }
200
201
202 static double
203 Sine(double x, double e)
204 {
205         /* This is just the sine wave raised to the exponent.  BUT, you can't
206          * raise negative numbers to fractional exponents.  So we have a special
207          * XtoY routune which handles it in a way useful to superquadrics.
208          */
209
210         return (XtoY(sin(x), e));
211 }
212
213
214 static double
215 Cosine(double x, double e)
216 {
217         return (XtoY(cos(x), e));
218 }
219
220
221 static void
222 MakeUpStuff(int allstuff, superquadricsstruct * sp)
223 {
224         int         dostuff;
225         int         t, pat;
226         GLfloat     r, g, b, r2, g2, b2;
227
228         /* randomize it. */
229
230         if (sp->maxcount < 2)
231                 allstuff = 1;
232         dostuff = allstuff * 15;
233         if (!dostuff) {
234                 dostuff = myrand(3) + 1;
235                 if (myrand(2) || (dostuff & 1))
236                         dostuff |= 4;
237                 if (myrand(2))
238                         dostuff |= 8;
239         }
240         if (dostuff & 1) {
241                 sp->later.xExponent = (((long) floor(myrandreal() * 250 + 0.5)) / 100.0) + 0.1;
242                 sp->later.yExponent = (((long) floor(myrandreal() * 250 + 0.5)) / 100.0) + 0.1;
243
244                 /* Increase the 2.0 .. 2.5 range to 2.0 .. 3.0 */
245                 if (sp->later.xExponent > 2.0)
246                         sp->later.xExponent = (sp->later.xExponent * 2.0) - 2.0;
247                 if (sp->later.yExponent > 2.0)
248                         sp->later.yExponent = (sp->later.yExponent * 2.0) - 2.0;
249         }
250         if (dostuff & 2) {
251                 do {
252                         sp->later.Mode = myrand(3L) + 1;
253                 } while (!allstuff && (sp->later.Mode == sp->now.Mode));
254                 /* On init: make sure it can stay in mode 1 if it feels like it. */
255         }
256         if (dostuff & 4) {
257                 if (sp->mono) {
258                         if (sp->wireframe) {
259                                 b = g = r = 1.0;
260                                 b2 = g2 = r2 = 1.0;
261                         } else {
262                                 b = g = r = (GLfloat) (140 + myrand(100)) / 255.0;
263                                 b2 = g2 = r2 = ((r > 0.69) ? (1.0 - r) : r);
264                         }
265                 } else {
266                         r = (GLfloat) (40 + myrand(200)) / 255.0;
267                         g = (GLfloat) (40 + myrand(200)) / 255.0;
268                         b = (GLfloat) (40 + myrand(200)) / 255.0;
269
270                         r2 = ((myrand(4) && ((r < 0.31) || (r > 0.69))) ? (1.0 - r) : r);
271                         g2 = ((myrand(4) && ((g < 0.31) || (g > 0.69))) ? (1.0 - g) : g);
272                         b2 = ((myrand(4) && ((b < 0.31) || (b > 0.69))) ? (1.0 - b) : b);
273                 }
274
275                 pat = myrand(4);
276                 for (t = 0; t < 4; ++t) {
277                         sp->later.r[t] = sp->pats[pat][t] ? r : r2;
278                         sp->later.g[t] = sp->pats[pat][t] ? g : g2;
279                         sp->later.b[t] = sp->pats[pat][t] ? b : b2;
280                 }
281         }
282         if (dostuff & 8) {
283                 sp->later.rotx = myrand(360) - 180;
284                 sp->later.rotz = myrand(160) - 80;
285         }
286 }
287
288 static void
289 inputs(superquadricsstruct * sp)
290 {
291         int         iv;
292         double      u, v, mode3, cn3, inverter2, flatu, flatv;
293
294         if (sp->Mode < 1.000001) {
295                 mode3 = 1.0;
296                 cn3 = 0.0;
297                 inverter2 = 1.0;
298         } else if (sp->Mode < 2.000001) {
299                 mode3 = 1.0;
300                 cn3 = (sp->Mode - 1.0) * 1.5;
301                 inverter2 = (sp->Mode - 1.0) * -2.0 + 1.0;
302         } else {
303                 mode3 = (sp->Mode - 1.0);
304                 cn3 = (sp->Mode - 2.0) / 2.0 + 1.5;
305                 inverter2 = -1.0;
306         }
307
308         if (sp->flatshade) {
309                 flatu = M_PI / (sp->resolution - 1);
310                 flatv = mode3 * M_PI / ((sp->resolution - 1) * 2);
311         } else {
312                 flatu = flatv = 0.0;
313         }
314
315         /* (void) printf("Calculating....\n"); */
316         for (iv = 1; iv <= sp->resolution; iv++) {
317
318                 /* u ranges from PI down to -PI */
319                 u = (1 - iv) * 2 * M_PI / (sp->resolution - 1) + M_PI;
320
321                 /* v ranges from PI/2 down to -PI/2 */
322                 v = (1 - iv) * mode3 * M_PI / (sp->resolution - 1) + M_PI * (mode3 / 2.0);
323
324                 /* Use of xExponent */
325                 sp->se[iv] = Sine(u, sp->xExponent);
326                 sp->ce[iv] = Cosine(u, sp->xExponent);
327                 sp->sn[iv] = Sine(v, sp->yExponent);
328                 sp->cn[iv] = Cosine(v, sp->yExponent) * inverter2 + cn3;
329
330                 /* Normal vector computations only */
331                 sp->sw[iv] = Sine(u + flatu, 2 - sp->xExponent);
332                 sp->cw[iv] = Cosine(u + flatu, 2 - sp->xExponent);
333                 sp->ss[iv] = Sine(v + flatv, 2 - sp->yExponent) * inverter2;
334                 sp->cs[iv] = Cosine(v + flatv, 2 - sp->yExponent);
335         }                       /* next */
336
337         /* Now fix up the endpoints */
338         sp->se[sp->resolution] = sp->se[1];
339         sp->ce[sp->resolution] = sp->ce[1];
340
341         if (sp->Mode > 2.999999) {
342                 sp->sn[sp->resolution] = sp->sn[1];
343                 sp->cn[sp->resolution] = sp->cn[1];
344         }
345 }
346
347
348 static int
349 DoneScale(superquadricsstruct * sp)
350 {
351         double      xx, yy, zz, xp = 0, yp = 0, zp = 0, xn, yn, zn, xnp = 0,
352                     ynp = 0, znp = 0;
353         int         ih, iv;
354     int polys = 0;
355
356         /* Hey don't knock my 2-letter variable names.  Simon's BASIC rules, man! ;-> */
357         /* Just kidding..... */
358         int         toggle = 0;
359
360         for (ih = 1; ih <= sp->resolution; ih++) {
361                 toggle ^= 2;
362                 for (iv = 1; iv <= sp->resolution; iv++) {
363                         toggle ^= 1;
364                         if (sp->wireframe)
365                                 glColor3f(sp->curmat[toggle][0], sp->curmat[toggle][1], sp->curmat[toggle][2]);
366                         else
367                                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sp->curmat[toggle]);
368
369                         xx = sp->cn[iv] * sp->ce[ih];
370                         zz = sp->cn[iv] * sp->se[ih];
371                         yy = sp->sn[iv];
372
373                         if (sp->wireframe) {
374                                 if ((ih > 1) || (iv > 1)) {
375                                         glBegin(GL_LINES);
376                                         if (ih > 1) {
377                                                 glVertex3f(xx, yy, zz);
378                                                 glVertex3f(sp->Prevxx[iv], sp->Prevyy[iv], sp->Prevzz[iv]);
379                         polys++;
380                                         }
381                                         if (iv > 1) {
382                                                 glVertex3f(xx, yy, zz);
383                                                 glVertex3f(sp->Prevxx[iv - 1], sp->Prevyy[iv - 1], sp->Prevzz[iv - 1]);
384                         polys++;
385                                         }
386 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
387    * MesaGL 2.2 and -mono.  This has been fixed in MesaGL 2.3 and later. */
388                                         glEnd();
389                                 }
390                         } else {
391                                 if ((sp->cs[iv] > 1e+10) || (sp->cs[iv] < -1e+10)) {
392                                         xn = sp->cs[iv];
393                                         zn = sp->cs[iv];
394                                         yn = sp->ss[iv];
395                                 } else {
396                                         xn = sp->cs[iv] * sp->cw[ih];
397                                         zn = sp->cs[iv] * sp->sw[ih];
398                                         yn = sp->ss[iv];
399                                 }
400                                 if ((ih > 1) && (iv > 1)) {
401                                         glNormal3f(xn, yn, zn);
402                                         glBegin(GL_POLYGON);
403                                         glVertex3f(xx, yy, zz);
404                                         if (!sp->flatshade)
405                                                 glNormal3f(sp->Prevxn[iv], sp->Prevyn[iv], sp->Prevzn[iv]);
406                                         glVertex3f(sp->Prevxx[iv], sp->Prevyy[iv], sp->Prevzz[iv]);
407                                         if (!sp->flatshade)
408                                                 glNormal3f(xnp, ynp, znp);
409                                         glVertex3f(xp, yp, zp);
410                                         if (!sp->flatshade)
411                                                 glNormal3f(sp->Prevxn[iv - 1], sp->Prevyn[iv - 1], sp->Prevzn[iv - 1]);
412                                         glVertex3f(sp->Prevxx[iv - 1], sp->Prevyy[iv - 1], sp->Prevzz[iv - 1]);
413                     polys++;
414                                         glEnd();
415                                 }
416                                 if (sp->shownorms) {
417                                         if (!sp->flatshade)
418                                                 glShadeModel(GL_FLAT);
419                                         glDisable(GL_LIGHTING);
420                                         glBegin(GL_LINES);
421                                         glVertex3f(xx, yy, zz);
422                                         glVertex3f(xx + xn, yy + yn, zz + zn);
423                     polys++;
424                                         glEnd();
425                                         if (!sp->flatshade)
426                                                 glShadeModel(GL_SMOOTH);
427                                         glEnable(GL_LIGHTING);
428                                 }
429                                 xnp = sp->Prevxn[iv];
430                                 ynp = sp->Prevyn[iv];
431                                 znp = sp->Prevzn[iv];
432                                 sp->Prevxn[iv] = xn;
433                                 sp->Prevyn[iv] = yn;
434                                 sp->Prevzn[iv] = zn;
435                         }
436
437                         xp = sp->Prevxx[iv];
438                         yp = sp->Prevyy[iv];
439                         zp = sp->Prevzz[iv];
440                         sp->Prevxx[iv] = xx;
441                         sp->Prevyy[iv] = yy;
442                         sp->Prevzz[iv] = zz;
443
444                 }               /* next */
445         }                       /* next */
446     return polys;
447 }
448
449 /**** End of really old code ****/
450
451 static void
452 SetCull(int init, superquadricsstruct * sp)
453 {
454         if (init) {
455         glDisable(GL_CULL_FACE);
456                 sp->cullmode = 0;
457                 return;
458         }
459         if (sp->Mode < 1.0001) {
460                 if (sp->cullmode != 1) {
461                         glEnable(GL_CULL_FACE);
462                         glCullFace(GL_BACK);
463                         sp->cullmode = 1;
464                 }
465         } else if (sp->Mode > 2.9999) {
466                 if (sp->cullmode != 2) {
467                         glEnable(GL_CULL_FACE);
468                         glCullFace(GL_FRONT);
469                         sp->cullmode = 2;
470                 }
471         } else {
472                 if (sp->cullmode) {
473                         glDisable(GL_CULL_FACE);
474                         sp->cullmode = 0;
475                 }
476         }
477 }
478
479 static void
480 SetCurrentShape(superquadricsstruct * sp)
481 {
482         int         t;
483
484         sp->xExponent = sp->now.xExponent = sp->later.xExponent;
485         sp->yExponent = sp->now.yExponent = sp->later.yExponent;
486
487         for (t = 0; t < 4; ++t) {
488                 sp->curmat[t][0] = sp->now.r[t] = sp->later.r[t];
489                 sp->curmat[t][1] = sp->now.g[t] = sp->later.g[t];
490                 sp->curmat[t][2] = sp->now.b[t] = sp->later.b[t];
491         }
492
493         sp->Mode = (double) (sp->now.Mode = sp->later.Mode);
494         sp->rotx = sp->now.rotx = sp->later.rotx;
495         sp->rotz = sp->now.rotz = sp->later.rotz;
496
497         sp->counter = -sp->maxwait;
498
499         inputs(sp);
500 }
501
502 static void
503 NextSuperquadric(superquadricsstruct * sp)
504 {
505         double      fnow, flater;
506         int         t;
507
508         sp->roty -= sp->spinspeed;
509         while (sp->roty >= 360.0)
510                 sp->roty -= 360.0;
511         while (sp->roty < 0.0)
512                 sp->roty += 360.0;
513
514         --sp->viewcount;
515
516         if (sp->counter > 0) {
517                 if (--sp->counter == 0) {
518                         SetCurrentShape(sp);
519                         if (sp->counter == 0) {         /* Happens if sp->maxwait == 0 */
520                                 MakeUpStuff(0, sp);
521                                 sp->counter = sp->maxcount;
522                         }
523                 } else {
524                         fnow = (double) sp->counter / (double) sp->maxcount;
525                         flater = (double) (sp->maxcount - sp->counter) / (double) sp->maxcount;
526                         sp->xExponent = sp->now.xExponent * fnow + sp->later.xExponent * flater;
527                         sp->yExponent = sp->now.yExponent * fnow + sp->later.yExponent * flater;
528
529                         for (t = 0; t < 4; ++t) {
530                                 sp->curmat[t][0] = sp->now.r[t] * fnow + sp->later.r[t] * flater;
531                                 sp->curmat[t][1] = sp->now.g[t] * fnow + sp->later.g[t] * flater;
532                                 sp->curmat[t][2] = sp->now.b[t] * fnow + sp->later.b[t] * flater;
533                         }
534
535                         sp->Mode = (double) sp->now.Mode * fnow + (double) sp->later.Mode * flater;
536                         sp->rotx = (double) sp->now.rotx * fnow + (double) sp->later.rotx * flater;
537                         sp->rotz = (double) sp->now.rotz * fnow + (double) sp->later.rotz * flater;
538
539                         inputs(sp);
540                 }
541         } else {
542                 if (++sp->counter >= 0) {
543                         MakeUpStuff(0, sp);
544                         sp->counter = sp->maxcount;
545                 }
546         }
547 }
548
549 static int
550 DisplaySuperquadrics(superquadricsstruct * sp)
551 {
552     int polys = 0;
553         glDrawBuffer(GL_BACK);
554         if (sp->wireframe)
555                 glClear(GL_COLOR_BUFFER_BIT);
556         else
557                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
558
559         if (sp->viewcount < 1) {
560                 sp->viewcount = sp->viewwait;
561 /*              ReshapeSuperquadrics(-1, -1);*/
562         }
563         glPushMatrix();
564         glTranslatef(0.0, 0.0, -((GLfloat) (sp->dist) / 16.0) - (sp->Mode * 3.0 - 1.0));        /* viewing transform  */
565         glRotatef(sp->rotx, 1.0, 0.0, 0.0);     /* pitch */
566         glRotatef(sp->rotz, 0.0, 0.0, 1.0);     /* bank */
567         glRotatef(sp->roty, 0.0, 1.0, 0.0);     /* "spin", like heading but comes after P & B */
568
569         SetCull(0, sp);
570
571         polys = DoneScale(sp);
572
573         glPopMatrix();
574
575         /* Remember to flush & swap the buffers after calling this function! */
576     return polys;
577 }
578
579 static int
580 NextSuperquadricDisplay(superquadricsstruct * sp)
581 {
582         NextSuperquadric(sp);
583         return DisplaySuperquadrics(sp);
584 }
585
586 #define MINSIZE 200
587 static void
588 ReshapeSuperquadrics(int w, int h)
589 {
590 #if 0
591         int         maxsize, cursize;
592
593         maxsize = (w < h) ? w : h;
594         if (maxsize <= MINSIZE) {
595                 cursize = maxsize;
596         } else {
597                 cursize = myrand(maxsize - MINSIZE) + MINSIZE;
598         }
599         if ((w > cursize) && (h > cursize)) {
600                 glViewport(myrand(w - cursize), myrand(h - cursize), cursize, cursize);
601                 w = h = cursize;
602         } else {
603                 glViewport(0, 0, w, h);
604         }
605 #else
606     glViewport(0, 0, w, h);
607 #endif
608
609         glMatrixMode(GL_PROJECTION);
610         glLoadIdentity();
611         gluPerspective(15.0, (GLfloat) w / (GLfloat) h, 0.1, 200.0);
612         glMatrixMode(GL_MODELVIEW);
613         glLoadIdentity();
614 }
615
616 static void
617 InitSuperquadrics(int wfmode, int snorm, int res, int count, float speed, superquadricsstruct * sp)
618 {
619         GLfloat     ambient[] =
620         {0.4, 0.4, 0.4, 1.0};
621         GLfloat     position[] =
622         {10.0, 1.0, 1.0, 10.0};
623         GLfloat     mat_diffuse[] =
624         {1.0, 0.5, 0.5, 1.0};
625         GLfloat     mat_specular[] =
626         {0.8, 0.8, 0.8, 1.0};
627         GLfloat     mat_shininess[] =
628         {50.0};
629
630         int         t;
631
632         for (t = 0; t < 4; ++t) {
633                 sp->curmat[t][0] = 0.0;
634                 sp->curmat[t][1] = 0.0;
635                 sp->curmat[t][2] = 0.0;
636                 sp->curmat[t][3] = 1.0;
637     }
638
639         sp->rotx = 35.0;
640         sp->roty = 0.0;
641         sp->rotz = 0.0;
642         sp->dist = (16 << 3);
643         sp->wireframe = sp->flatshade = sp->shownorms = 0;
644         sp->maxcount = count;
645         if (sp->maxcount < 1)
646                 sp->maxcount = 1;
647         sp->maxwait = sp->maxcount >> 1;
648         SetCull(1, sp);
649
650     sp->mono = 0;
651         sp->spinspeed = speed;
652         sp->viewcount = sp->viewwait = (sp->maxcount < 2) ? 1 : (sp->maxcount << 3);
653
654         if (res < MinRes)
655                 res = MinRes;
656         if (res > MaxRes)
657                 res = MaxRes;
658         sp->resolution = res;
659
660         if (wfmode == 2)
661                 sp->flatshade = 1;
662         else if (wfmode)
663                 sp->wireframe = 1;
664
665         if (snorm)
666                 sp->shownorms = 1;
667
668     glClearDepth(1.0);
669
670         if (sp->wireframe) {
671                 glShadeModel(GL_FLAT);
672                 glDisable(GL_LIGHTING);
673                 glColor3f(mat_diffuse[0], mat_diffuse[1], mat_diffuse[2]);
674         } else {
675                 if (sp->flatshade) {
676                         glShadeModel(GL_FLAT);
677                         position[0] = 1.0;
678                         position[3] = 0.0;
679                 }
680                 glEnable(GL_LIGHTING);
681                 glEnable(GL_LIGHT0);
682                 glDepthFunc(GL_LEQUAL);
683                 glEnable(GL_DEPTH_TEST);
684
685                 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
686                 glLightfv(GL_LIGHT0, GL_POSITION, position);
687
688                 /*glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse); */
689                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
690                 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
691
692                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
693
694                 glFrontFace(GL_CW);
695                 glEnable(GL_NORMALIZE);
696         }
697
698         MakeUpStuff(1, sp);
699         SetCurrentShape(sp);
700         MakeUpStuff(1, sp);     /* Initialize it */
701         sp->counter = sp->maxcount;
702 }
703
704 /* End of superquadrics main functions */
705
706 ENTRYPOINT void
707 init_superquadrics(ModeInfo * mi)
708 {
709         Display    *display = MI_DISPLAY(mi);
710         Window      window = MI_WINDOW(mi);
711         int         screen = MI_SCREEN(mi);
712
713         superquadricsstruct *sp;
714
715         if (superquadrics == NULL) {
716                 if ((superquadrics = (superquadricsstruct *) calloc(MI_NUM_SCREENS(mi),
717                                       sizeof (superquadricsstruct))) == NULL)
718                         return;
719         }
720         sp = &superquadrics[screen];
721         sp->mono = (MI_IS_MONO(mi) ? 1 : 0);
722
723     sp->pats[1][1] = 1;
724     sp->pats[1][3] = 1;
725     sp->pats[2][2] = 1;
726     sp->pats[2][3] = 1;
727     sp->pats[3][1] = 1;
728     sp->pats[3][2] = 1;
729
730 /*              {0, 0, 0, 0},
731                 {0, 1, 0, 1},
732                 {0, 0, 1, 1},
733                 {0, 1, 1, 0}
734  */
735
736         if ((sp->glx_context = init_GL(mi)) != NULL) {
737
738                 InitSuperquadrics(MI_IS_WIREFRAME(mi), 0,
739                                   MI_COUNT(mi), MI_CYCLES(mi), spinspeed, sp);
740                 ReshapeSuperquadrics(MI_WIDTH(mi), MI_HEIGHT(mi));
741
742                 DisplaySuperquadrics(sp);
743                 glFinish();
744                 glXSwapBuffers(display, window);
745         } else {
746                 MI_CLEARWINDOW(mi);
747         }
748 }
749
750 ENTRYPOINT void
751 draw_superquadrics(ModeInfo * mi)
752 {
753         superquadricsstruct *sp = &superquadrics[MI_SCREEN(mi)];
754         Display    *display = MI_DISPLAY(mi);
755         Window      window = MI_WINDOW(mi);
756
757         if (!sp->glx_context)
758                 return;
759
760         glXMakeCurrent(display, window, *(sp->glx_context));
761
762     mi->polygon_count = NextSuperquadricDisplay(sp);
763
764     if (mi->fps_p) do_fps (mi);
765         glFinish();
766         glXSwapBuffers(display, window);
767 }
768
769 ENTRYPOINT void
770 refresh_superquadrics(ModeInfo * mi)
771 {
772         /* Nothing happens here */
773 }
774
775 ENTRYPOINT void
776 reshape_superquadrics(ModeInfo * mi, int width, int height)
777 {
778   ReshapeSuperquadrics(MI_WIDTH(mi), MI_HEIGHT(mi));
779 }
780
781 ENTRYPOINT void
782 release_superquadrics(ModeInfo * mi)
783 {
784         if (superquadrics != NULL) {
785                 (void) free((void *) superquadrics);
786                 superquadrics = NULL;
787         }
788         FreeAllGL(mi);
789 }
790
791
792 #endif
793
794 /* End of superquadrics.c */
795
796 XSCREENSAVER_MODULE ("Superquadrics", superquadrics)