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