http://www.uw-madison.lkams.kernel.org/pub/mirrors/fink/distfiles/xscreensaver-4...
[xscreensaver] / hacks / glx / hypertorus.c
1 /* hypertorus --- Shows a hypertorus that rotates in 4d */
2
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)hypertorus.c  1.1 03/05/18 xlockmore";
5
6 #endif
7
8 /* Copyright (c) 2003 Carsten Steger <carsten@mirsanmir.org>. */
9
10 /*
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * REVISION HISTORY:
24  * C. Steger - 03/05/18: Initial version
25  */
26
27 /*
28  * This program shows the Clifford torus as it rotates in 4d.  The Clifford
29  * torus is a torus lies on the "surface" of the hypersphere in 4d.  The
30  * program projects the 4d torus to 3d using either a perspective or an
31  * orthographic projection.  Of the two alternatives, the perspecitve
32  * projection looks much more appealing.  In orthographic projections the
33  * torus degenerates into a doubly covered cylinder for some angles.  The
34  * projected 3d torus can then be projected to the screen either perspectively
35  * or orthographically.  There are three display modes for the torus: mesh
36  * (wireframe), solid, or transparent.  Furthermore, the appearance of the
37  * torus can be as a solid object or as a set of see-through bands.  Finally,
38  * the colors with with the torus is drawn can be set to either two-sided or
39  * to colorwheel.  In the first case, the torus is drawn with red on the
40  * outside and green on the inside.  This mode enables you to see that the
41  * torus turns inside-out as it rotates in 4d.  The second mode draws the
42  * torus in a fully saturated color wheel.  This gives a very nice effect
43  * when combined with the see-through bands mode.  The rotation speed for
44  * each of the six planes around which the torus rotates can be chosen.
45  * This program is very much inspired by Thomas Banchoff's book "Beyond the
46  * Third Dimension: Geometry, Computer Graphics, and Higher Dimensions",
47  * Scientific American Library, 1990.
48  */
49
50 #ifndef M_PI
51 #define M_PI 3.14159265358979323846
52 #endif
53
54 #define DISP_WIREFRAME            0
55 #define DISP_WIREFRAME_STR       "0"
56 #define DISP_SURFACE              1
57 #define DISP_SURFACE_STR         "1"
58 #define DISP_TRANSPARENT          2
59 #define DISP_TRANSPARENT_STR     "2"
60
61 #define APPEARANCE_SOLID          0
62 #define APPEARANCE_SOLID_STR     "0"
63 #define APPEARANCE_BANDS          1
64 #define APPEARANCE_BANDS_STR     "1"
65
66 #define COLORS_TWOSIDED           0
67 #define COLORS_TWOSIDED_STR      "0"
68 #define COLORS_COLORWHEEL         1
69 #define COLORS_COLORWHEEL_STR    "1"
70
71 #define DISP_3D_PERSPECTIVE       0
72 #define DISP_3D_PERSPECTIVE_STR  "0"
73 #define DISP_3D_ORTHOGRAPHIC      1
74 #define DISP_3D_ORTHOGRAPHIC_STR "1"
75
76 #define DISP_4D_PERSPECTIVE       0
77 #define DISP_4D_PERSPECTIVE_STR  "0"
78 #define DISP_4D_ORTHOGRAPHIC      1
79 #define DISP_4D_ORTHOGRAPHIC_STR "1"
80
81 #define DALPHA                    1.1
82 #define DALPHA_STR               "1.1"
83 #define DBETA                     1.3
84 #define DBETA_STR                "1.3"
85 #define DDELTA                    1.5
86 #define DDELTA_STR               "1.5"
87 #define DZETA                     1.7
88 #define DZETA_STR                "1.7"
89 #define DETA                      1.9
90 #define DETA_STR                 "1.9"
91 #define DTHETA                    2.1
92 #define DTHETA_STR               "2.1"
93
94 #define DEF_DISPLAY_MODE          DISP_SURFACE_STR   
95 #define DEF_APPEARANCE            APPEARANCE_BANDS_STR
96 #define DEF_COLORS                COLORS_COLORWHEEL_STR
97 #define DEF_3D_PROJECTION         DISP_3D_PERSPECTIVE_STR
98 #define DEF_4D_PROJECTION         DISP_4D_PERSPECTIVE_STR
99 #define DEF_DALPHA                DALPHA_STR
100 #define DEF_DBETA                 DBETA_STR
101 #define DEF_DDELTA                DDELTA_STR
102 #define DEF_DZETA                 DZETA_STR
103 #define DEF_DETA                  DETA_STR
104 #define DEF_DTHETA                DTHETA_STR
105
106 #ifdef STANDALONE
107 # define PROGCLASS       "Hypertorus"
108 # define HACK_INIT       init_hypertorus
109 # define HACK_DRAW       draw_hypertorus
110 # define HACK_RESHAPE    reshape_hypertorus
111 # define hypertorus_opts xlockmore_opts
112 # define DEFAULTS        "*delay:      25000 \n" \
113                          "*showFPS:    False \n" \
114                          "*wireframe:  False \n" \
115                          "*displayMode: "  DEF_DISPLAY_MODE  " \n" \
116                          "*appearance: "   DEF_APPEARANCE    " \n" \
117                          "*colors: "       DEF_COLORS        " \n" \
118                          "*projection3d: " DEF_3D_PROJECTION " \n" \
119                          "*projection4d: " DEF_4D_PROJECTION " \n" \
120                          "speedwx: "       DEF_DALPHA        " \n" \
121                          "speedwy: "       DEF_DBETA         " \n" \
122                          "speedwz: "       DEF_DDELTA        " \n" \
123                          "speedxy: "       DEF_DZETA         " \n" \
124                          "speedxz: "       DEF_DETA          " \n" \
125                          "speedyz: "       DEF_DTHETA        " \n"
126 # include "xlockmore.h"         /* from the xscreensaver distribution */
127 #else  /* !STANDALONE */
128 # include "xlock.h"             /* from the xlockmore distribution */
129 #endif /* !STANDALONE */
130
131 #ifdef USE_GL
132
133 #include <GL/gl.h>
134 #include <GL/glu.h>
135
136
137 #ifdef USE_MODULES
138 ModStruct   hypertorus_description =
139 {"hypertorus", "init_hypertorus", "draw_hypertorus", "release_hypertorus",
140  "draw_hypertorus", "change_hypertorus", NULL, &hypertorus_opts,
141  25000, 1, 1, 1, 1.0, 4, "",
142  "Shows a hypertorus rotating in 4d", 0, NULL};
143
144 #endif
145
146
147 static int display_mode;
148 static int appearance;
149 static int colors;
150 static int projection_3d;
151 static int projection_4d;
152 static float speed_wx;
153 static float speed_wy;
154 static float speed_wz;
155 static float speed_xy;
156 static float speed_xz;
157 static float speed_yz;
158
159 /* 4D rotation angles */
160 static float alpha, beta, delta, zeta, eta, theta;
161 static float aspect;
162
163 static const float offset4d[4] = {
164   0.0, 0.0, 0.0, 2.0
165 };
166
167 static const float offset3d[4] = {
168   0.0, 0.0, -2.0, 0.0
169 };
170
171
172 static XrmOptionDescRec opts[] =
173 {
174   {"-mesh",            ".hypertorus.displayMode",  XrmoptionNoArg,
175                        (caddr_t)DISP_WIREFRAME_STR },
176   {"-surface",         ".hypertorus.displayMode",  XrmoptionNoArg,
177                        (caddr_t)DISP_SURFACE_STR },
178   {"-transparent",     ".hypertorus.displayMode",  XrmoptionNoArg,
179                        (caddr_t)DISP_TRANSPARENT_STR },
180   {"-solid",           ".hypertorus.appearance",   XrmoptionNoArg,
181                        (caddr_t)APPEARANCE_SOLID_STR },
182   {"-bands",           ".hypertorus.appearance",   XrmoptionNoArg,
183                        (caddr_t)APPEARANCE_BANDS_STR },
184   {"-twosided",        ".hypertorus.colors",       XrmoptionNoArg,
185                        (caddr_t)COLORS_TWOSIDED_STR },
186   {"-colorwheel",      ".hypertorus.colors",       XrmoptionNoArg,
187                        (caddr_t)COLORS_COLORWHEEL_STR },
188   {"-perspective-3d",  ".hypertorus.projection3d", XrmoptionNoArg,
189                        (caddr_t)DISP_3D_PERSPECTIVE_STR },
190   {"-orthographic-3d", ".hypertorus.projection3d", XrmoptionNoArg,
191                        (caddr_t)DISP_3D_ORTHOGRAPHIC_STR },
192   {"-perspective-4d",  ".hypertorus.projection4d", XrmoptionNoArg,
193                        (caddr_t)DISP_4D_PERSPECTIVE_STR },
194   {"-orthographic-4d", ".hypertorus.projection4d", XrmoptionNoArg,
195                        (caddr_t)DISP_4D_ORTHOGRAPHIC_STR },
196   {"-speed-wx",        ".hypertorus.speedwx",      XrmoptionSepArg,
197                        (caddr_t)NULL },
198   {"-speed-wy",        ".hypertorus.speedwy",      XrmoptionSepArg,
199                        (caddr_t)NULL },
200   {"-speed-wz",        ".hypertorus.speedwz",      XrmoptionSepArg,
201                        (caddr_t)NULL },
202   {"-speed-xy",        ".hypertorus.speedxy",      XrmoptionSepArg,
203                        (caddr_t)NULL },
204   {"-speed-xz",        ".hypertorus.speedxz",      XrmoptionSepArg,
205                        (caddr_t)NULL },
206   {"-speed-yz",        ".hypertorus.speedyz",      XrmoptionSepArg,
207                        (caddr_t)NULL }
208 };
209
210 static argtype vars[] =
211 {
212   { (caddr_t *) &display_mode,  "displayMode",  "DisplayMode",
213     DEF_DISPLAY_MODE,  t_Int },
214   { (caddr_t *) &appearance,    "appearance",   "Appearance",
215     DEF_APPEARANCE,    t_Int },
216   { (caddr_t *) &colors,        "colors",       "Colors",
217     DEF_COLORS,        t_Int },
218   { (caddr_t *) &projection_3d, "projection3d", "Projection3d",
219     DEF_3D_PROJECTION, t_Int },
220   { (caddr_t *) &projection_4d, "projection4d", "Projection4d",
221     DEF_4D_PROJECTION, t_Int },
222   { (caddr_t *) &speed_wx,      "speedwx",      "Speedwx",
223     DEF_DALPHA,        t_Float},
224   { (caddr_t *) &speed_wy,      "speedwy",      "Speedwy",
225     DEF_DBETA,         t_Float},
226   { (caddr_t *) &speed_wz,      "speedwz",      "Speedwz",
227     DEF_DDELTA,        t_Float},
228   { (caddr_t *) &speed_xy,      "speedxy",      "Speedxy",
229     DEF_DZETA,         t_Float},
230   { (caddr_t *) &speed_xz,      "speedxz",      "Speedxz",
231     DEF_DETA,          t_Float},
232   { (caddr_t *) &speed_yz,      "speedyz",      "Speedyz",
233     DEF_DTHETA,        t_Float}
234 };
235
236 static OptionStruct desc[] =
237 {
238   { "-mesh",            "display the torus as a wireframe mesh" },
239   { "-surface",         "display the torus as a solid surface" },
240   { "-transparent",     "display the torus as a transparent surface" },
241   { "-solid",           "display the torus as a solid object" },
242   { "-bands",           "display the torus as see-through bands" },
243   { "-twosided",        "display the torus with two colors" },
244   { "-colorwheel",      "display the torus with a smooth color wheel" },
245   { "-perspective-3d",  "project the torus perspectively from 3d to 2d" },
246   { "-orthographic-3d", "project the torus orthographically from 3d to 2d" },
247   { "-perspective-4d",  "project the torus perspectively from 4d to 3d" },
248   { "-orthographic-4d", "project the torus orthographically from 4d to 3d" },
249   { "-speed-wx <arg>",  "rotation speed around the wx plane" },
250   { "-speed-wy <arg>",  "rotation speed around the wy plane" },
251   { "-speed-wz <arg>",  "rotation speed around the wz plane" },
252   { "-speed-xy <arg>",  "rotation speed around the xy plane" },
253   { "-speed-xz <arg>",  "rotation speed around the xz plane" },
254   { "-speed-yz <arg>",  "rotation speed around the yz plane" }
255 };
256
257 ModeSpecOpt hypertorus_opts =
258 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
259
260
261 typedef struct {
262   GLint       WindH, WindW;
263   GLXContext *glx_context;
264 } hypertorusstruct;
265
266 static hypertorusstruct *hyper = (hypertorusstruct *) NULL;
267
268
269 /* Add a rotation around the wx-plane to the matrix m. */
270 static void rotatewx(float m[4][4], float phi)
271 {
272   float c, s, u, v;
273   int i;
274
275   phi *= M_PI/180.0;
276   c = cos(phi);
277   s = sin(phi);
278   for (i=0; i<4; i++)
279   {
280     u = m[i][1];
281     v = m[i][2];
282     m[i][1] = c*u+s*v;
283     m[i][2] = -s*u+c*v;
284   }
285 }
286
287
288 /* Add a rotation around the wy-plane to the matrix m. */
289 static void rotatewy(float m[4][4], float phi)
290 {
291   float c, s, u, v;
292   int i;
293
294   phi *= M_PI/180.0;
295   c = cos(phi);
296   s = sin(phi);
297   for (i=0; i<4; i++)
298   {
299     u = m[i][0];
300     v = m[i][2];
301     m[i][0] = c*u-s*v;
302     m[i][2] = s*u+c*v;
303   }
304 }
305
306
307 /* Add a rotation around the wz-plane to the matrix m. */
308 static void rotatewz(float m[4][4], float phi)
309 {
310   float c, s, u, v;
311   int i;
312
313   phi *= M_PI/180.0;
314   c = cos(phi);
315   s = sin(phi);
316   for (i=0; i<4; i++)
317   {
318     u = m[i][0];
319     v = m[i][1];
320     m[i][0] = c*u+s*v;
321     m[i][1] = -s*u+c*v;
322   }
323 }
324
325
326 /* Add a rotation around the xy-plane to the matrix m. */
327 static void rotatexy(float m[4][4], float phi)
328 {
329   float c, s, u, v;
330   int i;
331
332   phi *= M_PI/180.0;
333   c = cos(phi);
334   s = sin(phi);
335   for (i=0; i<4; i++)
336   {
337     u = m[i][2];
338     v = m[i][3];
339     m[i][2] = c*u+s*v;
340     m[i][3] = -s*u+c*v;
341   }
342 }
343
344
345 /* Add a rotation around the xz-plane to the matrix m. */
346 static void rotatexz(float m[4][4], float phi)
347 {
348   float c, s, u, v;
349   int i;
350
351   phi *= M_PI/180.0;
352   c = cos(phi);
353   s = sin(phi);
354   for (i=0; i<4; i++)
355   {
356     u = m[i][1];
357     v = m[i][3];
358     m[i][1] = c*u-s*v;
359     m[i][3] = s*u+c*v;
360   }
361 }
362
363
364 /* Add a rotation around the yz-plane to the matrix m. */
365 static void rotateyz(float m[4][4], float phi)
366 {
367   float c, s, u, v;
368   int i;
369
370   phi *= M_PI/180.0;
371   c = cos(phi);
372   s = sin(phi);
373   for (i=0; i<4; i++)
374   {
375     u = m[i][0];
376     v = m[i][3];
377     m[i][0] = c*u-s*v;
378     m[i][3] = s*u+c*v;
379   }
380 }
381
382
383 /* Compute a fully saturated and bright color based on an angle. */
384 static void color(double angle)
385 {
386   int s;
387   double t;
388   float color[4];
389
390   if (colors != COLORS_COLORWHEEL)
391     return;
392
393   if (angle >= 0.0)
394     angle = fmod(angle,2*M_PI);
395   else
396     angle = fmod(angle,-2*M_PI);
397   s = floor(angle/(M_PI/3));
398   t = angle/(M_PI/3)-s;
399   if (s >= 6)
400     s = 0;
401   switch (s)
402   {
403     case 0:
404       color[0] = 1.0;
405       color[1] = t;
406       color[2] = 0.0;
407       break;
408     case 1:
409       color[0] = 1.0-t;
410       color[1] = 1.0;
411       color[2] = 0.0;
412       break;
413     case 2:
414       color[0] = 0.0;
415       color[1] = 1.0;
416       color[2] = t;
417       break;
418     case 3:
419       color[0] = 0.0;
420       color[1] = 1.0-t;
421       color[2] = 1.0;
422       break;
423     case 4:
424       color[0] = t;
425       color[1] = 0.0;
426       color[2] = 1.0;
427       break;
428     case 5:
429       color[0] = 1.0;
430       color[1] = 0.0;
431       color[2] = 1.0-t;
432       break;
433   }
434   if (display_mode == DISP_TRANSPARENT)
435     color[3] = 0.5;
436   else
437     color[3] = 1.0;
438   glColor3fv(color);
439   glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color);
440 }
441
442
443 /* Draw a hyperturus projected into 3D. */
444 static void hypertorus(double umin, double umax, double vmin, double vmax,
445                        int numu, int numv)
446 {
447   static GLfloat mat_diff_red[] = { 1.0, 0.0, 0.0, 1.0 };
448   static GLfloat mat_diff_green[] = { 0.0, 1.0, 0.0, 1.0 };
449   static GLfloat mat_diff_trans_red[] = { 1.0, 0.0, 0.0, 0.5 };
450   static GLfloat mat_diff_trans_green[] = { 0.0, 1.0, 0.0, 0.5 };
451   float p[3], pu[3], pv[3], n[3], mat[4][4];
452   int i, j, k, l, m;
453   double u, v, ur, vr;
454   double cu, su, cv, sv;
455   double xx[4], xxu[4], xxv[4], x[4], xu[4], xv[4];
456   double r, s, t;
457
458   /* Compute the rotation that rotates the hypercube in 4D. */
459   for (i=0; i<4; i++)
460     for (j=0; j<4; j++)
461       mat[i][j] = (i==j);
462   rotatewx(mat,alpha);
463   rotatewy(mat,beta);
464   rotatewz(mat,delta);
465   rotatexy(mat,zeta);
466   rotatexz(mat,eta);
467   rotateyz(mat,theta);
468
469   if (colors != COLORS_COLORWHEEL)
470   {
471     glColor3fv(mat_diff_red);
472     if (display_mode == DISP_TRANSPARENT)
473     {
474       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_red);
475       glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_green);
476     }
477     else
478     {
479       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_red);
480       glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_green);
481     }
482   }
483
484   ur = umax-umin;
485   vr = vmax-vmin;
486   for (i=0; i<numu; i++)
487   {
488     if (appearance == APPEARANCE_BANDS && ((i & 3) >= 2))
489       continue;
490     if (display_mode == DISP_WIREFRAME)
491       glBegin(GL_QUAD_STRIP);
492     else
493       glBegin(GL_TRIANGLE_STRIP);
494     for (j=0; j<=numv; j++)
495     {
496       for (k=0; k<=1; k++)
497       {
498         l = (i+k);
499         m = j;
500         u = ur*l/numu+umin;
501         v = vr*m/numv+vmin;
502         color(u);
503         cu = cos(u);
504         su = sin(u);
505         cv = cos(v);
506         sv = sin(v);
507         xx[0] = cu;
508         xx[1] = su;
509         xx[2] = cv;
510         xx[3] = sv;
511         xxu[0] = -su;
512         xxu[1] = cu;
513         xxu[2] = 0.0;
514         xxu[3] = 0.0;
515         xxv[0] = 0.0;
516         xxv[1] = 0.0;
517         xxv[2] = -sv;
518         xxv[3] = cv;
519         for (l=0; l<4; l++)
520         {
521           r = 0.0;
522           s = 0.0;
523           t = 0.0;
524           for (m=0; m<4; m++)
525           {
526             r += mat[l][m]*xx[m];
527             s += mat[l][m]*xxu[m];
528             t += mat[l][m]*xxv[m];
529           }
530           x[l] = r;
531           xu[l] = s;
532           xv[l] = t;
533         }
534         if (projection_4d == DISP_4D_ORTHOGRAPHIC)
535         {
536           for (l=0; l<3; l++)
537           {
538             p[l] = (x[l]+offset4d[l])/1.5+offset3d[l];
539             pu[l] = xu[l];
540             pv[l] = xv[l];
541           }
542         }
543         else
544         {
545           s = x[3]+offset4d[3];
546           t = s*s;
547           for (l=0; l<3; l++)
548           {
549             r = x[l]+offset4d[l];
550             p[l] = r/s+offset3d[l];
551             pu[l] = (xu[l]*s-r*xu[3])/t;
552             pv[l] = (xv[l]*s-r*xv[3])/t;
553           }
554         }
555         n[0] = pu[1]*pv[2]-pu[2]*pv[1];
556         n[1] = pu[2]*pv[0]-pu[0]*pv[2];
557         n[2] = pu[0]*pv[1]-pu[1]*pv[0];
558         t = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
559         n[0] /= t;
560         n[1] /= t;
561         n[2] /= t;
562         glNormal3fv(n);
563         glVertex3fv(p);
564       }
565     }
566     glEnd();
567   }
568 }
569
570
571 static void init(ModeInfo *mi)
572 {
573   static GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
574   static GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
575   static GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
576   static GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
577   static GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
578
579   alpha = 0.0;
580   beta = 0.0;
581   delta = 0.0;
582   zeta = 0.0;
583   eta = 0.0;
584   theta = 0.0;
585
586   glMatrixMode(GL_PROJECTION);
587   glLoadIdentity();
588   if (projection_3d == DISP_3D_PERSPECTIVE)
589     gluPerspective(60.0,1.0,0.1,100.0);
590   else
591     glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
592   glMatrixMode(GL_MODELVIEW);
593   glLoadIdentity();
594
595   if (display_mode == DISP_WIREFRAME)
596   {
597     glDisable(GL_DEPTH_TEST);
598     glShadeModel(GL_FLAT);
599     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
600     glDisable(GL_LIGHTING);
601     glDisable(GL_LIGHT0);
602     glDisable(GL_BLEND);
603   }
604   else if (display_mode == DISP_SURFACE)
605   {
606     glEnable(GL_DEPTH_TEST);
607     glDepthFunc(GL_LESS);
608     glShadeModel(GL_SMOOTH);
609     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
610     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
611     glEnable(GL_LIGHTING);
612     glEnable(GL_LIGHT0);
613     glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
614     glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
615     glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
616     glLightfv(GL_LIGHT0,GL_POSITION,light_position);
617     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
618     glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
619     glDepthMask(GL_TRUE);
620     glDisable(GL_BLEND);
621   }
622   else if (display_mode == DISP_TRANSPARENT)
623   {
624     glDisable(GL_DEPTH_TEST);
625     glShadeModel(GL_SMOOTH);
626     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
627     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
628     glEnable(GL_LIGHTING);
629     glEnable(GL_LIGHT0);
630     glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
631     glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
632     glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
633     glLightfv(GL_LIGHT0,GL_POSITION,light_position);
634     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
635     glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
636     glDepthMask(GL_FALSE);
637     glEnable(GL_BLEND);
638     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
639   }
640   else
641   {
642     glDisable(GL_DEPTH_TEST);
643     glShadeModel(GL_FLAT);
644     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
645     glDisable(GL_LIGHTING);
646     glDisable(GL_LIGHT0);
647     glDisable(GL_BLEND);
648   }
649 }
650
651
652 /* Redisplay the hypertorus. */
653 static void display_hypertorus(void)
654 {
655   alpha += speed_wx;
656   if (alpha >= 360.0)
657     alpha -= 360.0;
658   beta += speed_wy;
659   if (beta >= 360.0)
660     beta -= 360.0;
661   delta += speed_wz;
662   if (delta >= 360.0)
663     delta -= 360.0;
664   zeta += speed_xy;
665   if (zeta >= 360.0)
666     zeta -= 360.0;
667   eta += speed_xz;
668   if (eta >= 360.0)
669     eta -= 360.0;
670   theta += speed_yz;
671   if (theta >= 360.0)
672     theta -= 360.0;
673
674   glMatrixMode(GL_PROJECTION);
675   glLoadIdentity();
676   if (projection_3d == DISP_3D_ORTHOGRAPHIC)
677   {
678     if (aspect >= 1.0)
679       glOrtho(-aspect,aspect,-1.0,1.0,0.1,100.0);
680     else
681       glOrtho(-1.0,1.0,-1.0/aspect,1.0/aspect,0.1,100.0);
682   }
683   else
684   {
685     gluPerspective(60.0,aspect,0.1,100.0);
686   }
687   glMatrixMode(GL_MODELVIEW);
688   glLoadIdentity();
689
690   if (display_mode == DISP_WIREFRAME)
691     hypertorus(0.0,2.0*M_PI,0.0,2.0*M_PI,40,40);
692   else
693     hypertorus(0.0,2.0*M_PI,0.0,2.0*M_PI,60,60);
694 }
695
696
697 void reshape_hypertorus(ModeInfo * mi, int width, int height)
698 {
699   hypertorusstruct *hp = &hyper[MI_SCREEN(mi)];
700
701   hp->WindW = (GLint)width;
702   hp->WindH = (GLint)height;
703   glViewport(0,0,width,height);
704   aspect = (GLfloat)width/(GLfloat)height;
705 }
706
707
708 /*
709  *-----------------------------------------------------------------------------
710  *-----------------------------------------------------------------------------
711  *    Xlock hooks.
712  *-----------------------------------------------------------------------------
713  *-----------------------------------------------------------------------------
714  */
715
716 /*
717  *-----------------------------------------------------------------------------
718  *    Initialize hypertorus.  Called each time the window changes.
719  *-----------------------------------------------------------------------------
720  */
721
722 void init_hypertorus(ModeInfo * mi)
723 {
724   hypertorusstruct *hp;
725
726   if (hyper == NULL)
727   {
728     hyper = (hypertorusstruct *)calloc(MI_NUM_SCREENS(mi),
729                                        sizeof(hypertorusstruct));
730     if (hyper == NULL)
731       return;
732   }
733   hp = &hyper[MI_SCREEN(mi)];
734
735   if ((hp->glx_context = init_GL(mi)) != NULL)
736   {
737     reshape_hypertorus(mi,MI_WIDTH(mi),MI_HEIGHT(mi));
738     glDrawBuffer(GL_BACK);
739     init(mi);
740   }
741   else
742   {
743     MI_CLEARWINDOW(mi);
744   }
745 }
746
747 /*
748  *-----------------------------------------------------------------------------
749  *    Called by the mainline code periodically to update the display.
750  *-----------------------------------------------------------------------------
751  */
752 void draw_hypertorus(ModeInfo * mi)
753 {
754   Display          *display = MI_DISPLAY(mi);
755   Window           window = MI_WINDOW(mi);
756   hypertorusstruct *hp;
757
758   if (hyper == NULL)
759     return;
760   hp = &hyper[MI_SCREEN(mi)];
761
762   MI_IS_DRAWN(mi) = True;
763   if (!hp->glx_context)
764     return;
765
766   glXMakeCurrent(display,window,*(hp->glx_context));
767
768   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
769   glLoadIdentity();
770
771   display_hypertorus();
772
773   if (MI_IS_FPS(mi))
774     do_fps (mi);
775
776   glFlush();
777
778   glXSwapBuffers(display,window);
779 }
780
781
782 /*
783  *-----------------------------------------------------------------------------
784  *    The display is being taken away from us.  Free up malloc'ed 
785  *      memory and X resources that we've alloc'ed.  Only called
786  *      once, we must zap everything for every screen.
787  *-----------------------------------------------------------------------------
788  */
789
790 void release_hypertorus(ModeInfo * mi)
791 {
792   if (hyper != NULL)
793   {
794     int screen;
795
796     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
797     {
798       hypertorusstruct *hp = &hyper[screen];
799
800       if (hp->glx_context)
801         hp->glx_context = (GLXContext *)NULL;
802     }
803     (void) free((void *)hyper);
804     hyper = (hypertorusstruct *)NULL;
805   }
806   FreeAllGL(mi);
807 }
808
809 void change_hypertorus(ModeInfo * mi)
810 {
811   hypertorusstruct *hp = &hyper[MI_SCREEN(mi)];
812
813   if (!hp->glx_context)
814     return;
815
816   glXMakeCurrent(MI_DISPLAY(mi),MI_WINDOW(mi),*(hp->glx_context));
817   init(mi);
818 }
819
820 #endif /* USE_GL */