a54fbfe6cd6372db91f1b1d06ab45d641f1ce5b6
[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
115 # include "xlockmore.h"         /* from the xscreensaver distribution */
116 #else  /* !STANDALONE */
117 # include "xlock.h"             /* from the xlockmore distribution */
118 #endif /* !STANDALONE */
119
120 #ifdef USE_GL
121
122 #include <GL/gl.h>
123 #include <GL/glu.h>
124
125
126 #ifdef USE_MODULES
127 ModStruct   hypertorus_description =
128 {"hypertorus", "init_hypertorus", "draw_hypertorus", "release_hypertorus",
129  "draw_hypertorus", "change_hypertorus", NULL, &hypertorus_opts,
130  25000, 1, 1, 1, 1.0, 4, "",
131  "Shows a hypertorus rotating in 4d", 0, NULL};
132
133 #endif
134
135
136 static int display_mode;
137 static int appearance;
138 static int colors;
139 static int projection_3d;
140 static int projection_4d;
141 static float speed_wx;
142 static float speed_wy;
143 static float speed_wz;
144 static float speed_xy;
145 static float speed_xz;
146 static float speed_yz;
147
148 /* 4D rotation angles */
149 static float alpha, beta, delta, zeta, eta, theta;
150 static float aspect;
151
152 static const float offset4d[4] = {
153   0.0, 0.0, 0.0, 2.0
154 };
155
156 static const float offset3d[4] = {
157   0.0, 0.0, -2.0, 0.0
158 };
159
160
161 static XrmOptionDescRec opts[] =
162 {
163   {"-mesh",            ".hypertorus.displayMode",  XrmoptionNoArg,
164                        DISP_WIREFRAME_STR },
165   {"-surface",         ".hypertorus.displayMode",  XrmoptionNoArg,
166                        DISP_SURFACE_STR },
167   {"-transparent",     ".hypertorus.displayMode",  XrmoptionNoArg,
168                        DISP_TRANSPARENT_STR },
169   {"-solid",           ".hypertorus.appearance",   XrmoptionNoArg,
170                        APPEARANCE_SOLID_STR },
171   {"-bands",           ".hypertorus.appearance",   XrmoptionNoArg,
172                        APPEARANCE_BANDS_STR },
173   {"-twosided",        ".hypertorus.colors",       XrmoptionNoArg,
174                        COLORS_TWOSIDED_STR },
175   {"-colorwheel",      ".hypertorus.colors",       XrmoptionNoArg,
176                        COLORS_COLORWHEEL_STR },
177   {"-perspective-3d",  ".hypertorus.projection3d", XrmoptionNoArg,
178                        DISP_3D_PERSPECTIVE_STR },
179   {"-orthographic-3d", ".hypertorus.projection3d", XrmoptionNoArg,
180                        DISP_3D_ORTHOGRAPHIC_STR },
181   {"-perspective-4d",  ".hypertorus.projection4d", XrmoptionNoArg,
182                        DISP_4D_PERSPECTIVE_STR },
183   {"-orthographic-4d", ".hypertorus.projection4d", XrmoptionNoArg,
184                        DISP_4D_ORTHOGRAPHIC_STR },
185   {"-speed-wx",        ".hypertorus.speedwx",      XrmoptionSepArg, 0 },
186   {"-speed-wy",        ".hypertorus.speedwy",      XrmoptionSepArg, 0 },
187   {"-speed-wz",        ".hypertorus.speedwz",      XrmoptionSepArg, 0 },
188   {"-speed-xy",        ".hypertorus.speedxy",      XrmoptionSepArg, 0 },
189   {"-speed-xz",        ".hypertorus.speedxz",      XrmoptionSepArg, 0 },
190   {"-speed-yz",        ".hypertorus.speedyz",      XrmoptionSepArg, 0 }
191 };
192
193 static argtype vars[] =
194 {
195   { &display_mode,  "displayMode",  "DisplayMode",
196     DEF_DISPLAY_MODE,  t_Int },
197   { &appearance,    "appearance",   "Appearance",
198     DEF_APPEARANCE,    t_Int },
199   { &colors,        "colors",       "Colors",
200     DEF_COLORS,        t_Int },
201   { &projection_3d, "projection3d", "Projection3d",
202     DEF_3D_PROJECTION, t_Int },
203   { &projection_4d, "projection4d", "Projection4d",
204     DEF_4D_PROJECTION, t_Int },
205   { &speed_wx,      "speedwx",      "Speedwx",
206     DEF_DALPHA,        t_Float},
207   { &speed_wy,      "speedwy",      "Speedwy",
208     DEF_DBETA,         t_Float},
209   { &speed_wz,      "speedwz",      "Speedwz",
210     DEF_DDELTA,        t_Float},
211   { &speed_xy,      "speedxy",      "Speedxy",
212     DEF_DZETA,         t_Float},
213   { &speed_xz,      "speedxz",      "Speedxz",
214     DEF_DETA,          t_Float},
215   { &speed_yz,      "speedyz",      "Speedyz",
216     DEF_DTHETA,        t_Float}
217 };
218
219 static OptionStruct desc[] =
220 {
221   { "-mesh",            "display the torus as a wireframe mesh" },
222   { "-surface",         "display the torus as a solid surface" },
223   { "-transparent",     "display the torus as a transparent surface" },
224   { "-solid",           "display the torus as a solid object" },
225   { "-bands",           "display the torus as see-through bands" },
226   { "-twosided",        "display the torus with two colors" },
227   { "-colorwheel",      "display the torus with a smooth color wheel" },
228   { "-perspective-3d",  "project the torus perspectively from 3d to 2d" },
229   { "-orthographic-3d", "project the torus orthographically from 3d to 2d" },
230   { "-perspective-4d",  "project the torus perspectively from 4d to 3d" },
231   { "-orthographic-4d", "project the torus orthographically from 4d to 3d" },
232   { "-speed-wx <arg>",  "rotation speed around the wx plane" },
233   { "-speed-wy <arg>",  "rotation speed around the wy plane" },
234   { "-speed-wz <arg>",  "rotation speed around the wz plane" },
235   { "-speed-xy <arg>",  "rotation speed around the xy plane" },
236   { "-speed-xz <arg>",  "rotation speed around the xz plane" },
237   { "-speed-yz <arg>",  "rotation speed around the yz plane" }
238 };
239
240 ModeSpecOpt hypertorus_opts =
241 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
242
243
244 typedef struct {
245   GLint       WindH, WindW;
246   GLXContext *glx_context;
247 } hypertorusstruct;
248
249 static hypertorusstruct *hyper = (hypertorusstruct *) NULL;
250
251
252 /* Add a rotation around the wx-plane to the matrix m. */
253 static void rotatewx(float m[4][4], float phi)
254 {
255   float c, s, u, v;
256   int i;
257
258   phi *= M_PI/180.0;
259   c = cos(phi);
260   s = sin(phi);
261   for (i=0; i<4; i++)
262   {
263     u = m[i][1];
264     v = m[i][2];
265     m[i][1] = c*u+s*v;
266     m[i][2] = -s*u+c*v;
267   }
268 }
269
270
271 /* Add a rotation around the wy-plane to the matrix m. */
272 static void rotatewy(float m[4][4], float phi)
273 {
274   float c, s, u, v;
275   int i;
276
277   phi *= M_PI/180.0;
278   c = cos(phi);
279   s = sin(phi);
280   for (i=0; i<4; i++)
281   {
282     u = m[i][0];
283     v = m[i][2];
284     m[i][0] = c*u-s*v;
285     m[i][2] = s*u+c*v;
286   }
287 }
288
289
290 /* Add a rotation around the wz-plane to the matrix m. */
291 static void rotatewz(float m[4][4], float phi)
292 {
293   float c, s, u, v;
294   int i;
295
296   phi *= M_PI/180.0;
297   c = cos(phi);
298   s = sin(phi);
299   for (i=0; i<4; i++)
300   {
301     u = m[i][0];
302     v = m[i][1];
303     m[i][0] = c*u+s*v;
304     m[i][1] = -s*u+c*v;
305   }
306 }
307
308
309 /* Add a rotation around the xy-plane to the matrix m. */
310 static void rotatexy(float m[4][4], float phi)
311 {
312   float c, s, u, v;
313   int i;
314
315   phi *= M_PI/180.0;
316   c = cos(phi);
317   s = sin(phi);
318   for (i=0; i<4; i++)
319   {
320     u = m[i][2];
321     v = m[i][3];
322     m[i][2] = c*u+s*v;
323     m[i][3] = -s*u+c*v;
324   }
325 }
326
327
328 /* Add a rotation around the xz-plane to the matrix m. */
329 static void rotatexz(float m[4][4], float phi)
330 {
331   float c, s, u, v;
332   int i;
333
334   phi *= M_PI/180.0;
335   c = cos(phi);
336   s = sin(phi);
337   for (i=0; i<4; i++)
338   {
339     u = m[i][1];
340     v = m[i][3];
341     m[i][1] = c*u-s*v;
342     m[i][3] = s*u+c*v;
343   }
344 }
345
346
347 /* Add a rotation around the yz-plane to the matrix m. */
348 static void rotateyz(float m[4][4], float phi)
349 {
350   float c, s, u, v;
351   int i;
352
353   phi *= M_PI/180.0;
354   c = cos(phi);
355   s = sin(phi);
356   for (i=0; i<4; i++)
357   {
358     u = m[i][0];
359     v = m[i][3];
360     m[i][0] = c*u-s*v;
361     m[i][3] = s*u+c*v;
362   }
363 }
364
365
366 /* Compute a fully saturated and bright color based on an angle. */
367 static void color(double angle)
368 {
369   int s;
370   double t;
371   float color[4];
372
373   if (colors != COLORS_COLORWHEEL)
374     return;
375
376   if (angle >= 0.0)
377     angle = fmod(angle,2*M_PI);
378   else
379     angle = fmod(angle,-2*M_PI);
380   s = floor(angle/(M_PI/3));
381   t = angle/(M_PI/3)-s;
382   if (s >= 6)
383     s = 0;
384   switch (s)
385   {
386     case 0:
387       color[0] = 1.0;
388       color[1] = t;
389       color[2] = 0.0;
390       break;
391     case 1:
392       color[0] = 1.0-t;
393       color[1] = 1.0;
394       color[2] = 0.0;
395       break;
396     case 2:
397       color[0] = 0.0;
398       color[1] = 1.0;
399       color[2] = t;
400       break;
401     case 3:
402       color[0] = 0.0;
403       color[1] = 1.0-t;
404       color[2] = 1.0;
405       break;
406     case 4:
407       color[0] = t;
408       color[1] = 0.0;
409       color[2] = 1.0;
410       break;
411     case 5:
412       color[0] = 1.0;
413       color[1] = 0.0;
414       color[2] = 1.0-t;
415       break;
416   }
417   if (display_mode == DISP_TRANSPARENT)
418     color[3] = 0.5;
419   else
420     color[3] = 1.0;
421   glColor3fv(color);
422   glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color);
423 }
424
425
426 /* Draw a hyperturus projected into 3D. */
427 static void hypertorus(double umin, double umax, double vmin, double vmax,
428                        int numu, int numv)
429 {
430   static GLfloat mat_diff_red[] = { 1.0, 0.0, 0.0, 1.0 };
431   static GLfloat mat_diff_green[] = { 0.0, 1.0, 0.0, 1.0 };
432   static GLfloat mat_diff_trans_red[] = { 1.0, 0.0, 0.0, 0.5 };
433   static GLfloat mat_diff_trans_green[] = { 0.0, 1.0, 0.0, 0.5 };
434   float p[3], pu[3], pv[3], n[3], mat[4][4];
435   int i, j, k, l, m;
436   double u, v, ur, vr;
437   double cu, su, cv, sv;
438   double xx[4], xxu[4], xxv[4], x[4], xu[4], xv[4];
439   double r, s, t;
440
441   /* Compute the rotation that rotates the hypercube in 4D. */
442   for (i=0; i<4; i++)
443     for (j=0; j<4; j++)
444       mat[i][j] = (i==j);
445   rotatewx(mat,alpha);
446   rotatewy(mat,beta);
447   rotatewz(mat,delta);
448   rotatexy(mat,zeta);
449   rotatexz(mat,eta);
450   rotateyz(mat,theta);
451
452   if (colors != COLORS_COLORWHEEL)
453   {
454     glColor3fv(mat_diff_red);
455     if (display_mode == DISP_TRANSPARENT)
456     {
457       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_red);
458       glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_green);
459     }
460     else
461     {
462       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_red);
463       glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_green);
464     }
465   }
466
467   ur = umax-umin;
468   vr = vmax-vmin;
469   for (i=0; i<numu; i++)
470   {
471     if (appearance == APPEARANCE_BANDS && ((i & 3) >= 2))
472       continue;
473     if (display_mode == DISP_WIREFRAME)
474       glBegin(GL_QUAD_STRIP);
475     else
476       glBegin(GL_TRIANGLE_STRIP);
477     for (j=0; j<=numv; j++)
478     {
479       for (k=0; k<=1; k++)
480       {
481         l = (i+k);
482         m = j;
483         u = ur*l/numu+umin;
484         v = vr*m/numv+vmin;
485         color(u);
486         cu = cos(u);
487         su = sin(u);
488         cv = cos(v);
489         sv = sin(v);
490         xx[0] = cu;
491         xx[1] = su;
492         xx[2] = cv;
493         xx[3] = sv;
494         xxu[0] = -su;
495         xxu[1] = cu;
496         xxu[2] = 0.0;
497         xxu[3] = 0.0;
498         xxv[0] = 0.0;
499         xxv[1] = 0.0;
500         xxv[2] = -sv;
501         xxv[3] = cv;
502         for (l=0; l<4; l++)
503         {
504           r = 0.0;
505           s = 0.0;
506           t = 0.0;
507           for (m=0; m<4; m++)
508           {
509             r += mat[l][m]*xx[m];
510             s += mat[l][m]*xxu[m];
511             t += mat[l][m]*xxv[m];
512           }
513           x[l] = r;
514           xu[l] = s;
515           xv[l] = t;
516         }
517         if (projection_4d == DISP_4D_ORTHOGRAPHIC)
518         {
519           for (l=0; l<3; l++)
520           {
521             p[l] = (x[l]+offset4d[l])/1.5+offset3d[l];
522             pu[l] = xu[l];
523             pv[l] = xv[l];
524           }
525         }
526         else
527         {
528           s = x[3]+offset4d[3];
529           t = s*s;
530           for (l=0; l<3; l++)
531           {
532             r = x[l]+offset4d[l];
533             p[l] = r/s+offset3d[l];
534             pu[l] = (xu[l]*s-r*xu[3])/t;
535             pv[l] = (xv[l]*s-r*xv[3])/t;
536           }
537         }
538         n[0] = pu[1]*pv[2]-pu[2]*pv[1];
539         n[1] = pu[2]*pv[0]-pu[0]*pv[2];
540         n[2] = pu[0]*pv[1]-pu[1]*pv[0];
541         t = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
542         n[0] /= t;
543         n[1] /= t;
544         n[2] /= t;
545         glNormal3fv(n);
546         glVertex3fv(p);
547       }
548     }
549     glEnd();
550   }
551 }
552
553
554 static void init(ModeInfo *mi)
555 {
556   static GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
557   static GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
558   static GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
559   static GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
560   static GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
561
562   alpha = 0.0;
563   beta = 0.0;
564   delta = 0.0;
565   zeta = 0.0;
566   eta = 0.0;
567   theta = 0.0;
568
569   glMatrixMode(GL_PROJECTION);
570   glLoadIdentity();
571   if (projection_3d == DISP_3D_PERSPECTIVE)
572     gluPerspective(60.0,1.0,0.1,100.0);
573   else
574     glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
575   glMatrixMode(GL_MODELVIEW);
576   glLoadIdentity();
577
578   if (display_mode == DISP_WIREFRAME)
579   {
580     glDisable(GL_DEPTH_TEST);
581     glShadeModel(GL_FLAT);
582     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
583     glDisable(GL_LIGHTING);
584     glDisable(GL_LIGHT0);
585     glDisable(GL_BLEND);
586   }
587   else if (display_mode == DISP_SURFACE)
588   {
589     glEnable(GL_DEPTH_TEST);
590     glDepthFunc(GL_LESS);
591     glShadeModel(GL_SMOOTH);
592     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
593     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
594     glEnable(GL_LIGHTING);
595     glEnable(GL_LIGHT0);
596     glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
597     glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
598     glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
599     glLightfv(GL_LIGHT0,GL_POSITION,light_position);
600     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
601     glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
602     glDepthMask(GL_TRUE);
603     glDisable(GL_BLEND);
604   }
605   else if (display_mode == DISP_TRANSPARENT)
606   {
607     glDisable(GL_DEPTH_TEST);
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_FALSE);
620     glEnable(GL_BLEND);
621     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
622   }
623   else
624   {
625     glDisable(GL_DEPTH_TEST);
626     glShadeModel(GL_FLAT);
627     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
628     glDisable(GL_LIGHTING);
629     glDisable(GL_LIGHT0);
630     glDisable(GL_BLEND);
631   }
632 }
633
634
635 /* Redisplay the hypertorus. */
636 static void display_hypertorus(void)
637 {
638   alpha += speed_wx;
639   if (alpha >= 360.0)
640     alpha -= 360.0;
641   beta += speed_wy;
642   if (beta >= 360.0)
643     beta -= 360.0;
644   delta += speed_wz;
645   if (delta >= 360.0)
646     delta -= 360.0;
647   zeta += speed_xy;
648   if (zeta >= 360.0)
649     zeta -= 360.0;
650   eta += speed_xz;
651   if (eta >= 360.0)
652     eta -= 360.0;
653   theta += speed_yz;
654   if (theta >= 360.0)
655     theta -= 360.0;
656
657   glMatrixMode(GL_PROJECTION);
658   glLoadIdentity();
659   if (projection_3d == DISP_3D_ORTHOGRAPHIC)
660   {
661     if (aspect >= 1.0)
662       glOrtho(-aspect,aspect,-1.0,1.0,0.1,100.0);
663     else
664       glOrtho(-1.0,1.0,-1.0/aspect,1.0/aspect,0.1,100.0);
665   }
666   else
667   {
668     gluPerspective(60.0,aspect,0.1,100.0);
669   }
670   glMatrixMode(GL_MODELVIEW);
671   glLoadIdentity();
672
673   if (display_mode == DISP_WIREFRAME)
674     hypertorus(0.0,2.0*M_PI,0.0,2.0*M_PI,40,40);
675   else
676     hypertorus(0.0,2.0*M_PI,0.0,2.0*M_PI,60,60);
677 }
678
679
680 void reshape_hypertorus(ModeInfo * mi, int width, int height)
681 {
682   hypertorusstruct *hp = &hyper[MI_SCREEN(mi)];
683
684   hp->WindW = (GLint)width;
685   hp->WindH = (GLint)height;
686   glViewport(0,0,width,height);
687   aspect = (GLfloat)width/(GLfloat)height;
688 }
689
690
691 /*
692  *-----------------------------------------------------------------------------
693  *-----------------------------------------------------------------------------
694  *    Xlock hooks.
695  *-----------------------------------------------------------------------------
696  *-----------------------------------------------------------------------------
697  */
698
699 /*
700  *-----------------------------------------------------------------------------
701  *    Initialize hypertorus.  Called each time the window changes.
702  *-----------------------------------------------------------------------------
703  */
704
705 void init_hypertorus(ModeInfo * mi)
706 {
707   hypertorusstruct *hp;
708
709   if (hyper == NULL)
710   {
711     hyper = (hypertorusstruct *)calloc(MI_NUM_SCREENS(mi),
712                                        sizeof(hypertorusstruct));
713     if (hyper == NULL)
714       return;
715   }
716   hp = &hyper[MI_SCREEN(mi)];
717
718   if ((hp->glx_context = init_GL(mi)) != NULL)
719   {
720     reshape_hypertorus(mi,MI_WIDTH(mi),MI_HEIGHT(mi));
721     glDrawBuffer(GL_BACK);
722     init(mi);
723   }
724   else
725   {
726     MI_CLEARWINDOW(mi);
727   }
728 }
729
730 /*
731  *-----------------------------------------------------------------------------
732  *    Called by the mainline code periodically to update the display.
733  *-----------------------------------------------------------------------------
734  */
735 void draw_hypertorus(ModeInfo * mi)
736 {
737   Display          *display = MI_DISPLAY(mi);
738   Window           window = MI_WINDOW(mi);
739   hypertorusstruct *hp;
740
741   if (hyper == NULL)
742     return;
743   hp = &hyper[MI_SCREEN(mi)];
744
745   MI_IS_DRAWN(mi) = True;
746   if (!hp->glx_context)
747     return;
748
749   glXMakeCurrent(display,window,*(hp->glx_context));
750
751   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
752   glLoadIdentity();
753
754   display_hypertorus();
755
756   if (MI_IS_FPS(mi))
757     do_fps (mi);
758
759   glFlush();
760
761   glXSwapBuffers(display,window);
762 }
763
764
765 /*
766  *-----------------------------------------------------------------------------
767  *    The display is being taken away from us.  Free up malloc'ed 
768  *      memory and X resources that we've alloc'ed.  Only called
769  *      once, we must zap everything for every screen.
770  *-----------------------------------------------------------------------------
771  */
772
773 void release_hypertorus(ModeInfo * mi)
774 {
775   if (hyper != NULL)
776   {
777     int screen;
778
779     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
780     {
781       hypertorusstruct *hp = &hyper[screen];
782
783       if (hp->glx_context)
784         hp->glx_context = (GLXContext *)NULL;
785     }
786     (void) free((void *)hyper);
787     hyper = (hypertorusstruct *)NULL;
788   }
789   FreeAllGL(mi);
790 }
791
792 void change_hypertorus(ModeInfo * mi)
793 {
794   hypertorusstruct *hp = &hyper[MI_SCREEN(mi)];
795
796   if (!hp->glx_context)
797     return;
798
799   glXMakeCurrent(MI_DISPLAY(mi),MI_WINDOW(mi),*(hp->glx_context));
800   init(mi);
801 }
802
803 #endif /* USE_GL */