From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / hacks / glx / moebius.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* moebius --- Moebius Strip II, an Escher-like GL scene with ants. */
3
4 #if 0
5 static const char sccsid[] = "@(#)moebius.c     5.01 2001/03/01 xlockmore";
6 #endif
7
8 /*-
9  * Permission to use, copy, modify, and distribute this software and its
10  * documentation for any purpose and without fee is hereby granted,
11  * provided that the above copyright notice appear in all copies and that
12  * both that copyright notice and this permission notice appear in
13  * supporting documentation.
14  *
15  * This file is provided AS IS with no warranties of any kind.  The author
16  * shall have no liability with respect to the infringement of copyrights,
17  * trade secrets or any patents by this file or any part thereof.  In no
18  * event will the author be liable for any lost revenue or profits or
19  * other special, indirect and consequential damages.
20  *
21  * The RotateAroundU() routine was adapted from the book
22  *    "Computer Graphics Principles and Practice
23  *     Foley - vanDam - Feiner - Hughes
24  *     Second Edition" Pag. 227, exercise 5.15.
25  *
26  * This mode shows some interesting scenes that are impossible OR very
27  * wierd to build in the real universe. Much of the scenes are inspirated
28  * on Mauritz Cornelis Escher's works which derivated the mode's name.
29  * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
30  * say he was a mathematician.
31  *
32  * Thanks goes to Brian Paul for making it possible and inexpensive to use
33  * OpenGL at home.
34  *
35  * Since I'm not a native English speaker, my apologies for any grammatical
36  * mistakes.
37  *
38  * My e-mail address is
39  * mfvianna@centroin.com.br
40  *
41  * Marcelo F. Vianna (Jun-01-1997)
42  *
43  * Revision History:
44  * 05-Apr-2002: Removed all gllist uses (fix some bug with nvidia driver)
45  * 01-Mar-2001: backported from xscreensaver by lassauge@mail.dotcom.fr
46  *    Feb-2001: Made motion and rotation be smoother Jamie Zawinski
47  *              <jwz@jwz.org>
48  * 01-Nov-2000: Allocation checks
49  * 01-Jan-1998: Mode separated from escher and renamed
50  * 08-Jun-1997: New scene implemented: "Impossible Cage" based in a M.C.
51  *              Escher's painting with the same name (quite similar). The
52  *              first GL mode to use texture mapping.
53  *              The "Impossible Cage" scene doesn't use DEPTH BUFFER, the
54  *              wood planks are drawn consistently using GL_CULL_FACE, and
55  *              the painter's algorithm is used to sort the planks.
56  *              Marcelo F. Vianna.
57  * 07-Jun-1997: Speed ups in Moebius Strip using GL_CULL_FACE.
58  *              Marcelo F. Vianna.
59  * 03-Jun-1997: Initial Release (Only one scene: "Moebius Strip")
60  *              The Moebius Strip scene was inspirated in a M.C. Escher's
61  *              painting named Moebius Strip II in wich ants walk across a
62  *              Moebius Strip path, sometimes meeting each other and sometimes
63  *              being in "opposite faces" (note that the moebius strip has
64  *              only one face and one edge).
65  *              Marcelo F. Vianna.
66  */
67
68 /*-
69  * Texture mapping is only available on RGBA contexts, Mono and color index
70  * visuals DO NOT support texture mapping in OpenGL.
71  *
72  * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
73  * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
74  * is not officially supported for both OpenGL and Mesa, but seems to not crash
75  * Mesa.
76  *
77  * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
78  */
79
80 #ifdef STANDALONE
81 # define MODE_moebius
82 # define refresh_moebius 0
83 # define DEFAULTS                       "*delay:                20000   \n"                     \
84                                                         "*showFPS:      False   \n"
85
86 # include "xlockmore.h"         /* from the xscreensaver distribution */
87 #else /* !STANDALONE */
88 # include "xlock.h"             /* from the xlockmore distribution */
89 #endif /* !STANDALONE */
90
91 #ifdef HAVE_COCOA
92 # include "jwxyz.h"
93 #else
94 # include <X11/Xlib.h>
95 # include <GL/gl.h>
96 # include <GL/glu.h>
97 #endif
98
99 #ifdef HAVE_JWZGLES
100 # include "jwzgles.h"
101 #endif /* HAVE_JWZGLES */
102
103 #ifdef MODE_moebius
104
105 #if 0 /* Hey, this never actually used the texture at all! */
106 #if 0
107 #include "e_textures.h"
108 #else
109 #include "xpm-ximage.h"
110 #include "../images/wood.xpm"
111 #endif
112 #endif /* 0 */
113
114 #include "sphere.h"
115 #include "tube.h"
116
117 #include "rotator.h"
118 #include "gltrackball.h"
119
120 #define DEF_SOLIDMOEBIUS  "False"
121 #define DEF_DRAWANTS  "True"
122
123 static int  solidmoebius;
124 static int  drawants;
125
126 static XrmOptionDescRec opts[] =
127 {
128   {"-solidmoebius", ".moebius.solidmoebius", XrmoptionNoArg, "on"},
129   {"+solidmoebius", ".moebius.solidmoebius", XrmoptionNoArg, "off"},
130   {"-ants", ".moebius.drawants", XrmoptionNoArg, "on"},
131   {"+ants", ".moebius.drawants", XrmoptionNoArg, "off"}
132 };
133 static argtype vars[] =
134 {
135   {&solidmoebius, "solidmoebius", "Solidmoebius", DEF_SOLIDMOEBIUS, t_Bool},
136   {&drawants, "drawants", "Drawants", DEF_DRAWANTS, t_Bool}
137
138 };
139 static OptionStruct desc[] =
140 {
141         {"-/+solidmoebius", "select between a SOLID or a NET Moebius Strip"},
142         {"-/+drawants", "turn on/off walking ants"}
143 };
144
145 ENTRYPOINT ModeSpecOpt moebius_opts =
146 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
147
148 #ifdef USE_MODULES
149 ModStruct   moebius_description =
150 {"moebius", "init_moebius", "draw_moebius", "release_moebius",
151  "draw_moebius", "change_moebius", (char *) NULL, &moebius_opts,
152  1000, 1, 1, 1, 4, 1.0, "",
153  "Shows Moebius Strip II, an Escher-like GL scene with ants", 0, NULL};
154
155 #endif
156
157 #define Scale4Window               0.3
158 #define Scale4Iconic               0.4
159
160 #define sqr(A)                     ((A)*(A))
161
162 #ifndef Pi
163 #define Pi                         M_PI
164 #endif
165
166 #define ObjMoebiusStrip 0
167 #define ObjAntBody      1
168 #define MaxObj          2
169
170 /*************************************************************************/
171
172 typedef struct {
173         GLint       WindH, WindW;
174         GLfloat     step;
175         GLfloat     ant_position;
176         float       ant_step;
177         GLXContext *glx_context;
178     rotator    *rot;
179     trackball_state *trackball;
180     Bool        button_down_p;
181 } moebiusstruct;
182
183 static const float front_shininess[] = {60.0};
184 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
185 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
186 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
187 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
188 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
189 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
190 static const float lmodel_twoside[] = {GL_TRUE};
191
192 static const float MaterialRed[] = {0.7, 0.0, 0.0, 1.0};
193 static const float MaterialGreen[] = {0.1, 0.5, 0.2, 1.0};
194 static const float MaterialBlue[] = {0.0, 0.0, 0.7, 1.0};
195 static const float MaterialCyan[] = {0.2, 0.5, 0.7, 1.0};
196 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
197 static const float MaterialMagenta[] = {0.6, 0.2, 0.5, 1.0};
198 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
199 static const float MaterialGray[] = {0.2, 0.2, 0.2, 1.0};
200 static const float MaterialGray5[] = {0.5, 0.5, 0.5, 1.0};
201 static const float MaterialGray6[] = {0.6, 0.6, 0.6, 1.0};
202 static const float MaterialGray8[] = {0.8, 0.8, 0.8, 1.0};
203
204 static moebiusstruct *moebius = (moebiusstruct *) NULL;
205
206 #define NUM_SCENES      2
207
208 static Bool
209 mySphere(float radius)
210 {
211 #if 0
212         GLUquadricObj *quadObj;
213
214         if ((quadObj = gluNewQuadric()) == 0)
215                 return False;
216         gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
217         gluSphere(quadObj, radius, 16, 16);
218         gluDeleteQuadric(quadObj);
219 #else
220     glPushMatrix();
221     glScalef (radius, radius, radius);
222     unit_sphere (16, 16, False);
223     glPopMatrix();
224 #endif
225         return True;
226 }
227
228 static Bool
229 myCone(float radius)
230 {
231 #if 0
232         GLUquadricObj *quadObj;
233
234         if ((quadObj = gluNewQuadric()) == 0)
235                 return False;
236         gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
237         gluCylinder(quadObj, radius, 0, radius * 3, 8, 1);
238         gluDeleteQuadric(quadObj);
239 #else
240     cone (0, 0, 0,
241           0, 0, radius * 3,
242           radius, 0,
243           8, True, True, False);
244 #endif
245         return True;
246 }
247
248 static Bool
249 draw_moebius_ant(moebiusstruct * mp, const float *Material, int mono)
250 {
251         float       cos1 = cos(mp->ant_step);
252         float       cos2 = cos(mp->ant_step + 2 * Pi / 3);
253         float       cos3 = cos(mp->ant_step + 4 * Pi / 3);
254         float       sin1 = sin(mp->ant_step);
255         float       sin2 = sin(mp->ant_step + 2 * Pi / 3);
256         float       sin3 = sin(mp->ant_step + 4 * Pi / 3);
257
258         if (mono)
259                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
260         else
261                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Material);
262         glEnable(GL_CULL_FACE);
263         glPushMatrix();
264         glScalef(1, 1.3, 1);
265         if (!mySphere(0.18))
266                 return False;
267         glScalef(1, 1 / 1.3, 1);
268         glTranslatef(0.00, 0.30, 0.00);
269         if (!mySphere(0.2))
270                 return False;
271
272         glTranslatef(-0.05, 0.17, 0.05);
273         glRotatef(-90, 1, 0, 0);
274         glRotatef(-25, 0, 1, 0);
275         if (!myCone(0.05))
276                 return False;
277         glTranslatef(0.00, 0.10, 0.00);
278         if (!myCone(0.05))
279                 return False;
280         glRotatef(25, 0, 1, 0);
281         glRotatef(90, 1, 0, 0);
282
283         glScalef(1, 1.3, 1);
284         glTranslatef(0.15, -0.65, 0.05);
285         if (!mySphere(0.25))
286                 return False;
287         glScalef(1, 1 / 1.3, 1);
288         glPopMatrix();
289         glDisable(GL_CULL_FACE);
290
291         glDisable(GL_LIGHTING);
292         /* ANTENNAS */
293         glBegin(GL_LINES);
294         if (mono)
295                 glColor3fv(MaterialGray5);
296         else
297                 glColor3fv(Material);
298         glVertex3f(0.00, 0.30, 0.00);
299         glColor3fv(MaterialGray);
300         glVertex3f(0.40, 0.70, 0.40);
301         if (mono)
302                 glColor3fv(MaterialGray5);
303         else
304                 glColor3fv(Material);
305         glVertex3f(0.00, 0.30, 0.00);
306         glColor3fv(MaterialGray);
307         glVertex3f(0.40, 0.70, -0.40);
308         glEnd();
309         glBegin(GL_POINTS);
310         if (mono)
311                 glColor3fv(MaterialGray6);
312         else
313                 glColor3fv(MaterialRed);
314         glVertex3f(0.40, 0.70, 0.40);
315         glVertex3f(0.40, 0.70, -0.40);
316         glEnd();
317
318         /* LEFT-FRONT ARM */
319         glBegin(GL_LINE_STRIP);
320         if (mono)
321                 glColor3fv(MaterialGray5);
322         else
323                 glColor3fv(Material);
324         glVertex3f(0.00, 0.05, 0.18);
325         glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
326         glColor3fv(MaterialGray);
327         glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
328         glEnd();
329
330         /* LEFT-CENTER ARM */
331         glBegin(GL_LINE_STRIP);
332         if (mono)
333                 glColor3fv(MaterialGray5);
334         else
335                 glColor3fv(Material);
336         glVertex3f(0.00, 0.00, 0.18);
337         glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
338         glColor3fv(MaterialGray);
339         glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
340         glEnd();
341
342         /* LEFT-BACK ARM */
343         glBegin(GL_LINE_STRIP);
344         if (mono)
345                 glColor3fv(MaterialGray5);
346         else
347                 glColor3fv(Material);
348         glVertex3f(0.00, -0.05, 0.18);
349         glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
350         glColor3fv(MaterialGray);
351         glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
352         glEnd();
353
354         /* RIGHT-FRONT ARM */
355         glBegin(GL_LINE_STRIP);
356         if (mono)
357                 glColor3fv(MaterialGray5);
358         else
359                 glColor3fv(Material);
360         glVertex3f(0.00, 0.05, -0.18);
361         glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
362         glColor3fv(MaterialGray);
363         glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
364         glEnd();
365
366         /* RIGHT-CENTER ARM */
367         glBegin(GL_LINE_STRIP);
368         if (mono)
369                 glColor3fv(MaterialGray5);
370         else
371                 glColor3fv(Material);
372         glVertex3f(0.00, 0.00, -0.18);
373         glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
374         glColor3fv(MaterialGray);
375         glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
376         glEnd();
377
378         /* RIGHT-BACK ARM */
379         glBegin(GL_LINE_STRIP);
380         if (mono)
381                 glColor3fv(MaterialGray5);
382         else
383                 glColor3fv(Material);
384         glVertex3f(0.00, -0.05, -0.18);
385         glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
386         glColor3fv(MaterialGray);
387         glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
388         glEnd();
389
390         glBegin(GL_POINTS);
391         if (mono)
392                 glColor3fv(MaterialGray8);
393         else
394                 glColor3fv(MaterialMagenta);
395         glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
396         glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
397         glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
398         glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
399         glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
400         glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
401         glEnd();
402
403         glEnable(GL_LIGHTING);
404
405         mp->ant_step += 0.3;
406         return True;
407 }
408
409 static void
410 RotateAaroundU(float Ax, float Ay, float Az,
411                float Ux, float Uy, float Uz,
412                float *Cx, float *Cy, float *Cz,
413                float Theta)
414 {
415         float       cosO = cos(Theta);
416         float       sinO = sin(Theta);
417         float       one_cosO = 1 - cosO;
418         float       Ux2 = sqr(Ux);
419         float       Uy2 = sqr(Uy);
420         float       Uz2 = sqr(Uz);
421         float       UxUy = Ux * Uy;
422         float       UxUz = Ux * Uz;
423         float       UyUz = Uy * Uz;
424
425         *Cx = (Ux2 + cosO * (1 - Ux2)) * Ax + (UxUy * one_cosO - Uz * sinO) * Ay + (UxUz * one_cosO + Uy * sinO) * Az;
426         *Cy = (UxUy * one_cosO + Uz * sinO) * Ax + (Uy2 + cosO * (1 - Uy2)) * Ay + (UyUz * one_cosO - Ux * sinO) * Az;
427         *Cz = (UxUz * one_cosO - Uy * sinO) * Ax + (UyUz * one_cosO + Ux * sinO) * Ay + (Uz2 + cosO * (1 - Uz2)) * Az;
428 }
429
430 #define MoebiusDivisions 40
431 #define MoebiusTransversals 4
432 static Bool
433 draw_moebius_strip(ModeInfo * mi)
434 {
435         GLfloat     Phi, Theta;
436         GLfloat     cPhi, sPhi;
437         moebiusstruct *mp = &moebius[MI_SCREEN(mi)];
438         int         i, j;
439         int         mono = MI_IS_MONO(mi);
440
441         float       Cx, Cy, Cz;
442
443 #ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
444     solidmoebius = True;
445 #endif
446
447         if (solidmoebius) {
448                 glBegin(GL_QUAD_STRIP);
449                 Phi = 0;
450                 i = 0;
451                 while (i < (MoebiusDivisions * 2 + 1)) {
452                         Theta = Phi / 2;
453                         cPhi = cos(Phi);
454                         sPhi = sin(Phi);
455
456                         i++;
457                         if (mono)
458                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
459                         else if (i % 2)
460                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
461                         else
462                                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
463
464                         RotateAaroundU(cPhi, sPhi, 0, -sPhi, cPhi, 0, &Cx, &Cy, &Cz, Theta);
465                         glNormal3f(Cx, Cy, Cz);
466                         RotateAaroundU(0, 0, 1, -sPhi, cPhi, 0, &Cx, &Cy, &Cz, Theta);
467                         glVertex3f(cPhi * 3 + Cx, sPhi * 3 + Cy, +Cz);
468                         glVertex3f(cPhi * 3 - Cx, sPhi * 3 - Cy, -Cz);
469
470                         Phi += Pi / MoebiusDivisions;
471                 }
472                 glEnd();
473         } else {
474                 for (j = -MoebiusTransversals; j < MoebiusTransversals; j++) {
475                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
476                         glBegin(GL_QUAD_STRIP);
477                         Phi = 0;
478                         i = 0;
479                         while (i < (MoebiusDivisions * 2 + 1)) {
480                                 Theta = Phi / 2;
481                                 cPhi = cos(Phi);
482                                 sPhi = sin(Phi);
483
484                                 RotateAaroundU(cPhi, sPhi, 0, -sPhi, cPhi, 0, &Cx, &Cy, &Cz, Theta);
485                                 glNormal3f(Cx, Cy, Cz);
486                                 RotateAaroundU(0, 0, 1, -sPhi, cPhi, 0, &Cx, &Cy, &Cz, Theta);
487                                 j++;
488                                 if (j == MoebiusTransversals || mono)
489                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
490                                 else if (i % 2)
491                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
492                                 else
493                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
494                                 glVertex3f(cPhi * 3 + Cx / MoebiusTransversals * j, sPhi * 3 + Cy / MoebiusTransversals * j, +Cz / MoebiusTransversals * j);
495                                 j--;
496                                 if (j == -MoebiusTransversals || mono)
497                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
498                                 else if (i % 2)
499                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
500                                 else
501                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
502                                 glVertex3f(cPhi * 3 + Cx / MoebiusTransversals * j, sPhi * 3 + Cy / MoebiusTransversals * j, +Cz / MoebiusTransversals * j);
503
504                                 Phi += Pi / MoebiusDivisions;
505                                 i++;
506                         }
507                         glEnd();
508                 }
509                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
510         }
511
512         if (drawants) {
513                 /* DRAW BLUE ANT */
514                 glPushMatrix();
515                 glRotatef(mp->ant_position + 180, 0, 0, 1);
516                 glTranslatef(3, 0, 0);
517                 glRotatef(mp->ant_position / 2 + 90, 0, 1, 0);
518                 glTranslatef(0.28, 0, -0.45);
519                 if (!draw_moebius_ant(mp, MaterialYellow, mono))
520                         return False;
521                 glPopMatrix();
522
523                 /* DRAW YELLOW ANT */
524                 glPushMatrix();
525                 glRotatef(mp->ant_position, 0, 0, 1);
526                 glTranslatef(3, 0, 0);
527                 glRotatef(mp->ant_position / 2, 0, 1, 0);
528                 glTranslatef(0.28, 0, -0.45);
529                 if (!draw_moebius_ant(mp, MaterialBlue, mono))
530                         return False;
531                 glPopMatrix();
532
533                 /* DRAW GREEN ANT */
534                 glPushMatrix();
535                 glRotatef(-mp->ant_position, 0, 0, 1);
536                 glTranslatef(3, 0, 0);
537                 glRotatef(-mp->ant_position / 2, 0, 1, 0);
538                 glTranslatef(0.28, 0, 0.45);
539                 glRotatef(180, 1, 0, 0);
540                 if (!draw_moebius_ant(mp, MaterialGreen, mono))
541                         return False;
542                 glPopMatrix();
543
544                 /* DRAW CYAN ANT */
545                 glPushMatrix();
546                 glRotatef(-mp->ant_position + 180, 0, 0, 1);
547                 glTranslatef(3, 0, 0);
548                 glRotatef(-mp->ant_position / 2 + 90, 0, 1, 0);
549                 glTranslatef(0.28, 0, 0.45);
550                 glRotatef(180, 1, 0, 0);
551                 if (!draw_moebius_ant(mp, MaterialCyan, mono))
552                         return False;
553                 glPopMatrix();
554         }
555         mp->ant_position += 1;
556         return True;
557 }
558 #undef MoebiusDivisions
559 #undef MoebiusTransversals
560
561 ENTRYPOINT void
562 reshape_moebius (ModeInfo * mi, int width, int height)
563 {
564         moebiusstruct *mp = &moebius[MI_SCREEN(mi)];
565
566         glViewport(0, 0, mp->WindW = (GLint) width, mp->WindH = (GLint) height);
567         glMatrixMode(GL_PROJECTION);
568         glLoadIdentity();
569         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
570         glMatrixMode(GL_MODELVIEW);
571         if (width >= 1024) {
572                 glLineWidth(3);
573                 glPointSize(3);
574         } else if (width >= 512) {
575                 glLineWidth(2);
576                 glPointSize(2);
577         } else {
578                 glLineWidth(1);
579                 glPointSize(1);
580         }
581 }
582
583 static void
584 pinit(ModeInfo *mi)
585 {
586   /* int status; */
587         glClearDepth(1.0);
588         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
589         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
590         glLightfv(GL_LIGHT0, GL_POSITION, position0);
591         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
592         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
593         glLightfv(GL_LIGHT1, GL_POSITION, position1);
594         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
595         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
596         glEnable(GL_LIGHTING);
597         glEnable(GL_LIGHT0);
598         glEnable(GL_LIGHT1);
599         glEnable(GL_NORMALIZE);
600         glFrontFace(GL_CCW);
601         glCullFace(GL_BACK);
602
603         /* moebius */
604         glShadeModel(GL_SMOOTH);
605         glEnable(GL_DEPTH_TEST);
606         glDisable(GL_CULL_FACE);
607
608 #if 0
609         glEnable(GL_TEXTURE_2D);
610         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
611
612 #if 0
613     clear_gl_error();
614         status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
615                                WoodTextureWidth, WoodTextureHeight,
616                                GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
617     if (status)
618       {
619         const char *s = (char *) gluErrorString (status);
620         fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
621                  progname, WoodTextureWidth, WoodTextureHeight,
622                  (s ? s : "(unknown)"));
623         exit (1);
624       }
625     check_gl_error("mipmapping");
626 #else
627     {
628       XImage *img = xpm_to_ximage (mi->dpy,
629                                    mi->xgwa.visual,
630                                    mi->xgwa.colormap,
631                                    wood_texture);
632           glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
633                     img->width, img->height, 0,
634                     GL_RGBA,
635                     /* GL_UNSIGNED_BYTE, */
636                     GL_UNSIGNED_INT_8_8_8_8_REV,
637                     img->data);
638       check_gl_error("texture");
639       XDestroyImage (img);
640     }
641 #endif
642
643         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
644         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
645         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
646         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
647         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
648 #endif
649
650         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
651         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
652 }
653
654
655
656 ENTRYPOINT void
657 release_moebius (ModeInfo * mi)
658 {
659         if (moebius != NULL) {
660                 (void) free((void *) moebius);
661                 moebius = (moebiusstruct *) NULL;
662         }
663         FreeAllGL(mi);
664 }
665
666 ENTRYPOINT Bool
667 moebius_handle_event (ModeInfo *mi, XEvent *event)
668 {
669   moebiusstruct *mp = &moebius[MI_SCREEN(mi)];
670
671   if (gltrackball_event_handler (event, mp->trackball,
672                                  MI_WIDTH (mi), MI_HEIGHT (mi),
673                                  &mp->button_down_p))
674     return True;
675
676   return False;
677 }
678
679
680 ENTRYPOINT void
681 init_moebius (ModeInfo * mi)
682 {
683         moebiusstruct *mp;
684
685         if (moebius == NULL) {
686                 if ((moebius = (moebiusstruct *) calloc(MI_NUM_SCREENS(mi),
687                                             sizeof (moebiusstruct))) == NULL)
688                         return;
689         }
690         mp = &moebius[MI_SCREEN(mi)];
691         mp->step = NRAND(90);
692         mp->ant_position = NRAND(90);
693
694     {
695       double rot_speed = 0.3;
696       mp->rot = make_rotator (rot_speed, rot_speed, rot_speed, 1, 0, True);
697       mp->trackball = gltrackball_init (True);
698     }
699
700         if ((mp->glx_context = init_GL(mi)) != NULL) {
701
702                 reshape_moebius(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
703                 glDrawBuffer(GL_BACK);
704                 pinit(mi);
705         } else {
706                 MI_CLEARWINDOW(mi);
707         }
708 }
709
710 ENTRYPOINT void
711 draw_moebius (ModeInfo * mi)
712 {
713         moebiusstruct *mp;
714
715         Display    *display = MI_DISPLAY(mi);
716         Window      window = MI_WINDOW(mi);
717
718         if (moebius == NULL)
719             return;
720         mp = &moebius[MI_SCREEN(mi)];
721
722         MI_IS_DRAWN(mi) = True;
723
724         if (!mp->glx_context)
725                 return;
726
727         glXMakeCurrent(display, window, *(mp->glx_context));
728
729         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
730
731         glPushMatrix();
732
733         glTranslatef(0.0, 0.0, -10.0);
734
735     gltrackball_rotate (mp->trackball);
736
737         if (!MI_IS_ICONIC(mi)) {
738                 glScalef(Scale4Window * mp->WindH / mp->WindW, Scale4Window, Scale4Window);
739         } else {
740                 glScalef(Scale4Iconic * mp->WindH / mp->WindW, Scale4Iconic, Scale4Iconic);
741         }
742
743     {
744       double x, y, z;
745       get_rotation (mp->rot, &x, &y, &z, !mp->button_down_p);
746       glRotatef (x * 360, 1.0, 0.0, 0.0);
747       glRotatef (y * 360, 0.0, 1.0, 0.0);
748       glRotatef (z * 360, 0.0, 0.0, 1.0);
749     }
750
751         /* moebius */
752         if (!draw_moebius_strip(mi)) {
753                 release_moebius(mi);
754                 return;
755         }
756
757         glPopMatrix();
758
759     if (MI_IS_FPS(mi)) do_fps (mi);
760         glFlush();
761
762         glXSwapBuffers(display, window);
763
764         mp->step += 0.025;
765 }
766
767 #ifndef STANDALONE
768 ENTRYPOINT void
769 change_moebius (ModeInfo * mi)
770 {
771         moebiusstruct *mp = &moebius[MI_SCREEN(mi)];
772
773         if (!mp->glx_context)
774                 return;
775
776         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
777         pinit();
778 }
779 #endif /* !STANDALONE */
780
781
782 XSCREENSAVER_MODULE ("Moebius", moebius)
783
784 #endif