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