1 /* -*- Mode: C; tab-width: 4 -*- */
3 #if !defined( lint ) && !defined( SABER )
4 /*static const char sccsid[] = "@(#)gleidescope.c 1.0 03/06/27 xlockmore";*/
7 /* enable -grab switch */
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
25 * 20030627 1.0 acd First Release.
26 * Texture loading code from 'glplanet'
27 * by Jamie Zawinski <jwz@jwz.org>
28 * 20030810 1.1 acd Added size flag.
29 * Now grabs screen / video / picture
30 * (uses code from 'glslideshow' by
31 * Mike Oliphant, Ben Buxton, Jamie Zawinski).
34 * Added fade code (also from glslideshow).
35 * 20031013 1.2 acd Migrated to compile without warnings under
37 * 20031023 1.3 acd Better code to limit twisting speeds.
38 * Tweaked initial rotation values.
39 * Move, Rotate, Zoom now chosen at random if
40 * no preference is given.
41 * Made grid slightly bigger so you can't see
42 * the edge when zooming and moving.
46 **----------------------------------------------------------------------------
48 **----------------------------------------------------------------------------
54 "*showFPS: False \n" \
58 # define refresh_gleidescope 0
59 # include "xlockmore.h" /* from the xscreensaver distribution */
60 #else /* !STANDALONE */
61 # include "xlock.h" /* from the xlockmore distribution */
62 #endif /* !STANDALONE */
67 #include "xpm-ximage.h"
68 #include "grab-ximage.h"
70 /* acd TODO should all these be in gleidestruct? */
71 /* they can't be, because of the idiotic way the xlockmore "argtype vars"
72 interface works. -jwz */
74 static Bool grab; /* grab images */
76 static Bool move; /* moving camera */
77 static Bool nomove; /* no moving camera */
78 static Bool rotate; /* rotate in place */
79 static Bool norotate; /* no rotate in place */
80 static Bool zoom; /* zooming camera */
81 static Bool nozoom; /* no zooming camera */
82 static char *image; /* name of texture to load */
83 static int duration; /* length of time to display grabbed image */
85 #define MAX_TANGLE_VEL 2.0
86 #define MAX_RANGLE_VEL 1.5
88 static XrmOptionDescRec opts[] =
91 {"-grab", ".gleidescope.grab", XrmoptionNoArg, "true"},
93 {"-move", ".gleidescope.move", XrmoptionNoArg, "true"},
94 {"-no-move", ".gleidescope.nomove", XrmoptionNoArg, "true"},
95 {"-rotate", ".gleidescope.rotate", XrmoptionNoArg, "true"},
96 {"-no-rotate", ".gleidescope.norotate", XrmoptionNoArg, "true"},
97 /*{"-size", ".gleidescope.size", XrmoptionNoArg, "-1"},*/
98 {"-zoom", ".gleidescope.zoom", XrmoptionNoArg, "true"},
99 {"-no-zoom", ".gleidescope.nozoom", XrmoptionNoArg, "true"},
100 {"-image", ".gleidescope.image", XrmoptionSepArg, "DEFAULT"},
101 {"-duration", ".gleidescope.duration", XrmoptionSepArg, "30"},
105 static argtype vars[] = {
107 {&grab, "grab", "Grab", "False", t_Bool},
109 {&move, "move", "Move", "False", t_Bool},
110 {&nomove, "nomove", "noMove", "False", t_Bool},
111 {&rotate, "rotate", "Rotate", "False", t_Bool},
112 {&norotate, "norotate", "noRotate", "False", t_Bool},
113 /*{&size, "size", "Size", "-1", t_Int},*/
114 {&zoom, "zoom", "Zoom", "False", t_Bool},
115 {&nozoom, "nozoom", "noZoom", "False", t_Bool},
116 {&image, "image", "Image", "DEFAULT", t_String},
117 {&duration, "duration", "Duration", "30", t_Int},
120 static OptionStruct desc[] = {
122 {"-grab", "grab images to create animation"},
124 {"-move", "camera will move"},
125 {"-no-move", "camera won't move"},
126 {"-rotate", "camera will rotate"},
127 {"-no-rotate", "camera won't rotate"},
128 /*{"-size", "size of the hexagons (1-10)"},*/
129 {"-zoom", "camera will zoom"},
130 {"-no-zoom", "camera won't zoom"},
131 {"-image", "xpm / xbm image file to use for texture"},
132 {"-duration", "length of time texture will be used"},
135 ENTRYPOINT ModeSpecOpt gleidescope_opts = {
136 sizeof opts / sizeof opts[0], opts,
137 sizeof vars / sizeof vars[0], vars,
142 ModStruct gleidescope_description = {
143 "gleidescope", "init_gleidescope", "draw_gleidescope", "release_gleidescope",
144 "draw_gleidescope", "init_gleidescope", NULL, &gleidescope_opts,
145 1000, 1, 2, 1, 4, 1.0, "",
146 "GL Kaleidescope", 0, NULL};
150 **-----------------------------------------------------------------------------
152 **-----------------------------------------------------------------------------
155 typedef struct hex_s {
156 GLfloat x, y, z; /* position */
165 #define MAX_FADE 500 /* number of fade cycles */
168 float cam_x_speed, cam_z_speed, cam_y_speed;
169 int cam_x_phase, cam_z_phase, cam_y_phase;
171 GLXContext *glx_context;
173 GLfloat max_tx, max_ty; /* maximum texture sizes */
174 GLuint textures[2]; /* texture handles */
175 GLuint visible; /* texture handle for new texture */
182 float tangle; /* texture angle */
183 float tangle_vel; /* texture velocity */
184 float tangle_acc; /* texture acceleration */
186 float rangle; /* rotate angle */
187 float rangle_vel; /* rotate velocity */
188 float rangle_acc; /* rotate acceleration */
197 Bool waiting_for_image_p;
201 #define XOFFSET (0.8660254f) /* sin 60' */
202 #define YOFFSET (1.5000000f) /* cos 60' + 1 */
208 /* generates a grid with edges of given size */
209 /* acd TODO - replace hex[] with this and allow size and distance as parameters */
212 generate_grid(int size)
219 for (y = -size ; y <= size ; y++) {
220 for (x = -i ; x <= i ; x += 2) {
221 printf("{XOFFSET * %d, YOFFSET * %d, 0},\n", x, y);
234 static const hex_t hex[] = {
235 /* edges of size 7 */
236 /* number of hexagons required to cover screen depends on camera distance */
237 /* at a distance of 10 this is just about enough. */
238 {XOFFSET * -6, YOFFSET * -6, 0},
239 {XOFFSET * -4, YOFFSET * -6, 0},
240 {XOFFSET * -2, YOFFSET * -6, 0},
241 {XOFFSET * 0, YOFFSET * -6, 0},
242 {XOFFSET * 2, YOFFSET * -6, 0},
243 {XOFFSET * 4, YOFFSET * -6, 0},
244 {XOFFSET * 6, YOFFSET * -6, 0},
246 {XOFFSET * -7, YOFFSET * -5, 0},
247 {XOFFSET * -5, YOFFSET * -5, 0},
248 {XOFFSET * -3, YOFFSET * -5, 0},
249 {XOFFSET * -1, YOFFSET * -5, 0},
250 {XOFFSET * 1, YOFFSET * -5, 0},
251 {XOFFSET * 3, YOFFSET * -5, 0},
252 {XOFFSET * 5, YOFFSET * -5, 0},
253 {XOFFSET * 7, YOFFSET * -5, 0},
255 {XOFFSET * -8, YOFFSET * -4, 0},
256 {XOFFSET * -6, YOFFSET * -4, 0},
257 {XOFFSET * -4, YOFFSET * -4, 0},
258 {XOFFSET * -2, YOFFSET * -4, 0},
259 {XOFFSET * 0, YOFFSET * -4, 0},
260 {XOFFSET * 2, YOFFSET * -4, 0},
261 {XOFFSET * 4, YOFFSET * -4, 0},
262 {XOFFSET * 6, YOFFSET * -4, 0},
263 {XOFFSET * 8, YOFFSET * -4, 0},
265 {XOFFSET * -9, YOFFSET * -3, 0},
266 {XOFFSET * -7, YOFFSET * -3, 0},
267 {XOFFSET * -5, YOFFSET * -3, 0},
268 {XOFFSET * -3, YOFFSET * -3, 0},
269 {XOFFSET * -1, YOFFSET * -3, 0},
270 {XOFFSET * 1, YOFFSET * -3, 0},
271 {XOFFSET * 3, YOFFSET * -3, 0},
272 {XOFFSET * 5, YOFFSET * -3, 0},
273 {XOFFSET * 7, YOFFSET * -3, 0},
274 {XOFFSET * 9, YOFFSET * -3, 0},
276 {XOFFSET * -10, YOFFSET * -2, 0},
277 {XOFFSET * -8, YOFFSET * -2, 0},
278 {XOFFSET * -6, YOFFSET * -2, 0},
279 {XOFFSET * -4, YOFFSET * -2, 0},
280 {XOFFSET * -2, YOFFSET * -2, 0},
281 {XOFFSET * 0, YOFFSET * -2, 0},
282 {XOFFSET * 2, YOFFSET * -2, 0},
283 {XOFFSET * 4, YOFFSET * -2, 0},
284 {XOFFSET * 6, YOFFSET * -2, 0},
285 {XOFFSET * 8, YOFFSET * -2, 0},
286 {XOFFSET * 10, YOFFSET * -2, 0},
288 {XOFFSET * -11, YOFFSET * -1, 0},
289 {XOFFSET * -9, YOFFSET * -1, 0},
290 {XOFFSET * -7, YOFFSET * -1, 0},
291 {XOFFSET * -5, YOFFSET * -1, 0},
292 {XOFFSET * -3, YOFFSET * -1, 0},
293 {XOFFSET * -1, YOFFSET * -1, 0},
294 {XOFFSET * 1, YOFFSET * -1, 0},
295 {XOFFSET * 3, YOFFSET * -1, 0},
296 {XOFFSET * 5, YOFFSET * -1, 0},
297 {XOFFSET * 7, YOFFSET * -1, 0},
298 {XOFFSET * 9, YOFFSET * -1, 0},
299 {XOFFSET * 11, YOFFSET * -1, 0},
301 {XOFFSET * -12, YOFFSET * 0, 0},
302 {XOFFSET * -10, YOFFSET * 0, 0},
303 {XOFFSET * -8, YOFFSET * 0, 0},
304 {XOFFSET * -6, YOFFSET * 0, 0},
305 {XOFFSET * -4, YOFFSET * 0, 0},
306 {XOFFSET * -2, YOFFSET * 0, 0},
307 {XOFFSET * 0, YOFFSET * 0, 0},
308 {XOFFSET * 2, YOFFSET * 0, 0},
309 {XOFFSET * 4, YOFFSET * 0, 0},
310 {XOFFSET * 6, YOFFSET * 0, 0},
311 {XOFFSET * 8, YOFFSET * 0, 0},
312 {XOFFSET * 10, YOFFSET * 0, 0},
313 {XOFFSET * 12, YOFFSET * 0, 0},
315 {XOFFSET * -11, YOFFSET * 1, 0},
316 {XOFFSET * -9, YOFFSET * 1, 0},
317 {XOFFSET * -7, YOFFSET * 1, 0},
318 {XOFFSET * -5, YOFFSET * 1, 0},
319 {XOFFSET * -3, YOFFSET * 1, 0},
320 {XOFFSET * -1, YOFFSET * 1, 0},
321 {XOFFSET * 1, YOFFSET * 1, 0},
322 {XOFFSET * 3, YOFFSET * 1, 0},
323 {XOFFSET * 5, YOFFSET * 1, 0},
324 {XOFFSET * 7, YOFFSET * 1, 0},
325 {XOFFSET * 9, YOFFSET * 1, 0},
326 {XOFFSET * 11, YOFFSET * 1, 0},
328 {XOFFSET * -10, YOFFSET * 2, 0},
329 {XOFFSET * -8, YOFFSET * 2, 0},
330 {XOFFSET * -6, YOFFSET * 2, 0},
331 {XOFFSET * -4, YOFFSET * 2, 0},
332 {XOFFSET * -2, YOFFSET * 2, 0},
333 {XOFFSET * 0, YOFFSET * 2, 0},
334 {XOFFSET * 2, YOFFSET * 2, 0},
335 {XOFFSET * 4, YOFFSET * 2, 0},
336 {XOFFSET * 6, YOFFSET * 2, 0},
337 {XOFFSET * 8, YOFFSET * 2, 0},
338 {XOFFSET * 10, YOFFSET * 2, 0},
340 {XOFFSET * -9, YOFFSET * 3, 0},
341 {XOFFSET * -7, YOFFSET * 3, 0},
342 {XOFFSET * -5, YOFFSET * 3, 0},
343 {XOFFSET * -3, YOFFSET * 3, 0},
344 {XOFFSET * -1, YOFFSET * 3, 0},
345 {XOFFSET * 1, YOFFSET * 3, 0},
346 {XOFFSET * 3, YOFFSET * 3, 0},
347 {XOFFSET * 5, YOFFSET * 3, 0},
348 {XOFFSET * 7, YOFFSET * 3, 0},
349 {XOFFSET * 9, YOFFSET * 3, 0},
351 {XOFFSET * -8, YOFFSET * 4, 0},
352 {XOFFSET * -6, YOFFSET * 4, 0},
353 {XOFFSET * -4, YOFFSET * 4, 0},
354 {XOFFSET * -2, YOFFSET * 4, 0},
355 {XOFFSET * 0, YOFFSET * 4, 0},
356 {XOFFSET * 2, YOFFSET * 4, 0},
357 {XOFFSET * 4, YOFFSET * 4, 0},
358 {XOFFSET * 6, YOFFSET * 4, 0},
359 {XOFFSET * 8, YOFFSET * 4, 0},
361 {XOFFSET * -7, YOFFSET * 5, 0},
362 {XOFFSET * -5, YOFFSET * 5, 0},
363 {XOFFSET * -3, YOFFSET * 5, 0},
364 {XOFFSET * -1, YOFFSET * 5, 0},
365 {XOFFSET * 1, YOFFSET * 5, 0},
366 {XOFFSET * 3, YOFFSET * 5, 0},
367 {XOFFSET * 5, YOFFSET * 5, 0},
368 {XOFFSET * 7, YOFFSET * 5, 0},
370 {XOFFSET * -6, YOFFSET * 6, 0},
371 {XOFFSET * -4, YOFFSET * 6, 0},
372 {XOFFSET * -2, YOFFSET * 6, 0},
373 {XOFFSET * 0, YOFFSET * 6, 0},
374 {XOFFSET * 2, YOFFSET * 6, 0},
375 {XOFFSET * 4, YOFFSET * 6, 0},
376 {XOFFSET * 6, YOFFSET * 6, 0},
380 **----------------------------------------------------------------------------
382 **----------------------------------------------------------------------------
385 static gleidestruct *gleidescope = NULL;
389 *load defaults in config structure
391 static void setdefaultconfig(void)
404 gleidescope_handle_event(ModeInfo *mi, XEvent *event)
406 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
409 printf("event:%d\n", event->xany.type);
410 printf("button:%d\n", event->xbutton.button);
412 switch(event->xany.type)
416 if (event->xbutton.button == Button1 ||
417 event->xbutton.button == Button3)
419 /* store initial values of mouse */
420 gp->xstart = event->xbutton.x;
421 gp->ystart = event->xbutton.y;
424 gp->button_down_p = True;
428 else if (event->xbutton.button == Button4)
433 else if (event->xbutton.button == Button5)
443 if (event->xbutton.button == Button1 ||
444 event->xbutton.button == Button3)
447 gp->button_down_p = False;
454 if (gp->button_down_p)
456 /* update mouse position */
457 gp->xmouse += (double)(event->xmotion.x - gp->xstart) / MI_WIDTH(mi);
458 gp->ymouse += (double)(event->xmotion.y - gp->ystart) / MI_HEIGHT(mi);
459 gp->xstart = event->xmotion.x;
460 gp->ystart = event->xmotion.y;
472 image_loaded_cb (const char *filename, XRectangle *geometry,
473 int image_width, int image_height,
474 int texture_width, int texture_height,
477 gleidestruct *gp = (gleidestruct *) closure;
479 gp->max_tx = (GLfloat) image_width / texture_width;
480 gp->max_ty = (GLfloat) image_height / texture_height;
482 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
483 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
484 (gp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
486 gp->waiting_for_image_p = False;
487 gp->start_time = time ((time_t *) 0);
492 getSnapshot(ModeInfo *mi, GLuint name)
494 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
496 if (MI_IS_WIREFRAME(mi))
500 load_texture_async (mi->xgwa.screen, mi->window,
501 *gp->glx_context, 0, 0, gp->mipmap_p,
502 name, image_loaded_cb, gp);
507 setup_file_texture (ModeInfo *mi, char *filename, GLuint name)
509 Display *dpy = mi->dpy;
510 Visual *visual = mi->xgwa.visual;
513 Colormap cmap = mi->xgwa.colormap;
514 XImage *image = xpm_file_to_ximage (dpy, visual, cmap, filename);
516 /* use this texture */
517 glBindTexture(GL_TEXTURE_2D, name);
520 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
521 image->width, image->height, 0,
522 GL_RGBA, GL_UNSIGNED_BYTE, image->data);
523 sprintf (buf, "texture: %.100s (%dx%d)",
524 filename, image->width, image->height);
527 /* setup parameters for texturing */
528 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
529 glPixelStorei(GL_UNPACK_ROW_LENGTH, image->width);
531 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
532 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
533 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
534 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
535 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
539 setup_texture(ModeInfo * mi, GLuint id)
541 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
543 if (!image || !*image || !strcmp(image, "DEFAULT")) {
544 /* no image specified - grab screen */
546 /* max_tx and max_ty set in getSnapshot() */
548 /* use supplied image file */
549 setup_file_texture(mi, image, id);
551 /* set tx params to use whole image */
556 check_gl_error("texture initialization");
558 /* Need to flip the texture top for bottom for some reason. */
559 glMatrixMode (GL_TEXTURE);
561 glMatrixMode (GL_MODELVIEW);
564 #define VERTEX0 glVertex3f( 0.0000f, 0.000f, 0.0f);
565 #define VERTEX1 glVertex3f( 0.0000f, 1.000f, 0.0f);
566 #define VERTEX2 glVertex3f( XOFFSET, 0.500f, 0.0f);
567 #define VERTEX3 glVertex3f( XOFFSET, -0.500f, 0.0f);
568 #define VERTEX4 glVertex3f( 0.0000f, -1.000f, 0.0f);
569 #define VERTEX5 glVertex3f(-XOFFSET, -0.500f, 0.0f);
570 #define VERTEX6 glVertex3f(-XOFFSET, 0.500f, 0.0f);
573 draw_hexagons(ModeInfo *mi, int translucency, GLuint texture)
577 GLfloat t1x, t1y, t2x, t2y, t3x, t3y;
578 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
584 col[3] = (float)translucency / MAX_FADE;
586 /* calculate vertices of equilateral triangle within image. */
587 /* t1 is always in centre */
588 t1x = gp->max_tx / 2;
589 t1y = gp->max_ty / 2;
591 t2x = (gp->max_tx / 2) * (1 + cos((gp->ymouse * 2 * M_PI) + (gp->tangle * M_PI / 180)));
592 t2y = (gp->max_ty / 2) * (1 + sin((gp->ymouse * 2 * M_PI) + (gp->tangle * M_PI / 180)));
593 /* t3 is always 60' further around than t2 */
594 tangle2 = (gp->ymouse * 2 * M_PI) + (gp->tangle * M_PI / 180) + (M_PI * 2 / 6);
595 t3x = (gp->max_tx / 2) * (1 + (cos(tangle2)));
596 t3y = (gp->max_ty / 2) * (1 + (sin(tangle2)));
597 /* NB image is flipped vertically hence: */
601 /*printf("texcoords:[%f,%f]->[%f,%f](%f,%f)\n", t1x, t1y, t2x, t2y, gp->max_tx, gp->max_ty);*/
603 glColor4f(1.0, 1.0, 1.0, (float)translucency / MAX_FADE);
604 glEnable(GL_TEXTURE_2D);
606 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
607 glDepthMask(GL_FALSE);
608 glBindTexture(GL_TEXTURE_2D, gp->textures[texture]);
610 for (i = 0 ; i < sizeof(hex) / sizeof(hex[0]) ; i++) {
614 glTranslatef(hex[i].x, hex[i].y, 0.0);
616 glBegin(GL_TRIANGLES);
619 ** six triangles to each hexagon
622 glTexCoord2f(t1x, t1y);
624 glTexCoord2f(t2x, t2y);
626 glTexCoord2f(t3x, t3y);
629 glTexCoord2f(t1x, t1y);
631 glTexCoord2f(t3x, t3y);
633 glTexCoord2f(t2x, t2y);
636 glTexCoord2f(t1x, t1y);
638 glTexCoord2f(t2x, t2y);
640 glTexCoord2f(t3x, t3y);
643 glTexCoord2f(t1x, t1y);
645 glTexCoord2f(t3x, t3y);
647 glTexCoord2f(t2x, t2y);
650 glTexCoord2f(t1x, t1y);
652 glTexCoord2f(t2x, t2y);
654 glTexCoord2f(t3x, t3y);
657 glTexCoord2f(t1x, t1y);
659 glTexCoord2f(t3x, t3y);
661 glTexCoord2f(t2x, t2y);
669 #ifdef DISPLAY_TEXTURE
671 /* acd debug - display (bigger, centred) texture */
672 glScalef(2.0, 2.0, 2.0);
673 glTranslatef(-0.5, -0.5, 0.0);
675 glTexCoord2f(0.0, 0.0);
676 glVertex3f(0.0, 0.0, -0.1);
677 glTexCoord2f(1.0, 0.0);
678 glVertex3f(1.0, 0.0, -0.1);
679 glTexCoord2f(1.0, 1.0);
680 glVertex3f(1.0, 1.0, -0.1);
681 glTexCoord2f(0.0, 1.0);
682 glVertex3f(0.0, 1.0, -0.1);
684 /* acd debug - display texture triangle */
685 glColor4f(1.0, 1.0, 1.0, 1.0);
686 glBegin(GL_LINE_LOOP);
687 glVertex3f(t1x, t1y, -0.11);
688 glVertex3f(t2x, t2y, -0.11);
689 glVertex3f(t3x, t3y, -0.11);
694 glDisable(GL_TEXTURE_2D);
695 glDepthMask(GL_TRUE);
700 * main rendering loop
705 GLfloat x_angle, y_angle, z_angle;
706 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
710 glClearColor(0.5, 0.5, 0.5, 1.0);
711 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
716 x_angle = gp->cam_x_phase + gp->tic * gp->cam_x_speed;
717 y_angle = gp->cam_y_phase + gp->tic * gp->cam_y_speed;
718 z_angle = gp->cam_z_phase + gp->tic * gp->cam_z_speed;
721 v1.x = 2 * sin(x_angle);
722 v1.y = 2 * sin(y_angle);
728 /* size is changed in pinit() to be distance from plane */
729 gp->size = MI_SIZE(mi);
736 if (gp->size != -1) {
737 /* user defined size */
740 /* max distance given by adding the constant and the multiplier */
741 v1.z = 5.0 + 4.0 * sin(z_angle);
747 /* update rotation angle (but not if mouse button down) */
748 if (rotate && !gp->button_down_p)
750 float new_rangle_vel = 0.0;
752 /* update camera rotation angle and velocity */
753 gp->rangle += gp->rangle_vel;
754 new_rangle_vel = gp->rangle_vel + gp->rangle_acc;
755 if (new_rangle_vel > -MAX_RANGLE_VEL && new_rangle_vel < MAX_RANGLE_VEL)
757 /* new velocity is within limits */
758 gp->rangle_vel = new_rangle_vel;
761 /* randomly change twisting speed */
762 if ((random() % 1000) < 1)
764 gp->rangle_acc = frand(0.002) - 0.001;
769 /* this makes the image wobble - requires -move and a larger grid */
770 gluLookAt(0, 0, v1.z, v1.x, v1.y, 0.0, 0.0, 1.0, 0.0);
772 /* no wobble - camera always perpendicular to grid */
774 /* rotating camera rather than entire space - smoother */
778 sin((gp->xmouse * M_PI * 2) + gp->rangle * M_PI / 180),
779 cos((gp->xmouse * M_PI * 2) + gp->rangle * M_PI / 180),
783 /* light position same as camera */
792 draw_hexagons(mi, MAX_FADE, gp->visible);
796 /* fading - show both textures with alpha */
797 draw_hexagons(mi, MAX_FADE - gp->fade, gp->visible);
798 draw_hexagons(mi, gp->fade, 1 - gp->visible);
803 /* have we faded enough? */
804 if (gp->fade > MAX_FADE)
808 gp->visible = 1 - gp->visible;
812 /* increment texture angle based on time, velocity etc */
813 /* but only if button is not down */
814 if (!gp->button_down_p)
816 float new_tangle_vel = 0.0;
818 gp->tangle += gp->tangle_vel;
820 /* work out new texture angle velocity */
821 new_tangle_vel = gp->tangle_vel + gp->tangle_acc;
822 if (new_tangle_vel > -MAX_TANGLE_VEL && new_tangle_vel < MAX_TANGLE_VEL)
824 /* new velocity is inside limits */
825 gp->tangle_vel = new_tangle_vel;
828 /* randomly change texture angle acceleration */
829 if ((random() % 1000) < 1)
831 gp->tangle_acc = frand(0.002) - 0.001;
839 * new window size or exposure
841 ENTRYPOINT void reshape_gleidescope(ModeInfo *mi, int width, int height)
843 GLfloat h = (GLfloat) height / (GLfloat) width;
845 glViewport(0, 0, (GLint) width, (GLint) height);
846 glMatrixMode(GL_PROJECTION);
848 gluPerspective(50.0, 1/h, 0.1, 2000.0);
849 glMatrixMode (GL_MODELVIEW);
858 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
860 /* set start time - star_time = 0 implies non-dynamic texture */
861 gp->start_time = (time_t)0;
863 /* set the texture size to default */
870 glShadeModel(GL_SMOOTH);
871 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
872 glEnable(GL_DEPTH_TEST);
873 glEnable(GL_CULL_FACE);
874 glDisable(GL_LIGHTING);
876 /* space for textures */
877 glGenTextures(1, &gp->textures[0]);
878 glGenTextures(1, &gp->textures[1]);
881 setup_texture(mi, gp->textures[gp->visible]);
884 ** want to choose a value for arg randomly if neither -arg nor -no-arg
885 ** is specified. xscreensaver libraries don't seem to let you do this -
886 ** if something isn't true then it is false (pesky two-state boolean values).
887 ** so, i've defined both -arg and -no-arg to arguments and added the
889 ** (btw if both -arg and -no-arg are defined then arg is set to False)
891 if (zoom == False && nozoom == False)
893 /* no zoom preference - randomise */
894 zoom = (((random() & 0x1) == 0x1) ? True : False);
896 else if (nozoom == True)
898 /* definately no zoom */
902 if (move == False && nomove == False)
904 /* no move preference - randomise */
905 move = (((random() & 0x1) == 0x1) ? True : False);
907 else if (nomove == True)
909 /* definately no move */
913 if (rotate == False && norotate == False)
915 /* no rotate preference - randomise */
916 rotate = (((random() & 0x1) == 0x1) ? True : False);
918 else if (norotate == True)
920 /* definately no rotate */
924 /* define cam variables */
925 gp->cam_x_speed = frand(3.0) - 1.5;
926 gp->cam_x_phase = random() % 360;
927 gp->cam_y_speed = frand(3.0) - 1.5;
928 gp->cam_y_phase = random() % 360;
929 gp->cam_z_speed = frand(3.0) - 1.5;
930 gp->cam_z_phase = random() % 360;
932 /* initial angular speeds */
933 gp->rangle_vel = frand(0.2) - 0.1;
934 gp->tangle_vel = frand(0.2) - 0.1;
935 gp->rangle_acc = frand(0.002) - 0.001;
936 gp->tangle_acc = frand(0.002) - 0.001;
941 gp->rangle_vel *= speed;
942 gp->tangle_vel *= speed;
943 gp->rangle_acc *= speed;
944 gp->tangle_acc *= speed;
947 /* distance is 11 - size */
948 if (gp->size != -1) {
950 fprintf(stderr, "-size given. ignoring -zoom.\n");
955 } else if (gp->size >= 10) {
958 gp->size = 11 - gp->size;
962 printf("phases [%d, %d, %d]\n", gp->cam_x_phase, gp->cam_y_phase, gp->cam_z_phase);
967 init_gleidescope(ModeInfo * mi)
970 int screen = MI_SCREEN(mi);
973 if (gleidescope == NULL) {
974 gleidescope = (gleidestruct *) calloc(MI_NUM_SCREENS(mi), sizeof (gleidestruct));
975 if (gleidescope == NULL) {
979 gp = &gleidescope[screen];
980 gp->window = MI_WINDOW(mi);
983 if ((gp->glx_context = init_GL(mi)) != NULL) {
985 reshape_gleidescope(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
987 glDrawBuffer(GL_BACK);
989 /* do initialisation */
998 draw_gleidescope(ModeInfo * mi)
1000 gleidestruct *gp = &gleidescope[MI_SCREEN(mi)];
1001 Display *display = MI_DISPLAY(mi);
1002 Window window = MI_WINDOW(mi);
1005 if (!gp->glx_context)
1008 /* Just keep running before the texture has come in. */
1009 /* if (gp->waiting_for_image_p) return; */
1011 glDrawBuffer(GL_BACK);
1013 glXMakeCurrent(display, window, *(gp->glx_context));
1021 glXSwapBuffers(display, window);
1029 /* need to change texture? */
1030 if ((gp->start_time != 0) && (duration != -1) && gp->fade == 0) {
1031 if (gp->start_time + duration <= time((time_t *)0)) {
1032 /* get new snapshot (into back buffer) and start fade count */
1033 getSnapshot(mi, gp->textures[1 - gp->visible]);
1040 release_gleidescope(ModeInfo * mi)
1042 if (gleidescope != NULL) {
1045 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1046 gleidestruct *gp = &gleidescope[screen];
1048 /* acd - is this needed? */
1049 if (gp->glx_context) {
1050 /* Display lists MUST be freed while their glXContext is current. */
1051 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1053 /* acd - was code here for freeing things that are no longer in struct */
1056 (void) free((void *) gleidescope);
1063 XSCREENSAVER_MODULE ("Gleidescope", gleidescope)