1 /* hypertorus --- Shows a hypertorus that rotates in 4d */
4 static const char sccsid[] = "@(#)hypertorus.c 1.1 03/05/18 xlockmore";
7 /* Copyright (c) 2003 Carsten Steger <carsten@mirsanmir.org>. */
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.
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.
23 * C. Steger - 03/05/18: Initial version
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.
50 #define M_PI 3.14159265358979323846
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"
60 #define APPEARANCE_SOLID 0
61 #define APPEARANCE_SOLID_STR "0"
62 #define APPEARANCE_BANDS 1
63 #define APPEARANCE_BANDS_STR "1"
65 #define COLORS_TWOSIDED 0
66 #define COLORS_TWOSIDED_STR "0"
67 #define COLORS_COLORWHEEL 1
68 #define COLORS_COLORWHEEL_STR "1"
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"
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"
81 #define DALPHA_STR "1.1"
83 #define DBETA_STR "1.3"
85 #define DDELTA_STR "1.5"
87 #define DZETA_STR "1.7"
89 #define DETA_STR "1.9"
91 #define DTHETA_STR "2.1"
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
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" \
115 # include "xlockmore.h" /* from the xscreensaver distribution */
116 #else /* !STANDALONE */
117 # include "xlock.h" /* from the xlockmore distribution */
118 #endif /* !STANDALONE */
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};
136 static int display_mode;
137 static int appearance;
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;
148 /* 4D rotation angles */
149 static float alpha, beta, delta, zeta, eta, theta;
152 static const float offset4d[4] = {
156 static const float offset3d[4] = {
161 static XrmOptionDescRec opts[] =
163 {"-mesh", ".hypertorus.displayMode", XrmoptionNoArg,
164 DISP_WIREFRAME_STR },
165 {"-surface", ".hypertorus.displayMode", XrmoptionNoArg,
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 }
193 static argtype vars[] =
195 { &display_mode, "displayMode", "DisplayMode",
196 DEF_DISPLAY_MODE, t_Int },
197 { &appearance, "appearance", "Appearance",
198 DEF_APPEARANCE, t_Int },
199 { &colors, "colors", "Colors",
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",
209 { &speed_wz, "speedwz", "Speedwz",
210 DEF_DDELTA, t_Float},
211 { &speed_xy, "speedxy", "Speedxy",
213 { &speed_xz, "speedxz", "Speedxz",
215 { &speed_yz, "speedyz", "Speedyz",
219 static OptionStruct desc[] =
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" }
240 ModeSpecOpt hypertorus_opts =
241 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
246 GLXContext *glx_context;
249 static hypertorusstruct *hyper = (hypertorusstruct *) NULL;
252 /* Add a rotation around the wx-plane to the matrix m. */
253 static void rotatewx(float m[4][4], float phi)
271 /* Add a rotation around the wy-plane to the matrix m. */
272 static void rotatewy(float m[4][4], float phi)
290 /* Add a rotation around the wz-plane to the matrix m. */
291 static void rotatewz(float m[4][4], float phi)
309 /* Add a rotation around the xy-plane to the matrix m. */
310 static void rotatexy(float m[4][4], float phi)
328 /* Add a rotation around the xz-plane to the matrix m. */
329 static void rotatexz(float m[4][4], float phi)
347 /* Add a rotation around the yz-plane to the matrix m. */
348 static void rotateyz(float m[4][4], float phi)
366 /* Compute a fully saturated and bright color based on an angle. */
367 static void color(double angle)
373 if (colors != COLORS_COLORWHEEL)
377 angle = fmod(angle,2*M_PI);
379 angle = fmod(angle,-2*M_PI);
380 s = floor(angle/(M_PI/3));
381 t = angle/(M_PI/3)-s;
417 if (display_mode == DISP_TRANSPARENT)
422 glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color);
426 /* Draw a hyperturus projected into 3D. */
427 static void hypertorus(double umin, double umax, double vmin, double vmax,
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];
437 double cu, su, cv, sv;
438 double xx[4], xxu[4], xxv[4], x[4], xu[4], xv[4];
441 /* Compute the rotation that rotates the hypercube in 4D. */
452 if (colors != COLORS_COLORWHEEL)
454 glColor3fv(mat_diff_red);
455 if (display_mode == DISP_TRANSPARENT)
457 glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_red);
458 glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_green);
462 glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_red);
463 glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_green);
469 for (i=0; i<numu; i++)
471 if (appearance == APPEARANCE_BANDS && ((i & 3) >= 2))
473 if (display_mode == DISP_WIREFRAME)
474 glBegin(GL_QUAD_STRIP);
476 glBegin(GL_TRIANGLE_STRIP);
477 for (j=0; j<=numv; j++)
509 r += mat[l][m]*xx[m];
510 s += mat[l][m]*xxu[m];
511 t += mat[l][m]*xxv[m];
517 if (projection_4d == DISP_4D_ORTHOGRAPHIC)
521 p[l] = (x[l]+offset4d[l])/1.5+offset3d[l];
528 s = x[3]+offset4d[3];
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;
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]);
554 static void init(ModeInfo *mi)
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 };
569 glMatrixMode(GL_PROJECTION);
571 if (projection_3d == DISP_3D_PERSPECTIVE)
572 gluPerspective(60.0,1.0,0.1,100.0);
574 glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
575 glMatrixMode(GL_MODELVIEW);
578 if (display_mode == DISP_WIREFRAME)
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);
587 else if (display_mode == DISP_SURFACE)
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);
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);
605 else if (display_mode == DISP_TRANSPARENT)
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);
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);
621 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
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);
635 /* Redisplay the hypertorus. */
636 static void display_hypertorus(void)
657 glMatrixMode(GL_PROJECTION);
659 if (projection_3d == DISP_3D_ORTHOGRAPHIC)
662 glOrtho(-aspect,aspect,-1.0,1.0,0.1,100.0);
664 glOrtho(-1.0,1.0,-1.0/aspect,1.0/aspect,0.1,100.0);
668 gluPerspective(60.0,aspect,0.1,100.0);
670 glMatrixMode(GL_MODELVIEW);
673 if (display_mode == DISP_WIREFRAME)
674 hypertorus(0.0,2.0*M_PI,0.0,2.0*M_PI,40,40);
676 hypertorus(0.0,2.0*M_PI,0.0,2.0*M_PI,60,60);
680 void reshape_hypertorus(ModeInfo * mi, int width, int height)
682 hypertorusstruct *hp = &hyper[MI_SCREEN(mi)];
684 hp->WindW = (GLint)width;
685 hp->WindH = (GLint)height;
686 glViewport(0,0,width,height);
687 aspect = (GLfloat)width/(GLfloat)height;
692 *-----------------------------------------------------------------------------
693 *-----------------------------------------------------------------------------
695 *-----------------------------------------------------------------------------
696 *-----------------------------------------------------------------------------
700 *-----------------------------------------------------------------------------
701 * Initialize hypertorus. Called each time the window changes.
702 *-----------------------------------------------------------------------------
705 void init_hypertorus(ModeInfo * mi)
707 hypertorusstruct *hp;
711 hyper = (hypertorusstruct *)calloc(MI_NUM_SCREENS(mi),
712 sizeof(hypertorusstruct));
716 hp = &hyper[MI_SCREEN(mi)];
718 if ((hp->glx_context = init_GL(mi)) != NULL)
720 reshape_hypertorus(mi,MI_WIDTH(mi),MI_HEIGHT(mi));
721 glDrawBuffer(GL_BACK);
731 *-----------------------------------------------------------------------------
732 * Called by the mainline code periodically to update the display.
733 *-----------------------------------------------------------------------------
735 void draw_hypertorus(ModeInfo * mi)
737 Display *display = MI_DISPLAY(mi);
738 Window window = MI_WINDOW(mi);
739 hypertorusstruct *hp;
743 hp = &hyper[MI_SCREEN(mi)];
745 MI_IS_DRAWN(mi) = True;
746 if (!hp->glx_context)
749 glXMakeCurrent(display,window,*(hp->glx_context));
751 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
754 display_hypertorus();
761 glXSwapBuffers(display,window);
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 *-----------------------------------------------------------------------------
773 void release_hypertorus(ModeInfo * mi)
779 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
781 hypertorusstruct *hp = &hyper[screen];
784 hp->glx_context = (GLXContext *)NULL;
786 (void) free((void *)hyper);
787 hyper = (hypertorusstruct *)NULL;
792 void change_hypertorus(ModeInfo * mi)
794 hypertorusstruct *hp = &hyper[MI_SCREEN(mi)];
796 if (!hp->glx_context)
799 glXMakeCurrent(MI_DISPLAY(mi),MI_WINDOW(mi),*(hp->glx_context));