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