2 * flipscreen3d - takes snapshots of the screen and flips it around
4 * version 1.0 - Oct 24, 2001
6 * Copyright (C) 2001 Ben Buxton (bb@cactii.net)
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation. No representations are made about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
18 #define DEFAULTS "*delay: 20000 \n" \
19 "*showFPS: False \n" \
20 "*wireframe: False \n" \
22 "*suppressRotationAnimation: True\n" \
24 # define free_screenflip 0
25 # define release_screenflip 0
26 # include "xlockmore.h" /* from the xscreensaver distribution */
27 # include "gltrackball.h"
28 #else /* !STANDALONE */
29 # include "xlock.h" /* from the xlockmore distribution */
30 #endif /* !STANDALONE */
32 /* lifted from lament.c */
33 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
34 #define RANDSIGN() ((random() & 1) ? 1 : -1)
39 /* Should be in <GL/glext.h> */
40 # ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
41 # define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
43 # ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
44 # define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
47 #define DEF_ROTATE "True"
54 #define countof(x) (sizeof((x))/sizeof((*x)))
57 static XrmOptionDescRec opts[] = {
58 {"+rotate", ".screenflip.rotate", XrmoptionNoArg, "false" },
59 {"-rotate", ".screenflip.rotate", XrmoptionNoArg, "true" },
63 static argtype vars[] = {
64 {&rotate, "rotate", "Rotate", DEF_ROTATE, t_Bool},
69 ENTRYPOINT ModeSpecOpt screenflip_opts = {countof(opts), opts, countof(vars), vars, NULL};
73 ModStruct screenflip_description =
74 {"screenflip", "init_screenflip", "draw_screenflip", NULL,
75 "draw_screenflip", "init_screenflip", NULL, &screenflip_opts,
76 1000, 1, 2, 1, 4, 1.0, "",
77 "Screenflips", 0, NULL};
83 GLXContext *glx_context;
87 int tw, th; /* texture width, height */
88 GLfloat min_tx, min_ty;
89 GLfloat max_tx, max_ty;
90 GLfloat qx, qy, qw, qh; /* the quad we'll draw */
93 int fadetime; /* fade before regrab */
95 trackball_state *trackball;
98 GLfloat show_colors[4];
99 GLfloat stretch_val_x, stretch_val_y;
100 GLfloat stretch_val_dx, stretch_val_dy;
102 GLfloat curx, cury, curz;
105 GLfloat rot, drot, odrot, ddrot, orot;
106 float theta, rho, dtheta, drho, gamma, dgamma;
110 Bool waiting_for_image_p;
117 static Screenflip *screenflip = NULL;
119 #include "grab-ximage.h"
121 static const GLfloat viewer[] = {0.0, 0.0, 15.0};
124 static void getSnapshot (ModeInfo *);
128 screenflip_handle_event (ModeInfo *mi, XEvent *event)
130 Screenflip *c = &screenflip[MI_SCREEN(mi)];
132 if (gltrackball_event_handler (event, c->trackball,
133 MI_WIDTH (mi), MI_HEIGHT (mi),
136 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
138 if (!c->waiting_for_image_p)
149 /* draw the texture mapped quad (actually two back to back)*/
150 static void showscreen(Screenflip *c, int frozen, int wire)
155 /* r -= 0.02; g -= 0.02; b -= 0.02; */
156 c->show_colors[3] -= 0.02;
157 if (c->show_colors[3] < 0) {
161 } else if (c->show_colors[3] < 0) {
162 c->show_colors[0] = c->show_colors[1] =
163 c->show_colors[2] = c->show_colors[3] = 1;
164 c->stretch_val_x = c->stretch_val_y =
165 c->stretch_val_dx = c->stretch_val_dy = 0;
167 if (c->stretch_val_dx == 0 && !frozen && !(random() % 25))
168 c->stretch_val_dx = (float)(random() % 100) / 5000;
169 if (c->stretch_val_dy == 0 && !frozen && !(random() % 25))
170 c->stretch_val_dy = (float)(random() % 100) / 5000;
178 w *= sin (c->stretch_val_x) + 1;
179 x *= sin (c->stretch_val_x) + 1;
180 if (!c->button_down_p) {
181 if (!c->fadetime) c->stretch_val_x += c->stretch_val_dx;
182 if (c->stretch_val_x > 2*M_PI && !(random() % 5))
183 c->stretch_val_dx = (float)(random() % 100) / 5000;
185 c->stretch_val_x -= 2*M_PI;
188 if (!c->button_down_p && !c->fadetime) c->stretch_val_y += c->stretch_val_dy;
189 h *= sin (c->stretch_val_y) / 2 + 1;
190 y *= sin (c->stretch_val_y) / 2 + 1;
191 if (!c->button_down_p) {
192 if (c->stretch_val_y > 2*M_PI && !(random() % 5))
193 c->stretch_val_dy = (float)(random() % 100) / 5000;
195 c->stretch_val_y -= 2*M_PI;
199 glColor4f(c->show_colors[0], c->show_colors[1],
200 c->show_colors[2], c->show_colors[3]);
204 glEnable(GL_TEXTURE_2D);
206 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
207 glDepthMask(GL_FALSE);
210 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
213 glTexCoord2f(c->max_tx, c->max_ty); glVertex3f(w, h, 0);
214 glTexCoord2f(c->max_tx, c->min_ty); glVertex3f(w, y, 0);
215 glTexCoord2f(c->min_tx, c->min_ty); glVertex3f(x, y, 0);
216 glTexCoord2f(c->min_tx, c->max_ty); glVertex3f(x, h, 0);
218 glNormal3f(0, 0, -1);
219 glTexCoord2f(c->min_tx, c->min_ty); glVertex3f(x, y, -0.05);
220 glTexCoord2f(c->max_tx, c->min_ty); glVertex3f(w, y, -0.05);
221 glTexCoord2f(c->max_tx, c->max_ty); glVertex3f(w, h, -0.05);
222 glTexCoord2f(c->min_tx, c->max_ty); glVertex3f(x, h, -0.05);
226 glDisable(GL_TEXTURE_2D);
227 glDepthMask(GL_TRUE);
229 glBegin(GL_LINE_LOOP);
239 /* This function is responsible for 'zooming back' the square after
240 * a new chunk has been grabbed with getSnapshot(), and positioning
241 * it suitably on the screen. Once positioned (where we begin to rotate),
242 * it just does a glTranslatef() and returns 1
245 static int inposition(Screenflip *c)
252 if (c->curx == 0) c->curx = c->qx;
253 if (c->cury == 0) c->cury = c->qy;
260 if (c->curz > -10 || c->curx > wx + 0.1 || c->curx < wx - 0.1 ||
261 c->cury > wy + 0.1 || c->cury < wy - 0.1) {
280 glTranslatef(0, 0, c->curz);
283 glTranslatef(0, 0, c->curz);
289 static void drawgrid(void)
293 glColor3f(0, 0.7, 0);
295 for (i = 0 ; i <= 50; i+=2) {
296 glVertex3f( -25, -15, i-70);
297 glVertex3f( 25, -15, i-70);
298 glVertex3f( i-25, -15, -70);
299 glVertex3f( i-25, -15, -20);
306 static void display(Screenflip *c, int wire)
309 GLfloat rot = current_device_rotation();
311 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
313 gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
317 glRotatef(rot, 0, 0, 1);
318 if ((rot > 45 && rot < 135) ||
319 (rot < -45 && rot > -135))
321 GLfloat s = c->winw / (GLfloat) c->winh;
322 glScalef (s, 1/s, 1);
328 glTranslatef(5 * sin(c->theta), 5 * sin(c->rho), 10 * cos(c->gamma) - 10);
329 /* randomly change the speed */
330 if (!c->button_down_p && !(random() % 300)) {
332 c->drho = 1/60 - (float)(random() % 100)/3000;
334 c->dtheta = 1/60 - (float)(random() % 100)/3000;
336 c->dgamma = 1/60 - (float)(random() % 100)/3000;
338 glRotatef(rot, 0, 0, 1);
339 gltrackball_rotate (c->trackball);
340 glRotatef(-rot, 0, 0, 1);
341 if (rotate) glRotatef(c->rot, c->rx, c->ry, c->rz);
342 /* update variables with each frame */
343 if(!c->button_down_p && !c->fadetime) {
344 c->theta += c->dtheta;
346 c->gamma += c->dgamma;
350 /* dont let our rotation speed get too high */
351 if (c->drot > 5 && c->ddrot > 0)
352 c->ddrot = 0 - (GLfloat)(random() % 100) / 1000;
353 else if (c->drot < -5 && c->ddrot < 0)
354 c->ddrot = (GLfloat)(random() % 100) / 1000;
355 } else { /* reset some paramaters */
356 c->ddrot = 0.05 - (GLfloat)(random() % 100) / 1000;
357 c->theta = c->rho = c->gamma = 0;
361 if (!c->button_down_p && !c->fadetime && (c->rot >= 360 || c->rot <= -360) && !(random() % 7)) { /* rotate change */
362 c->rx = (GLfloat)(random() % 100) / 100;
363 c->ry = (GLfloat)(random() % 100) / 100;
364 c->rz = (GLfloat)(random() % 100) / 100;
366 if (c->odrot * c->drot < 0 && c->tw < c->winw && !(random() % 10)) {
367 c->fadetime = 1; /* randomly fade and get new snapshot */
371 if (c->rot > 360 || c->rot < -360) /* dont overflow rotation! */
373 showscreen(c, frozen, wire);
378 ENTRYPOINT void reshape_screenflip(ModeInfo *mi, int width, int height)
380 Screenflip *c = &screenflip[MI_SCREEN(mi)];
381 glViewport(0,0,(GLint)width, (GLint) height);
382 glMatrixMode(GL_PROJECTION);
384 gluPerspective(45, 1, 2.0, 85);
385 glMatrixMode(GL_MODELVIEW);
391 image_loaded_cb (const char *filename, XRectangle *geometry,
392 int image_width, int image_height,
393 int texture_width, int texture_height,
396 Screenflip *c = (Screenflip *) closure;
398 c->tw = texture_width;
399 c->th = texture_height;
400 c->min_tx = (GLfloat) geometry->x / c->tw;
401 c->min_ty = (GLfloat) geometry->y / c->th;
402 c->max_tx = (GLfloat) (geometry->x + geometry->width) / c->tw;
403 c->max_ty = (GLfloat) (geometry->y + geometry->height) / c->th;
405 c->qx = -QW/2 + ((GLfloat) geometry->x * QW / image_width);
406 c->qy = QH/2 - ((GLfloat) geometry->y * QH / image_height);
407 c->qw = QW * ((GLfloat) geometry->width / image_width);
408 c->qh = QH * ((GLfloat) geometry->height / image_height);
410 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
411 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
412 (c->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
414 if (c->anisotropic >= 1.0)
415 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
418 c->waiting_for_image_p = False;
419 c->first_image_p = False;
423 static void getSnapshot (ModeInfo *modeinfo)
425 Screenflip *c = &screenflip[MI_SCREEN(modeinfo)];
427 if (MI_IS_WIREFRAME(modeinfo))
430 c->waiting_for_image_p = True;
432 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
433 *c->glx_context, 0, 0, c->mipmap_p, c->texid,
437 ENTRYPOINT void init_screenflip(ModeInfo *mi)
439 int screen = MI_SCREEN(mi);
442 MI_INIT(mi, screenflip);
443 c = &screenflip[screen];
444 c->window = MI_WINDOW(mi);
446 c->trackball = gltrackball_init (False);
448 if ((c->glx_context = init_GL(mi)) != NULL) {
449 reshape_screenflip(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
453 c->winh = MI_WIN_HEIGHT(mi);
454 c->winw = MI_WIN_WIDTH(mi);
463 c->show_colors[0] = c->show_colors[1] =
464 c->show_colors[2] = c->show_colors[3] = 1;
466 if (! MI_IS_WIREFRAME(mi))
468 glShadeModel(GL_SMOOTH);
469 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
470 glEnable(GL_DEPTH_TEST);
471 glEnable(GL_CULL_FACE);
473 glDisable(GL_LIGHTING);
476 if (strstr ((char *) glGetString(GL_EXTENSIONS),
477 "GL_EXT_texture_filter_anisotropic"))
478 glGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &c->anisotropic);
480 c->anisotropic = 0.0;
482 glGenTextures(1, &c->texid);
484 c->first_image_p = True;
488 ENTRYPOINT void draw_screenflip(ModeInfo *mi)
490 Screenflip *c = &screenflip[MI_SCREEN(mi)];
491 Window w = MI_WINDOW(mi);
492 Display *disp = MI_DISPLAY(mi);
497 /* Wait for the first image; for subsequent images, load them in the
498 background while animating. */
499 if (c->waiting_for_image_p && c->first_image_p)
502 glXMakeCurrent(disp, w, *(c->glx_context));
504 glBindTexture(GL_TEXTURE_2D, c->texid);
509 display(c, MI_IS_WIREFRAME(mi));
511 if(mi->fps_p) do_fps(mi);
513 glXSwapBuffers(disp, w);
516 XSCREENSAVER_MODULE_2 ("FlipScreen3D", flipscreen3d, screenflip)