http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[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 # define superquadrics_handle_event 0
80 # include "xlockmore.h"                         /* from the xscreensaver distribution */
81 #else  /* !STANDALONE */
82 # include "xlock.h"                                     /* from the xlockmore distribution */
83 #endif /* !STANDALONE */
84
85 #ifdef USE_GL
86
87 /*-
88  * Note for low-CPU-speed machines:  If your frame rate is so low that
89  * attempts at animation appear futile, try using "-cycles 1", which puts
90  * Superquadrics into kind of a slide-show mode.  It will still use up
91  * all of your CPU power, but it may look nicer.
92  */
93
94 #define DEF_SPINSPEED  "5.0"
95
96 static float spinspeed;
97
98 static XrmOptionDescRec opts[] =
99 {
100   {"-spinspeed", ".superquadrics.spinspeed", XrmoptionSepArg, 0}
101 };
102 static argtype vars[] =
103 {
104   {&spinspeed, "spinspeed", "Spinspeed", DEF_SPINSPEED, t_Float}
105 };
106 static OptionStruct desc[] =
107 {
108         {"-spinspeed num", "speed of rotation, in degrees per frame"}
109 };
110
111 ENTRYPOINT ModeSpecOpt superquadrics_opts =
112 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
113
114 #ifdef USE_MODULES
115 ModStruct   superquadrics_description =
116 {"superquadrics", "init_superquadrics", "draw_superquadrics", "release_superquadrics",
117  "refresh_superquadrics", "init_superquadrics", NULL, &superquadrics_opts,
118  1000, 25, 40, 1, 4, 1.0, "",
119  "Shows 3D mathematical shapes", 0, NULL};
120
121 #endif
122
123 #define MaxRes          50
124 #define MinRes          5
125
126 typedef double dimi[MaxRes + 1];
127
128 typedef struct {
129         double      xExponent, yExponent;
130         GLfloat     r[4], g[4], b[4];
131         long        Mode;
132         int         rotx, rotz;
133 } state;
134
135 typedef struct {
136         GLXContext *glx_context;
137         int         dist, wireframe, flatshade, shownorms, maxcount, maxwait;
138         int         counter, viewcount, viewwait, mono;
139         GLfloat     curmat[4][4], rotx, roty, rotz, spinspeed;
140         /* In dimi: the first letter stands for cosine/sine, the second
141          *          stands for North, South, East, or West.  I think.
142          */
143         dimi        cs, se, sw, sn, ss, ce, cw, cn, Prevxx, Prevyy, Prevzz,
144                     Prevxn, Prevyn, Prevzn;
145         double      xExponent, yExponent, Mode;
146         int         resolution;
147         state       now, later;
148
149     int         pats[4][4];
150         int             cullmode;
151
152 } superquadricsstruct;
153
154 static superquadricsstruct *superquadrics = NULL;
155
156 #define CLIP_NORMALS 10000.0
157
158 static void ReshapeSuperquadrics(int w, int h);
159
160 static int
161 myrand(int range)
162 {
163         return ((int) (((float) range) * LRAND() / (MAXRAND)));
164 }
165
166 static float
167 myrandreal(void)
168 {
169         return (LRAND() / (MAXRAND));
170 }
171
172 /* Some old, old, OLD code follows.  Ahh this takes me back..... */
173
174 /* Output from p2c, the Pascal-to-C translator */
175 /* From input file "squad.pas" */
176
177 static double
178 XtoY(double x, double y)
179 {
180         double      z, a;
181
182         /* This is NOT your typical raise-X-to-the-Y-power function.  Do not attempt
183          * to replace this with a standard exponent function.  If you must, just
184          * replace the "a = exp(y * log(z));" line with something faster.
185          */
186
187         z = fabs(x);
188         if (z < 1e-20) {
189                 a = 0.0;
190                 return a;
191         }
192         a = exp(y * log(z));
193         if (a > CLIP_NORMALS)
194                 a = CLIP_NORMALS;
195         if (x < 0)
196                 a = -a;
197         return a;
198 }
199
200
201 static double
202 Sine(double x, double e)
203 {
204         /* This is just the sine wave raised to the exponent.  BUT, you can't
205          * raise negative numbers to fractional exponents.  So we have a special
206          * XtoY routune which handles it in a way useful to superquadrics.
207          */
208
209         return (XtoY(sin(x), e));
210 }
211
212
213 static double
214 Cosine(double x, double e)
215 {
216         return (XtoY(cos(x), e));
217 }
218
219
220 static void
221 MakeUpStuff(int allstuff, superquadricsstruct * sp)
222 {
223         int         dostuff;
224         int         t, pat;
225         GLfloat     r, g, b, r2, g2, b2;
226
227         /* randomize it. */
228
229         if (sp->maxcount < 2)
230                 allstuff = 1;
231         dostuff = allstuff * 15;
232         if (!dostuff) {
233                 dostuff = myrand(3) + 1;
234                 if (myrand(2) || (dostuff & 1))
235                         dostuff |= 4;
236                 if (myrand(2))
237                         dostuff |= 8;
238         }
239         if (dostuff & 1) {
240                 sp->later.xExponent = (((long) floor(myrandreal() * 250 + 0.5)) / 100.0) + 0.1;
241                 sp->later.yExponent = (((long) floor(myrandreal() * 250 + 0.5)) / 100.0) + 0.1;
242
243                 /* Increase the 2.0 .. 2.5 range to 2.0 .. 3.0 */
244                 if (sp->later.xExponent > 2.0)
245                         sp->later.xExponent = (sp->later.xExponent * 2.0) - 2.0;
246                 if (sp->later.yExponent > 2.0)
247                         sp->later.yExponent = (sp->later.yExponent * 2.0) - 2.0;
248         }
249         if (dostuff & 2) {
250                 do {
251                         sp->later.Mode = myrand(3L) + 1;
252                 } while (!allstuff && (sp->later.Mode == sp->now.Mode));
253                 /* On init: make sure it can stay in mode 1 if it feels like it. */
254         }
255         if (dostuff & 4) {
256                 if (sp->mono) {
257                         if (sp->wireframe) {
258                                 b = g = r = 1.0;
259                                 b2 = g2 = r2 = 1.0;
260                         } else {
261                                 b = g = r = (GLfloat) (140 + myrand(100)) / 255.0;
262                                 b2 = g2 = r2 = ((r > 0.69) ? (1.0 - r) : r);
263                         }
264                 } else {
265                         r = (GLfloat) (40 + myrand(200)) / 255.0;
266                         g = (GLfloat) (40 + myrand(200)) / 255.0;
267                         b = (GLfloat) (40 + myrand(200)) / 255.0;
268
269                         r2 = ((myrand(4) && ((r < 0.31) || (r > 0.69))) ? (1.0 - r) : r);
270                         g2 = ((myrand(4) && ((g < 0.31) || (g > 0.69))) ? (1.0 - g) : g);
271                         b2 = ((myrand(4) && ((b < 0.31) || (b > 0.69))) ? (1.0 - b) : b);
272                 }
273
274                 pat = myrand(4);
275                 for (t = 0; t < 4; ++t) {
276                         sp->later.r[t] = sp->pats[pat][t] ? r : r2;
277                         sp->later.g[t] = sp->pats[pat][t] ? g : g2;
278                         sp->later.b[t] = sp->pats[pat][t] ? b : b2;
279                 }
280         }
281         if (dostuff & 8) {
282                 sp->later.rotx = myrand(360) - 180;
283                 sp->later.rotz = myrand(160) - 80;
284         }
285 }
286
287 static void
288 inputs(superquadricsstruct * sp)
289 {
290         int         iv;
291         double      u, v, mode3, cn3, inverter2, flatu, flatv;
292
293         if (sp->Mode < 1.000001) {
294                 mode3 = 1.0;
295                 cn3 = 0.0;
296                 inverter2 = 1.0;
297         } else if (sp->Mode < 2.000001) {
298                 mode3 = 1.0;
299                 cn3 = (sp->Mode - 1.0) * 1.5;
300                 inverter2 = (sp->Mode - 1.0) * -2.0 + 1.0;
301         } else {
302                 mode3 = (sp->Mode - 1.0);
303                 cn3 = (sp->Mode - 2.0) / 2.0 + 1.5;
304                 inverter2 = -1.0;
305         }
306
307         if (sp->flatshade) {
308                 flatu = M_PI / (sp->resolution - 1);
309                 flatv = mode3 * M_PI / ((sp->resolution - 1) * 2);
310         } else {
311                 flatu = flatv = 0.0;
312         }
313
314         /* (void) printf("Calculating....\n"); */
315         for (iv = 1; iv <= sp->resolution; iv++) {
316
317                 /* u ranges from PI down to -PI */
318                 u = (1 - iv) * 2 * M_PI / (sp->resolution - 1) + M_PI;
319
320                 /* v ranges from PI/2 down to -PI/2 */
321                 v = (1 - iv) * mode3 * M_PI / (sp->resolution - 1) + M_PI * (mode3 / 2.0);
322
323                 /* Use of xExponent */
324                 sp->se[iv] = Sine(u, sp->xExponent);
325                 sp->ce[iv] = Cosine(u, sp->xExponent);
326                 sp->sn[iv] = Sine(v, sp->yExponent);
327                 sp->cn[iv] = Cosine(v, sp->yExponent) * inverter2 + cn3;
328
329                 /* Normal vector computations only */
330                 sp->sw[iv] = Sine(u + flatu, 2 - sp->xExponent);
331                 sp->cw[iv] = Cosine(u + flatu, 2 - sp->xExponent);
332                 sp->ss[iv] = Sine(v + flatv, 2 - sp->yExponent) * inverter2;
333                 sp->cs[iv] = Cosine(v + flatv, 2 - sp->yExponent);
334         }                       /* next */
335
336         /* Now fix up the endpoints */
337         sp->se[sp->resolution] = sp->se[1];
338         sp->ce[sp->resolution] = sp->ce[1];
339
340         if (sp->Mode > 2.999999) {
341                 sp->sn[sp->resolution] = sp->sn[1];
342                 sp->cn[sp->resolution] = sp->cn[1];
343         }
344 }
345
346
347 static void
348 DoneScale(superquadricsstruct * sp)
349 {
350         double      xx, yy, zz, xp = 0, yp = 0, zp = 0, xn, yn, zn, xnp = 0,
351                     ynp = 0, znp = 0;
352         int         ih, iv;
353
354         /* Hey don't knock my 2-letter variable names.  Simon's BASIC rules, man! ;-> */
355         /* Just kidding..... */
356         int         toggle = 0;
357
358         for (ih = 1; ih <= sp->resolution; ih++) {
359                 toggle ^= 2;
360                 for (iv = 1; iv <= sp->resolution; iv++) {
361                         toggle ^= 1;
362                         if (sp->wireframe)
363                                 glColor3f(sp->curmat[toggle][0], sp->curmat[toggle][1], sp->curmat[toggle][2]);
364                         else
365                                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sp->curmat[toggle]);
366
367                         xx = sp->cn[iv] * sp->ce[ih];
368                         zz = sp->cn[iv] * sp->se[ih];
369                         yy = sp->sn[iv];
370
371                         if (sp->wireframe) {
372                                 if ((ih > 1) || (iv > 1)) {
373                                         glBegin(GL_LINES);
374                                         if (ih > 1) {
375                                                 glVertex3f(xx, yy, zz);
376                                                 glVertex3f(sp->Prevxx[iv], sp->Prevyy[iv], sp->Prevzz[iv]);
377                                         }
378                                         if (iv > 1) {
379                                                 glVertex3f(xx, yy, zz);
380                                                 glVertex3f(sp->Prevxx[iv - 1], sp->Prevyy[iv - 1], sp->Prevzz[iv - 1]);
381                                         }
382 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
383    * MesaGL 2.2 and -mono.  This has been fixed in MesaGL 2.3 and later. */
384                                         glEnd();
385                                 }
386                         } else {
387                                 if ((sp->cs[iv] > 1e+10) || (sp->cs[iv] < -1e+10)) {
388                                         xn = sp->cs[iv];
389                                         zn = sp->cs[iv];
390                                         yn = sp->ss[iv];
391                                 } else {
392                                         xn = sp->cs[iv] * sp->cw[ih];
393                                         zn = sp->cs[iv] * sp->sw[ih];
394                                         yn = sp->ss[iv];
395                                 }
396                                 if ((ih > 1) && (iv > 1)) {
397                                         glNormal3f(xn, yn, zn);
398                                         glBegin(GL_POLYGON);
399                                         glVertex3f(xx, yy, zz);
400                                         if (!sp->flatshade)
401                                                 glNormal3f(sp->Prevxn[iv], sp->Prevyn[iv], sp->Prevzn[iv]);
402                                         glVertex3f(sp->Prevxx[iv], sp->Prevyy[iv], sp->Prevzz[iv]);
403                                         if (!sp->flatshade)
404                                                 glNormal3f(xnp, ynp, znp);
405                                         glVertex3f(xp, yp, zp);
406                                         if (!sp->flatshade)
407                                                 glNormal3f(sp->Prevxn[iv - 1], sp->Prevyn[iv - 1], sp->Prevzn[iv - 1]);
408                                         glVertex3f(sp->Prevxx[iv - 1], sp->Prevyy[iv - 1], sp->Prevzz[iv - 1]);
409                                         glEnd();
410                                 }
411                                 if (sp->shownorms) {
412                                         if (!sp->flatshade)
413                                                 glShadeModel(GL_FLAT);
414                                         glDisable(GL_LIGHTING);
415                                         glBegin(GL_LINES);
416                                         glVertex3f(xx, yy, zz);
417                                         glVertex3f(xx + xn, yy + yn, zz + zn);
418                                         glEnd();
419                                         if (!sp->flatshade)
420                                                 glShadeModel(GL_SMOOTH);
421                                         glEnable(GL_LIGHTING);
422                                 }
423                                 xnp = sp->Prevxn[iv];
424                                 ynp = sp->Prevyn[iv];
425                                 znp = sp->Prevzn[iv];
426                                 sp->Prevxn[iv] = xn;
427                                 sp->Prevyn[iv] = yn;
428                                 sp->Prevzn[iv] = zn;
429                         }
430
431                         xp = sp->Prevxx[iv];
432                         yp = sp->Prevyy[iv];
433                         zp = sp->Prevzz[iv];
434                         sp->Prevxx[iv] = xx;
435                         sp->Prevyy[iv] = yy;
436                         sp->Prevzz[iv] = zz;
437
438                 }               /* next */
439         }                       /* next */
440 }
441
442 /**** End of really old code ****/
443
444 static void
445 SetCull(int init, superquadricsstruct * sp)
446 {
447         if (init) {
448         glDisable(GL_CULL_FACE);
449                 sp->cullmode = 0;
450                 return;
451         }
452         if (sp->Mode < 1.0001) {
453                 if (sp->cullmode != 1) {
454                         glEnable(GL_CULL_FACE);
455                         glCullFace(GL_BACK);
456                         sp->cullmode = 1;
457                 }
458         } else if (sp->Mode > 2.9999) {
459                 if (sp->cullmode != 2) {
460                         glEnable(GL_CULL_FACE);
461                         glCullFace(GL_FRONT);
462                         sp->cullmode = 2;
463                 }
464         } else {
465                 if (sp->cullmode) {
466                         glDisable(GL_CULL_FACE);
467                         sp->cullmode = 0;
468                 }
469         }
470 }
471
472 static void
473 SetCurrentShape(superquadricsstruct * sp)
474 {
475         int         t;
476
477         sp->xExponent = sp->now.xExponent = sp->later.xExponent;
478         sp->yExponent = sp->now.yExponent = sp->later.yExponent;
479
480         for (t = 0; t < 4; ++t) {
481                 sp->curmat[t][0] = sp->now.r[t] = sp->later.r[t];
482                 sp->curmat[t][1] = sp->now.g[t] = sp->later.g[t];
483                 sp->curmat[t][2] = sp->now.b[t] = sp->later.b[t];
484         }
485
486         sp->Mode = (double) (sp->now.Mode = sp->later.Mode);
487         sp->rotx = sp->now.rotx = sp->later.rotx;
488         sp->rotz = sp->now.rotz = sp->later.rotz;
489
490         sp->counter = -sp->maxwait;
491
492         inputs(sp);
493 }
494
495 static void
496 NextSuperquadric(superquadricsstruct * sp)
497 {
498         double      fnow, flater;
499         int         t;
500
501         sp->roty -= sp->spinspeed;
502         while (sp->roty >= 360.0)
503                 sp->roty -= 360.0;
504         while (sp->roty < 0.0)
505                 sp->roty += 360.0;
506
507         --sp->viewcount;
508
509         if (sp->counter > 0) {
510                 if (--sp->counter == 0) {
511                         SetCurrentShape(sp);
512                         if (sp->counter == 0) {         /* Happens if sp->maxwait == 0 */
513                                 MakeUpStuff(0, sp);
514                                 sp->counter = sp->maxcount;
515                         }
516                 } else {
517                         fnow = (double) sp->counter / (double) sp->maxcount;
518                         flater = (double) (sp->maxcount - sp->counter) / (double) sp->maxcount;
519                         sp->xExponent = sp->now.xExponent * fnow + sp->later.xExponent * flater;
520                         sp->yExponent = sp->now.yExponent * fnow + sp->later.yExponent * flater;
521
522                         for (t = 0; t < 4; ++t) {
523                                 sp->curmat[t][0] = sp->now.r[t] * fnow + sp->later.r[t] * flater;
524                                 sp->curmat[t][1] = sp->now.g[t] * fnow + sp->later.g[t] * flater;
525                                 sp->curmat[t][2] = sp->now.b[t] * fnow + sp->later.b[t] * flater;
526                         }
527
528                         sp->Mode = (double) sp->now.Mode * fnow + (double) sp->later.Mode * flater;
529                         sp->rotx = (double) sp->now.rotx * fnow + (double) sp->later.rotx * flater;
530                         sp->rotz = (double) sp->now.rotz * fnow + (double) sp->later.rotz * flater;
531
532                         inputs(sp);
533                 }
534         } else {
535                 if (++sp->counter >= 0) {
536                         MakeUpStuff(0, sp);
537                         sp->counter = sp->maxcount;
538                 }
539         }
540 }
541
542 static void
543 DisplaySuperquadrics(superquadricsstruct * sp)
544 {
545         glDrawBuffer(GL_BACK);
546         if (sp->wireframe)
547                 glClear(GL_COLOR_BUFFER_BIT);
548         else
549                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
550
551         if (sp->viewcount < 1) {
552                 sp->viewcount = sp->viewwait;
553 /*              ReshapeSuperquadrics(-1, -1);*/
554         }
555         glPushMatrix();
556         glTranslatef(0.0, 0.0, -((GLfloat) (sp->dist) / 16.0) - (sp->Mode * 3.0 - 1.0));        /* viewing transform  */
557         glRotatef(sp->rotx, 1.0, 0.0, 0.0);     /* pitch */
558         glRotatef(sp->rotz, 0.0, 0.0, 1.0);     /* bank */
559         glRotatef(sp->roty, 0.0, 1.0, 0.0);     /* "spin", like heading but comes after P & B */
560
561         SetCull(0, sp);
562
563         DoneScale(sp);
564
565         glPopMatrix();
566
567         /* Remember to flush & swap the buffers after calling this function! */
568 }
569
570 static void
571 NextSuperquadricDisplay(superquadricsstruct * sp)
572 {
573         NextSuperquadric(sp);
574         DisplaySuperquadrics(sp);
575 }
576
577 #define MINSIZE 200
578 static void
579 ReshapeSuperquadrics(int w, int h)
580 {
581 #if 0
582         int         maxsize, cursize;
583
584         maxsize = (w < h) ? w : h;
585         if (maxsize <= MINSIZE) {
586                 cursize = maxsize;
587         } else {
588                 cursize = myrand(maxsize - MINSIZE) + MINSIZE;
589         }
590         if ((w > cursize) && (h > cursize)) {
591                 glViewport(myrand(w - cursize), myrand(h - cursize), cursize, cursize);
592                 w = h = cursize;
593         } else {
594                 glViewport(0, 0, w, h);
595         }
596 #else
597     glViewport(0, 0, w, h);
598 #endif
599
600         glMatrixMode(GL_PROJECTION);
601         glLoadIdentity();
602         gluPerspective(15.0, (GLfloat) w / (GLfloat) h, 0.1, 200.0);
603         glMatrixMode(GL_MODELVIEW);
604         glLoadIdentity();
605 }
606
607 static void
608 InitSuperquadrics(int wfmode, int snorm, int res, int count, float speed, superquadricsstruct * sp)
609 {
610         GLfloat     ambient[] =
611         {0.4, 0.4, 0.4, 1.0};
612         GLfloat     position[] =
613         {10.0, 1.0, 1.0, 10.0};
614         GLfloat     mat_diffuse[] =
615         {1.0, 0.5, 0.5, 1.0};
616         GLfloat     mat_specular[] =
617         {0.8, 0.8, 0.8, 1.0};
618         GLfloat     mat_shininess[] =
619         {50.0};
620
621         int         t;
622
623         for (t = 0; t < 4; ++t) {
624                 sp->curmat[t][0] = 0.0;
625                 sp->curmat[t][1] = 0.0;
626                 sp->curmat[t][2] = 0.0;
627                 sp->curmat[t][3] = 1.0;
628     }
629
630         sp->rotx = 35.0;
631         sp->roty = 0.0;
632         sp->rotz = 0.0;
633         sp->dist = (16 << 3);
634         sp->wireframe = sp->flatshade = sp->shownorms = 0;
635         sp->maxcount = count;
636         if (sp->maxcount < 1)
637                 sp->maxcount = 1;
638         sp->maxwait = sp->maxcount >> 1;
639         SetCull(1, sp);
640
641     sp->mono = 0;
642         sp->spinspeed = speed;
643         sp->viewcount = sp->viewwait = (sp->maxcount < 2) ? 1 : (sp->maxcount << 3);
644
645         if (res < MinRes)
646                 res = MinRes;
647         if (res > MaxRes)
648                 res = MaxRes;
649         sp->resolution = res;
650
651         if (wfmode == 2)
652                 sp->flatshade = 1;
653         else if (wfmode)
654                 sp->wireframe = 1;
655
656         if (snorm)
657                 sp->shownorms = 1;
658
659     glClearColor(0.0,0.0,0.0,1.0);
660     glClearDepth(1.0);
661
662         if (sp->wireframe) {
663                 glShadeModel(GL_FLAT);
664                 glDisable(GL_LIGHTING);
665                 glColor3f(mat_diffuse[0], mat_diffuse[1], mat_diffuse[2]);
666         } else {
667                 if (sp->flatshade) {
668                         glShadeModel(GL_FLAT);
669                         position[0] = 1.0;
670                         position[3] = 0.0;
671                 }
672                 glEnable(GL_LIGHTING);
673                 glEnable(GL_LIGHT0);
674                 glDepthFunc(GL_LEQUAL);
675                 glEnable(GL_DEPTH_TEST);
676
677                 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
678                 glLightfv(GL_LIGHT0, GL_POSITION, position);
679
680                 /*glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse); */
681                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
682                 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
683
684                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
685
686                 glFrontFace(GL_CW);
687                 glEnable(GL_NORMALIZE);
688         }
689
690         MakeUpStuff(1, sp);
691         SetCurrentShape(sp);
692         MakeUpStuff(1, sp);     /* Initialize it */
693         sp->counter = sp->maxcount;
694 }
695
696 /* End of superquadrics main functions */
697
698 ENTRYPOINT void
699 init_superquadrics(ModeInfo * mi)
700 {
701         Display    *display = MI_DISPLAY(mi);
702         Window      window = MI_WINDOW(mi);
703         int         screen = MI_SCREEN(mi);
704
705         superquadricsstruct *sp;
706
707         if (superquadrics == NULL) {
708                 if ((superquadrics = (superquadricsstruct *) calloc(MI_NUM_SCREENS(mi),
709                                       sizeof (superquadricsstruct))) == NULL)
710                         return;
711         }
712         sp = &superquadrics[screen];
713         sp->mono = (MI_IS_MONO(mi) ? 1 : 0);
714
715     sp->pats[1][1] = 1;
716     sp->pats[1][3] = 1;
717     sp->pats[2][2] = 1;
718     sp->pats[2][3] = 1;
719     sp->pats[3][1] = 1;
720     sp->pats[3][2] = 1;
721
722 /*              {0, 0, 0, 0},
723                 {0, 1, 0, 1},
724                 {0, 0, 1, 1},
725                 {0, 1, 1, 0}
726  */
727
728         if ((sp->glx_context = init_GL(mi)) != NULL) {
729
730                 InitSuperquadrics(MI_IS_WIREFRAME(mi), 0,
731                                   MI_COUNT(mi), MI_CYCLES(mi), spinspeed, sp);
732                 ReshapeSuperquadrics(MI_WIDTH(mi), MI_HEIGHT(mi));
733
734                 DisplaySuperquadrics(sp);
735                 glFinish();
736                 glXSwapBuffers(display, window);
737         } else {
738                 MI_CLEARWINDOW(mi);
739         }
740 }
741
742 ENTRYPOINT void
743 draw_superquadrics(ModeInfo * mi)
744 {
745         superquadricsstruct *sp = &superquadrics[MI_SCREEN(mi)];
746         Display    *display = MI_DISPLAY(mi);
747         Window      window = MI_WINDOW(mi);
748
749         if (!sp->glx_context)
750                 return;
751
752         glXMakeCurrent(display, window, *(sp->glx_context));
753
754         NextSuperquadricDisplay(sp);
755
756     if (mi->fps_p) do_fps (mi);
757         glFinish();
758         glXSwapBuffers(display, window);
759 }
760
761 ENTRYPOINT void
762 refresh_superquadrics(ModeInfo * mi)
763 {
764         /* Nothing happens here */
765 }
766
767 ENTRYPOINT void
768 reshape_superquadrics(ModeInfo * mi, int width, int height)
769 {
770   ReshapeSuperquadrics(MI_WIDTH(mi), MI_HEIGHT(mi));
771 }
772
773 ENTRYPOINT void
774 release_superquadrics(ModeInfo * mi)
775 {
776         if (superquadrics != NULL) {
777                 (void) free((void *) superquadrics);
778                 superquadrics = NULL;
779         }
780         FreeAllGL(mi);
781 }
782
783
784 #endif
785
786 /* End of superquadrics.c */
787
788 XSCREENSAVER_MODULE ("Superquadrics", superquadrics)