From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / gleidescope.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* vim: set ai ts=4 sw=4: */
3
4 #if 0
5 /*static const char sccsid[] = "@(#)gleidescope.c       1.0 03/06/27 xlockmore";*/
6 #endif
7
8 /* enable -grab switch for animations */
9 #undef  GRAB
10
11 #undef DISPLAY_TEXTURE
12
13 /*-
14  * Permission to use, copy, modify, and distribute this software and its
15  * documentation for any purpose and without fee is hereby granted,
16  * provided that the above copyright notice appear in all copies and that
17  * both that copyright notice and this permission notice appear in
18  * supporting documentation.
19  *
20  * This file is provided AS IS with no warranties of any kind.  The author
21  * shall have no liability with respect to the infringement of copyrights,
22  * trade secrets or any patents by this file or any part thereof.  In no
23  * event will the author be liable for any lost revenue or profits or
24  * other special, indirect and consequential damages.
25  *
26  *      Revision History:
27  *
28  *      20030627        1.0             acd             First Release.
29  *                                                              Texture loading code from 'glplanet'
30  *                                                                      by Jamie Zawinski <jwz@jwz.org>
31  *      20030810        1.1             acd             Added size flag.
32  *                                                              Now grabs screen / video / picture
33  *                                                                      (uses code from 'glslideshow' by
34  *                                                                      Mike Oliphant, Ben Buxton, Jamie Zawinski).
35  *                                                              Added -duration.
36  *                                                              Added mouse code.
37  *                                                              Added fade code (also from glslideshow).
38  *      20031013        1.2             acd             Migrated to compile without warnings under
39  *                                                                      xscreensaver 4.13.
40  *      20031023        1.3             acd             Better code to limit twisting speeds.
41  *                                                              Tweaked initial rotation values.
42  *                                                              Move, Rotate, Zoom now chosen at random if
43  *                                                                      no preference is given.
44  *                                                              Made grid slightly bigger so you can't see
45  *                                                                      the edge when zooming and moving.
46  *      20061226        1.4             acd             Now uses GL Display Lists.
47  *      20070318        1.5             acd             Generates textures.
48  *                                                              Fixed texture size problem (and introduced another).
49  *      20070412        1.6             acd             Textures now have independant sizes.
50  *      20070413        1.7             acd             Added Lissajous movement pattern.
51  *      20070414        1.8             acd             Added corners movement pattern.
52  *      20080319        1.9             acd             Changes to arguments for saner gleidescope.xml.
53  *
54  * TODO
55  * generate textures myself - render random shapes to 256x256 texture. (done)
56  * lower res for checks and random - use 256 and 4x4 or 8x8 pixels. (disabled for now)
57  * gnome-saver doesn't let you specify source directory so add that to this.
58  * image loading routine is too slow - rotation grinds to a halt - stop using it. (better in version 5)
59  * image loading also looks bad for non-square images - edges are black. (fixed)
60  * possible to see edge of the world on widescreen terminals - use larger grid and hidden hex removal?
61  * fading textures may have different max_tx - use two sets. (done)
62  * choice of movement patterns. (3 implemented, chooseable at compile time)
63  * look into rangle and tangle.
64  */
65
66 /*
67 **----------------------------------------------------------------------------
68 ** Defines
69 **----------------------------------------------------------------------------
70 */
71
72 #ifdef STANDALONE
73 # define DEFAULTS \
74                 "*delay:                20000           \n"     \
75                 "*showFPS:              False           \n"     \
76                 "*size:                 0                       \n"     \
77                 "*useSHM:               True            \n" \
78                 "*suppressRotationAnimation: True\n" \
79
80 # define refresh_gleidescope 0
81 # define release_gleidescope 0
82 # include "xlockmore.h"                         /* from the xscreensaver distribution */
83 #else  /* !STANDALONE */
84 # include "xlock.h"                                     /* from the xlockmore distribution */
85 #endif /* !STANDALONE */
86
87 #ifdef USE_GL
88
89 #include "colors.h"
90 #include "xpm-ximage.h"
91 #include "grab-ximage.h"
92
93 #ifdef GRAB
94 void grab_frame(Display *display, Window window);
95 #endif
96
97 /* acd TODO should all these be in gleidestruct? */
98 /* they can't be, because of the idiotic way the xlockmore "argtype vars"
99    interface works. -jwz */
100 #ifdef GRAB
101 static Bool             grab;                   /* grab images */
102 #endif
103 static Bool             move;                   /* moving camera */
104 static Bool             nomove;                 /* no moving camera */
105 static Bool             rotate;                 /* rotate in place */
106 static Bool             norotate;               /* no rotate in place */
107 static Bool             zoom;                   /* zooming camera */
108 static Bool             nozoom;                 /* no zooming camera */
109 static char             *image_str;             /* name of texture to load */
110 static int              duration;               /* length of time to display grabbed image */
111
112 #define MAX_CAM_SPEED                   1.0
113 #define MAX_ANGLE_VEL                   1.0
114 #define INITIAL_ANGLE_VEL               0.2
115 #define INITIAL_ANGLE_ACC               0.001
116 #define TWISTING_PROBABILITY    1000    /* 1 in ... of change of acceleration */
117
118 #define RADIANS (M_PI / 180)
119 #define ANGLE_120       (M_PI * 2 / 3)
120 #define ANGLE_240       (M_PI * 4 / 3)
121
122 #define DEF_GRAB        "False"
123 #define DEF_MOVE        "False"
124 #define DEF_NOMOVE      "False"
125 #define DEF_ROTATE      "False"
126 #define DEF_NOROTATE    "False"
127 #define DEF_ZOOM        "False"
128 #define DEF_NOZOOM      "False"
129 #define DEF_IMAGE       "DEFAULT"
130 #define DEF_DURATION    "30"
131
132
133 static XrmOptionDescRec opts[] =
134 {
135 #ifdef GRAB
136         {"-grab",               ".gleidescope.grab",            XrmoptionNoArg,         "true"},
137 #endif
138         {"-move",               ".gleidescope.move",            XrmoptionNoArg,         "true"},
139         {"-no-move",    ".gleidescope.nomove",          XrmoptionNoArg,         "true"},
140         {"-rotate",             ".gleidescope.rotate",          XrmoptionNoArg,         "true"},
141         {"-no-rotate",  ".gleidescope.norotate",        XrmoptionNoArg,         "true"},
142         {"-zoom",               ".gleidescope.zoom",            XrmoptionNoArg,         "true"},
143         {"-no-zoom",    ".gleidescope.nozoom",          XrmoptionNoArg,         "true"},
144         {"-image",              ".gleidescope.image",           XrmoptionSepArg,        "DEFAULT"},
145         {"-duration",   ".gleidescope.duration",        XrmoptionSepArg,        "30"},
146 };
147
148
149 static argtype vars[] = {
150 #ifdef GRAB
151         {&grab,                 "grab",         "Grab",         DEF_GRAB,       t_Bool},
152 #endif
153         {&move,                 "move",         "Move",         DEF_MOVE,       t_Bool},
154         {&nomove,               "nomove",       "noMove",       DEF_NOMOVE,     t_Bool},
155         {&rotate,               "rotate",       "Rotate",       DEF_ROTATE,     t_Bool},
156         {&norotate,             "norotate",     "noRotate",     DEF_NOROTATE,   t_Bool},
157         {&zoom,                 "zoom",         "Zoom",         DEF_ZOOM,       t_Bool},
158         {&nozoom,               "nozoom",       "noZoom",       DEF_NOZOOM,     t_Bool},
159         {&image_str,            "image",        "Image",        DEF_IMAGE,      t_String},
160         {&duration,             "duration",     "Duration",     DEF_DURATION,           t_Int},
161 };
162
163 static OptionStruct desc[] = {
164 #ifdef GRAB
165         {"-grab",               "grab images to create animation"},
166 #endif
167         {"-move",               "camera will move"},
168         {"-no-move",    "camera won't move"},
169         {"-rotate",             "camera will rotate"},
170         {"-no-rotate",  "camera won't rotate"},
171         {"-zoom",               "camera will zoom"},
172         {"-no-zoom",    "camera won't zoom"},
173         {"-image",              "xpm / xbm image file to use for texture"},
174         {"-duration",   "length of time texture will be used"},
175 };
176
177 ENTRYPOINT ModeSpecOpt gleidescope_opts = {
178         sizeof opts / sizeof opts[0], opts,
179         sizeof vars / sizeof vars[0], vars,
180         desc
181 };
182
183 #ifdef USE_MODULES
184 ModStruct   gleidescope_description = { 
185      "gleidescope", "init_gleidescope", "draw_gleidescope", NULL,
186      "draw_gleidescope", "init_gleidescope", NULL, &gleidescope_opts,
187      1000, 1, 2, 1, 4, 1.0, "",
188      "GL Kaleidescope", 0, NULL};
189 #endif
190
191 /*
192 **-----------------------------------------------------------------------------
193 **      Typedefs
194 **-----------------------------------------------------------------------------
195 */
196
197 typedef struct hex_s {
198         GLfloat x, y, z;                /* position */
199 } hex_t;
200
201 typedef struct {
202         GLfloat x;
203         GLfloat y;
204         GLfloat z;
205 } vectorf;
206
207 typedef struct {
208         GLfloat x;
209         GLfloat y;
210 } vector2f;
211
212 typedef struct {
213         GLuint                  id;                             /* opengl texture id */
214         GLfloat                 width, height;  /* texture width and height */
215         GLfloat                 min_tx, min_ty; /* minimum texture sizes */
216         GLfloat                 max_tx, max_ty; /* maximum texture sizes */
217         time_t                  start_time;
218         Bool                    button_down_p;
219     Bool                        mipmap_p;
220     Bool                        waiting_for_image_p;
221         /* r_phase is for triangle rotation speed */
222         GLfloat                 x_period, y_period, r_period;
223         GLfloat                 x_phase, y_phase, r_phase;
224 } texture;
225
226 #define MAX_FADE        500     /* number of fade cycles */
227
228 typedef struct {
229         float                   cam_x_speed, cam_z_speed, cam_y_speed;
230         int                             cam_x_phase, cam_z_phase, cam_y_phase;
231         float                   tic;
232         GLXContext              *glx_context;
233         Window                  window;
234         texture                 textures[2];    /* texture handles */
235         GLuint                  visible;                /* index for current texture */
236         GLint                   fade;
237         time_t                  start_time;
238         Bool                    button_down_p;
239
240     int         size;
241         int             list;
242
243     float       tangle;         /* texture angle (degrees) */
244     float       tangle_vel;     /* texture velocity */
245     float       tangle_acc;     /* texture acceleration */
246
247     float       rangle;         /* rotate angle */
248     float       rangle_vel;     /* rotate velocity */
249     float       rangle_acc;     /* rotate acceleration */
250
251     /* mouse */
252     int xstart;
253     int ystart;
254     double xmouse;
255     double ymouse;
256
257     Bool mipmap_p;
258     Bool waiting_for_image_p;
259
260 } gleidestruct;
261
262 #define frandrange(x, y)        (x + frand(y - x))
263
264 #define XOFFSET (0.8660254f)    /* sin 60' */
265 #define YOFFSET (1.5000000f)    /* cos 60' + 1 */
266
267 #if 0
268
269 #define SIZE    3
270
271 /* generates a grid with edges of given size */
272 /* acd TODO - replace hex[] with this and allow size and distance as parameters */
273
274 int
275 generate_grid(int size)
276
277         int     i, x, y;
278
279         gp->size--;
280
281         i = gp->size;
282         for (y = -size ; y <= size ; y++) {
283                 for (x = -i ; x <= i ; x += 2) {
284                         printf("{XOFFSET * %d, YOFFSET * %d, 0},\n", x, y);
285                 }
286                 printf("\n");
287                 if (y < 0) {
288                         i++;
289                 } else {
290                         i--;
291                 }
292         }
293         return 0;
294 }
295 #endif
296
297 /* acd - this is terrible - 120+ hexes */
298 static const hex_t hex[] = {
299         /* edges of size 7 */
300         /* number of hexagons required to cover screen depends on camera distance */
301         /* at a distance of 10 this is just about enough. */
302         {XOFFSET * -6, YOFFSET * -6, 0},
303         {XOFFSET * -4, YOFFSET * -6, 0},
304         {XOFFSET * -2, YOFFSET * -6, 0},
305         {XOFFSET * 0, YOFFSET * -6, 0},
306         {XOFFSET * 2, YOFFSET * -6, 0},
307         {XOFFSET * 4, YOFFSET * -6, 0},
308         {XOFFSET * 6, YOFFSET * -6, 0},
309
310         {XOFFSET * -7, YOFFSET * -5, 0},
311         {XOFFSET * -5, YOFFSET * -5, 0},
312         {XOFFSET * -3, YOFFSET * -5, 0},
313         {XOFFSET * -1, YOFFSET * -5, 0},
314         {XOFFSET * 1, YOFFSET * -5, 0},
315         {XOFFSET * 3, YOFFSET * -5, 0},
316         {XOFFSET * 5, YOFFSET * -5, 0},
317         {XOFFSET * 7, YOFFSET * -5, 0},
318
319         {XOFFSET * -8, YOFFSET * -4, 0},
320         {XOFFSET * -6, YOFFSET * -4, 0},
321         {XOFFSET * -4, YOFFSET * -4, 0},
322         {XOFFSET * -2, YOFFSET * -4, 0},
323         {XOFFSET * 0, YOFFSET * -4, 0},
324         {XOFFSET * 2, YOFFSET * -4, 0},
325         {XOFFSET * 4, YOFFSET * -4, 0},
326         {XOFFSET * 6, YOFFSET * -4, 0},
327         {XOFFSET * 8, YOFFSET * -4, 0},
328
329         {XOFFSET * -9, YOFFSET * -3, 0},
330         {XOFFSET * -7, YOFFSET * -3, 0},
331         {XOFFSET * -5, YOFFSET * -3, 0},
332         {XOFFSET * -3, YOFFSET * -3, 0},
333         {XOFFSET * -1, YOFFSET * -3, 0},
334         {XOFFSET * 1, YOFFSET * -3, 0},
335         {XOFFSET * 3, YOFFSET * -3, 0},
336         {XOFFSET * 5, YOFFSET * -3, 0},
337         {XOFFSET * 7, YOFFSET * -3, 0},
338         {XOFFSET * 9, YOFFSET * -3, 0},
339
340         {XOFFSET * -10, YOFFSET * -2, 0},
341         {XOFFSET * -8, YOFFSET * -2, 0},
342         {XOFFSET * -6, YOFFSET * -2, 0},
343         {XOFFSET * -4, YOFFSET * -2, 0},
344         {XOFFSET * -2, YOFFSET * -2, 0},
345         {XOFFSET * 0, YOFFSET * -2, 0},
346         {XOFFSET * 2, YOFFSET * -2, 0},
347         {XOFFSET * 4, YOFFSET * -2, 0},
348         {XOFFSET * 6, YOFFSET * -2, 0},
349         {XOFFSET * 8, YOFFSET * -2, 0},
350         {XOFFSET * 10, YOFFSET * -2, 0},
351
352         {XOFFSET * -11, YOFFSET * -1, 0},
353         {XOFFSET * -9, YOFFSET * -1, 0},
354         {XOFFSET * -7, YOFFSET * -1, 0},
355         {XOFFSET * -5, YOFFSET * -1, 0},
356         {XOFFSET * -3, YOFFSET * -1, 0},
357         {XOFFSET * -1, YOFFSET * -1, 0},
358         {XOFFSET * 1, YOFFSET * -1, 0},
359         {XOFFSET * 3, YOFFSET * -1, 0},
360         {XOFFSET * 5, YOFFSET * -1, 0},
361         {XOFFSET * 7, YOFFSET * -1, 0},
362         {XOFFSET * 9, YOFFSET * -1, 0},
363         {XOFFSET * 11, YOFFSET * -1, 0},
364
365         {XOFFSET * -12, YOFFSET * 0, 0},
366         {XOFFSET * -10, YOFFSET * 0, 0},
367         {XOFFSET * -8, YOFFSET * 0, 0},
368         {XOFFSET * -6, YOFFSET * 0, 0},
369         {XOFFSET * -4, YOFFSET * 0, 0},
370         {XOFFSET * -2, YOFFSET * 0, 0},
371         {XOFFSET * 0, YOFFSET * 0, 0},
372         {XOFFSET * 2, YOFFSET * 0, 0},
373         {XOFFSET * 4, YOFFSET * 0, 0},
374         {XOFFSET * 6, YOFFSET * 0, 0},
375         {XOFFSET * 8, YOFFSET * 0, 0},
376         {XOFFSET * 10, YOFFSET * 0, 0},
377         {XOFFSET * 12, YOFFSET * 0, 0},
378
379         {XOFFSET * -11, YOFFSET * 1, 0},
380         {XOFFSET * -9, YOFFSET * 1, 0},
381         {XOFFSET * -7, YOFFSET * 1, 0},
382         {XOFFSET * -5, YOFFSET * 1, 0},
383         {XOFFSET * -3, YOFFSET * 1, 0},
384         {XOFFSET * -1, YOFFSET * 1, 0},
385         {XOFFSET * 1, YOFFSET * 1, 0},
386         {XOFFSET * 3, YOFFSET * 1, 0},
387         {XOFFSET * 5, YOFFSET * 1, 0},
388         {XOFFSET * 7, YOFFSET * 1, 0},
389         {XOFFSET * 9, YOFFSET * 1, 0},
390         {XOFFSET * 11, YOFFSET * 1, 0},
391
392         {XOFFSET * -10, YOFFSET * 2, 0},
393         {XOFFSET * -8, YOFFSET * 2, 0},
394         {XOFFSET * -6, YOFFSET * 2, 0},
395         {XOFFSET * -4, YOFFSET * 2, 0},
396         {XOFFSET * -2, YOFFSET * 2, 0},
397         {XOFFSET * 0, YOFFSET * 2, 0},
398         {XOFFSET * 2, YOFFSET * 2, 0},
399         {XOFFSET * 4, YOFFSET * 2, 0},
400         {XOFFSET * 6, YOFFSET * 2, 0},
401         {XOFFSET * 8, YOFFSET * 2, 0},
402         {XOFFSET * 10, YOFFSET * 2, 0},
403
404         {XOFFSET * -9, YOFFSET * 3, 0},
405         {XOFFSET * -7, YOFFSET * 3, 0},
406         {XOFFSET * -5, YOFFSET * 3, 0},
407         {XOFFSET * -3, YOFFSET * 3, 0},
408         {XOFFSET * -1, YOFFSET * 3, 0},
409         {XOFFSET * 1, YOFFSET * 3, 0},
410         {XOFFSET * 3, YOFFSET * 3, 0},
411         {XOFFSET * 5, YOFFSET * 3, 0},
412         {XOFFSET * 7, YOFFSET * 3, 0},
413         {XOFFSET * 9, YOFFSET * 3, 0},
414
415         {XOFFSET * -8, YOFFSET * 4, 0},
416         {XOFFSET * -6, YOFFSET * 4, 0},
417         {XOFFSET * -4, YOFFSET * 4, 0},
418         {XOFFSET * -2, YOFFSET * 4, 0},
419         {XOFFSET * 0, YOFFSET * 4, 0},
420         {XOFFSET * 2, YOFFSET * 4, 0},
421         {XOFFSET * 4, YOFFSET * 4, 0},
422         {XOFFSET * 6, YOFFSET * 4, 0},
423         {XOFFSET * 8, YOFFSET * 4, 0},
424
425         {XOFFSET * -7, YOFFSET * 5, 0},
426         {XOFFSET * -5, YOFFSET * 5, 0},
427         {XOFFSET * -3, YOFFSET * 5, 0},
428         {XOFFSET * -1, YOFFSET * 5, 0},
429         {XOFFSET * 1, YOFFSET * 5, 0},
430         {XOFFSET * 3, YOFFSET * 5, 0},
431         {XOFFSET * 5, YOFFSET * 5, 0},
432         {XOFFSET * 7, YOFFSET * 5, 0},
433
434         {XOFFSET * -6, YOFFSET * 6, 0},
435         {XOFFSET * -4, YOFFSET * 6, 0},
436         {XOFFSET * -2, YOFFSET * 6, 0},
437         {XOFFSET * 0, YOFFSET * 6, 0},
438         {XOFFSET * 2, YOFFSET * 6, 0},
439         {XOFFSET * 4, YOFFSET * 6, 0},
440         {XOFFSET * 6, YOFFSET * 6, 0},
441 };
442
443 /*
444 **----------------------------------------------------------------------------
445 ** Local Variables
446 **----------------------------------------------------------------------------
447 */
448
449 static  gleidestruct *gleidescope = NULL;
450
451 #if 0
452 /*
453  *load defaults in config structure
454  */
455 static void setdefaultconfig(void)
456 {
457 #ifdef GRAB
458         grab = False;
459 #endif
460         move = False;
461         rotate = False;
462         zoom = False;
463         image = NULL;
464 }
465 #endif
466
467 ENTRYPOINT Bool
468 gleidescope_handle_event(ModeInfo *mi, XEvent *event)
469 {
470         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
471
472         /*
473         printf("event:%d\n", event->xany.type);
474         printf("button:%d\n", event->xbutton.button);
475         */
476         if (event->xany.type == ButtonPress)
477       {
478                         if (event->xbutton.button == Button1 ||
479                 event->xbutton.button == Button3)
480                         {
481                                 /* store initial values of mouse */
482                                 gp->xstart = event->xbutton.x;
483                                 gp->ystart = event->xbutton.y;
484
485                                 /* button is down */
486                                 gp->button_down_p = True;
487                                 return True;
488                         }
489 #if 0   /* TODO */
490                         else if (event->xbutton.button == Button4)
491                         {
492                                 /* zoom in */
493                                 return True;
494                         }
495                         else if (event->xbutton.button == Button5)
496                         {
497                                 /* zoom out */
498                                 return True;
499                         }
500 #endif
501             } else if (event->xany.type == ButtonRelease)
502       {
503                         if (event->xbutton.button == Button1 ||
504                 event->xbutton.button == Button3)
505                         {
506                                 /* button is up */
507                                 gp->button_down_p = False;
508                                 return True;
509                         }
510             } else if (event->xany.type == MotionNotify)
511       {
512                         if (gp->button_down_p)
513                         {
514                                 /* update mouse position */
515                                 gp->xmouse += (double)(event->xmotion.x - gp->xstart) / MI_WIDTH(mi);
516                                 gp->ymouse += (double)(event->xmotion.y - gp->ystart) / MI_HEIGHT(mi);
517                                 gp->xstart = event->xmotion.x;
518                                 gp->ystart = event->xmotion.y;
519
520                                 return True;
521                         }
522       }
523   else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
524     {
525       gp->start_time = -1;
526       gp->fade = 0;
527       return True;
528     }
529
530         return False;
531 }
532
533
534 static void
535 image_loaded_cb (const char *filename, XRectangle *geometry,
536                  int image_width, int image_height, 
537                  int texture_width, int texture_height,
538                  void *closure)
539 {
540         texture *tp = (texture *) closure;
541
542 #if 0
543         gp->max_tx = (GLfloat) image_width  / texture_width;
544         gp->max_ty = (GLfloat) image_height / texture_height;
545 #endif
546
547         /* new - taken from flipscreen */
548         tp->width = texture_width;
549         tp->height = texture_height;
550         tp->min_tx = (GLfloat) geometry->x / tp->width;
551         tp->min_ty = (GLfloat) geometry->y / tp->height;
552         tp->max_tx = (GLfloat) (geometry->x + geometry->width)  / tp->width;
553         tp->max_ty = (GLfloat) (geometry->y + geometry->height) / tp->height;
554
555 #ifdef DEBUG
556         printf("Image w,h: (%d, %d)\n", image_width, image_height);
557         printf("Texture w,h: (%d, %d)\n", texture_width, texture_height);
558         printf("Geom x,y: (%d, %d)\n", geometry->x, geometry->y);
559         printf("Geom w,h: (%d, %d)\n", geometry->width, geometry->height);
560         printf("Max Tx,Ty: (%f, %f)\n", tp->max_tx, tp->max_ty);
561 #endif
562
563         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
564         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
565                         (tp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
566
567         tp->waiting_for_image_p = False;
568         tp->start_time = time ((time_t *) 0);
569 }
570
571 static void
572 getSnapshot(ModeInfo *mi, texture *texture)
573 {
574         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
575
576 #ifdef DEBUG
577         printf("getSnapshot");
578 #endif
579
580         if (MI_IS_WIREFRAME(mi))
581                 return;
582
583     gp->mipmap_p = True;
584     load_texture_async (mi->xgwa.screen, mi->window,
585                         *gp->glx_context, 0, 0, gp->mipmap_p, 
586                         texture->id, image_loaded_cb, texture);
587         texture->start_time = time((time_t *)0);
588 }
589
590 #define TEXTURE_SIZE    256
591
592 static void
593 plot(unsigned char *buffer, int x, int y, int r, int g, int b, int a) {
594         int c;
595         if (x < 0 || x >= TEXTURE_SIZE || y < 0 || y >= TEXTURE_SIZE) {
596                 return;
597         }
598         c = ((x * TEXTURE_SIZE) + y) * 4;
599         /*printf("(%d,%d)[%d]\n", x, y, c);*/
600         buffer[c + 0] = r;
601         buffer[c + 1] = g;
602         buffer[c + 2] = b;
603         buffer[c + 3] = a;
604 }
605
606 #if 0
607 static void
608 plot2(unsigned char *buffer, int x, int y, int r, int g, int b, int a) {
609         int c;
610         if (x < 0 || x >= TEXTURE_SIZE || y < 0 || y >= TEXTURE_SIZE) {
611                 return;
612         }
613         c = ((x * TEXTURE_SIZE) + y) * 4;
614         /*printf("(%d,%d)[%d]\n", x, y, c);*/
615         buffer[c + 0] = r;
616         buffer[c + 1] = g;
617         buffer[c + 2] = b;
618         buffer[c + 3] = a;
619         
620         if (y + 1 < TEXTURE_SIZE) {
621                 buffer[c + 4] = r;
622                 buffer[c + 5] = g;
623                 buffer[c + 6] = b;
624                 buffer[c + 7] = a;
625         }
626
627         if (x + 1 < TEXTURE_SIZE) {
628                 c += (TEXTURE_SIZE * 4);
629                 buffer[c + 0] = r;
630                 buffer[c + 1] = g;
631                 buffer[c + 2] = b;
632                 buffer[c + 3] = a;
633                 if (y + 1 < TEXTURE_SIZE) {
634                         buffer[c + 4] = r;
635                         buffer[c + 5] = g;
636                         buffer[c + 6] = b;
637                         buffer[c + 7] = a;
638                 }
639         }
640 }
641 #endif
642
643 /* draw geometric shapes to texture */
644 /* modifies passed in buffer */
645 static void
646 draw_shapes (unsigned char *buffer) {
647         int a = 0xff;
648         int x, y, w, h;
649         int i, j;
650         int s;
651         float left, right;
652
653         for (i = 0 ; i < TEXTURE_SIZE * TEXTURE_SIZE * 4 ; i += 4) {
654                 buffer[i + 0] = 0x00;
655                 buffer[i + 1] = 0x00;
656                 buffer[i + 2] = 0x00;
657                 buffer[i + 3] = 0xff;
658         }
659
660         for (s = 0 ; s < 25 ; s++) {
661                 int shape = random() % 3;
662
663                 /* 8 bits */
664                 int r = (random() & 0xff);
665                 int g = (random() & 0xff);
666                 int b = (random() & 0xff);
667
668                 switch (shape) {
669                         case 0:
670                                 /* rectangle */
671                                 x = (random() % TEXTURE_SIZE) - (TEXTURE_SIZE / 4);     /* top left */
672                                 y = (random() % TEXTURE_SIZE) - (TEXTURE_SIZE / 4);
673                                 w = 10 + random() % (TEXTURE_SIZE / 4); /* size */
674                                 h = 10 + random() % (TEXTURE_SIZE / 4);
675 #ifdef DEBUG
676                                 printf("Rectangle: (%d, %d)(%d, %d)\n", x, y, w, h);
677 #endif
678                                 if (x < 0) {
679                                         x = 0;
680                                 }
681                                 if (y < 0) {
682                                         y = 0;
683                                 }
684                                 for (i = x ; i < x + w && i < TEXTURE_SIZE; i++) {
685                                         for (j = y ; j < y + h && j < TEXTURE_SIZE; j++) {
686                                                 plot(buffer, i, j, r, g, b, a);
687                                         }
688                                 }
689                                 break;
690
691                         case 1:
692                                 /* circle */
693                                 x = random() % TEXTURE_SIZE;    /* centre */
694                                 y = random() % TEXTURE_SIZE;
695                                 h = 10 + random() % (TEXTURE_SIZE / 8); /* radius */
696 #ifdef DEBUG
697                                 printf("Circle: %d, %d, %d\n", x, y, h);
698 #endif
699                                 for (i = 0 ; i < h ; i++) {
700                                         int xdist = i * i;
701                                         for (j = 0 ; j < h ; j++) {
702                                                 int ydist = j * j;
703                                                 /*
704                                                 printf("xdist: %d\n", xdist);
705                                                 printf("ydist: %d\n", ydist);
706                                                 printf("radius: %d\n", h * h);
707                                                 */
708                                                 if ((xdist + ydist) < (h * h)) {
709                                                         plot(buffer, x + i, y + j, r, b, g, a);
710                                                         /* check we haven't already done these */
711                                                         if (j != 0) {
712                                                                 plot(buffer, x + i, y - j, r, b, g, a);
713                                                         }
714                                                         if (i != 0) {
715                                                                 plot(buffer, x - i, y + j, r, b, g, a);
716                                                                 if (j != 0) {
717                                                                 plot(buffer, x - i, y - j, r, b, g, a);
718                                                                 }
719                                                         }
720                                                 }
721                                         }
722                                 }
723                                 break;
724
725                         case 2:
726                                 /* triangle */
727                                 x = random() % TEXTURE_SIZE;    /* top */
728                                 y = random() % TEXTURE_SIZE;
729                                 h = 10 + random() % (TEXTURE_SIZE / 4); /* height */
730 #ifdef DEBUG
731                                 printf("Triangle: %d, %d, %d\n", x, y, h);
732 #endif
733                                 left = x;
734                                 right = x;
735                                 for (i = 0 ; i < h ; i++) {
736                                         for (j = left ; j < right ; j++) {
737                                                 plot(buffer, j, y + i, r, g, b, a);
738                                         }
739                                         left -= .5;
740                                         right += .5;
741                                 }
742                                 break;
743                 }
744         }
745 }
746
747 static void
748 setup_random_texture (ModeInfo *mi, texture *texture)
749 {
750         int width = 0, height = 0;
751         char buf[1024];
752         unsigned char *my_data = NULL;
753 #if 0
754         int i, j, c;
755         int style;
756         int r0, g0, b0, a0, r1, g1, b1, a1;
757 #endif
758
759 #ifdef DEBUG
760         printf("RandomTexture\n");
761 #endif
762
763         /* use this texture */
764         glBindTexture(GL_TEXTURE_2D, texture->id);
765
766         clear_gl_error();
767
768         /*
769          * code for various generated patterns - noise, stripes, checks etc.
770          * random geometric shapes looked the best.
771          */
772
773 #if 0
774         style = random() & 0x3;
775         r0 = random() & 0xff; 
776         g0 = random() & 0xff; 
777         b0 = random() & 0xff; 
778         a0 = 0xff;
779         r1 = random() & 0xff; 
780         g1 = random() & 0xff; 
781         b1 = random() & 0xff; 
782         a1 = 0xff;
783
784         switch (style) {
785                 case 0: /* random */
786                         printf("Random0\n");
787                         height = width = TEXTURE_SIZE;
788                         my_data = (void *)malloc(width * height * 4);
789                         for (i = 0 ; i < width ; i += 2) {
790                                 for (j = 0 ; j < height ; j += 2) {
791                                         r0 = random() & 0xff; 
792                                         g0 = random() & 0xff; 
793                                         b0 = random() & 0xff; 
794                                         a0 = 0xff;
795                                         plot2(my_data, i, j, r0, g0, b0, a0);
796                                 }
797                         }
798                         break;
799
800                 case 1: /* shapes */
801 #endif
802 #ifdef DEBUG
803                         printf("Shapes\n");
804 #endif
805                         height = width = TEXTURE_SIZE;
806                         my_data = (void *)malloc(width * height * 4);
807                         draw_shapes(my_data);
808 #if 0
809                         break;
810
811                 case 2: /* check */
812                         printf("Check\n");
813                         height = width = TEXTURE_SIZE;
814                         my_data = (void *)malloc(width * height * 4);
815                         for (i = 0 ; i < height ; i += 2) {
816                                 for (j = 0 ; j < width ; j += 2) {
817                                         if (((i + j) & 0x3) == 0) {
818                                                 plot2(my_data, i, j, r0, g0, b0, a0);
819                                         } else {
820                                                 plot2(my_data, i, j, r1, g1, b1, a1);
821                                         }
822                                 }
823                         }
824                         break;
825
826                 case 3: /* random stripes */
827                         printf("Stripes 2\n");
828                         height = width = TEXTURE_SIZE;
829                         my_data = (void *)malloc(width * height * 4);
830                         for (i = 0 ; i < height ; i += 2) {
831                                 r0 = random() & 0xff; 
832                                 g0 = random() & 0xff; 
833                                 b0 = random() & 0xff; 
834                                 a0 = 0xff;
835                                 for (j = 0 ; j < width ; j += 2) {
836                                         plot2(my_data, i, j, r0, g0, b0, a0);
837                                 }
838                         }
839                         break;
840         }
841 #endif
842
843         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
844                         width, height, 0,
845                         GL_RGBA, GL_UNSIGNED_BYTE, my_data);
846         sprintf (buf, "random texture: (%dx%d)",
847                         width, height);
848         check_gl_error(buf);
849
850         /* setup parameters for texturing */
851         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
852         glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
853
854         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
855         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
856         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
857         if (random() & 0x1) {
858                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
859                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
860         } else {
861                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
862                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
863         }
864
865         if (my_data != NULL) {
866                 free(my_data);
867                 my_data = NULL;
868         }
869
870         /* use full texture */
871         /* acd - was 1.0 */
872         texture->min_tx = 0.0;
873         texture->max_tx = 2.0;
874         texture->min_ty = 0.0;
875         texture->max_ty = 2.0;
876         texture->start_time = time((time_t *)0);
877 }
878
879 static Bool
880 setup_file_texture (ModeInfo *mi, char *filename, texture *texture)
881 {
882         Display *dpy = mi->dpy;
883         Visual *visual = mi->xgwa.visual;
884         char buf[1024];
885
886         Colormap cmap = mi->xgwa.colormap;
887         XImage *image = xpm_file_to_ximage (dpy, visual, cmap, filename);
888     if (!image) return False;
889
890 #ifdef DEBUG
891         printf("FileTexture\n");
892 #endif
893
894         /* use this texture */
895         glBindTexture(GL_TEXTURE_2D, texture->id);
896
897         clear_gl_error();
898         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
899                         image->width, image->height, 0,
900                         GL_RGBA, GL_UNSIGNED_BYTE, image->data);
901         sprintf (buf, "texture: %.100s (%dx%d)",
902                         filename, image->width, image->height);
903         check_gl_error(buf);
904
905         /* setup parameters for texturing */
906         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
907         glPixelStorei(GL_UNPACK_ROW_LENGTH, image->width);
908
909         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
910         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
911         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
912         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
913         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
914
915         /* use full texture */
916         texture->min_tx = 0.0;
917         texture->max_tx = 1.0;
918         texture->min_ty = 0.0;
919         texture->max_ty = 1.0;
920         texture->start_time = time((time_t *)0);
921     return True;
922 }
923
924 static void
925 setup_texture(ModeInfo * mi, texture *texture)
926 {
927         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
928
929         if (!image_str || !*image_str || !strcmp(image_str, "DEFAULT")) {
930     BUILTIN:
931                 /* no image specified - use system settings */
932 #ifdef DEBUG
933                 printf("SetupTexture: get_snapshot\n");
934 #endif
935                 getSnapshot(mi, texture);
936         } else {
937                 if (strcmp(image_str, "GENERATE") == 0) {
938 #ifdef DEBUG
939                         printf("SetupTexture: random_texture\n");
940 #endif
941                         setup_random_texture(mi, texture);
942                 } else {
943                         /* use supplied image file */
944 #ifdef DEBUG
945                         printf("SetupTexture: file_texture\n");
946 #endif
947                         if (! setup_file_texture(mi, image_str, texture))
948               goto BUILTIN;
949                 }
950         }
951         /* copy start time from texture */
952         gp->start_time = texture->start_time;
953
954         check_gl_error("texture initialization");
955
956         /* acd 
957          * resultant loaded image is upside down BUT
958          * it's a kaledescope and half of the hexagon is backwards anyway...
959          */
960
961         /* TODO: values for lissajous movement */
962         texture->x_period = frandrange(-2.0, 2.0);
963         texture->y_period = frandrange(-2.0, 2.0);
964         texture->r_period = frandrange(-2.0, 2.0);
965         texture->x_phase = frand(M_PI * 2);
966         texture->y_phase = frand(M_PI * 2);
967         texture->r_phase = frand(M_PI * 2);
968 #ifdef DEBUG
969         printf("XPeriod %f XPhase %f\n", texture->x_period, texture->x_phase);
970         printf("YPeriod %f YPhase %f\n", texture->y_period, texture->y_phase);
971         printf("RPeriod %f RPhase %f\n", texture->r_period, texture->r_phase);
972 #endif
973 }
974
975 #define VERTEX0 glVertex3f( 0.0000f,  0.000f, 0.0f);
976 #define VERTEX1 glVertex3f( 0.0000f,  1.000f, 0.0f);
977 #define VERTEX2 glVertex3f( XOFFSET,  0.500f, 0.0f);
978 #define VERTEX3 glVertex3f( XOFFSET, -0.500f, 0.0f);
979 #define VERTEX4 glVertex3f( 0.0000f, -1.000f, 0.0f);
980 #define VERTEX5 glVertex3f(-XOFFSET, -0.500f, 0.0f);
981 #define VERTEX6 glVertex3f(-XOFFSET,  0.500f, 0.0f);
982
983 /*
984 ** Three different functions for calculating texture coordinates
985 ** which modify how the texture triangle moves over the source image.
986 ** Choose one.
987 */
988
989 #if 0
990 /* the classic equilateral triangle rotating around centre */
991 static void
992 calculate_texture_coords(ModeInfo *mi, texture *texture, vector2f t[3]) {
993
994         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
995         GLfloat centre_x = 0.5;
996         GLfloat centre_y = 0.5;
997         GLfloat radius_x = (texture->max_tx - texture->min_tx) / 2;
998         GLfloat radius_y = (texture->max_ty - texture->min_ty) / 2;
999         GLfloat tangle2;
1000         t[0].x = centre_x;
1001         t[0].y = centre_y;
1002
1003         /* t[1] */
1004         t[1].x = centre_x + .95 * radius_x * cos((gp->ymouse * 2 * M_PI) + (gp->tangle * RADIANS));
1005         t[1].y = centre_y + .95 * radius_y * sin((gp->ymouse * 2 * M_PI) + (gp->tangle * RADIANS));
1006
1007         /* t[2] is always 60' further around than t2 */
1008         tangle2 = (gp->ymouse * 2 * M_PI) + (gp->tangle * RADIANS) + (M_PI * 2 / 6);
1009         t[2].x = centre_x + .95 * radius_x * cos(tangle2);
1010         t[2].y = centre_y + .95 * radius_y * sin(tangle2);
1011 #if 0
1012         printf("texcoords:[%f,%f]->[%f,%f](%f,%f)\n", t[0].x, t[0].y, t[1].x, t[1].y, texture->max_tx, texture->max_ty);
1013 #endif
1014 }
1015 #endif
1016
1017 #if 1
1018 /* new lissajous movement pattern */
1019 static void
1020 calculate_texture_coords(ModeInfo *mi, texture *texture, vector2f t[3]) {
1021
1022         /* equilateral triangle rotating around centre */
1023         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1024         GLfloat width = texture->max_tx - texture->min_tx;
1025         GLfloat height = texture->max_ty - texture->min_ty;
1026         /* centre */
1027         GLfloat centre_x = texture->min_tx + (width * .5);
1028         GLfloat centre_y = texture->min_ty + (height * .5);
1029         /* m radius and t radius should be = .5 */
1030         /* triangle radius is 30% available space */
1031         GLfloat t_radius_x = width * .3;
1032         GLfloat t_radius_y = height * .3;
1033         /* movement radius is 30% available space */
1034         GLfloat m_radius_x = width * .2;
1035         GLfloat m_radius_y = height * .2;
1036         GLfloat angle2;
1037
1038         /* centre of triangle */
1039         GLfloat angle = (gp->ymouse * 2 * M_PI) + (gp->tangle * RADIANS);       /* to radians */
1040         GLfloat t_centre_x = centre_x + m_radius_x * cos(texture->x_period * angle + texture->x_phase);
1041         GLfloat t_centre_y = centre_y + m_radius_y * sin(texture->y_period * angle + texture->y_phase);
1042
1043 #if 0
1044         printf("WH: %f, %f - tWH: %f, %f\n", width, height, texture->width, texture->height);
1045         printf("size: (%f, %f)\n", width, height);
1046         printf("centre: (%f, %f)\n", centre_x, centre_y);
1047 #endif
1048
1049         angle2 = texture->r_period * angle + texture->r_phase;
1050         t[0].x = t_centre_x + t_radius_x * cos(angle2);
1051         t[0].y = t_centre_y + t_radius_y * sin(angle2);
1052         t[1].x = t_centre_x + t_radius_x * cos(angle2 + ANGLE_120);
1053         t[1].y = t_centre_y + t_radius_y * sin(angle2 + ANGLE_120);
1054         t[2].x = t_centre_x + t_radius_x * cos(angle2 + ANGLE_240);
1055         t[2].y = t_centre_y + t_radius_y * sin(angle2 + ANGLE_240);
1056
1057 #if 0
1058         printf("texcoords:[%f,%f]->[%f,%f](%f,%f)\n", t[0].x, t[0].y, t[1].x, t[1].y, texture->max_tx, texture->max_ty);
1059 #endif
1060 }
1061 #endif
1062  
1063 #if 0
1064 /* corners into corners - meant to maximise coverage */
1065 static void
1066 calculate_texture_coords(ModeInfo *mi, texture *texture, vector2f t[3]) {
1067
1068         /* equilateral triangle rotating around centre */
1069         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1070         GLfloat width = texture->max_tx - texture->min_tx;
1071         GLfloat height = texture->max_ty - texture->min_ty;
1072         /* centre */
1073         GLfloat centre_x = texture->min_tx + (width * .5);
1074         GLfloat centre_y = texture->min_ty + (height * .5);
1075         /* m radius and t radius should be = .5 */
1076         /* triangle radius calculated using maths 8) */
1077 #define TRADIUS (M_SQRT2 - 1.0)
1078 #define MRADIUS (1.0 - (M_SQRT2 / 2.0))
1079         GLfloat t_radius_x = width * TRADIUS * .95;
1080         GLfloat t_radius_y = height * TRADIUS * .95;
1081         /* movement radius also calculated using maths */
1082         GLfloat m_radius_x = width * MRADIUS * .95;
1083         GLfloat m_radius_y = height * MRADIUS * .95;
1084         GLfloat angle, angle2;
1085         GLfloat t_centre_x, t_centre_y;
1086
1087         /* centre of triangle */
1088         angle = gp->tangle * RADIANS;   /* to radians */
1089         t_centre_x = centre_x + m_radius_x * cos(angle);
1090         t_centre_y = centre_y + m_radius_y * sin(angle);
1091 #if 0
1092         printf("angle: %f, %f\n", angle, gp->tangle);
1093         printf("tcentre: %f,%f\n", t_centre_x, t_centre_y);
1094         printf("tradius: %f,%f\n", t_radius_x, t_radius_y);
1095
1096         printf("size: (%f, %f)\n", width, height);
1097         printf("centre: (%f, %f)\n", centre_x, centre_y);
1098         printf("centre: (%f, %f)\n", centre_x, centre_y);
1099         printf("TRADIUS: %f\n", TRADIUS);
1100         printf("MRADIUS: %f\n", MRADIUS);
1101 #endif
1102
1103         /* angle2 is tied to tangle */
1104         angle2 = (180.0 - ((30.0 / 90.0) * gp->tangle)) * RADIANS; 
1105 #if 0
1106         printf("Angle1: %f\tAngle2: %f\n", angle / RADIANS, angle2 / RADIANS);
1107 #endif
1108         t[0].x = t_centre_x + t_radius_x * cos(angle2);
1109         t[0].y = t_centre_y + t_radius_y * sin(angle2);
1110         t[1].x = t_centre_x + t_radius_x * cos(angle2 + ANGLE_120);
1111         t[1].y = t_centre_y + t_radius_y * sin(angle2 + ANGLE_120);
1112         t[2].x = t_centre_x + t_radius_x * cos(angle2 + ANGLE_240);
1113         t[2].y = t_centre_y + t_radius_y * sin(angle2 + ANGLE_240);
1114
1115 #if 0
1116         printf("texcoords:[%f,%f][%f,%f][%f,%f]\n", t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y);
1117 #endif
1118 }
1119 #endif
1120
1121 static int
1122 draw_hexagons(ModeInfo *mi, int translucency, texture *texture)
1123 {
1124     int polys = 0;
1125         int             i;
1126         vector2f t[3];
1127         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1128
1129         calculate_texture_coords(mi, texture, t);
1130
1131         glColor4f(1.0, 1.0, 1.0, (float)translucency / MAX_FADE);
1132         glEnable(GL_TEXTURE_2D);
1133         glEnable(GL_BLEND);
1134         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1135         glDepthMask(GL_FALSE);
1136         glBindTexture(GL_TEXTURE_2D, texture->id);
1137
1138         if (gp->list == -1) {
1139                 gp->list = glGenLists(1);
1140         }
1141
1142         /* compile new list */
1143         glNewList(gp->list, GL_COMPILE);
1144         glBegin(GL_TRIANGLES);
1145
1146         /*
1147         ** six triangles to each hexagon
1148         */
1149
1150         glTexCoord2f(t[0].x, t[0].y);
1151         VERTEX0;
1152         glTexCoord2f(t[1].x, t[1].y);
1153         VERTEX1;
1154         glTexCoord2f(t[2].x, t[2].y);
1155         VERTEX6;
1156
1157         glTexCoord2f(t[0].x, t[0].y);
1158         VERTEX0;
1159         glTexCoord2f(t[2].x, t[2].y);
1160         VERTEX6;
1161         glTexCoord2f(t[1].x, t[1].y);
1162         VERTEX5;
1163
1164         glTexCoord2f(t[0].x, t[0].y);
1165         VERTEX0;
1166         glTexCoord2f(t[1].x, t[1].y);
1167         VERTEX5;
1168         glTexCoord2f(t[2].x, t[2].y);
1169         VERTEX4;
1170
1171         glTexCoord2f(t[0].x, t[0].y);
1172         VERTEX0;
1173         glTexCoord2f(t[2].x, t[2].y);
1174         VERTEX4;
1175         glTexCoord2f(t[1].x, t[1].y);
1176         VERTEX3;
1177
1178         glTexCoord2f(t[0].x, t[0].y);
1179         VERTEX0;
1180         glTexCoord2f(t[1].x, t[1].y);
1181         VERTEX3;
1182         glTexCoord2f(t[2].x, t[2].y);
1183         VERTEX2;
1184
1185         glTexCoord2f(t[0].x, t[0].y);
1186         VERTEX0;
1187         glTexCoord2f(t[2].x, t[2].y);
1188         VERTEX2;
1189         glTexCoord2f(t[1].x, t[1].y);
1190         VERTEX1;
1191
1192         glEnd();
1193         glEndList();
1194
1195         /* call the list n times */
1196         for (i = 0 ; i < sizeof(hex) / sizeof(hex[0]) ; i++) {
1197
1198                 glPushMatrix();
1199
1200                 glTranslatef(hex[i].x, hex[i].y, 0.0);
1201                 glCallList(gp->list);
1202         polys += 6;
1203
1204                 glPopMatrix();
1205         }
1206         
1207 #ifdef DISPLAY_TEXTURE
1208         glPushMatrix();
1209         /* acd debug - display (bigger, centred) texture */
1210         glScalef(2.0, 2.0, 2.0);
1211         glTranslatef(-0.5, -0.5, 0.0);
1212         glBegin(GL_QUADS);
1213         glTexCoord2f(0.0, 0.0);
1214         glVertex3f(0.0, 0.0, -0.1);
1215         glTexCoord2f(1.0, 0.0);
1216         glVertex3f(1.0, 0.0, -0.1);
1217         glTexCoord2f(1.0, 1.0);
1218         glVertex3f(1.0, 1.0, -0.1);
1219         glTexCoord2f(0.0, 1.0);
1220         glVertex3f(0.0, 1.0, -0.1);
1221     polys++;
1222         glEnd();
1223         /* acd debug - display texture triangle */
1224         glColor4f(1.0, 0.5, 1.0, 1.0);
1225         glBegin(GL_LINE_LOOP);
1226         glVertex3f(t[0].x, t[0].y, -0.11);
1227         glVertex3f(t[1].x, t[1].y, -0.11);
1228         glVertex3f(t[2].x, t[2].y, -0.11);
1229     polys++;
1230         glEnd();
1231         glPopMatrix();
1232 #endif
1233
1234         glDisable(GL_TEXTURE_2D);
1235         glDepthMask(GL_TRUE);
1236         glDisable(GL_BLEND);
1237     return polys;
1238 }
1239
1240 /*
1241  * main rendering loop
1242  */
1243 static void
1244 draw(ModeInfo * mi)
1245 {
1246         GLfloat x_angle, y_angle, z_angle;
1247         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1248         vectorf v1;
1249
1250     mi->polygon_count = 0;
1251
1252         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1253         glLoadIdentity();
1254
1255         gp->tic += 0.005f;
1256
1257         x_angle = gp->cam_x_phase + gp->tic * gp->cam_x_speed;
1258         y_angle = gp->cam_y_phase + gp->tic * gp->cam_y_speed;
1259         z_angle = gp->cam_z_phase + gp->tic * gp->cam_z_speed;
1260
1261         if (move) {
1262                 v1.x = 1 * sin(x_angle);
1263                 v1.y = 1 * sin(y_angle);
1264         } else {
1265                 v1.x = 0;
1266                 v1.y = 0;
1267         }
1268
1269         /* size is changed in pinit() to be distance from plane */
1270         gp->size = MI_SIZE(mi);
1271         if (gp->size > 10) {
1272                 gp->size = 10;
1273         }
1274         if (gp->size <= 0) {
1275                 gp->size = 0;
1276         }
1277         if (gp->size > 0) {
1278                 /* user defined size */
1279                 v1.z = gp->size;
1280         } else if (zoom) {
1281                 /* max distance given by adding the constant and the multiplier */
1282                 v1.z = 5.0 + 3.0 * sin(z_angle);
1283         } else {
1284                 /* default */
1285                 v1.z = 7.0;
1286         }
1287
1288         /* update rotation angle (but not if mouse button down) */
1289         if (rotate && !gp->button_down_p)
1290         {
1291                 float   new_rangle_vel = 0.0;
1292
1293                 /* update camera rotation angle and velocity */
1294                 gp->rangle += gp->rangle_vel;
1295                 new_rangle_vel = gp->rangle_vel + gp->rangle_acc;
1296                 if (new_rangle_vel > -MAX_ANGLE_VEL && new_rangle_vel < MAX_ANGLE_VEL)
1297                 {
1298                         /* new velocity is within limits */
1299                         gp->rangle_vel = new_rangle_vel;
1300                 }
1301
1302                 /* randomly change twisting speed - 3ff = 1024 */
1303                 if ((random() % TWISTING_PROBABILITY) < 1.0) {
1304                         gp->rangle_acc = INITIAL_ANGLE_ACC * frand(1.0);
1305                         if (gp->rangle_vel > 0.0) {
1306                                 gp->rangle_acc = -gp->rangle_acc;
1307                         }
1308                 }
1309         }
1310 #if 0
1311         printf("Rangle: %f : %f : %f\n", gp->rangle, gp->rangle_vel, gp->rangle_acc);
1312         printf("Tangle: %f : %f : %f\n", gp->tangle, gp->tangle_vel, gp->tangle_acc);
1313 #endif
1314
1315 #ifdef WOBBLE
1316         /* this makes the image wobble - requires -move and a larger grid */
1317         gluLookAt(0, 0, v1.z, v1.x, v1.y, 0.0, 0.0, 1.0, 0.0);
1318 #else
1319         /* no wobble - camera always perpendicular to grid */
1320
1321         /* rotating camera rather than entire space - smoother */
1322         gluLookAt(
1323                         v1.x, v1.y, v1.z,
1324                         v1.x, v1.y, 0.0,
1325                         sin((gp->xmouse * M_PI * 2) + gp->rangle * RADIANS),
1326                         cos((gp->xmouse * M_PI * 2) + gp->rangle * RADIANS),
1327                         0.0);
1328 #endif
1329
1330 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
1331     {
1332       GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
1333       int o = (int) current_device_rotation();
1334       if (o != 0 && o != 180 && o != -180)
1335         glScalef (1/h, 1/h, 1/h);
1336     }
1337 # endif
1338
1339         if (gp->fade == 0)
1340         {
1341                 /* not fading */
1342       mi->polygon_count += 
1343         draw_hexagons(mi, MAX_FADE, &gp->textures[gp->visible]);
1344         }
1345         else
1346         {
1347                 /* fading - show both textures with alpha
1348                 NB first is always max alpha */
1349         mi->polygon_count += 
1350           draw_hexagons(mi, MAX_FADE, &gp->textures[1 - gp->visible]);
1351         mi->polygon_count += 
1352                   draw_hexagons(mi, MAX_FADE - gp->fade, &gp->textures[gp->visible]);
1353
1354                 /* fade some more */
1355                 gp->fade++;
1356
1357                 /* have we faded enough? */
1358                 if (gp->fade > MAX_FADE)
1359                 {
1360                         /* stop fading */
1361                         gp->fade = 0;
1362                         gp->visible = 1 - gp->visible;
1363                 }
1364         }
1365
1366         /* increment texture angle based on time, velocity etc */
1367         /* but only if button is not down */
1368         if (!gp->button_down_p)
1369         {
1370                 float           new_tangle_vel = 0.0;
1371
1372                 gp->tangle += gp->tangle_vel;
1373
1374                 /* work out new texture angle velocity */
1375                 new_tangle_vel = gp->tangle_vel + gp->tangle_acc;
1376                 if (new_tangle_vel > -MAX_ANGLE_VEL && new_tangle_vel < MAX_ANGLE_VEL)
1377                 {
1378                         /* new velocity is inside limits */
1379                         gp->tangle_vel = new_tangle_vel;
1380                 }
1381
1382                 /* randomly change twisting speed - 3ff = 1024 */
1383                 if ((random() % TWISTING_PROBABILITY) < 1.0) {
1384                         gp->tangle_acc = INITIAL_ANGLE_ACC * frand(1.0);
1385                         if (gp->tangle_vel > 0.0) {
1386                                 gp->tangle_acc = -gp->tangle_acc;
1387                         }
1388                 }
1389         }
1390
1391         glFlush();
1392 }
1393
1394 /* 
1395  * new window size or exposure 
1396  */
1397 ENTRYPOINT void reshape_gleidescope(ModeInfo *mi, int width, int height)
1398 {
1399         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1400         GLfloat         h = (GLfloat) height / (GLfloat) width;
1401
1402         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(gp->glx_context));
1403
1404         glViewport(0, 0, (GLint) width, (GLint) height);
1405         glMatrixMode(GL_PROJECTION);
1406         glLoadIdentity();
1407         gluPerspective(50.0, 1/h, 0.1, 2000.0);
1408         glMatrixMode (GL_MODELVIEW);
1409         glLineWidth(1);
1410         glPointSize(1);   
1411 }
1412
1413 static void
1414 pinit(ModeInfo * mi)
1415 {
1416         gleidestruct    *gp = &gleidescope[MI_SCREEN(mi)];
1417
1418         /* set start time - star_time = 0 implies non-dynamic texture */
1419         gp->start_time = (time_t)0;
1420
1421         /* set the texture size to default */
1422         /*
1423         gp->max_tx = 1.0;
1424         gp->max_ty = 1.0;
1425         */
1426
1427         /* no fading */
1428         gp->fade = 0;
1429
1430         glShadeModel(GL_SMOOTH);
1431         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1432         glEnable(GL_DEPTH_TEST);
1433         glEnable(GL_CULL_FACE);
1434         glDisable(GL_LIGHTING);
1435
1436         /* space for textures */
1437         glGenTextures(1, &gp->textures[0].id);
1438         glGenTextures(1, &gp->textures[1].id);
1439         gp->visible = 0;
1440
1441         setup_texture(mi, &gp->textures[gp->visible]);
1442
1443         /*
1444         **      want to choose a value for arg randomly if neither -arg nor -no-arg
1445         **      is specified. xscreensaver libraries don't seem to let you do this -
1446         **      if something isn't true then it is false (pesky two-state boolean values).
1447         **      so, i've defined both -arg and -no-arg to arguments and added the 
1448         **      following logic.
1449         **      (btw if both -arg and -no-arg are defined then arg is set to False)
1450         */
1451         if (zoom == False && nozoom == False)
1452         {
1453                 /* no zoom preference - randomise */
1454                 zoom = (((random() & 0x1) == 0x1) ? True : False);
1455         }
1456         else if (nozoom == True)
1457         {
1458                 /* definately no zoom */
1459                 zoom = False;
1460         }
1461
1462         if (move == False && nomove == False)
1463         {
1464                 /* no move preference - randomise */
1465                 move = (((random() & 0x1) == 0x1) ? True : False);
1466         }
1467         else if (nomove == True)
1468         {
1469                 /* definately no move */
1470                 move = False;
1471         }
1472
1473         if (rotate == False && norotate == False)
1474         {
1475                 /* no rotate preference - randomise */
1476                 rotate = (((random() & 0x1) == 0x1) ? True : False);
1477         }
1478         else if (norotate == True)
1479         {
1480                 /* definately no rotate */
1481                 rotate = False;
1482         }
1483
1484         /* define cam variables */
1485         gp->cam_x_speed = MAX_CAM_SPEED * frandrange(-.5, 0.5);
1486         gp->cam_x_phase = random() % 360;
1487         gp->cam_y_speed = MAX_CAM_SPEED * frandrange(-.5, 0.5);
1488         gp->cam_y_phase = random() % 360;
1489         gp->cam_z_speed = MAX_CAM_SPEED * frandrange(-.5, 0.5);
1490         gp->cam_z_phase = random() % 360;
1491
1492         /* initial angular speeds */
1493         gp->rangle_vel = INITIAL_ANGLE_VEL * frandrange(-.5, 0.5);
1494         gp->tangle_vel = INITIAL_ANGLE_VEL * frandrange(-.5, 0.5);
1495         gp->rangle_acc = INITIAL_ANGLE_ACC * frandrange(-.5, 0.5);
1496         gp->tangle_acc = INITIAL_ANGLE_ACC * frandrange(-.5, 0.5);
1497
1498     /* jwz */
1499 #if 0
1500     {
1501       GLfloat speed = 15;
1502       gp->rangle_vel *= speed;
1503       gp->tangle_vel *= speed;
1504       gp->rangle_acc *= speed;
1505       gp->tangle_acc *= speed;
1506     }
1507 #endif
1508
1509         /* distance is 11 - size */
1510         if (gp->size != -1) {
1511                 if (zoom) {
1512                         fprintf(stderr, "-size given. ignoring -zoom.\n");
1513                         zoom = False;
1514                 }
1515                 if (gp->size < 1) {
1516                         gp->size = 1;
1517                 } else if (gp->size >= 10) {
1518                         gp->size = 10;
1519                 }
1520                 gp->size = 11 - gp->size;
1521         }
1522
1523 #ifdef DEBUG
1524 printf("phases [%d, %d, %d]\n", gp->cam_x_phase, gp->cam_y_phase, gp->cam_z_phase);
1525 #endif
1526 }
1527
1528 static void free_gleidescope(ModeInfo * mi);
1529
1530 ENTRYPOINT void
1531 init_gleidescope(ModeInfo * mi)
1532 {
1533         gleidestruct *gp;
1534         int screen = MI_SCREEN(mi);
1535
1536         MI_INIT(mi, gleidescope, free_gleidescope);
1537         gp = &gleidescope[screen];
1538         gp->window = MI_WINDOW(mi);
1539     gp->size = -1;
1540     gp->list = -1;
1541
1542         if ((gp->glx_context = init_GL(mi)) != NULL) {
1543
1544                 reshape_gleidescope(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1545         clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */
1546
1547                 glDrawBuffer(GL_BACK);
1548
1549                 /* do initialisation */
1550                 pinit(mi);
1551
1552         } else {
1553                 MI_CLEARWINDOW(mi);
1554         }
1555 }
1556
1557 ENTRYPOINT void
1558 draw_gleidescope(ModeInfo * mi)
1559 {
1560         gleidestruct    *gp = &gleidescope[MI_SCREEN(mi)];
1561         Display         *display = MI_DISPLAY(mi);
1562         Window          window = MI_WINDOW(mi);
1563
1564
1565         if (!gp->glx_context)
1566                 return;
1567
1568     /* Just keep running before the texture has come in. */
1569     /* if (gp->waiting_for_image_p) return; */
1570
1571         glDrawBuffer(GL_BACK);
1572
1573         glXMakeCurrent(display, window, *(gp->glx_context));
1574         draw(mi);
1575
1576         if (mi->fps_p) {
1577                 do_fps (mi);
1578         }
1579
1580         glFinish();
1581         glXSwapBuffers(display, window);
1582
1583 #ifdef GRAB
1584         if (grab) {
1585                 grab_frame(display, window);
1586         }
1587 #endif
1588
1589         /* need to change texture? */
1590         if ((gp->start_time != 0) && (duration != -1) && gp->fade == 0) {
1591                 if (gp->start_time + duration <= time((time_t *)0)) {
1592 #ifdef DEBUG
1593                         printf("Start Time: %lu - Current Time: %lu\n", (unsigned long)gp->start_time, (unsigned long)time((time_t *)0));
1594                         printf("Changing Texture\n");
1595 #endif
1596                         /* get new snapshot (into back buffer) and start fade count */
1597                         setup_texture(mi, &gp->textures[1 - gp->visible]);
1598                         /* restart fading */
1599                         gp->fade = 1;
1600                 }
1601         }
1602 }
1603
1604 static void
1605 free_gleidescope(ModeInfo * mi)
1606 {
1607         gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1608
1609         /* acd -  is this needed? */
1610         if (gp->glx_context) {
1611                 /* Display lists MUST be freed while their glXContext is current. */
1612                 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1613
1614                 /* acd - was code here for freeing things that are no longer in struct */
1615         }
1616 }
1617
1618 XSCREENSAVER_MODULE ("Gleidescope", gleidescope)
1619
1620 #endif