1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* superquadrics --- 3D mathematical shapes */
5 static const char sccsid[] = "@(#)superquadrics.c 4.07 97/11/24 xlockmore";
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.
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.
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.
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 ;)
74 # define DEFAULTS "*delay: 40000 \n" \
77 "*showFPS: False \n" \
78 "*wireframe: False \n"
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 */
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.
95 #define DEF_SPINSPEED "5.0"
97 static float spinspeed;
99 static XrmOptionDescRec opts[] =
101 {"-spinspeed", ".superquadrics.spinspeed", XrmoptionSepArg, 0}
103 static argtype vars[] =
105 {&spinspeed, "spinspeed", "Spinspeed", DEF_SPINSPEED, t_Float}
107 static OptionStruct desc[] =
109 {"-spinspeed num", "speed of rotation, in degrees per frame"}
112 ENTRYPOINT ModeSpecOpt superquadrics_opts =
113 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
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};
127 typedef double dimi[MaxRes + 1];
130 double xExponent, yExponent;
131 GLfloat r[4], g[4], b[4];
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.
144 dimi cs, se, sw, sn, ss, ce, cw, cn, Prevxx, Prevyy, Prevzz,
145 Prevxn, Prevyn, Prevzn;
146 double xExponent, yExponent, Mode;
153 } superquadricsstruct;
155 static superquadricsstruct *superquadrics = NULL;
157 #define CLIP_NORMALS 10000.0
159 static void ReshapeSuperquadrics(int w, int h);
164 return ((int) (((float) range) * LRAND() / (MAXRAND)));
170 return (LRAND() / (MAXRAND));
173 /* Some old, old, OLD code follows. Ahh this takes me back..... */
175 /* Output from p2c, the Pascal-to-C translator */
176 /* From input file "squad.pas" */
179 XtoY(double x, double y)
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.
194 if (a > CLIP_NORMALS)
203 Sine(double x, double e)
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.
210 return (XtoY(sin(x), e));
215 Cosine(double x, double e)
217 return (XtoY(cos(x), e));
222 MakeUpStuff(int allstuff, superquadricsstruct * sp)
226 GLfloat r, g, b, r2, g2, b2;
230 if (sp->maxcount < 2)
232 dostuff = allstuff * 15;
234 dostuff = myrand(3) + 1;
235 if (myrand(2) || (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;
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;
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. */
262 b = g = r = (GLfloat) (140 + myrand(100)) / 255.0;
263 b2 = g2 = r2 = ((r > 0.69) ? (1.0 - r) : r);
266 r = (GLfloat) (40 + myrand(200)) / 255.0;
267 g = (GLfloat) (40 + myrand(200)) / 255.0;
268 b = (GLfloat) (40 + myrand(200)) / 255.0;
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);
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;
283 sp->later.rotx = myrand(360) - 180;
284 sp->later.rotz = myrand(160) - 80;
289 inputs(superquadricsstruct * sp)
292 double u, v, mode3, cn3, inverter2, flatu, flatv;
294 if (sp->Mode < 1.000001) {
298 } else if (sp->Mode < 2.000001) {
300 cn3 = (sp->Mode - 1.0) * 1.5;
301 inverter2 = (sp->Mode - 1.0) * -2.0 + 1.0;
303 mode3 = (sp->Mode - 1.0);
304 cn3 = (sp->Mode - 2.0) / 2.0 + 1.5;
309 flatu = M_PI / (sp->resolution - 1);
310 flatv = mode3 * M_PI / ((sp->resolution - 1) * 2);
315 /* (void) printf("Calculating....\n"); */
316 for (iv = 1; iv <= sp->resolution; iv++) {
318 /* u ranges from PI down to -PI */
319 u = (1 - iv) * 2 * M_PI / (sp->resolution - 1) + M_PI;
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);
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;
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);
337 /* Now fix up the endpoints */
338 sp->se[sp->resolution] = sp->se[1];
339 sp->ce[sp->resolution] = sp->ce[1];
341 if (sp->Mode > 2.999999) {
342 sp->sn[sp->resolution] = sp->sn[1];
343 sp->cn[sp->resolution] = sp->cn[1];
349 DoneScale(superquadricsstruct * sp)
351 double xx, yy, zz, xp = 0, yp = 0, zp = 0, xn, yn, zn, xnp = 0,
356 /* Hey don't knock my 2-letter variable names. Simon's BASIC rules, man! ;-> */
357 /* Just kidding..... */
360 for (ih = 1; ih <= sp->resolution; ih++) {
362 for (iv = 1; iv <= sp->resolution; iv++) {
365 glColor3f(sp->curmat[toggle][0], sp->curmat[toggle][1], sp->curmat[toggle][2]);
367 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sp->curmat[toggle]);
369 xx = sp->cn[iv] * sp->ce[ih];
370 zz = sp->cn[iv] * sp->se[ih];
374 if ((ih > 1) || (iv > 1)) {
377 glVertex3f(xx, yy, zz);
378 glVertex3f(sp->Prevxx[iv], sp->Prevyy[iv], sp->Prevzz[iv]);
382 glVertex3f(xx, yy, zz);
383 glVertex3f(sp->Prevxx[iv - 1], sp->Prevyy[iv - 1], sp->Prevzz[iv - 1]);
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. */
391 if ((sp->cs[iv] > 1e+10) || (sp->cs[iv] < -1e+10)) {
396 xn = sp->cs[iv] * sp->cw[ih];
397 zn = sp->cs[iv] * sp->sw[ih];
400 if ((ih > 1) && (iv > 1)) {
401 glNormal3f(xn, yn, zn);
403 glVertex3f(xx, yy, zz);
405 glNormal3f(sp->Prevxn[iv], sp->Prevyn[iv], sp->Prevzn[iv]);
406 glVertex3f(sp->Prevxx[iv], sp->Prevyy[iv], sp->Prevzz[iv]);
408 glNormal3f(xnp, ynp, znp);
409 glVertex3f(xp, yp, zp);
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]);
418 glShadeModel(GL_FLAT);
419 glDisable(GL_LIGHTING);
421 glVertex3f(xx, yy, zz);
422 glVertex3f(xx + xn, yy + yn, zz + zn);
426 glShadeModel(GL_SMOOTH);
427 glEnable(GL_LIGHTING);
429 xnp = sp->Prevxn[iv];
430 ynp = sp->Prevyn[iv];
431 znp = sp->Prevzn[iv];
449 /**** End of really old code ****/
452 SetCull(int init, superquadricsstruct * sp)
455 glDisable(GL_CULL_FACE);
459 if (sp->Mode < 1.0001) {
460 if (sp->cullmode != 1) {
461 glEnable(GL_CULL_FACE);
465 } else if (sp->Mode > 2.9999) {
466 if (sp->cullmode != 2) {
467 glEnable(GL_CULL_FACE);
468 glCullFace(GL_FRONT);
473 glDisable(GL_CULL_FACE);
480 SetCurrentShape(superquadricsstruct * sp)
484 sp->xExponent = sp->now.xExponent = sp->later.xExponent;
485 sp->yExponent = sp->now.yExponent = sp->later.yExponent;
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];
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;
497 sp->counter = -sp->maxwait;
503 NextSuperquadric(superquadricsstruct * sp)
508 sp->roty -= sp->spinspeed;
509 while (sp->roty >= 360.0)
511 while (sp->roty < 0.0)
516 if (sp->counter > 0) {
517 if (--sp->counter == 0) {
519 if (sp->counter == 0) { /* Happens if sp->maxwait == 0 */
521 sp->counter = sp->maxcount;
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;
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;
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;
542 if (++sp->counter >= 0) {
544 sp->counter = sp->maxcount;
550 DisplaySuperquadrics(superquadricsstruct * sp)
553 glDrawBuffer(GL_BACK);
555 glClear(GL_COLOR_BUFFER_BIT);
557 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
559 if (sp->viewcount < 1) {
560 sp->viewcount = sp->viewwait;
561 /* ReshapeSuperquadrics(-1, -1);*/
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 */
571 glScalef(0.7, 0.7, 0.7); /* jwz: scale it down a bit */
572 polys = DoneScale(sp);
576 /* Remember to flush & swap the buffers after calling this function! */
581 NextSuperquadricDisplay(superquadricsstruct * sp)
583 NextSuperquadric(sp);
584 return DisplaySuperquadrics(sp);
589 ReshapeSuperquadrics(int w, int h)
592 int maxsize, cursize;
594 maxsize = (w < h) ? w : h;
595 if (maxsize <= MINSIZE) {
598 cursize = myrand(maxsize - MINSIZE) + MINSIZE;
600 if ((w > cursize) && (h > cursize)) {
601 glViewport(myrand(w - cursize), myrand(h - cursize), cursize, cursize);
604 glViewport(0, 0, w, h);
607 glViewport(0, 0, w, h);
610 glMatrixMode(GL_PROJECTION);
612 gluPerspective(15.0, (GLfloat) w / (GLfloat) h, 0.1, 200.0);
613 glMatrixMode(GL_MODELVIEW);
618 InitSuperquadrics(int wfmode, int snorm, int res, int count, float speed, superquadricsstruct * sp)
621 {0.4, 0.4, 0.4, 1.0};
623 {10.0, 1.0, 1.0, 10.0};
624 GLfloat mat_diffuse[] =
625 {1.0, 0.5, 0.5, 1.0};
626 GLfloat mat_specular[] =
627 {0.8, 0.8, 0.8, 1.0};
628 GLfloat mat_shininess[] =
633 for (t = 0; t < 4; ++t) {
634 sp->curmat[t][0] = 0.0;
635 sp->curmat[t][1] = 0.0;
636 sp->curmat[t][2] = 0.0;
637 sp->curmat[t][3] = 1.0;
643 sp->dist = (16 << 3);
644 sp->wireframe = sp->flatshade = sp->shownorms = 0;
645 sp->maxcount = count;
646 if (sp->maxcount < 1)
648 sp->maxwait = sp->maxcount >> 1;
652 sp->spinspeed = speed;
653 sp->viewcount = sp->viewwait = (sp->maxcount < 2) ? 1 : (sp->maxcount << 3);
659 sp->resolution = res;
672 glShadeModel(GL_FLAT);
673 glDisable(GL_LIGHTING);
674 glColor3f(mat_diffuse[0], mat_diffuse[1], mat_diffuse[2]);
677 glShadeModel(GL_FLAT);
681 glEnable(GL_LIGHTING);
683 glDepthFunc(GL_LEQUAL);
684 glEnable(GL_DEPTH_TEST);
686 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
687 glLightfv(GL_LIGHT0, GL_POSITION, position);
689 /*glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse); */
690 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
691 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
693 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
696 glEnable(GL_NORMALIZE);
701 MakeUpStuff(1, sp); /* Initialize it */
702 sp->counter = sp->maxcount;
705 /* End of superquadrics main functions */
708 init_superquadrics(ModeInfo * mi)
710 Display *display = MI_DISPLAY(mi);
711 Window window = MI_WINDOW(mi);
712 int screen = MI_SCREEN(mi);
714 superquadricsstruct *sp;
716 if (superquadrics == NULL) {
717 if ((superquadrics = (superquadricsstruct *) calloc(MI_NUM_SCREENS(mi),
718 sizeof (superquadricsstruct))) == NULL)
721 sp = &superquadrics[screen];
722 sp->mono = (MI_IS_MONO(mi) ? 1 : 0);
737 if ((sp->glx_context = init_GL(mi)) != NULL) {
739 InitSuperquadrics(MI_IS_WIREFRAME(mi), 0,
740 MI_COUNT(mi), MI_CYCLES(mi), spinspeed, sp);
741 ReshapeSuperquadrics(MI_WIDTH(mi), MI_HEIGHT(mi));
743 DisplaySuperquadrics(sp);
745 glXSwapBuffers(display, window);
752 draw_superquadrics(ModeInfo * mi)
754 superquadricsstruct *sp = &superquadrics[MI_SCREEN(mi)];
755 Display *display = MI_DISPLAY(mi);
756 Window window = MI_WINDOW(mi);
758 if (!sp->glx_context)
761 glXMakeCurrent(display, window, *(sp->glx_context));
763 mi->polygon_count = NextSuperquadricDisplay(sp);
765 if (mi->fps_p) do_fps (mi);
767 glXSwapBuffers(display, window);
771 refresh_superquadrics(ModeInfo * mi)
773 /* Nothing happens here */
777 reshape_superquadrics(ModeInfo * mi, int width, int height)
779 ReshapeSuperquadrics(MI_WIDTH(mi), MI_HEIGHT(mi));
783 release_superquadrics(ModeInfo * mi)
785 if (superquadrics != NULL) {
786 (void) free((void *) superquadrics);
787 superquadrics = NULL;
795 /* End of superquadrics.c */
797 XSCREENSAVER_MODULE ("Superquadrics", superquadrics)