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