From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / romanboy.c
1 /* romanboy --- Shows a 3d immersion of the real projective plane
2    that rotates in 3d or on which you can walk and that can deform
3    smoothly between the Roman surface and the Boy surface. */
4
5 #if 0
6 static const char sccsid[] = "@(#)romanboy.c  1.1 14/10/03 xlockmore";
7 #endif
8
9 /* Copyright (c) 2013-2014 Carsten Steger <carsten@mirsanmir.org>. */
10
11 /*
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation for any purpose and without fee is hereby granted,
14  * provided that the above copyright notice appear in all copies and that
15  * both that copyright notice and this permission notice appear in
16  * supporting documentation.
17  *
18  * This file is provided AS IS with no warranties of any kind.  The author
19  * shall have no liability with respect to the infringement of copyrights,
20  * trade secrets or any patents by this file or any part thereof.  In no
21  * event will the author be liable for any lost revenue or profits or
22  * other special, indirect and consequential damages.
23  *
24  * REVISION HISTORY:
25  * C. Steger - 14/10/03: Initial version
26  */
27
28 /*
29  * This program shows a 3d immersion of the real projective plane
30  * that smoothly deforms between the Roman surface and the Boy
31  * surface.  You can walk on the projective plane or turn in 3d.  The
32  * smooth deformation (homotopy) between these two famous immersions
33  * of the real projective plane was constructed by François Apéry.
34  *
35  * The real projective plane is a non-orientable surface.  To make
36  * this apparent, the two-sided color mode can be used.
37  * Alternatively, orientation markers (curling arrows) can be drawn as
38  * a texture map on the surface of the projective plane.  While
39  * walking on the projective plane, you will notice that the
40  * orientation of the curling arrows changes (which it must because
41  * the projective plane is non-orientable).
42  *
43  * The real projective plane is a model for the projective geometry in
44  * 2d space.  One point can be singled out as the origin.  A line can
45  * be singled out as the line at infinity, i.e., a line that lies at
46  * an infinite distance to the origin.  The line at infinity is
47  * topologically a circle.  Points on the line at infinity are also
48  * used to model directions in projective geometry.  The origin can be
49  * visualized in different manners.  When using distance colors, the
50  * origin is the point that is displayed as fully saturated red, which
51  * is easier to see as the center of the reddish area on the
52  * projective plane.  Alternatively, when using distance bands, the
53  * origin is the center of the only band that projects to a disk.
54  * When using direction bands, the origin is the point where all
55  * direction bands collapse to a point.  Finally, when orientation
56  * markers are being displayed, the origin the the point where all
57  * orientation markers are compressed to a point.  The line at
58  * infinity can also be visualized in different ways.  When using
59  * distance colors, the line at infinity is the line that is displayed
60  * as fully saturated magenta.  When two-sided colors are used, the
61  * line at infinity lies at the points where the red and green "sides"
62  * of the projective plane meet (of course, the real projective plane
63  * only has one side, so this is a design choice of the
64  * visualization).  Alternatively, when orientation markers are being
65  * displayed, the line at infinity is the place where the orientation
66  * markers change their orientation.
67  *
68  * Note that when the projective plane is displayed with bands, the
69  * orientation markers are placed in the middle of the bands.  For
70  * distance bands, the bands are chosen in such a way that the band at
71  * the origin is only half as wide as the remaining bands, which
72  * results in a disk being displayed at the origin that has the same
73  * diameter as the remaining bands.  This choice, however, also
74  * implies that the band at infinity is half as wide as the other
75  * bands.  Since the projective plane is attached to itself (in a
76  * complicated fashion) at the line at infinity, effectively the band
77  * at infinity is again as wide as the remaining bands.  However,
78  * since the orientation markers are displayed in the middle of the
79  * bands, this means that only one half of the orientation markers
80  * will be displayed twice at the line at infinity if distance bands
81  * are used.  If direction bands are used or if the projective plane
82  * is displayed as a solid surface, the orientation markers are
83  * displayed fully at the respective sides of the line at infinity.
84  *
85  * The immersed projective plane can be projected to the screen either
86  * perspectively or orthographically.  When using the walking modes,
87  * perspective projection to the screen will be used.
88  *
89  * There are three display modes for the projective plane: mesh
90  * (wireframe), solid, or transparent.  Furthermore, the appearance of
91  * the projective plane can be as a solid object or as a set of
92  * see-through bands.  The bands can be distance bands, i.e., bands
93  * that lie at increasing distances from the origin, or direction
94  * bands, i.e., bands that lie at increasing angles with respect to
95  * the origin.
96  *
97  * When the projective plane is displayed with direction bands, you
98  * will be able to see that each direction band (modulo the "pinching"
99  * at the origin) is a Moebius strip, which also shows that the
100  * projective plane is non-orientable.
101  *
102  * Finally, the colors with with the projective plane is drawn can be
103  * set to two-sided, distance, or direction.  In two-sided mode, the
104  * projective plane is drawn with red on one "side" and green on the
105  * "other side".  As described above, the projective plane only has
106  * one side, so the color jumps from red to green along the line at
107  * infinity.  This mode enables you to see that the projective plane
108  * is non-orientable.  In distance mode, the projective plane is
109  * displayed with fully saturated colors that depend on the distance
110  * of the points on the projective plane to the origin.  The origin is
111  * displayed in red, the line at infinity is displayed in magenta.  If
112  * the projective plane is displayed as distance bands, each band will
113  * be displayed with a different color.  In direction mode, the
114  * projective plane is displayed with fully saturated colors that
115  * depend on the angle of the points on the projective plane with
116  * respect to the origin.  Angles in opposite directions to the origin
117  * (e.g., 15 and 205 degrees) are displayed in the same color since
118  * they are projectively equivalent.  If the projective plane is
119  * displayed as direction bands, each band will be displayed with a
120  * different color.
121  *
122  * The rotation speed for each of the three coordinate axes around
123  * which the projective plane rotates can be chosen.
124  *
125  * Furthermore, in the walking mode the walking direction in the 2d
126  * base square of the projective plane and the walking speed can be
127  * chosen.  The walking direction is measured as an angle in degrees
128  * in the 2d square that forms the coordinate system of the surface of
129  * the projective plane.  A value of 0 or 180 means that the walk is
130  * along a circle at a randomly chosen distance from the origin
131  * (parallel to a distance band).  A value of 90 or 270 means that the
132  * walk is directly from the origin to the line at infinity and back
133  * (analogous to a direction band).  Any other value results in a
134  * curved path from the origin to the line at infinity and back.
135  *
136  * By default, the immersion of the real projective plane smoothly
137  * deforms between the Roman and Boy surfaces.  It is possible to
138  * choose the speed of the deformation.  Furthermore, it is possible
139  * to switch the deformation off.  It is also possible to determine
140  * the initial deformation of the immersion.  This is mostly useful if
141  * the deformation is switched off, in which case it will determine
142  * the appearance of the surface.
143  *
144  * As a final option, it is possible to display generalized versions
145  * of the immersion discussed above by specifying the order of the
146  * surface.  The default surface order of 3 results in the immersion
147  * of the real projective described above.  The surface order can be
148  * chosen between 2 and 9.  Odd surface orders result in generalized
149  * immersions of the real projective plane, while even numbers result
150  * in a immersion of a topological sphere (which is orientable).  The
151  * most interesting even case is a surface order of 2, which results
152  * in an immersion of the halfway model of Morin's sphere eversion (if
153  * the deformation is switched off).
154  *
155  * This program is inspired by François Apéry's book "Models of the
156  * Real Projective Plane", Vieweg, 1987.
157  */
158
159 #include "curlicue.h"
160
161 #ifndef M_PI
162 #define M_PI 3.14159265358979323846
163 #endif
164
165 #define DISP_WIREFRAME             0
166 #define DISP_SURFACE               1
167 #define DISP_TRANSPARENT           2
168 #define NUM_DISPLAY_MODES          3
169
170 #define APPEARANCE_SOLID           0
171 #define APPEARANCE_DISTANCE_BANDS  1
172 #define APPEARANCE_DIRECTION_BANDS 2
173 #define NUM_APPEARANCES            3
174
175 #define COLORS_TWOSIDED            0
176 #define COLORS_DISTANCE            1
177 #define COLORS_DIRECTION           2
178 #define NUM_COLORS                 3
179
180 #define VIEW_WALK                  0
181 #define VIEW_TURN                  1
182 #define NUM_VIEW_MODES             2
183
184 #define DISP_PERSPECTIVE           0
185 #define DISP_ORTHOGRAPHIC          1
186 #define NUM_DISP_MODES             2
187
188 #define DEF_DISPLAY_MODE           "random"
189 #define DEF_APPEARANCE             "random"
190 #define DEF_COLORS                 "random"
191 #define DEF_VIEW_MODE              "random"
192 #define DEF_MARKS                  "False"
193 #define DEF_DEFORM                 "True"
194 #define DEF_PROJECTION             "random"
195 #define DEF_SPEEDX                 "1.1"
196 #define DEF_SPEEDY                 "1.3"
197 #define DEF_SPEEDZ                 "1.5"
198 #define DEF_WALK_DIRECTION         "83.0"
199 #define DEF_WALK_SPEED             "20.0"
200 #define DEF_DEFORM_SPEED           "10.0"
201 #define DEF_INIT_DEFORM            "1000.0"
202 #define DEF_SURFACE_ORDER          "3"
203
204 #ifdef STANDALONE
205 # define DEFAULTS           "*delay:      10000 \n" \
206                             "*showFPS:    False \n" \
207
208 # define refresh_romanboy 0
209 # define release_romanboy 0
210 # include "xlockmore.h"         /* from the xscreensaver distribution */
211 #else  /* !STANDALONE */
212 # include "xlock.h"             /* from the xlockmore distribution */
213 #endif /* !STANDALONE */
214
215 #ifdef USE_GL
216
217 #ifndef HAVE_JWXYZ
218 # include <X11/keysym.h>
219 #endif
220
221 #include "gltrackball.h"
222
223 #include <float.h>
224
225
226 #ifdef USE_MODULES
227 ModStruct romanboy_description =
228 {"romanboy", "init_romanboy", "draw_romanboy",
229  NULL, "draw_romanboy", "change_romanboy",
230  NULL, &romanboy_opts, 25000, 1, 1, 1, 1.0, 4, "",
231  "Rotate a 3d immersion of the real projective plane in 3d or walk on it",
232  0, NULL};
233
234 #endif
235
236
237 static char *mode;
238 static char *appear;
239 static char *color_mode;
240 static char *view_mode;
241 static Bool marks;
242 static Bool deform;
243 static char *proj;
244 static float speed_x;
245 static float speed_y;
246 static float speed_z;
247 static float walk_direction;
248 static float walk_speed;
249 static float deform_speed;
250 static float init_deform;
251 static int surface_order;
252
253
254 static XrmOptionDescRec opts[] =
255 {
256   {"-mode",                ".displayMode",   XrmoptionSepArg, 0 },
257   {"-wireframe",           ".displayMode",   XrmoptionNoArg,  "wireframe" },
258   {"-surface",             ".displayMode",   XrmoptionNoArg,  "surface" },
259   {"-transparent",         ".displayMode",   XrmoptionNoArg,  "transparent" },
260   {"-appearance",          ".appearance",    XrmoptionSepArg, 0 },
261   {"-solid",               ".appearance",    XrmoptionNoArg,  "solid" },
262   {"-distance-bands",      ".appearance",    XrmoptionNoArg,  "distance-bands" },
263   {"-direction-bands",     ".appearance",    XrmoptionNoArg,  "direction-bands" },
264   {"-colors",              ".colors",        XrmoptionSepArg, 0 },
265   {"-twosided-colors",     ".colors",        XrmoptionNoArg,  "two-sided" },
266   {"-distance-colors",     ".colors",        XrmoptionNoArg,  "distance" },
267   {"-direction-colors",    ".colors",        XrmoptionNoArg,  "direction" },
268   {"-view-mode",           ".viewMode",      XrmoptionSepArg, 0 },
269   {"-walk",                ".viewMode",      XrmoptionNoArg,  "walk" },
270   {"-turn",                ".viewMode",      XrmoptionNoArg,  "turn" },
271   {"-deform",              ".deform",        XrmoptionNoArg, "on"},
272   {"+deform",              ".deform",        XrmoptionNoArg, "off"},
273   {"-orientation-marks",   ".marks",         XrmoptionNoArg, "on"},
274   {"+orientation-marks",   ".marks",         XrmoptionNoArg, "off"},
275   {"-projection",          ".projection",    XrmoptionSepArg, 0 },
276   {"-perspective",         ".projection",    XrmoptionNoArg,  "perspective" },
277   {"-orthographic",        ".projection",    XrmoptionNoArg,  "orthographic" },
278   {"-speed-x",             ".speedx",        XrmoptionSepArg, 0 },
279   {"-speed-y",             ".speedy",        XrmoptionSepArg, 0 },
280   {"-speed-z",             ".speedz",        XrmoptionSepArg, 0 },
281   {"-walk-direction",      ".walkDirection", XrmoptionSepArg, 0 },
282   {"-walk-speed",          ".walkSpeed",     XrmoptionSepArg, 0 },
283   {"-deformation-speed",   ".deformSpeed",   XrmoptionSepArg, 0 },
284   {"-initial-deformation", ".initDeform",    XrmoptionSepArg, 0 },
285   {"-roman",               ".initDeform",    XrmoptionNoArg,  "0.0" },
286   {"-boy",                 ".initDeform",    XrmoptionNoArg,  "1000.0" },
287   {"-surface-order",       ".surfaceOrder",  XrmoptionSepArg, 0 },
288 };
289
290 static argtype vars[] =
291 {
292   { &mode,           "displayMode",   "DisplayMode",   DEF_DISPLAY_MODE,   t_String },
293   { &appear,         "appearance",    "Appearance",    DEF_APPEARANCE,     t_String },
294   { &color_mode,     "colors",        "Colors",        DEF_COLORS,         t_String },
295   { &view_mode,      "viewMode",      "ViewMode",      DEF_VIEW_MODE,      t_String },
296   { &deform,         "deform",        "Deform",        DEF_DEFORM,         t_Bool },
297   { &marks,          "marks",         "Marks",         DEF_MARKS,          t_Bool },
298   { &proj,           "projection",    "Projection",    DEF_PROJECTION,     t_String },
299   { &speed_x,        "speedx",        "Speedx",        DEF_SPEEDX,         t_Float},
300   { &speed_y,        "speedy",        "Speedy",        DEF_SPEEDY,         t_Float},
301   { &speed_z,        "speedz",        "Speedz",        DEF_SPEEDZ,         t_Float},
302   { &walk_direction, "walkDirection", "WalkDirection", DEF_WALK_DIRECTION, t_Float},
303   { &walk_speed,     "walkSpeed",     "WalkSpeed",     DEF_WALK_SPEED,     t_Float},
304   { &deform_speed,   "deformSpeed",   "DeformSpeed",   DEF_DEFORM_SPEED,   t_Float},
305   { &init_deform,    "initDeform",    "InitDeform",    DEF_INIT_DEFORM,    t_Float },
306   { &surface_order,  "surfaceOrder",  "SurfaceOrder",  DEF_SURFACE_ORDER,  t_Int }
307 };
308
309 ENTRYPOINT ModeSpecOpt romanboy_opts =
310 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, NULL};
311
312
313 /* Offset by which we walk above the projective plane */
314 #define DELTAY  0.01
315
316 /* Number of subdivisions of the projective plane */
317 #define NUMU 64
318 #define NUMV 128
319
320 /* Number of subdivisions per band */
321 #define NUMB 8
322
323
324 typedef struct {
325   GLint WindH, WindW;
326   GLXContext *glx_context;
327   /* Options */
328   int display_mode;
329   int appearance;
330   int colors;
331   int view;
332   int projection;
333   Bool marks;
334   /* 3D rotation angles */
335   float alpha, beta, delta;
336   /* Movement parameters */
337   float umove, vmove, dumove, dvmove;
338   int side, dir;
339   /* Deformation parameters */
340   float dd;
341   int defdir;
342   /* The type of the generalized Roman-Boy surface */
343   int g;
344   /* The viewing offset in 3d */
345   float offset3d[3];
346   /* The 3d coordinates of the projective plane and their derivatives */
347   float *pp;
348   float *pn;
349   /* The precomputed colors of the projective plane */
350   float *col;
351   /* The precomputed texture coordinates of the projective plane */
352   float *tex;
353   /* The "curlicue" texture */
354   GLuint tex_name;
355   /* Aspect ratio of the current window */
356   float aspect;
357   /* Trackball states */
358   trackball_state *trackball;
359   Bool button_pressed;
360   /* A random factor to modify the rotation speeds */
361   float speed_scale;
362 } romanboystruct;
363
364 static romanboystruct *romanboy = (romanboystruct *) NULL;
365
366
367 /* Add a rotation around the x-axis to the matrix m. */
368 static void rotatex(float m[3][3], float phi)
369 {
370   float c, s, u, v;
371   int i;
372
373   phi *= M_PI/180.0;
374   c = cos(phi);
375   s = sin(phi);
376   for (i=0; i<3; i++)
377   {
378     u = m[i][1];
379     v = m[i][2];
380     m[i][1] = c*u+s*v;
381     m[i][2] = -s*u+c*v;
382   }
383 }
384
385
386 /* Add a rotation around the y-axis to the matrix m. */
387 static void rotatey(float m[3][3], float phi)
388 {
389   float c, s, u, v;
390   int i;
391
392   phi *= M_PI/180.0;
393   c = cos(phi);
394   s = sin(phi);
395   for (i=0; i<3; i++)
396   {
397     u = m[i][0];
398     v = m[i][2];
399     m[i][0] = c*u-s*v;
400     m[i][2] = s*u+c*v;
401   }
402 }
403
404
405 /* Add a rotation around the z-axis to the matrix m. */
406 static void rotatez(float m[3][3], float phi)
407 {
408   float c, s, u, v;
409   int i;
410
411   phi *= M_PI/180.0;
412   c = cos(phi);
413   s = sin(phi);
414   for (i=0; i<3; i++)
415   {
416     u = m[i][0];
417     v = m[i][1];
418     m[i][0] = c*u+s*v;
419     m[i][1] = -s*u+c*v;
420   }
421 }
422
423
424 /* Compute the rotation matrix m from the rotation angles. */
425 static void rotateall(float al, float be, float de, float m[3][3])
426 {
427   int i, j;
428
429   for (i=0; i<3; i++)
430     for (j=0; j<3; j++)
431       m[i][j] = (i==j);
432   rotatex(m,al);
433   rotatey(m,be);
434   rotatez(m,de);
435 }
436
437
438 /* Multiply two rotation matrices: o=m*n. */
439 static void mult_rotmat(float m[3][3], float n[3][3], float o[3][3])
440 {
441   int i, j, k;
442
443   for (i=0; i<3; i++)
444   {
445     for (j=0; j<3; j++)
446     {
447       o[i][j] = 0.0;
448       for (k=0; k<3; k++)
449         o[i][j] += m[i][k]*n[k][j];
450     }
451   }
452 }
453
454
455 /* Compute a 3D rotation matrix from a unit quaternion. */
456 static void quat_to_rotmat(float p[4], float m[3][3])
457 {
458   double al, be, de;
459   double r00, r01, r02, r12, r22;
460
461   r00 = 1.0-2.0*(p[1]*p[1]+p[2]*p[2]);
462   r01 = 2.0*(p[0]*p[1]+p[2]*p[3]);
463   r02 = 2.0*(p[2]*p[0]-p[1]*p[3]);
464   r12 = 2.0*(p[1]*p[2]+p[0]*p[3]);
465   r22 = 1.0-2.0*(p[1]*p[1]+p[0]*p[0]);
466
467   al = atan2(-r12,r22)*180.0/M_PI;
468   be = atan2(r02,sqrt(r00*r00+r01*r01))*180.0/M_PI;
469   de = atan2(-r01,r00)*180.0/M_PI;
470
471   rotateall(al,be,de,m);
472 }
473
474
475 /* Compute a fully saturated and bright color based on an angle. */
476 static void color(romanboystruct *pp, double angle, float col[4])
477 {
478   int s;
479   double t;
480
481   if (pp->colors == COLORS_TWOSIDED)
482     return;
483
484   if (angle >= 0.0)
485     angle = fmod(angle,2.0*M_PI);
486   else
487     angle = fmod(angle,-2.0*M_PI);
488   s = floor(angle/(M_PI/3));
489   t = angle/(M_PI/3)-s;
490   if (s >= 6)
491     s = 0;
492   switch (s)
493   {
494     case 0:
495       col[0] = 1.0;
496       col[1] = t;
497       col[2] = 0.0;
498       break;
499     case 1:
500       col[0] = 1.0-t;
501       col[1] = 1.0;
502       col[2] = 0.0;
503       break;
504     case 2:
505       col[0] = 0.0;
506       col[1] = 1.0;
507       col[2] = t;
508       break;
509     case 3:
510       col[0] = 0.0;
511       col[1] = 1.0-t;
512       col[2] = 1.0;
513       break;
514     case 4:
515       col[0] = t;
516       col[1] = 0.0;
517       col[2] = 1.0;
518       break;
519     case 5:
520       col[0] = 1.0;
521       col[1] = 0.0;
522       col[2] = 1.0-t;
523       break;
524   }
525   if (pp->display_mode == DISP_TRANSPARENT)
526     col[3] = 0.7;
527   else
528     col[3] = 1.0;
529 }
530
531
532 /* Set up the projective plane colors and texture. */
533 static void setup_roman_boy_color_texture(ModeInfo *mi, double umin,
534                                           double umax, double vmin,
535                                           double vmax, int numu, int numv)
536 {
537   int i, j, k, g;
538   double u, v, ur, vr;
539   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
540
541   g = pp->g;
542   ur = umax-umin;
543   vr = vmax-vmin;
544   for (i=0; i<=numv; i++)
545   {
546     for (j=0; j<=numu; j++)
547     {
548       k = i*(numu+1)+j;
549       if (pp->appearance != APPEARANCE_DIRECTION_BANDS)
550         u = -ur*j/numu+umin;
551       else
552         u = ur*j/numu+umin;
553       v = vr*i/numv+vmin;
554       if (pp->colors == COLORS_DIRECTION)
555         color(pp,2.0*M_PI-fmod(2.0*u,2.0*M_PI),&pp->col[4*k]);
556       else /* pp->colors == COLORS_DISTANCE */
557         color(pp,v*(5.0/6.0),&pp->col[4*k]);
558       pp->tex[2*k+0] = -16*g*u/(2.0*M_PI);
559       if (pp->appearance == APPEARANCE_DISTANCE_BANDS)
560         pp->tex[2*k+1] = 32*v/(2.0*M_PI)-0.5;
561       else
562         pp->tex[2*k+1] = 32*v/(2.0*M_PI);
563     }
564   }
565 }
566
567
568 /* Draw a 3d immersion of the projective plane. */
569 static int roman_boy(ModeInfo *mi, double umin, double umax,
570                      double vmin, double vmax, int numu, int numv)
571 {
572   int polys = 0;
573   static const GLfloat mat_diff_red[]         = { 1.0, 0.0, 0.0, 1.0 };
574   static const GLfloat mat_diff_green[]       = { 0.0, 1.0, 0.0, 1.0 };
575   static const GLfloat mat_diff_trans_red[]   = { 1.0, 0.0, 0.0, 0.7 };
576   static const GLfloat mat_diff_trans_green[] = { 0.0, 1.0, 0.0, 0.7 };
577   float p[3], pu[3], pv[3], pm[3], n[3], b[3], mat[3][3];
578   int i, j, k, l, m, o, g;
579   double u, v, ur, vr, oz;
580   double xx[3], xxu[3], xxv[3];
581   double r, s, t;
582   double d, dd, radius;
583   double cu, su, cgu, sgu, cgm1u, sgm1u, cv, c2v, s2v, cv2;
584   double sqrt2og, h1m1og, gm1, nomx, nomy, nomux, nomuy, nomvx, nomvy;
585   double den, den2, denu, denv;
586   float qu[4], r1[3][3], r2[3][3];
587   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
588
589   g = pp->g;
590   dd = pp->dd;
591   d = ((6.0*dd-15.0)*dd+10.0)*dd*dd*dd;
592   r = 1.0+d*d*(1.0/2.0+d*d*(1.0/6.0+d*d*(1.0/3.0)));
593   radius = 1.0/r;
594   oz = 0.5*r;
595   if (pp->view == VIEW_WALK)
596   {
597     u = pp->umove;
598     v = pp->vmove;
599     if (g & 1)
600       v = 0.5*M_PI-0.25*v;
601     else
602       v = 0.5*M_PI-0.5*v;
603     sqrt2og = M_SQRT2/g;
604     h1m1og = 0.5*(1.0-1.0/g);
605     gm1 = g-1.0;
606     cu = cos(u);
607     su = sin(u);
608     cgu = cos(g*u);
609     sgu = sin(g*u);
610     cgm1u = cos(gm1*u);
611     sgm1u = sin(gm1*u);
612     cv = cos(v);
613     c2v = cos(2.0*v);
614     s2v = sin(2.0*v);
615     cv2 = cv*cv;
616     nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
617     nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
618     nomux = -sqrt2og*cv2*gm1*sgm1u-h1m1og*s2v*su;
619     nomuy = sqrt2og*cv2*gm1*cgm1u-h1m1og*s2v*cu;
620     nomvx = -sqrt2og*s2v*cgm1u+2.0*h1m1og*c2v*cu;
621     nomvy = -sqrt2og*s2v*sgm1u-2.0*h1m1og*c2v*su;
622     den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
623     den2 = den*den;
624     denu = 0.5*M_SQRT2*d*g*cgu*s2v;
625     denv = M_SQRT2*d*sgu*c2v;
626     xx[0] = nomx*den;
627     xx[1] = nomy*den;
628     xx[2] = cv2*den-oz;
629     /* Avoid degenerate tangential plane basis vectors. */
630     if (0.5*M_PI-fabs(v) < FLT_EPSILON)
631     {
632       if (0.5*M_PI-v < FLT_EPSILON)
633         v = 0.5*M_PI-FLT_EPSILON;
634       else
635         v = -0.5*M_PI+FLT_EPSILON;
636       cv = cos(v);
637       c2v = cos(2.0*v);
638       s2v = sin(2.0*v);
639       cv2 = cv*cv;
640       nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
641       nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
642       nomux = -sqrt2og*cv2*gm1*sgm1u-h1m1og*s2v*su;
643       nomuy = sqrt2og*cv2*gm1*cgm1u-h1m1og*s2v*cu;
644       nomvx = -sqrt2og*s2v*cgm1u+2.0*h1m1og*c2v*cu;
645       nomvy = -sqrt2og*s2v*sgm1u-2.0*h1m1og*c2v*su;
646       den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
647       den2 = den*den;
648       denu = 0.5*M_SQRT2*d*g*cgu*s2v;
649       denv = M_SQRT2*d*sgu*c2v;
650     }
651     xxu[0] = nomux*den+nomx*denu*den2;
652     xxu[1] = nomuy*den+nomy*denu*den2;
653     xxu[2] = cv2*denu*den2;
654     xxv[0] = nomvx*den+nomx*denv*den2;
655     xxv[1] = nomvy*den+nomy*denv*den2;
656     xxv[2] = -s2v*den+cv2*denv*den2;
657     for (l=0; l<3; l++)
658     {
659       p[l] = xx[l]*radius;
660       pu[l] = xxu[l]*radius;
661       pv[l] = xxv[l]*radius;
662     }
663     n[0] = pu[1]*pv[2]-pu[2]*pv[1];
664     n[1] = pu[2]*pv[0]-pu[0]*pv[2];
665     n[2] = pu[0]*pv[1]-pu[1]*pv[0];
666     t = 1.0/(pp->side*4.0*sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]));
667     n[0] *= t;
668     n[1] *= t;
669     n[2] *= t;
670     pm[0] = pu[0]*pp->dumove-pv[0]*0.25*pp->dvmove;
671     pm[1] = pu[1]*pp->dumove-pv[1]*0.25*pp->dvmove;
672     pm[2] = pu[2]*pp->dumove-pv[2]*0.25*pp->dvmove;
673     t = 1.0/(4.0*sqrt(pm[0]*pm[0]+pm[1]*pm[1]+pm[2]*pm[2]));
674     pm[0] *= t;
675     pm[1] *= t;
676     pm[2] *= t;
677     b[0] = n[1]*pm[2]-n[2]*pm[1];
678     b[1] = n[2]*pm[0]-n[0]*pm[2];
679     b[2] = n[0]*pm[1]-n[1]*pm[0];
680     t = 1.0/(4.0*sqrt(b[0]*b[0]+b[1]*b[1]+b[2]*b[2]));
681     b[0] *= t;
682     b[1] *= t;
683     b[2] *= t;
684
685     /* Compute alpha, beta, gamma from the three basis vectors.
686            |  -b[0]  -b[1]  -b[2] |
687        m = |   n[0]   n[1]   n[2] |
688            | -pm[0] -pm[1] -pm[2] |
689     */
690     pp->alpha = atan2(-n[2],-pm[2])*180/M_PI;
691     pp->beta = atan2(-b[2],sqrt(b[0]*b[0]+b[1]*b[1]))*180/M_PI;
692     pp->delta = atan2(b[1],-b[0])*180/M_PI;
693
694     /* Compute the rotation that rotates the projective plane in 3D. */
695     rotateall(pp->alpha,pp->beta,pp->delta,mat);
696
697     u = pp->umove;
698     v = pp->vmove;
699     if (g & 1)
700       v = 0.5*M_PI-0.25*v;
701     else
702       v = 0.5*M_PI-0.5*v;
703     sqrt2og = M_SQRT2/g;
704     h1m1og = 0.5*(1.0-1.0/g);
705     gm1 = g-1.0;
706     cu = cos(u);
707     su = sin(u);
708     sgu = sin(g*u);
709     cgm1u = cos(gm1*u);
710     sgm1u = sin(gm1*u);
711     cv = cos(v);
712     s2v = sin(2.0*v);
713     cv2 = cv*cv;
714     nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
715     nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
716     den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
717     xx[0] = nomx*den;
718     xx[1] = nomy*den;
719     xx[2] = cv2*den-oz;
720     for (l=0; l<3; l++)
721     {
722       r = 0.0;
723       for (m=0; m<3; m++)
724         r += mat[l][m]*xx[m];
725       p[l] = r*radius;
726     }
727
728     pp->offset3d[0] = -p[0];
729     pp->offset3d[1] = -p[1]-DELTAY;
730     pp->offset3d[2] = -p[2];
731   }
732   else
733   {
734     /* Compute the rotation that rotates the projective plane in 3D,
735        including the trackball rotations. */
736     rotateall(pp->alpha,pp->beta,pp->delta,r1);
737
738     gltrackball_get_quaternion(pp->trackball,qu);
739     quat_to_rotmat(qu,r2);
740
741     mult_rotmat(r2,r1,mat);
742   }
743
744   if (pp->colors == COLORS_TWOSIDED)
745   {
746     glColor3fv(mat_diff_red);
747     if (pp->display_mode == DISP_TRANSPARENT)
748     {
749       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_red);
750       glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_trans_green);
751     }
752     else
753     {
754       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diff_red);
755       glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diff_green);
756     }
757   }
758   glBindTexture(GL_TEXTURE_2D,pp->tex_name);
759
760   ur = umax-umin;
761   vr = vmax-vmin;
762
763   /* Set up the projective plane coordinates and normals. */
764   if (pp->appearance != APPEARANCE_DIRECTION_BANDS)
765   {
766     for (i=0; i<=numv; i++)
767     {
768       if (pp->appearance == APPEARANCE_DISTANCE_BANDS &&
769           ((i & (NUMB-1)) >= NUMB/4+1) && ((i & (NUMB-1)) < 3*NUMB/4))
770         continue;
771       for (j=0; j<=numu; j++)
772       {
773         o = i*(numu+1)+j;
774         u = ur*j/numu+umin;
775         v = vr*i/numv+vmin;
776         if (g & 1)
777           v = 0.5*M_PI-0.25*v;
778         else
779           v = 0.5*M_PI-0.5*v;
780         sqrt2og = M_SQRT2/g;
781         h1m1og = 0.5*(1.0-1.0/g);
782         gm1 = g-1.0;
783         cu = cos(u);
784         su = sin(u);
785         cgu = cos(g*u);
786         sgu = sin(g*u);
787         cgm1u = cos(gm1*u);
788         sgm1u = sin(gm1*u);
789         cv = cos(v);
790         c2v = cos(2.0*v);
791         s2v = sin(2.0*v);
792         cv2 = cv*cv;
793         nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
794         nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
795         nomux = -sqrt2og*cv2*gm1*sgm1u-h1m1og*s2v*su;
796         nomuy = sqrt2og*cv2*gm1*cgm1u-h1m1og*s2v*cu;
797         nomvx = -sqrt2og*s2v*cgm1u+2.0*h1m1og*c2v*cu;
798         nomvy = -sqrt2og*s2v*sgm1u-2.0*h1m1og*c2v*su;
799         den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
800         den2 = den*den;
801         denu = 0.5*M_SQRT2*d*g*cgu*s2v;
802         denv = M_SQRT2*d*sgu*c2v;
803         xx[0] = nomx*den;
804         xx[1] = nomy*den;
805         xx[2] = cv2*den-oz;
806         /* Avoid degenerate tangential plane basis vectors. */
807         if (0.5*M_PI-fabs(v) < FLT_EPSILON)
808         {
809           if (0.5*M_PI-v < FLT_EPSILON)
810             v = 0.5*M_PI-FLT_EPSILON;
811           else
812             v = -0.5*M_PI+FLT_EPSILON;
813           cv = cos(v);
814           c2v = cos(2.0*v);
815           s2v = sin(2.0*v);
816           cv2 = cv*cv;
817           nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
818           nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
819           nomux = -sqrt2og*cv2*gm1*sgm1u-h1m1og*s2v*su;
820           nomuy = sqrt2og*cv2*gm1*cgm1u-h1m1og*s2v*cu;
821           nomvx = -sqrt2og*s2v*cgm1u+2.0*h1m1og*c2v*cu;
822           nomvy = -sqrt2og*s2v*sgm1u-2.0*h1m1og*c2v*su;
823           den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
824           den2 = den*den;
825           denu = 0.5*M_SQRT2*d*g*cgu*s2v;
826           denv = M_SQRT2*d*sgu*c2v;
827         }
828         xxu[0] = nomux*den+nomx*denu*den2;
829         xxu[1] = nomuy*den+nomy*denu*den2;
830         xxu[2] = cv2*denu*den2;
831         xxv[0] = nomvx*den+nomx*denv*den2;
832         xxv[1] = nomvy*den+nomy*denv*den2;
833         xxv[2] = -s2v*den+cv2*denv*den2;
834         for (l=0; l<3; l++)
835         {
836           r = 0.0;
837           s = 0.0;
838           t = 0.0;
839           for (m=0; m<3; m++)
840           {
841             r += mat[l][m]*xx[m];
842             s += mat[l][m]*xxu[m];
843             t += mat[l][m]*xxv[m];
844           }
845           p[l] = r*radius+pp->offset3d[l];
846           pu[l] = s*radius;
847           pv[l] = t*radius;
848         }
849         n[0] = pu[1]*pv[2]-pu[2]*pv[1];
850         n[1] = pu[2]*pv[0]-pu[0]*pv[2];
851         n[2] = pu[0]*pv[1]-pu[1]*pv[0];
852         t = 1.0/sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
853         n[0] *= t;
854         n[1] *= t;
855         n[2] *= t;
856         pp->pp[3*o+0] = p[0];
857         pp->pp[3*o+1] = p[1];
858         pp->pp[3*o+2] = p[2];
859         pp->pn[3*o+0] = n[0];
860         pp->pn[3*o+1] = n[1];
861         pp->pn[3*o+2] = n[2];
862       }
863     }
864   }
865   else /* pp->appearance == APPEARANCE_DIRECTION_BANDS */
866   {
867     for (j=0; j<=numu; j++)
868     {
869       if ((j & (NUMB-1)) >= NUMB/2+1)
870         continue;
871       for (i=0; i<=numv; i++)
872       {
873         o = i*(numu+1)+j;
874         u = -ur*j/numu+umin;
875         v = vr*i/numv+vmin;
876         if (g & 1)
877           v = 0.5*M_PI-0.25*v;
878         else
879           v = 0.5*M_PI-0.5*v;
880         sqrt2og = M_SQRT2/g;
881         h1m1og = 0.5*(1.0-1.0/g);
882         gm1 = g-1.0;
883         cu = cos(u);
884         su = sin(u);
885         cgu = cos(g*u);
886         sgu = sin(g*u);
887         cgm1u = cos(gm1*u);
888         sgm1u = sin(gm1*u);
889         cv = cos(v);
890         c2v = cos(2.0*v);
891         s2v = sin(2.0*v);
892         cv2 = cv*cv;
893         nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
894         nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
895         nomux = -sqrt2og*cv2*gm1*sgm1u-h1m1og*s2v*su;
896         nomuy = sqrt2og*cv2*gm1*cgm1u-h1m1og*s2v*cu;
897         nomvx = -sqrt2og*s2v*cgm1u+2.0*h1m1og*c2v*cu;
898         nomvy = -sqrt2og*s2v*sgm1u-2.0*h1m1og*c2v*su;
899         den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
900         den2 = den*den;
901         denu = 0.5*M_SQRT2*d*g*cgu*s2v;
902         denv = M_SQRT2*d*sgu*c2v;
903         xx[0] = nomx*den;
904         xx[1] = nomy*den;
905         xx[2] = cv2*den-oz;
906         /* Avoid degenerate tangential plane basis vectors. */
907         if (0.5*M_PI-fabs(v) < FLT_EPSILON)
908         {
909           if (0.5*M_PI-v < FLT_EPSILON)
910             v = 0.5*M_PI-FLT_EPSILON;
911           else
912             v = -0.5*M_PI+FLT_EPSILON;
913           cv = cos(v);
914           c2v = cos(2.0*v);
915           s2v = sin(2.0*v);
916           cv2 = cv*cv;
917           nomx = sqrt2og*cv2*cgm1u+h1m1og*s2v*cu;
918           nomy = sqrt2og*cv2*sgm1u-h1m1og*s2v*su;
919           nomux = -sqrt2og*cv2*gm1*sgm1u-h1m1og*s2v*su;
920           nomuy = sqrt2og*cv2*gm1*cgm1u-h1m1og*s2v*cu;
921           nomvx = -sqrt2og*s2v*cgm1u+2.0*h1m1og*c2v*cu;
922           nomvy = -sqrt2og*s2v*sgm1u-2.0*h1m1og*c2v*su;
923           den = 1.0/(1.0-0.5*M_SQRT2*d*s2v*sgu);
924           den2 = den*den;
925           denu = 0.5*M_SQRT2*d*g*cgu*s2v;
926           denv = M_SQRT2*d*sgu*c2v;
927         }
928         xxu[0] = nomux*den+nomx*denu*den2;
929         xxu[1] = nomuy*den+nomy*denu*den2;
930         xxu[2] = cv2*denu*den2;
931         xxv[0] = nomvx*den+nomx*denv*den2;
932         xxv[1] = nomvy*den+nomy*denv*den2;
933         xxv[2] = -s2v*den+cv2*denv*den2;
934         for (l=0; l<3; l++)
935         {
936           r = 0.0;
937           s = 0.0;
938           t = 0.0;
939           for (m=0; m<3; m++)
940           {
941             r += mat[l][m]*xx[m];
942             s += mat[l][m]*xxu[m];
943             t += mat[l][m]*xxv[m];
944           }
945           p[l] = r*radius+pp->offset3d[l];
946           pu[l] = s*radius;
947           pv[l] = t*radius;
948         }
949         n[0] = pu[1]*pv[2]-pu[2]*pv[1];
950         n[1] = pu[2]*pv[0]-pu[0]*pv[2];
951         n[2] = pu[0]*pv[1]-pu[1]*pv[0];
952         t = 1.0/sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
953         n[0] *= t;
954         n[1] *= t;
955         n[2] *= t;
956         pp->pp[3*o+0] = p[0];
957         pp->pp[3*o+1] = p[1];
958         pp->pp[3*o+2] = p[2];
959         pp->pn[3*o+0] = n[0];
960         pp->pn[3*o+1] = n[1];
961         pp->pn[3*o+2] = n[2];
962       }
963     }
964   }
965
966   if (pp->appearance != APPEARANCE_DIRECTION_BANDS)
967   {
968     for (i=0; i<numv; i++)
969     {
970       if (pp->appearance == APPEARANCE_DISTANCE_BANDS &&
971           ((i & (NUMB-1)) >= NUMB/4) && ((i & (NUMB-1)) < 3*NUMB/4))
972         continue;
973       if (pp->display_mode == DISP_WIREFRAME)
974         glBegin(GL_QUAD_STRIP);
975       else
976         glBegin(GL_TRIANGLE_STRIP);
977       for (j=0; j<=numu; j++)
978       {
979         for (k=0; k<=1; k++)
980         {
981           l = (i+k);
982           m = j;
983           o = l*(numu+1)+m;
984           glTexCoord2fv(&pp->tex[2*o]);
985           if (pp->colors != COLORS_TWOSIDED)
986           {
987             glColor3fv(&pp->col[4*o]);
988             glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,
989                          &pp->col[4*o]);
990           }
991           glNormal3fv(&pp->pn[3*o]);
992           glVertex3fv(&pp->pp[3*o]);
993           polys++;
994         }
995       }
996       glEnd();
997     }
998   }
999   else /* pp->appearance == APPEARANCE_DIRECTION_BANDS */
1000   {
1001     for (j=0; j<numu; j++)
1002     {
1003       if ((j & (NUMB-1)) >= NUMB/2)
1004         continue;
1005       if (pp->display_mode == DISP_WIREFRAME)
1006         glBegin(GL_QUAD_STRIP);
1007       else
1008         glBegin(GL_TRIANGLE_STRIP);
1009       for (i=0; i<=numv; i++)
1010       {
1011         for (k=0; k<=1; k++)
1012         {
1013           l = i;
1014           m = (j+k);
1015           o = l*(numu+1)+m;
1016           glTexCoord2fv(&pp->tex[2*o]);
1017           if (pp->colors != COLORS_TWOSIDED)
1018           {
1019             glColor3fv(&pp->col[4*o]);
1020             glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,
1021                          &pp->col[4*o]);
1022           }
1023           glNormal3fv(&pp->pn[3*o]);
1024           glVertex3fv(&pp->pp[3*o]);
1025           polys++;
1026         }
1027       }
1028       glEnd();
1029     }
1030   }
1031
1032   polys /= 2;
1033   return polys;
1034 }
1035
1036
1037 /* Generate a texture image that shows the orientation reversal. */
1038 static void gen_texture(ModeInfo *mi)
1039 {
1040   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1041
1042   glGenTextures(1,&pp->tex_name);
1043   glBindTexture(GL_TEXTURE_2D,pp->tex_name);
1044   glPixelStorei(GL_UNPACK_ALIGNMENT,1);
1045   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
1046   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
1047   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
1048   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
1049   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
1050   glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEX_DIMENSION,TEX_DIMENSION,0,
1051                GL_LUMINANCE,GL_UNSIGNED_BYTE,texture);
1052 }
1053
1054
1055 static void init(ModeInfo *mi)
1056 {
1057   static const GLfloat light_ambient[]  = { 0.0, 0.0, 0.0, 1.0 };
1058   static const GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
1059   static const GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
1060   static const GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
1061   static const GLfloat mat_specular[]   = { 1.0, 1.0, 1.0, 1.0 };
1062   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1063
1064   if (deform_speed == 0.0)
1065     deform_speed = 10.0;
1066
1067   if (init_deform < 0.0)
1068     init_deform = 0.0;
1069   if (init_deform > 1000.0)
1070     init_deform = 1000.0;
1071
1072   if (walk_speed == 0.0)
1073     walk_speed = 20.0;
1074
1075   if (pp->view == VIEW_TURN)
1076   {
1077     pp->alpha = frand(360.0);
1078     pp->beta = frand(360.0);
1079     pp->delta = frand(360.0);
1080   }
1081   else
1082   {
1083     pp->alpha = 0.0;
1084     pp->beta = 0.0;
1085     pp->delta = 0.0;
1086   }
1087   pp->umove = frand(2.0*M_PI);
1088   pp->vmove = frand(2.0*M_PI);
1089   pp->dumove = 0.0;
1090   pp->dvmove = 0.0;
1091   pp->side = 1;
1092   if (sin(walk_direction*M_PI/180.0) >= 0.0)
1093     pp->dir = 1;
1094   else
1095     pp->dir = -1;
1096
1097   pp->dd = init_deform*0.001;
1098   pp->defdir = -1;
1099
1100   pp->offset3d[0] = 0.0;
1101   pp->offset3d[1] = 0.0;
1102   pp->offset3d[2] = -1.8;
1103
1104   gen_texture(mi);
1105   setup_roman_boy_color_texture(mi,0.0,2.0*M_PI,0.0,2.0*M_PI,pp->g*NUMU,NUMV);
1106
1107   if (pp->marks)
1108     glEnable(GL_TEXTURE_2D);
1109   else
1110     glDisable(GL_TEXTURE_2D);
1111
1112   glMatrixMode(GL_PROJECTION);
1113   glLoadIdentity();
1114   if (pp->projection == DISP_PERSPECTIVE || pp->view == VIEW_WALK)
1115   {
1116     if (pp->view == VIEW_WALK)
1117       gluPerspective(60.0,1.0,0.01,10.0);
1118     else
1119       gluPerspective(60.0,1.0,0.1,10.0);
1120   }
1121   else
1122   {
1123     glOrtho(-1.0,1.0,-1.0,1.0,0.1,10.0);
1124   }
1125   glMatrixMode(GL_MODELVIEW);
1126   glLoadIdentity();
1127
1128 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
1129   if (pp->display_mode == DISP_WIREFRAME)
1130     pp->display_mode = DISP_SURFACE;
1131 # endif
1132
1133   if (pp->display_mode == DISP_SURFACE)
1134   {
1135     glEnable(GL_DEPTH_TEST);
1136     glDepthFunc(GL_LESS);
1137     glShadeModel(GL_SMOOTH);
1138     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
1139     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
1140     glEnable(GL_LIGHTING);
1141     glEnable(GL_LIGHT0);
1142     glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
1143     glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
1144     glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
1145     glLightfv(GL_LIGHT0,GL_POSITION,light_position);
1146     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
1147     glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
1148     glDepthMask(GL_TRUE);
1149     glDisable(GL_BLEND);
1150   }
1151   else if (pp->display_mode == DISP_TRANSPARENT)
1152   {
1153     glDisable(GL_DEPTH_TEST);
1154     glShadeModel(GL_SMOOTH);
1155     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
1156     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
1157     glEnable(GL_LIGHTING);
1158     glEnable(GL_LIGHT0);
1159     glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
1160     glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
1161     glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
1162     glLightfv(GL_LIGHT0,GL_POSITION,light_position);
1163     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
1164     glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50.0);
1165     glDepthMask(GL_FALSE);
1166     glEnable(GL_BLEND);
1167     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
1168   }
1169   else  /* pp->display_mode == DISP_WIREFRAME */
1170   {
1171     glDisable(GL_DEPTH_TEST);
1172     glShadeModel(GL_FLAT);
1173     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
1174     glDisable(GL_LIGHTING);
1175     glDisable(GL_LIGHT0);
1176     glDisable(GL_BLEND);
1177   }
1178 }
1179
1180
1181 /* Redisplay the Klein bottle. */
1182 static void display_romanboy(ModeInfo *mi)
1183 {
1184   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1185
1186   if (!pp->button_pressed)
1187   {
1188     if (deform)
1189     {
1190       pp->dd += pp->defdir*deform_speed*0.001;
1191       if (pp->dd < 0.0)
1192       {
1193         pp->dd = -pp->dd;
1194         pp->defdir = -pp->defdir;
1195       }
1196       if (pp->dd > 1.0)
1197       {
1198         pp->dd = 2.0-pp->dd;
1199         pp->defdir = -pp->defdir;
1200       }
1201     }
1202     if (pp->view == VIEW_TURN)
1203     {
1204       pp->alpha += speed_x * pp->speed_scale;
1205       if (pp->alpha >= 360.0)
1206         pp->alpha -= 360.0;
1207       pp->beta += speed_y * pp->speed_scale;
1208       if (pp->beta >= 360.0)
1209         pp->beta -= 360.0;
1210       pp->delta += speed_z * pp->speed_scale;
1211       if (pp->delta >= 360.0)
1212         pp->delta -= 360.0;
1213     }
1214     if (pp->view == VIEW_WALK)
1215     {
1216       pp->dvmove = (pp->dir*sin(walk_direction*M_PI/180.0)*
1217                     walk_speed*M_PI/4096.0);
1218       pp->vmove += pp->dvmove;
1219       if (pp->vmove > 2.0*M_PI)
1220       {
1221         pp->vmove = 4.0*M_PI-pp->vmove;
1222         pp->umove = pp->umove-M_PI;
1223         if (pp->umove < 0.0)
1224           pp->umove += 2.0*M_PI;
1225         pp->side = -pp->side;
1226         pp->dir = -pp->dir;
1227         pp->dvmove = -pp->dvmove;
1228       }
1229       if (pp->vmove < 0.0)
1230       {
1231         pp->vmove = -pp->vmove;
1232         pp->umove = pp->umove-M_PI;
1233         if (pp->umove < 0.0)
1234           pp->umove += 2.0*M_PI;
1235         pp->dir = -pp->dir;
1236         pp->dvmove = -pp->dvmove;
1237       }
1238       pp->dumove = cos(walk_direction*M_PI/180.0)*walk_speed*M_PI/4096.0;
1239       pp->umove += pp->dumove;
1240       if (pp->umove >= 2.0*M_PI)
1241         pp->umove -= 2.0*M_PI;
1242       if (pp->umove < 0.0)
1243         pp->umove += 2.0*M_PI;
1244     }
1245   }
1246
1247   glMatrixMode(GL_PROJECTION);
1248   glLoadIdentity();
1249   if (pp->projection == DISP_PERSPECTIVE || pp->view == VIEW_WALK)
1250   {
1251     if (pp->view == VIEW_WALK)
1252       gluPerspective(60.0,pp->aspect,0.01,10.0);
1253     else
1254       gluPerspective(60.0,pp->aspect,0.1,10.0);
1255   }
1256   else
1257   {
1258     if (pp->aspect >= 1.0)
1259       glOrtho(-pp->aspect,pp->aspect,-1.0,1.0,0.1,10.0);
1260     else
1261       glOrtho(-1.0,1.0,-1.0/pp->aspect,1.0/pp->aspect,0.1,10.0);
1262   }
1263   glMatrixMode(GL_MODELVIEW);
1264   glLoadIdentity();
1265
1266   mi->polygon_count = roman_boy(mi,0.0,2.0*M_PI,0.0,2.0*M_PI,pp->g*NUMU,NUMV);
1267 }
1268
1269
1270 ENTRYPOINT void reshape_romanboy(ModeInfo *mi, int width, int height)
1271 {
1272   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1273
1274   pp->WindW = (GLint)width;
1275   pp->WindH = (GLint)height;
1276   glViewport(0,0,width,height);
1277   pp->aspect = (GLfloat)width/(GLfloat)height;
1278 }
1279
1280
1281 ENTRYPOINT Bool romanboy_handle_event(ModeInfo *mi, XEvent *event)
1282 {
1283   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1284
1285   if (event->xany.type == ButtonPress && event->xbutton.button == Button1)
1286   {
1287     pp->button_pressed = True;
1288     gltrackball_start(pp->trackball, event->xbutton.x, event->xbutton.y,
1289                       MI_WIDTH(mi), MI_HEIGHT(mi));
1290     return True;
1291   }
1292   else if (event->xany.type == ButtonRelease &&
1293            event->xbutton.button == Button1)
1294   {
1295     pp->button_pressed = False;
1296     return True;
1297   }
1298   else if (event->xany.type == MotionNotify && pp->button_pressed)
1299   {
1300     gltrackball_track(pp->trackball, event->xmotion.x, event->xmotion.y,
1301                       MI_WIDTH(mi), MI_HEIGHT(mi));
1302     return True;
1303   }
1304
1305   return False;
1306 }
1307
1308
1309 /*
1310  *-----------------------------------------------------------------------------
1311  *-----------------------------------------------------------------------------
1312  *    Xlock hooks.
1313  *-----------------------------------------------------------------------------
1314  *-----------------------------------------------------------------------------
1315  */
1316
1317 static void free_romanboy(ModeInfo *mi);
1318
1319 /*
1320  *-----------------------------------------------------------------------------
1321  *    Initialize romanboy.  Called each time the window changes.
1322  *-----------------------------------------------------------------------------
1323  */
1324
1325 ENTRYPOINT void init_romanboy(ModeInfo *mi)
1326 {
1327   romanboystruct *pp;
1328
1329   MI_INIT (mi, romanboy, free_romanboy);
1330   pp = &romanboy[MI_SCREEN(mi)];
1331
1332   if (surface_order < 2)
1333     pp->g = 2;
1334   else if (surface_order > 9)
1335     pp->g = 9;
1336   else
1337     pp->g = surface_order;
1338
1339   pp->pp = calloc(3*pp->g*(NUMU+1)*(NUMV+1),sizeof(float));
1340   pp->pn = calloc(3*pp->g*(NUMU+1)*(NUMV+1),sizeof(float));
1341   pp->col = calloc(4*pp->g*(NUMU+1)*(NUMV+1),sizeof(float));
1342   pp->tex = calloc(2*pp->g*(NUMU+1)*(NUMV+1),sizeof(float));
1343
1344   pp->trackball = gltrackball_init(True);
1345   pp->button_pressed = False;
1346
1347   /* Set the display mode. */
1348   if (!strcasecmp(mode,"random"))
1349   {
1350     pp->display_mode = random() % NUM_DISPLAY_MODES;
1351   }
1352   else if (!strcasecmp(mode,"wireframe"))
1353   {
1354     pp->display_mode = DISP_WIREFRAME;
1355   }
1356   else if (!strcasecmp(mode,"surface"))
1357   {
1358     pp->display_mode = DISP_SURFACE;
1359   }
1360   else if (!strcasecmp(mode,"transparent"))
1361   {
1362     pp->display_mode = DISP_TRANSPARENT;
1363   }
1364   else
1365   {
1366     pp->display_mode = random() % NUM_DISPLAY_MODES;
1367   }
1368
1369   pp->marks = marks;
1370
1371   /* Orientation marks don't make sense in wireframe mode. */
1372   if (pp->display_mode == DISP_WIREFRAME)
1373     pp->marks = False;
1374
1375   /* Set the appearance. */
1376   if (!strcasecmp(appear,"random"))
1377   {
1378     pp->appearance = random() % NUM_APPEARANCES;
1379   }
1380   else if (!strcasecmp(appear,"solid"))
1381   {
1382     pp->appearance = APPEARANCE_SOLID;
1383   }
1384   else if (!strcasecmp(appear,"distance-bands"))
1385   {
1386     pp->appearance = APPEARANCE_DISTANCE_BANDS;
1387   }
1388   else if (!strcasecmp(appear,"direction-bands"))
1389   {
1390     pp->appearance = APPEARANCE_DIRECTION_BANDS;
1391   }
1392   else
1393   {
1394     pp->appearance = random() % NUM_APPEARANCES;
1395   }
1396
1397   /* Set the color mode. */
1398   if (!strcasecmp(color_mode,"random"))
1399   {
1400     pp->colors = random() % NUM_COLORS;
1401   }
1402   else if (!strcasecmp(color_mode,"two-sided"))
1403   {
1404     pp->colors = COLORS_TWOSIDED;
1405   }
1406   else if (!strcasecmp(color_mode,"distance"))
1407   {
1408     pp->colors = COLORS_DISTANCE;
1409   }
1410   else if (!strcasecmp(color_mode,"direction"))
1411   {
1412     pp->colors = COLORS_DIRECTION;
1413   }
1414   else
1415   {
1416     pp->colors = random() % NUM_COLORS;
1417   }
1418
1419   /* Set the view mode. */
1420   if (!strcasecmp(view_mode,"random"))
1421   {
1422     pp->view = random() % NUM_VIEW_MODES;
1423   }
1424   else if (!strcasecmp(view_mode,"walk"))
1425   {
1426     pp->view = VIEW_WALK;
1427   }
1428   else if (!strcasecmp(view_mode,"turn"))
1429   {
1430     pp->view = VIEW_TURN;
1431   }
1432   else
1433   {
1434     pp->view = random() % NUM_VIEW_MODES;
1435   }
1436
1437   /* Set the 3d projection mode. */
1438   if (!strcasecmp(proj,"random"))
1439   {
1440     /* Orthographic projection only makes sense in turn mode. */
1441     if (pp->view == VIEW_TURN)
1442       pp->projection = random() % NUM_DISP_MODES;
1443     else
1444       pp->projection = DISP_PERSPECTIVE;
1445   }
1446   else if (!strcasecmp(proj,"perspective"))
1447   {
1448     pp->projection = DISP_PERSPECTIVE;
1449   }
1450   else if (!strcasecmp(proj,"orthographic"))
1451   {
1452     pp->projection = DISP_ORTHOGRAPHIC;
1453   }
1454   else
1455   {
1456     /* Orthographic projection only makes sense in turn mode. */
1457     if (pp->view == VIEW_TURN)
1458       pp->projection = random() % NUM_DISP_MODES;
1459     else
1460       pp->projection = DISP_PERSPECTIVE;
1461   }
1462
1463   /* make multiple screens rotate at slightly different rates. */
1464   pp->speed_scale = 0.9 + frand(0.3);
1465
1466   if ((pp->glx_context = init_GL(mi)) != NULL)
1467   {
1468     reshape_romanboy(mi,MI_WIDTH(mi),MI_HEIGHT(mi));
1469     glDrawBuffer(GL_BACK);
1470     init(mi);
1471   }
1472   else
1473   {
1474     MI_CLEARWINDOW(mi);
1475   }
1476 }
1477
1478 /*
1479  *-----------------------------------------------------------------------------
1480  *    Called by the mainline code periodically to update the display.
1481  *-----------------------------------------------------------------------------
1482  */
1483 ENTRYPOINT void draw_romanboy(ModeInfo *mi)
1484 {
1485   Display *display = MI_DISPLAY(mi);
1486   Window window = MI_WINDOW(mi);
1487   romanboystruct *pp;
1488
1489   if (romanboy == NULL)
1490     return;
1491   pp = &romanboy[MI_SCREEN(mi)];
1492
1493   MI_IS_DRAWN(mi) = True;
1494   if (!pp->glx_context)
1495     return;
1496
1497   glXMakeCurrent(display,window,*(pp->glx_context));
1498
1499   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1500   glLoadIdentity();
1501
1502   display_romanboy(mi);
1503
1504   if (MI_IS_FPS(mi))
1505     do_fps (mi);
1506
1507   glFlush();
1508
1509   glXSwapBuffers(display,window);
1510 }
1511
1512
1513 /*
1514  *-----------------------------------------------------------------------------
1515  *    The display is being taken away from us.  Free up malloc'ed 
1516  *      memory and X resources that we've alloc'ed.
1517  *-----------------------------------------------------------------------------
1518  */
1519
1520 static void free_romanboy(ModeInfo *mi)
1521 {
1522   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1523
1524   if (pp->pp)
1525     (void) free((void *)pp->pp);
1526   if (pp->pn)
1527     (void) free((void *)pp->pn);
1528   if (pp->col)
1529     (void) free((void *)pp->col);
1530   if (pp->tex)
1531     (void) free((void *)pp->tex);
1532 }
1533
1534 #ifndef STANDALONE
1535 ENTRYPOINT void change_romanboy(ModeInfo *mi)
1536 {
1537   romanboystruct *pp = &romanboy[MI_SCREEN(mi)];
1538
1539   if (!pp->glx_context)
1540     return;
1541
1542   glXMakeCurrent(MI_DISPLAY(mi),MI_WINDOW(mi),*(pp->glx_context));
1543   init(mi);
1544 }
1545 #endif /* !STANDALONE */
1546
1547 XSCREENSAVER_MODULE ("RomanBoy", romanboy)
1548
1549 #endif /* USE_GL */