1 /* projectiveplane --- Shows a 4d embedding of the real projective plane
2 that rotates in 4d or on which you can walk */
5 static const char sccsid[] = "@(#)projectiveplane.c 1.1 14/01/01 xlockmore";
8 /* Copyright (c) 2005-2014 Carsten Steger <carsten@mirsanmir.org>. */
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
24 * C. Steger - 14/01/03: Initial version
28 * This program shows a 4d embedding of the real projective plane.
29 * You can walk on the projective plane, see it turn in 4d, or walk on
30 * it while it turns in 4d. The fact that the surface is an embedding
31 * of the real projective plane in 4d can be seen in the depth colors
32 * mode: set all rotation speeds to 0 and the projection mode to 4d
33 * orthographic projection. In its default orientation, the embedding
34 * of the real projective plane will then project to the Roman
35 * surface, which has three lines of self-intersection. However, at
36 * the three lines of self-intersection the parts of the surface that
37 * intersect have different colors, i.e., different 4d depths.
39 * The real projective plane is a non-orientable surface. To make
40 * this apparent, the two-sided color mode can be used.
41 * Alternatively, orientation markers (curling arrows) can be drawn as
42 * a texture map on the surface of the projective plane. While
43 * walking on the projective plane, you will notice that the
44 * orientation of the curling arrows changes (which it must because
45 * the projective plane is non-orientable).
47 * The real projective plane is a model for the projective geometry in
48 * 2d space. One point can be singled out as the origin. A line can
49 * be singled out as the line at infinity, i.e., a line that lies at
50 * an infinite distance to the origin. The line at infinity is
51 * topologically a circle. Points on the line at infinity are also
52 * used to model directions in projective geometry. The origin can be
53 * visualized in different manners. When using distance colors, the
54 * origin is the point that is displayed as fully saturated red, which
55 * is easier to see as the center of the reddish area on the
56 * projective plane. Alternatively, when using distance bands, the
57 * origin is the center of the only band that projects to a disc.
58 * When using direction bands, the origin is the point where all
59 * direction bands collapse to a point. Finally, when orientation
60 * markers are being displayed, the origin the the point where all
61 * orientation markers are compressed to a point. The line at
62 * infinity can also be visualized in different ways. When using
63 * distance colors, the line at infinity is the line that is displayed
64 * as fully saturated magenta. When two-sided colors are used, the
65 * line at infinity lies at the points where the red and green "sides"
66 * of the projective plane meet (of course, the real projective plane
67 * only has one side, so this is a design choice of the
68 * visualization). Alternatively, when orientation markers are being
69 * displayed, the line at infinity is the place where the orientation
70 * markers change their orientation.
72 * Note that when the projective plane is displayed with bands, the
73 * orientation markers are placed in the middle of the bands. For
74 * distance bands, the bands are chosen in such a way that the band at
75 * the origin is only half as wide as the remaining bands, which
76 * results in a disc being displayed at the origin that has the same
77 * diameter as the remaining bands. This choice, however, also
78 * implies that the band at infinity is half as wide as the other
79 * bands. Since the projective plane is attached to itself (in a
80 * complicated fashion) at the line at infinity, effectively the band
81 * at infinity is again as wide as the remaining bands. However,
82 * since the orientation markers are displayed in the middle of the
83 * bands, this means that only one half of the orientation markers
84 * will be displayed twice at the line at infinity if distance bands
85 * are used. If direction bands are used or if the projective plane
86 * is displayed as a solid surface, the orientation markers are
87 * displayed fully at the respective sides of the line at infinity.
89 * The program projects the 4d projective plane to 3d using either a
90 * perspective or an orthographic projection. Which of the two
91 * alternatives looks more appealing is up to you. However, two
92 * famous surfaces are obtained if orthographic 4d projection is used:
93 * The Roman surface and the cross cap. If the projective plane is
94 * rotated in 4d, the result of the projection for certain rotations
95 * is a Roman surface and for certain rotations it is a cross cap.
96 * The easiest way to see this is to set all rotation speeds to 0 and
97 * the rotation speed around the yz plane to a value different from 0.
98 * However, for any 4d rotation speeds, the projections will generally
99 * cycle between the Roman surface and the cross cap. The difference
100 * is where the origin and the line at infinity will lie with respect
101 * to the self-intersections in the projections to 3d.
103 * The projected projective plane can then be projected to the screen
104 * either perspectively or orthographically. When using the walking
105 * modes, perspective projection to the screen will be used.
107 * There are three display modes for the projective plane: mesh
108 * (wireframe), solid, or transparent. Furthermore, the appearance of
109 * the projective plane can be as a solid object or as a set of
110 * see-through bands. The bands can be distance bands, i.e., bands
111 * that lie at increasing distances from the origin, or direction
112 * bands, i.e., bands that lie at increasing angles with respect to
115 * When the projective plane is displayed with direction bands, you
116 * will be able to see that each direction band (modulo the "pinching"
117 * at the origin) is a Moebius strip, which also shows that the
118 * projective plane is non-orientable.
120 * Finally, the colors with with the projective plane is drawn can be
121 * set to two-sided, distance, direction, or depth. In two-sided
122 * mode, the projective plane is drawn with red on one "side" and
123 * green on the "other side". As described above, the projective
124 * plane only has one side, so the color jumps from red to green along
125 * the line at infinity. This mode enables you to see that the
126 * projective plane is non-orientable. In distance mode, the
127 * projective plane is displayed with fully saturated colors that
128 * depend on the distance of the points on the projective plane to the
129 * origin. The origin is displayed in red, the line at infinity is
130 * displayed in magenta. If the projective plane is displayed as
131 * distance bands, each band will be displayed with a different color.
132 * In direction mode, the projective plane is displayed with fully
133 * saturated colors that depend on the angle of the points on the
134 * projective plane with respect to the origin. Angles in opposite
135 * directions to the origin (e.g., 15 and 205 degrees) are displayed
136 * in the same color since they are projectively equivalent. If the
137 * projective plane is displayed as direction bands, each band will be
138 * displayed with a different color. Finally, in depth mode the
139 * projective plane with colors chosen depending on the 4d "depth"
140 * (i.e., the w coordinate) of the points on the projective plane at
141 * its default orientation in 4d. As discussed above, this mode
142 * enables you to see that the projective plane does not intersect
145 * The rotation speed for each of the six planes around which the
146 * projective plane rotates can be chosen. For the walk-and-turn
147 * more, only the rotation speeds around the true 4d planes are used
148 * (the xy, xz, and yz planes).
150 * Furthermore, in the walking modes the walking direction in the 2d
151 * base square of the projective plane and the walking speed can be
152 * chosen. The walking direction is measured as an angle in degrees
153 * in the 2d square that forms the coordinate system of the surface of
154 * the projective plane. A value of 0 or 180 means that the walk is
155 * along a circle at a randomly chosen distance from the origin
156 * (parallel to a distance band). A value of 90 or 270 means that the
157 * walk is directly from the origin to the line at infinity and back
158 * (analogous to a direction band). Any other value results in a
159 * curved path from the origin to the line at infinity and back.
161 * This program is somewhat inspired by Thomas Banchoff's book "Beyond
162 * the Third Dimension: Geometry, Computer Graphics, and Higher
163 * Dimensions", Scientific American Library, 1990.
167 #define M_PI 3.14159265358979323846
170 #define DISP_WIREFRAME 0
171 #define DISP_SURFACE 1
172 #define DISP_TRANSPARENT 2
173 #define NUM_DISPLAY_MODES 3
175 #define APPEARANCE_SOLID 0
176 #define APPEARANCE_DISTANCE_BANDS 1
177 #define APPEARANCE_DIRECTION_BANDS 2
178 #define NUM_APPEARANCES 3
180 #define COLORS_TWOSIDED 0
181 #define COLORS_DISTANCE 1
182 #define COLORS_DIRECTION 2
183 #define COLORS_DEPTH 3
188 #define VIEW_WALKTURN 2
189 #define NUM_VIEW_MODES 3
191 #define DISP_3D_PERSPECTIVE 0
192 #define DISP_3D_ORTHOGRAPHIC 1
193 #define NUM_DISP_3D_MODES 2
195 #define DISP_4D_PERSPECTIVE 0
196 #define DISP_4D_ORTHOGRAPHIC 1
197 #define NUM_DISP_4D_MODES 2
199 #define DEF_DISPLAY_MODE "random"
200 #define DEF_APPEARANCE "random"
201 #define DEF_COLORS "random"
202 #define DEF_VIEW_MODE "random"
203 #define DEF_MARKS "False"
204 #define DEF_PROJECTION_3D "random"
205 #define DEF_PROJECTION_4D "random"
206 #define DEF_SPEEDWX "1.1"
207 #define DEF_SPEEDWY "1.3"
208 #define DEF_SPEEDWZ "1.5"
209 #define DEF_SPEEDXY "1.7"
210 #define DEF_SPEEDXZ "1.9"
211 #define DEF_SPEEDYZ "2.1"
212 #define DEF_WALK_DIRECTION "83.0"
213 #define DEF_WALK_SPEED "20.0"
216 # define DEFAULTS "*delay: 10000 \n" \
217 "*showFPS: False \n" \
219 # define refresh_projectiveplane 0
220 # include "xlockmore.h" /* from the xscreensaver distribution */
221 #else /* !STANDALONE */
222 # include "xlock.h" /* from the xlockmore distribution */
223 #endif /* !STANDALONE */
228 # include <X11/keysym.h>
231 #include "gltrackball.h"
237 ModStruct projectiveplane_description =
238 {"projectiveplane", "init_projectiveplane", "draw_projectiveplane",
239 "release_projectiveplane", "draw_projectiveplane", "change_projectiveplane",
240 NULL, &projectiveplane_opts, 25000, 1, 1, 1, 1.0, 4, "",
241 "Rotate a 4d embedding of the real projective plane in 4d or walk on it",
248 static int display_mode;
250 static int appearance;
251 static char *color_mode;
253 static char *view_mode;
256 static char *proj_3d;
257 static int projection_3d;
258 static char *proj_4d;
259 static int projection_4d;
260 static float speed_wx;
261 static float speed_wy;
262 static float speed_wz;
263 static float speed_xy;
264 static float speed_xz;
265 static float speed_yz;
266 static float walk_direction;
267 static float walk_speed;
270 static XrmOptionDescRec opts[] =
272 {"-mode", ".displayMode", XrmoptionSepArg, 0 },
273 {"-wireframe", ".displayMode", XrmoptionNoArg, "wireframe" },
274 {"-surface", ".displayMode", XrmoptionNoArg, "surface" },
275 {"-transparent", ".displayMode", XrmoptionNoArg, "transparent" },
276 {"-appearance", ".appearance", XrmoptionSepArg, 0 },
277 {"-solid", ".appearance", XrmoptionNoArg, "solid" },
278 {"-distance-bands", ".appearance", XrmoptionNoArg, "distance-bands" },
279 {"-direction-bands", ".appearance", XrmoptionNoArg, "direction-bands" },
280 {"-colors", ".colors", XrmoptionSepArg, 0 },
281 {"-twosided-colors", ".colors", XrmoptionNoArg, "two-sided" },
282 {"-distance-colors", ".colors", XrmoptionNoArg, "distance" },
283 {"-direction-colors", ".colors", XrmoptionNoArg, "direction" },
284 {"-depth-colors", ".colors", XrmoptionNoArg, "depth" },
285 {"-view-mode", ".viewMode", XrmoptionSepArg, 0 },
286 {"-walk", ".viewMode", XrmoptionNoArg, "walk" },
287 {"-turn", ".viewMode", XrmoptionNoArg, "turn" },
288 {"-walk-turn", ".viewMode", XrmoptionNoArg, "walk-turn" },
289 {"-orientation-marks", ".marks", XrmoptionNoArg, "on"},
290 {"+orientation-marks", ".marks", XrmoptionNoArg, "off"},
291 {"-projection-3d", ".projection3d", XrmoptionSepArg, 0 },
292 {"-perspective-3d", ".projection3d", XrmoptionNoArg, "perspective" },
293 {"-orthographic-3d", ".projection3d", XrmoptionNoArg, "orthographic" },
294 {"-projection-4d", ".projection4d", XrmoptionSepArg, 0 },
295 {"-perspective-4d", ".projection4d", XrmoptionNoArg, "perspective" },
296 {"-orthographic-4d", ".projection4d", XrmoptionNoArg, "orthographic" },
297 {"-speed-wx", ".speedwx", XrmoptionSepArg, 0 },
298 {"-speed-wy", ".speedwy", XrmoptionSepArg, 0 },
299 {"-speed-wz", ".speedwz", XrmoptionSepArg, 0 },
300 {"-speed-xy", ".speedxy", XrmoptionSepArg, 0 },
301 {"-speed-xz", ".speedxz", XrmoptionSepArg, 0 },
302 {"-speed-yz", ".speedyz", XrmoptionSepArg, 0 },
303 {"-walk-direction", ".walkDirection", XrmoptionSepArg, 0 },
304 {"-walk-speed", ".walkSpeed", XrmoptionSepArg, 0 }
307 static argtype vars[] =
309 { &mode, "displayMode", "DisplayMode", DEF_DISPLAY_MODE, t_String },
310 { &appear, "appearance", "Appearance", DEF_APPEARANCE, t_String },
311 { &color_mode, "colors", "Colors", DEF_COLORS, t_String },
312 { &view_mode, "viewMode", "ViewMode", DEF_VIEW_MODE, t_String },
313 { &marks, "marks", "Marks", DEF_MARKS, t_Bool },
314 { &proj_3d, "projection3d", "Projection3d", DEF_PROJECTION_3D, t_String },
315 { &proj_4d, "projection4d", "Projection4d", DEF_PROJECTION_4D, t_String },
316 { &speed_wx, "speedwx", "Speedwx", DEF_SPEEDWX, t_Float},
317 { &speed_wy, "speedwy", "Speedwy", DEF_SPEEDWY, t_Float},
318 { &speed_wz, "speedwz", "Speedwz", DEF_SPEEDWZ, t_Float},
319 { &speed_xy, "speedxy", "Speedxy", DEF_SPEEDXY, t_Float},
320 { &speed_xz, "speedxz", "Speedxz", DEF_SPEEDXZ, t_Float},
321 { &speed_yz, "speedyz", "Speedyz", DEF_SPEEDYZ, t_Float},
322 { &walk_direction, "walkDirection", "WalkDirection", DEF_WALK_DIRECTION, t_Float},
323 { &walk_speed, "walkSpeed", "WalkSpeed", DEF_WALK_SPEED, t_Float}
326 ENTRYPOINT ModeSpecOpt projectiveplane_opts =
327 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, NULL};
330 /* Offset by which we walk above the projective plane */
333 /* Number of subdivisions of the projective plane */
337 /* Number of subdivisions per band */
343 GLXContext *glx_context;
344 /* 4D rotation angles */
345 float alpha, beta, delta, zeta, eta, theta;
346 /* Movement parameters */
347 float umove, vmove, dumove, dvmove;
349 /* The viewing offset in 4d */
351 /* The viewing offset in 3d */
353 /* The 4d coordinates of the projective plane and their derivatives */
354 float x[(NUMU+1)*(NUMV+1)][4];
355 float xu[(NUMU+1)*(NUMV+1)][4];
356 float xv[(NUMU+1)*(NUMV+1)][4];
357 float pp[(NUMU+1)*(NUMV+1)][3];
358 float pn[(NUMU+1)*(NUMV+1)][3];
359 /* The precomputed colors of the projective plane */
360 float col[(NUMU+1)*(NUMV+1)][4];
361 /* The precomputed texture coordinates of the projective plane */
362 float tex[(NUMU+1)*(NUMV+1)][2];
363 /* The "curlicue" texture */
365 /* Aspect ratio of the current window */
367 /* Trackball states */
368 trackball_state *trackballs[2];
369 int current_trackball;
371 /* A random factor to modify the rotation speeds */
373 } projectiveplanestruct;
375 static projectiveplanestruct *projectiveplane = (projectiveplanestruct *) NULL;
378 /* A texture map containing a "curlicue" */
379 #define TEX_DIMENSION 64
380 static const unsigned char texture[TEX_DIMENSION*TEX_DIMENSION] = {
381 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
382 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
383 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
384 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
385 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
386 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
387 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
388 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
389 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
390 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
391 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
392 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
393 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
394 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
395 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
396 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
397 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
398 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
399 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
400 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
401 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
402 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
403 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
404 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
405 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
406 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
407 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
408 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
409 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
410 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
411 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
412 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
413 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
414 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
415 255,255,255,255,255,255,255, 58, 43, 43, 43, 43, 45, 70, 70, 70,
416 70, 70, 70, 70, 74, 98, 98, 98,100,194,255,255,255,255,255,255,
417 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
418 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
419 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
420 0, 0, 0, 0, 0, 0, 0, 30,186,255,255,255,255,255,255,255,
421 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
422 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
423 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
424 0, 0, 0, 0, 0, 1,111,244,255,255,255,255,255,255,255,255,
425 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
426 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
427 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
428 0, 0, 0, 0, 43,198,255,255,255,255,255,255,255,255,255,255,
429 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
430 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
431 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
432 0, 0, 5,123,248,255,255,255,255,255,255,255,255,255,255,255,
433 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
434 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
435 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
436 0, 50,209,255,255,255,255,255,255,255,255,255,255,255,255,255,
437 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,246,
438 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
439 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
440 74,252,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
441 255,255,255,255,255,255,255,255,255,255,255,255,255,255,138, 4,
442 66,229,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
443 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
444 1,170,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
445 255,255,255,255,255,255,255,255,255,255,255,255,255,153, 0, 0,
446 0, 53,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
447 255,255,255,255,255,255,255, 18, 0, 0, 0, 0, 0, 0, 0, 0,
448 0, 6,188,255,255,255,255,255,255,255,255,255,255,255,255,255,
449 255,255,255,255,255,255,255,255,255,255,255,255,213, 7, 0, 0,
450 0, 0,226,255,255,255,255,255,255,255,255,255,255,255,255,255,
451 255,255,255,255,255,255,255, 45, 0, 0, 0, 0, 0, 47, 0, 0,
452 0, 0, 22,225,255,255,255,255,255,255,255,255,255,255,255,255,
453 255,255,255,255,255,255,255,255,255,255,255,254, 54, 0, 0, 0,
454 0, 81,254,255,255,255,255,255,255,255,255,255,255,255,255,255,
455 255,255,255,255,255,255,255, 45, 0, 0, 0, 0, 56,247, 82, 0,
456 0, 0, 0, 59,253,255,255,255,255,255,255,255,255,255,255,255,
457 255,255,255,255,255,255,255,255,255,255,255,152, 0, 0, 0, 0,
458 52,243,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
459 255,255,255,255,255,255,255, 45, 0, 0, 0, 8,215,255,250, 56,
460 0, 0, 0, 0,142,255,255,255,255,255,255,255,255,255,255,255,
461 255,255,255,255,255,255,255,255,255,255,241, 19, 0, 0, 0, 15,
462 220,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
463 255,255,255,255,255,255,255, 45, 0, 0, 0,129,255,255,255,230,
464 23, 0, 0, 0, 12,230,255,255,255,255,255,255,255,255,255,255,
465 255,255,255,255,255,255,255,255,255,255,131, 0, 0, 0, 0,157,
466 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
467 255,255,255,255,255,255,255, 45, 0, 0, 49,250,255,255,255,255,
468 171, 0, 0, 0, 0,112,255,255,255,255,255,255,255,255,255,255,
469 255,255,255,255,255,255,255,255,255,246, 19, 0, 0, 0, 54,253,
470 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
471 255,255,255,255,255,255,255, 45, 0, 5,208,255,255,255,255,255,
472 255, 77, 0, 0, 0, 9,231,255,255,255,255,255,255,255,255,255,
473 255,255,255,255,255,255,255,255,255,163, 0, 0, 0, 0,186,255,
474 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
475 255,255,255,255,255,255,255, 45, 0,121,255,255,255,255,255,255,
476 255,211, 2, 0, 0, 0,134,255,255,255,255,255,255,255,255,255,
477 255,255,255,255,255,255,255,255,255, 69, 0, 0, 0, 50,255,255,
478 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
479 255,255,255,255,255,255,255, 45, 41,247,255,255,255,255,255,255,
480 255,255, 73, 0, 0, 0, 38,254,255,255,255,255,255,255,255,255,
481 255,255,255,255,255,255,255,255,237, 4, 0, 0, 0,145,255,255,
482 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
483 255,255,255,255,255,255,255, 52,201,255,255,255,255,255,255,255,
484 255,255,169, 0, 0, 0, 0,216,255,255,255,255,255,255,255,255,
485 255,255,255,255,255,255,255,255,181, 0, 0, 0, 0,229,255,255,
486 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
487 255,255,255,255,255,255,255,186,255,255,255,255,255,255,255,255,
488 255,255,247, 7, 0, 0, 0,150,255,255,255,255,255,255,255,255,
489 255,255,255,255,255,255,255,255,130, 0, 0, 0, 42,255,255,255,
490 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
491 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
492 255,255,255, 67, 0, 0, 0, 91,255,255,255,255,255,255,255,255,
493 255,255,255,255,255,255,255,255, 79, 0, 0, 0, 95,255,255,255,
494 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
495 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
496 255,255,255,120, 0, 0, 0, 56,255,255,255,255,255,255,255,255,
497 255,255,255,255,255,255,255,255, 55, 0, 0, 0,130,255,255,255,
498 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
499 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
500 255,255,255,157, 0, 0, 0, 21,255,255,255,255,255,255,255,255,
501 255,255,255,255,255,255,255,255, 34, 0, 0, 0,161,255,255,255,
502 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
503 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
504 255,255,255,179, 0, 0, 0, 2,250,255,255,255,255,255,255,255,
505 255,255,255,255,255,255,255,255, 27, 0, 0, 0,168,255,255,255,
506 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
507 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
508 255,255,255,200, 0, 0, 0, 0,249,255,255,255,255,255,255,255,
509 255,255,255,255,255,255,255,255, 27, 0, 0, 0,168,255,255,255,
510 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
511 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
512 255,255,255,200, 0, 0, 0, 0,249,255,255,255,255,255,255,255,
513 255,255,255,255,255,255,255,255, 27, 0, 0, 0,163,255,255,255,
514 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
515 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
516 255,255,255,183, 0, 0, 0, 0,249,255,255,255,255,255,255,255,
517 255,255,255,255,255,255,255,255, 42, 0, 0, 0,135,255,255,255,
518 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
519 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
520 255,255,255,161, 0, 0, 0, 17,254,255,255,255,255,255,255,255,
521 255,255,255,255,255,255,255,255, 76, 0, 0, 0,100,255,255,255,
522 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
523 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
524 255,255,255,126, 0, 0, 0, 48,255,255,255,255,255,255,255,255,
525 255,255,255,255,255,255,255,255,114, 0, 0, 0, 53,255,255,255,
526 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
527 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
528 255,255,255, 78, 0, 0, 0, 84,255,255,255,255,255,255,255,255,
529 255,255,255,255,255,255,255,255,165, 0, 0, 0, 3,241,255,255,
530 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
531 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
532 255,255,252, 16, 0, 0, 0,139,255,255,255,255,255,255,255,255,
533 255,255,255,255,255,255,255,255,228, 0, 0, 0, 0,161,255,255,
534 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
535 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
536 255,255,192, 0, 0, 0, 0,198,255,255,255,255,255,255,255,255,
537 255,255,255,255,255,255,255,255,255, 46, 0, 0, 0, 67,255,255,
538 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
539 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
540 255,255, 93, 0, 0, 0, 21,250,255,255,255,255,255,255,255,255,
541 255,255,255,255,255,255,255,255,255,139, 0, 0, 0, 1,211,255,
542 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
543 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
544 255,226, 7, 0, 0, 0,108,255,255,255,255,255,255,255,255,255,
545 255,255,255,255,255,255,255,255,255,230, 6, 0, 0, 0, 79,255,
546 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
547 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
548 255,106, 0, 0, 0, 1,206,255,255,255,255,255,255,255,255,255,
549 255,255,255,255,255,255,255,255,255,255, 97, 0, 0, 0, 0,183,
550 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
551 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
552 202, 3, 0, 0, 0, 67,255,255,255,255,255,255,255,255,255,255,
553 255,255,255,255,255,255,255,255,255,255,221, 8, 0, 0, 0, 27,
554 235,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
555 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,243,
556 40, 0, 0, 0, 0,198,255,255,255,255,255,255,255,255,255,255,
557 255,255,255,255,255,255,255,255,255,255,255,126, 0, 0, 0, 0,
558 71,252,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
559 255,255,255,255,255,255,255,255,255,255,255,255,255,255,253, 85,
560 0, 0, 0, 0, 96,255,255,255,255,255,255,255,255,255,255,255,
561 255,255,255,255,255,255,255,255,255,255,255,247, 44, 0, 0, 0,
562 0, 91,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
563 255,255,255,255,255,255,255,255,255,255,255,255,255,255,116, 0,
564 0, 0, 0, 25,233,255,255,255,255,255,255,255,255,255,255,255,
565 255,255,255,255,255,255,255,255,255,255,255,255,216, 11, 0, 0,
566 0, 0, 90,251,255,255,255,255,255,255,255,255,255,255,255,255,
567 255,255,255,255,255,255,255,255,255,255,255,255,252,112, 0, 0,
568 0, 0, 4,191,255,255,255,255,255,255,255,255,255,255,255,255,
569 255,255,255,255,255,255,255,255,255,255,255,255,255,174, 4, 0,
570 0, 0, 0, 72,235,255,255,255,255,255,255,255,255,255,255,255,
571 255,255,255,255,255,255,255,255,255,255,255,242, 84, 0, 0, 0,
572 0, 0,146,255,255,255,255,255,255,255,255,255,255,255,255,255,
573 255,255,255,255,255,255,255,255,255,255,255,255,255,255,150, 1,
574 0, 0, 0, 0, 27,181,255,255,255,255,255,255,255,255,255,255,
575 255,255,255,255,255,255,255,255,255,255,194, 39, 0, 0, 0, 0,
576 0,120,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
577 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,151,
578 4, 0, 0, 0, 0, 0, 77,209,255,255,255,255,255,255,255,255,
579 255,255,255,255,255,255,255,255,216, 92, 1, 0, 0, 0, 0, 0,
580 125,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
581 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
582 175, 12, 0, 0, 0, 0, 0, 1, 70,164,241,255,255,255,255,255,
583 255,255,255,255,255,242,171, 77, 2, 0, 0, 0, 0, 0, 4,150,
584 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
585 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
586 255,214, 41, 0, 0, 0, 0, 0, 0, 0, 4, 48, 98,138,163,163,
587 163,163,140,103, 55, 5, 0, 0, 0, 0, 0, 0, 0, 30,199,255,
588 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
589 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
590 255,255,245,125, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,105,240,255,255,
592 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
593 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
594 255,255,255,255,222,100, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
595 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 83,210,255,255,255,255,
596 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
597 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
598 255,255,255,255,255,255,228,136, 45, 0, 0, 0, 0, 0, 0, 0,
599 0, 0, 0, 0, 0, 0, 0, 37,125,220,255,255,255,255,255,255,
600 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
601 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
602 255,255,255,255,255,255,255,255,255,225,166,112, 74, 43, 32, 12,
603 8, 32, 40, 71,105,162,218,255,255,255,255,255,255,255,255,255,
604 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
605 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
606 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
607 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
608 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
609 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
610 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
611 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
612 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
613 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
614 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
615 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
616 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
617 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
618 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
619 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
620 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
621 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
622 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
623 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
624 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
625 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
626 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
627 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
628 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
629 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
630 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
631 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
632 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
633 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
634 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
635 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
636 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
640 /* Add a rotation around the wx-plane to the matrix m. */
641 static void rotatewx(float m[4][4], float phi)
659 /* Add a rotation around the wy-plane to the matrix m. */
660 static void rotatewy(float m[4][4], float phi)
678 /* Add a rotation around the wz-plane to the matrix m. */
679 static void rotatewz(float m[4][4], float phi)
697 /* Add a rotation around the xy-plane to the matrix m. */
698 static void rotatexy(float m[4][4], float phi)
716 /* Add a rotation around the xz-plane to the matrix m. */
717 static void rotatexz(float m[4][4], float phi)
735 /* Add a rotation around the yz-plane to the matrix m. */
736 static void rotateyz(float m[4][4], float phi)
754 /* Compute the rotation matrix m from the rotation angles. */
755 static void rotateall(float al, float be, float de, float ze, float et,
756 float th, float m[4][4])
772 /* Compute the rotation matrix m from the 4d rotation angles. */
773 static void rotateall4d(float ze, float et, float th, float m[4][4])
786 /* Multiply two rotation matrices: o=m*n. */
787 static void mult_rotmat(float m[4][4], float n[4][4], float o[4][4])
797 o[i][j] += m[i][k]*n[k][j];
803 /* Compute a 4D rotation matrix from two unit quaternions. */
804 static void quats_to_rotmat(float p[4], float q[4], float m[4][4])
806 double al, be, de, ze, et, th;
807 double r00, r01, r02, r12, r22;
809 r00 = 1.0-2.0*(p[1]*p[1]+p[2]*p[2]);
810 r01 = 2.0*(p[0]*p[1]+p[2]*p[3]);
811 r02 = 2.0*(p[2]*p[0]-p[1]*p[3]);
812 r12 = 2.0*(p[1]*p[2]+p[0]*p[3]);
813 r22 = 1.0-2.0*(p[1]*p[1]+p[0]*p[0]);
815 al = atan2(-r12,r22)*180.0/M_PI;
816 be = atan2(r02,sqrt(r00*r00+r01*r01))*180.0/M_PI;
817 de = atan2(-r01,r00)*180.0/M_PI;
819 r00 = 1.0-2.0*(q[1]*q[1]+q[2]*q[2]);
820 r01 = 2.0*(q[0]*q[1]+q[2]*q[3]);
821 r02 = 2.0*(q[2]*q[0]-q[1]*q[3]);
822 r12 = 2.0*(q[1]*q[2]+q[0]*q[3]);
823 r22 = 1.0-2.0*(q[1]*q[1]+q[0]*q[0]);
825 et = atan2(-r12,r22)*180.0/M_PI;
826 th = atan2(r02,sqrt(r00*r00+r01*r01))*180.0/M_PI;
827 ze = atan2(-r01,r00)*180.0/M_PI;
829 rotateall(al,be,de,ze,et,-th,m);
833 /* Compute a fully saturated and bright color based on an angle. */
834 static void color(double angle, float col[4])
839 if (colors == COLORS_TWOSIDED)
843 angle = fmod(angle,2.0*M_PI);
845 angle = fmod(angle,-2.0*M_PI);
846 s = floor(angle/(M_PI/3));
847 t = angle/(M_PI/3)-s;
883 if (display_mode == DISP_TRANSPARENT)
890 /* Set up the projective plane coordinates, colors, and texture. */
891 static void setup_projective_plane(ModeInfo *mi, double umin, double umax,
892 double vmin, double vmax)
896 double cu, su, cv2, sv2, cv4, sv4, c2u, s2u;
897 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
901 for (i=0; i<=NUMV; i++)
903 for (j=0; j<=NUMU; j++)
906 if (appearance != APPEARANCE_DIRECTION_BANDS)
918 if (colors == COLORS_DEPTH)
919 color(((su*su*sv4*sv4-cv4*cv4)+1.0)*M_PI*2.0/3.0,pp->col[k]);
920 else if (colors == COLORS_DIRECTION)
921 color(2.0*M_PI+fmod(2.0*u,2.0*M_PI),pp->col[k]);
922 else /* colors == COLORS_DISTANCE */
923 color(v*(5.0/6.0),pp->col[k]);
924 pp->tex[k][0] = -32*u/(2.0*M_PI);
925 if (appearance != APPEARANCE_DISTANCE_BANDS)
926 pp->tex[k][1] = 32*v/(2.0*M_PI);
928 pp->tex[k][1] = 32*v/(2.0*M_PI)-0.5;
929 pp->x[k][0] = 0.5*s2u*sv4*sv4;
930 pp->x[k][1] = 0.5*su*sv2;
931 pp->x[k][2] = 0.5*cu*sv2;
932 pp->x[k][3] = 0.5*(su*su*sv4*sv4-cv4*cv4);
933 /* Avoid degenerate tangential plane basis vectors. */
939 pp->xu[k][0] = c2u*sv4*sv4;
940 pp->xu[k][1] = 0.5*cu*sv2;
941 pp->xu[k][2] = -0.5*su*sv2;
942 pp->xu[k][3] = 0.5*s2u*sv4*sv4;
943 pp->xv[k][0] = 0.125*s2u*sv2;
944 pp->xv[k][1] = 0.25*su*cv2;
945 pp->xv[k][2] = 0.25*cu*cv2;
946 pp->xv[k][3] = 0.125*(su*su+1.0)*sv2;
952 /* Draw a 4d embedding of the projective plane projected into 3D. */
953 static int projective_plane(ModeInfo *mi, double umin, double umax,
954 double vmin, double vmax)
957 static const GLfloat mat_diff_red[] = { 1.0, 0.0, 0.0, 1.0 };
958 static const GLfloat mat_diff_green[] = { 0.0, 1.0, 0.0, 1.0 };
959 static const GLfloat mat_diff_trans_red[] = { 1.0, 0.0, 0.0, 0.7 };
960 static const GLfloat mat_diff_trans_green[] = { 0.0, 1.0, 0.0, 0.7 };
961 float p[3], pu[3], pv[3], pm[3], n[3], b[3], mat[4][4];
962 int i, j, k, l, m, o;
964 double xx[4], xxu[4], xxv[4], y[4], yu[4], yv[4];
966 double cu, su, cv2, sv2, cv4, sv4, c2u, s2u;
967 float q1[4], q2[4], r1[4][4], r2[4][4];
968 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
970 if (view == VIEW_WALK || view == VIEW_WALKTURN)
972 /* Compute the rotation that rotates the projective plane in 4D without
973 the trackball rotations. */
974 rotateall4d(pp->zeta,pp->eta,pp->theta,mat);
985 xx[0] = 0.5*s2u*sv4*sv4;
988 xx[3] = 0.5*(su*su*sv4*sv4-cv4*cv4);
989 /* Avoid degenerate tangential plane basis vectors. */
995 xxu[0] = c2u*sv4*sv4;
997 xxu[2] = -0.5*su*sv2;
998 xxu[3] = 0.5*s2u*sv4*sv4;
999 xxv[0] = 0.125*s2u*sv2;
1000 xxv[1] = 0.25*su*cv2;
1001 xxv[2] = 0.25*cu*cv2;
1002 xxv[3] = 0.125*(su*su+1.0)*sv2;
1005 y[l] = (mat[l][0]*xx[0]+mat[l][1]*xx[1]+
1006 mat[l][2]*xx[2]+mat[l][3]*xx[3]);
1007 yu[l] = (mat[l][0]*xxu[0]+mat[l][1]*xxu[1]+
1008 mat[l][2]*xxu[2]+mat[l][3]*xxu[3]);
1009 yv[l] = (mat[l][0]*xxv[0]+mat[l][1]*xxv[1]+
1010 mat[l][2]*xxv[2]+mat[l][3]*xxv[3]);
1012 if (projection_4d == DISP_4D_ORTHOGRAPHIC)
1016 p[l] = y[l]+pp->offset4d[l];
1023 s = y[3]+pp->offset4d[3];
1028 r = y[l]+pp->offset4d[l];
1030 pu[l] = (yu[l]*s-r*yu[3])*t;
1031 pv[l] = (yv[l]*s-r*yv[3])*t;
1034 n[0] = pu[1]*pv[2]-pu[2]*pv[1];
1035 n[1] = pu[2]*pv[0]-pu[0]*pv[2];
1036 n[2] = pu[0]*pv[1]-pu[1]*pv[0];
1037 t = 1.0/(pp->side*4.0*sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]));
1041 pm[0] = pu[0]*pp->dumove+pv[0]*pp->dvmove;
1042 pm[1] = pu[1]*pp->dumove+pv[1]*pp->dvmove;
1043 pm[2] = pu[2]*pp->dumove+pv[2]*pp->dvmove;
1044 t = 1.0/(4.0*sqrt(pm[0]*pm[0]+pm[1]*pm[1]+pm[2]*pm[2]));
1048 b[0] = n[1]*pm[2]-n[2]*pm[1];
1049 b[1] = n[2]*pm[0]-n[0]*pm[2];
1050 b[2] = n[0]*pm[1]-n[1]*pm[0];
1051 t = 1.0/(4.0*sqrt(b[0]*b[0]+b[1]*b[1]+b[2]*b[2]));
1056 /* Compute alpha, beta, delta from the three basis vectors.
1057 | -b[0] -b[1] -b[2] |
1058 m = | n[0] n[1] n[2] |
1059 | -pm[0] -pm[1] -pm[2] |
1061 pp->alpha = atan2(-n[2],-pm[2])*180/M_PI;
1062 pp->beta = atan2(-b[2],sqrt(b[0]*b[0]+b[1]*b[1]))*180/M_PI;
1063 pp->delta = atan2(b[1],-b[0])*180/M_PI;
1065 /* Compute the rotation that rotates the projective plane in 4D. */
1066 rotateall(pp->alpha,pp->beta,pp->delta,pp->zeta,pp->eta,pp->theta,mat);
1076 xx[0] = 0.5*s2u*sv4*sv4;
1079 xx[3] = 0.5*(su*su*sv4*sv4-cv4*cv4);
1084 r += mat[l][m]*xx[m];
1087 if (projection_4d == DISP_4D_ORTHOGRAPHIC)
1090 p[l] = y[l]+pp->offset4d[l];
1094 s = y[3]+pp->offset4d[3];
1096 p[l] = (y[l]+pp->offset4d[l])/s;
1099 pp->offset3d[0] = -p[0];
1100 pp->offset3d[1] = -p[1]-DELTAY;
1101 pp->offset3d[2] = -p[2];
1105 /* Compute the rotation that rotates the projective plane in 4D,
1106 including the trackball rotations. */
1107 rotateall(pp->alpha,pp->beta,pp->delta,pp->zeta,pp->eta,pp->theta,r1);
1109 gltrackball_get_quaternion(pp->trackballs[0],q1);
1110 gltrackball_get_quaternion(pp->trackballs[1],q2);
1111 quats_to_rotmat(q1,q2,r2);
1113 mult_rotmat(r2,r1,mat);
1116 /* Project the points from 4D to 3D. */
1117 for (i=0; i<=NUMV; i++)
1119 for (j=0; j<=NUMU; j++)
1124 y[l] = (mat[l][0]*pp->x[o][0]+mat[l][1]*pp->x[o][1]+
1125 mat[l][2]*pp->x[o][2]+mat[l][3]*pp->x[o][3]);
1126 yu[l] = (mat[l][0]*pp->xu[o][0]+mat[l][1]*pp->xu[o][1]+
1127 mat[l][2]*pp->xu[o][2]+mat[l][3]*pp->xu[o][3]);
1128 yv[l] = (mat[l][0]*pp->xv[o][0]+mat[l][1]*pp->xv[o][1]+
1129 mat[l][2]*pp->xv[o][2]+mat[l][3]*pp->xv[o][3]);
1131 if (projection_4d == DISP_4D_ORTHOGRAPHIC)
1135 pp->pp[o][l] = (y[l]+pp->offset4d[l])+pp->offset3d[l];
1142 s = y[3]+pp->offset4d[3];
1147 r = y[l]+pp->offset4d[l];
1148 pp->pp[o][l] = r*q+pp->offset3d[l];
1149 pu[l] = (yu[l]*s-r*yu[3])*t;
1150 pv[l] = (yv[l]*s-r*yv[3])*t;
1153 pp->pn[o][0] = pu[1]*pv[2]-pu[2]*pv[1];
1154 pp->pn[o][1] = pu[2]*pv[0]-pu[0]*pv[2];
1155 pp->pn[o][2] = pu[0]*pv[1]-pu[1]*pv[0];
1156 t = 1.0/sqrt(pp->pn[o][0]*pp->pn[o][0]+pp->pn[o][1]*pp->pn[o][1]+
1157 pp->pn[o][2]*pp->pn[o][2]);
1164 if (colors == COLORS_TWOSIDED)
1166 glColor3fv(mat_diff_red);
1167 if (display_mode == DISP_TRANSPARENT)
1169 glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_red);
1170 glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_green);
1174 glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_red);
1175 glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_green);
1178 glBindTexture(GL_TEXTURE_2D,pp->tex_name);
1180 if (appearance != APPEARANCE_DIRECTION_BANDS)
1182 for (i=0; i<NUMV; i++)
1184 if (appearance == APPEARANCE_DISTANCE_BANDS &&
1185 ((i & (NUMB-1)) >= NUMB/4) && ((i & (NUMB-1)) < 3*NUMB/4))
1187 if (display_mode == DISP_WIREFRAME)
1188 glBegin(GL_QUAD_STRIP);
1190 glBegin(GL_TRIANGLE_STRIP);
1191 for (j=0; j<=NUMU; j++)
1193 for (k=0; k<=1; k++)
1198 glNormal3fv(pp->pn[o]);
1199 glTexCoord2fv(pp->tex[o]);
1200 if (colors != COLORS_TWOSIDED)
1202 glColor3fv(pp->col[o]);
1203 glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,pp->col[o]);
1205 glVertex3fv(pp->pp[o]);
1212 else /* appearance == APPEARANCE_DIRECTION_BANDS */
1214 for (j=0; j<NUMU; j++)
1216 if ((j & (NUMB-1)) >= NUMB/2)
1218 if (display_mode == DISP_WIREFRAME)
1219 glBegin(GL_QUAD_STRIP);
1221 glBegin(GL_TRIANGLE_STRIP);
1222 for (i=0; i<=NUMV; i++)
1224 for (k=0; k<=1; k++)
1229 glNormal3fv(pp->pn[o]);
1230 glTexCoord2fv(pp->tex[o]);
1231 if (colors != COLORS_TWOSIDED)
1233 glColor3fv(pp->col[o]);
1234 glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,pp->col[o]);
1236 glVertex3fv(pp->pp[o]);
1249 /* Generate a texture image that shows the orientation reversal. */
1250 static void gen_texture(ModeInfo *mi)
1252 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
1254 glGenTextures(1,&pp->tex_name);
1255 glBindTexture(GL_TEXTURE_2D,pp->tex_name);
1256 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
1257 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
1258 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
1259 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
1260 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
1261 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
1262 glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEX_DIMENSION,TEX_DIMENSION,0,
1263 GL_LUMINANCE,GL_UNSIGNED_BYTE,texture);
1267 static void init(ModeInfo *mi)
1269 static const GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
1270 static const GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
1271 static const GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
1272 static const GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
1273 static const GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
1274 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
1276 if (walk_speed == 0.0)
1279 if (view == VIEW_TURN)
1281 pp->alpha = frand(360.0);
1282 pp->beta = frand(360.0);
1283 pp->delta = frand(360.0);
1297 pp->umove = frand(2.0*M_PI);
1298 pp->vmove = frand(2.0*M_PI);
1302 if (sin(walk_direction*M_PI/180.0) >= 0.0)
1307 pp->offset4d[0] = 0.0;
1308 pp->offset4d[1] = 0.0;
1309 pp->offset4d[2] = 0.0;
1310 pp->offset4d[3] = 1.2;
1311 pp->offset3d[0] = 0.0;
1312 pp->offset3d[1] = 0.0;
1313 pp->offset3d[2] = -1.2;
1314 pp->offset3d[3] = 0.0;
1317 setup_projective_plane(mi,0.0,2.0*M_PI,0.0,2.0*M_PI);
1320 glEnable(GL_TEXTURE_2D);
1322 glDisable(GL_TEXTURE_2D);
1324 glMatrixMode(GL_PROJECTION);
1326 if (projection_3d == DISP_3D_PERSPECTIVE ||
1327 view == VIEW_WALK || view == VIEW_WALKTURN)
1329 if (view == VIEW_WALK || view == VIEW_WALKTURN)
1330 gluPerspective(60.0,1.0,0.01,10.0);
1332 gluPerspective(60.0,1.0,0.1,10.0);
1336 glOrtho(-0.6,0.6,-0.6,0.6,0.1,10.0);
1338 glMatrixMode(GL_MODELVIEW);
1341 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
1342 if (display_mode == DISP_WIREFRAME)
1343 display_mode = DISP_SURFACE;
1346 if (display_mode == DISP_SURFACE)
1348 glEnable(GL_DEPTH_TEST);
1349 glDepthFunc(GL_LESS);
1350 glShadeModel(GL_SMOOTH);
1351 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
1352 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
1353 glEnable(GL_LIGHTING);
1354 glEnable(GL_LIGHT0);
1355 glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
1356 glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
1357 glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
1358 glLightfv(GL_LIGHT0,GL_POSITION,light_position);
1359 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
1360 glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
1361 glDepthMask(GL_TRUE);
1362 glDisable(GL_BLEND);
1364 else if (display_mode == DISP_TRANSPARENT)
1366 glDisable(GL_DEPTH_TEST);
1367 glShadeModel(GL_SMOOTH);
1368 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
1369 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
1370 glEnable(GL_LIGHTING);
1371 glEnable(GL_LIGHT0);
1372 glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
1373 glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
1374 glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
1375 glLightfv(GL_LIGHT0,GL_POSITION,light_position);
1376 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
1377 glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
1378 glDepthMask(GL_FALSE);
1380 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
1382 else /* display_mode == DISP_WIREFRAME */
1384 glDisable(GL_DEPTH_TEST);
1385 glShadeModel(GL_FLAT);
1386 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
1387 glDisable(GL_LIGHTING);
1388 glDisable(GL_LIGHT0);
1389 glDisable(GL_BLEND);
1394 /* Redisplay the Klein bottle. */
1395 static void display_projectiveplane(ModeInfo *mi)
1397 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
1399 if (!pp->button_pressed)
1401 if (view == VIEW_TURN)
1403 pp->alpha += speed_wx * pp->speed_scale;
1404 if (pp->alpha >= 360.0)
1406 pp->beta += speed_wy * pp->speed_scale;
1407 if (pp->beta >= 360.0)
1409 pp->delta += speed_wz * pp->speed_scale;
1410 if (pp->delta >= 360.0)
1412 pp->zeta += speed_xy * pp->speed_scale;
1413 if (pp->zeta >= 360.0)
1415 pp->eta += speed_xz * pp->speed_scale;
1416 if (pp->eta >= 360.0)
1418 pp->theta += speed_yz * pp->speed_scale;
1419 if (pp->theta >= 360.0)
1422 if (view == VIEW_WALKTURN)
1424 pp->zeta += speed_xy * pp->speed_scale;
1425 if (pp->zeta >= 360.0)
1427 pp->eta += speed_xz * pp->speed_scale;
1428 if (pp->eta >= 360.0)
1430 pp->theta += speed_yz * pp->speed_scale;
1431 if (pp->theta >= 360.0)
1434 if (view == VIEW_WALK || view == VIEW_WALKTURN)
1436 pp->dvmove = (pp->dir*sin(walk_direction*M_PI/180.0)*
1437 walk_speed*M_PI/4096.0);
1438 pp->vmove += pp->dvmove;
1439 if (pp->vmove > 2.0*M_PI)
1441 pp->vmove = 4.0*M_PI-pp->vmove;
1442 pp->umove = pp->umove-M_PI;
1443 if (pp->umove < 0.0)
1444 pp->umove += 2.0*M_PI;
1445 pp->side = -pp->side;
1447 pp->dvmove = -pp->dvmove;
1449 if (pp->vmove < 0.0)
1451 pp->vmove = -pp->vmove;
1452 pp->umove = pp->umove-M_PI;
1453 if (pp->umove < 0.0)
1454 pp->umove += 2.0*M_PI;
1456 pp->dvmove = -pp->dvmove;
1458 pp->dumove = cos(walk_direction*M_PI/180.0)*walk_speed*M_PI/4096.0;
1459 pp->umove += pp->dumove;
1460 if (pp->umove >= 2.0*M_PI)
1461 pp->umove -= 2.0*M_PI;
1462 if (pp->umove < 0.0)
1463 pp->umove += 2.0*M_PI;
1467 glMatrixMode(GL_PROJECTION);
1469 if (projection_3d == DISP_3D_PERSPECTIVE ||
1470 view == VIEW_WALK || view == VIEW_WALKTURN)
1472 if (view == VIEW_WALK || view == VIEW_WALKTURN)
1473 gluPerspective(60.0,pp->aspect,0.01,10.0);
1475 gluPerspective(60.0,pp->aspect,0.1,10.0);
1479 if (pp->aspect >= 1.0)
1480 glOrtho(-0.6*pp->aspect,0.6*pp->aspect,-0.6,0.6,0.1,10.0);
1482 glOrtho(-0.6,0.6,-0.6/pp->aspect,0.6/pp->aspect,0.1,10.0);
1484 glMatrixMode(GL_MODELVIEW);
1487 mi->polygon_count = projective_plane(mi,0.0,2.0*M_PI,0.0,2.0*M_PI);
1491 ENTRYPOINT void reshape_projectiveplane(ModeInfo *mi, int width, int height)
1493 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
1495 pp->WindW = (GLint)width;
1496 pp->WindH = (GLint)height;
1497 glViewport(0,0,width,height);
1498 pp->aspect = (GLfloat)width/(GLfloat)height;
1502 ENTRYPOINT Bool projectiveplane_handle_event(ModeInfo *mi, XEvent *event)
1504 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
1508 if (event->xany.type == KeyPress || event->xany.type == KeyRelease)
1509 XLookupString (&event->xkey, &c, 1, &sym, 0);
1511 if (event->xany.type == ButtonPress &&
1512 event->xbutton.button == Button1)
1514 pp->button_pressed = True;
1515 gltrackball_start(pp->trackballs[pp->current_trackball],
1516 event->xbutton.x, event->xbutton.y,
1517 MI_WIDTH(mi), MI_HEIGHT(mi));
1520 else if (event->xany.type == ButtonRelease &&
1521 event->xbutton.button == Button1)
1523 pp->button_pressed = False;
1526 else if (event->xany.type == KeyPress)
1528 if (sym == XK_Shift_L || sym == XK_Shift_R)
1530 pp->current_trackball = 1;
1531 if (pp->button_pressed)
1532 gltrackball_start(pp->trackballs[pp->current_trackball],
1533 event->xbutton.x, event->xbutton.y,
1534 MI_WIDTH(mi), MI_HEIGHT(mi));
1538 else if (event->xany.type == KeyRelease)
1540 if (sym == XK_Shift_L || sym == XK_Shift_R)
1542 pp->current_trackball = 0;
1543 if (pp->button_pressed)
1544 gltrackball_start(pp->trackballs[pp->current_trackball],
1545 event->xbutton.x, event->xbutton.y,
1546 MI_WIDTH(mi), MI_HEIGHT(mi));
1550 else if (event->xany.type == MotionNotify && pp->button_pressed)
1552 gltrackball_track(pp->trackballs[pp->current_trackball],
1553 event->xmotion.x, event->xmotion.y,
1554 MI_WIDTH(mi), MI_HEIGHT(mi));
1563 *-----------------------------------------------------------------------------
1564 *-----------------------------------------------------------------------------
1566 *-----------------------------------------------------------------------------
1567 *-----------------------------------------------------------------------------
1571 *-----------------------------------------------------------------------------
1572 * Initialize projectiveplane. Called each time the window changes.
1573 *-----------------------------------------------------------------------------
1576 ENTRYPOINT void init_projectiveplane(ModeInfo *mi)
1578 projectiveplanestruct *pp;
1580 if (projectiveplane == NULL)
1583 (projectiveplanestruct *)calloc(MI_NUM_SCREENS(mi),
1584 sizeof(projectiveplanestruct));
1585 if (projectiveplane == NULL)
1588 pp = &projectiveplane[MI_SCREEN(mi)];
1591 pp->trackballs[0] = gltrackball_init(True);
1592 pp->trackballs[1] = gltrackball_init(True);
1593 pp->current_trackball = 0;
1594 pp->button_pressed = False;
1596 /* Set the display mode. */
1597 if (!strcasecmp(mode,"random"))
1599 display_mode = random() % NUM_DISPLAY_MODES;
1601 else if (!strcasecmp(mode,"wireframe"))
1603 display_mode = DISP_WIREFRAME;
1605 else if (!strcasecmp(mode,"surface"))
1607 display_mode = DISP_SURFACE;
1609 else if (!strcasecmp(mode,"transparent"))
1611 display_mode = DISP_TRANSPARENT;
1615 display_mode = random() % NUM_DISPLAY_MODES;
1618 /* Orientation marks don't make sense in wireframe mode. */
1619 if (display_mode == DISP_WIREFRAME)
1622 /* Set the appearance. */
1623 if (!strcasecmp(appear,"random"))
1625 appearance = random() % NUM_APPEARANCES;
1627 else if (!strcasecmp(appear,"solid"))
1629 appearance = APPEARANCE_SOLID;
1631 else if (!strcasecmp(appear,"distance-bands"))
1633 appearance = APPEARANCE_DISTANCE_BANDS;
1635 else if (!strcasecmp(appear,"direction-bands"))
1637 appearance = APPEARANCE_DIRECTION_BANDS;
1641 appearance = random() % NUM_APPEARANCES;
1644 /* Set the color mode. */
1645 if (!strcasecmp(color_mode,"random"))
1647 colors = random() % NUM_COLORS;
1649 else if (!strcasecmp(color_mode,"two-sided"))
1651 colors = COLORS_TWOSIDED;
1653 else if (!strcasecmp(color_mode,"distance"))
1655 colors = COLORS_DISTANCE;
1657 else if (!strcasecmp(color_mode,"direction"))
1659 colors = COLORS_DIRECTION;
1661 else if (!strcasecmp(color_mode,"depth"))
1663 colors = COLORS_DEPTH;
1667 colors = random() % NUM_COLORS;
1670 /* Set the view mode. */
1671 if (!strcasecmp(view_mode,"random"))
1673 view = random() % NUM_VIEW_MODES;
1675 else if (!strcasecmp(view_mode,"walk"))
1679 else if (!strcasecmp(view_mode,"turn"))
1683 else if (!strcasecmp(view_mode,"walk-turn"))
1685 view = VIEW_WALKTURN;
1689 view = random() % NUM_VIEW_MODES;
1692 /* Set the 3d projection mode. */
1693 if (!strcasecmp(proj_3d,"random"))
1695 /* Orthographic projection only makes sense in turn mode. */
1696 if (view == VIEW_TURN)
1697 projection_3d = random() % NUM_DISP_3D_MODES;
1699 projection_3d = DISP_3D_PERSPECTIVE;
1701 else if (!strcasecmp(proj_3d,"perspective"))
1703 projection_3d = DISP_3D_PERSPECTIVE;
1705 else if (!strcasecmp(proj_3d,"orthographic"))
1707 projection_3d = DISP_3D_ORTHOGRAPHIC;
1711 /* Orthographic projection only makes sense in turn mode. */
1712 if (view == VIEW_TURN)
1713 projection_3d = random() % NUM_DISP_3D_MODES;
1715 projection_3d = DISP_3D_PERSPECTIVE;
1718 /* Set the 4d projection mode. */
1719 if (!strcasecmp(proj_4d,"random"))
1721 projection_4d = random() % NUM_DISP_4D_MODES;
1723 else if (!strcasecmp(proj_4d,"perspective"))
1725 projection_4d = DISP_4D_PERSPECTIVE;
1727 else if (!strcasecmp(proj_4d,"orthographic"))
1729 projection_4d = DISP_4D_ORTHOGRAPHIC;
1733 projection_4d = random() % NUM_DISP_4D_MODES;
1736 /* Modify the speeds to a useful range in walk-and-turn mode. */
1737 if (view == VIEW_WALKTURN)
1747 /* make multiple screens rotate at slightly different rates. */
1748 pp->speed_scale = 0.9 + frand(0.3);
1750 if ((pp->glx_context = init_GL(mi)) != NULL)
1752 reshape_projectiveplane(mi,MI_WIDTH(mi),MI_HEIGHT(mi));
1753 glDrawBuffer(GL_BACK);
1763 *-----------------------------------------------------------------------------
1764 * Called by the mainline code periodically to update the display.
1765 *-----------------------------------------------------------------------------
1767 ENTRYPOINT void draw_projectiveplane(ModeInfo *mi)
1769 Display *display = MI_DISPLAY(mi);
1770 Window window = MI_WINDOW(mi);
1771 projectiveplanestruct *pp;
1773 if (projectiveplane == NULL)
1775 pp = &projectiveplane[MI_SCREEN(mi)];
1777 MI_IS_DRAWN(mi) = True;
1778 if (!pp->glx_context)
1781 glXMakeCurrent(display,window,*(pp->glx_context));
1783 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1786 display_projectiveplane(mi);
1793 glXSwapBuffers(display,window);
1798 *-----------------------------------------------------------------------------
1799 * The display is being taken away from us. Free up malloc'ed
1800 * memory and X resources that we've alloc'ed. Only called
1801 * once, we must zap everything for every screen.
1802 *-----------------------------------------------------------------------------
1805 ENTRYPOINT void release_projectiveplane(ModeInfo *mi)
1807 if (projectiveplane != NULL)
1811 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
1813 projectiveplanestruct *pp = &projectiveplane[screen];
1815 if (pp->glx_context)
1816 pp->glx_context = (GLXContext *)NULL;
1818 (void) free((void *)projectiveplane);
1819 projectiveplane = (projectiveplanestruct *)NULL;
1825 ENTRYPOINT void change_projectiveplane(ModeInfo *mi)
1827 projectiveplanestruct *pp = &projectiveplane[MI_SCREEN(mi)];
1829 if (!pp->glx_context)
1832 glXMakeCurrent(MI_DISPLAY(mi),MI_WINDOW(mi),*(pp->glx_context));
1835 #endif /* !STANDALONE */
1837 XSCREENSAVER_MODULE ("ProjectivePlane", projectiveplane)