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" \
23 # define refresh_screenflip 0
24 # include "xlockmore.h" /* from the xscreensaver distribution */
25 # include "gltrackball.h"
26 #else /* !STANDALONE */
27 # include "xlock.h" /* from the xlockmore distribution */
28 #endif /* !STANDALONE */
30 /* lifted from lament.c */
31 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
32 #define RANDSIGN() ((random() & 1) ? 1 : -1)
43 #define countof(x) (sizeof((x))/sizeof((*x)))
46 static XrmOptionDescRec opts[] = {
47 {"+rotate", ".screenflip.rotate", XrmoptionNoArg, "false" },
48 {"-rotate", ".screenflip.rotate", XrmoptionNoArg, "true" },
52 static argtype vars[] = {
53 {&rotate, "rotate", "Rotate", "True", t_Bool},
58 ENTRYPOINT ModeSpecOpt screenflip_opts = {countof(opts), opts, countof(vars), vars, NULL};
62 ModStruct screenflip_description =
63 {"screenflip", "init_screenflip", "draw_screenflip", "release_screenflip",
64 "draw_screenflip", "init_screenflip", NULL, &screenflip_opts,
65 1000, 1, 2, 1, 4, 1.0, "",
66 "Screenflips", 0, NULL};
72 GLXContext *glx_context;
76 int tw, th; /* texture width, height */
77 GLfloat min_tx, min_ty;
78 GLfloat max_tx, max_ty;
79 GLfloat qx, qy, qw, qh; /* the quad we'll draw */
82 int fadetime; /* fade before regrab */
84 trackball_state *trackball;
87 GLfloat show_colors[4];
88 GLfloat stretch_val_x, stretch_val_y;
89 GLfloat stretch_val_dx, stretch_val_dy;
91 GLfloat curx, cury, curz;
94 GLfloat rot, drot, odrot, ddrot, orot;
95 float theta, rho, dtheta, drho, gamma, dgamma;
99 Bool waiting_for_image_p;
104 static Screenflip *screenflip = NULL;
106 #include "grab-ximage.h"
109 #define M_PI 3.14159265
112 static const GLfloat viewer[] = {0.0, 0.0, 15.0};
116 screenflip_handle_event (ModeInfo *mi, XEvent *event)
118 Screenflip *c = &screenflip[MI_SCREEN(mi)];
120 if (event->xany.type == ButtonPress &&
121 event->xbutton.button == Button1)
123 c->button_down_p = True;
124 gltrackball_start (c->trackball,
125 event->xbutton.x, event->xbutton.y,
126 MI_WIDTH (mi), MI_HEIGHT (mi));
129 else if (event->xany.type == ButtonRelease &&
130 event->xbutton.button == Button1)
132 c->button_down_p = False;
135 else if (event->xany.type == ButtonPress &&
136 (event->xbutton.button == Button4 ||
137 event->xbutton.button == Button5))
139 gltrackball_mousewheel (c->trackball, event->xbutton.button, 10,
140 !!event->xbutton.state);
143 else if (event->xany.type == MotionNotify &&
146 gltrackball_track (c->trackball,
147 event->xmotion.x, event->xmotion.y,
148 MI_WIDTH (mi), MI_HEIGHT (mi));
156 /* draw the texture mapped quad (actually two back to back)*/
157 static void showscreen(Screenflip *c, int frozen, int wire)
162 /* r -= 0.02; g -= 0.02; b -= 0.02; */
163 c->show_colors[3] -= 0.02;
164 if (c->show_colors[3] < 0) {
168 } else if (c->show_colors[3] < 0) {
169 c->show_colors[0] = c->show_colors[1] =
170 c->show_colors[2] = c->show_colors[3] = 1;
171 c->stretch_val_x = c->stretch_val_y =
172 c->stretch_val_dx = c->stretch_val_dy = 0;
174 if (c->stretch_val_dx == 0 && !frozen && !(random() % 25))
175 c->stretch_val_dx = (float)(random() % 100) / 5000;
176 if (c->stretch_val_dy == 0 && !frozen && !(random() % 25))
177 c->stretch_val_dy = (float)(random() % 100) / 5000;
185 w *= sin (c->stretch_val_x) + 1;
186 x *= sin (c->stretch_val_x) + 1;
187 if (!c->button_down_p) {
188 if (!c->fadetime) c->stretch_val_x += c->stretch_val_dx;
189 if (c->stretch_val_x > 2*M_PI && !(random() % 5))
190 c->stretch_val_dx = (float)(random() % 100) / 5000;
192 c->stretch_val_x -= 2*M_PI;
195 if (!c->button_down_p && !c->fadetime) c->stretch_val_y += c->stretch_val_dy;
196 h *= sin (c->stretch_val_y) / 2 + 1;
197 y *= sin (c->stretch_val_y) / 2 + 1;
198 if (!c->button_down_p) {
199 if (c->stretch_val_y > 2*M_PI && !(random() % 5))
200 c->stretch_val_dy = (float)(random() % 100) / 5000;
202 c->stretch_val_y -= 2*M_PI;
206 glColor4f(c->show_colors[0], c->show_colors[1],
207 c->show_colors[2], c->show_colors[3]);
211 glEnable(GL_TEXTURE_2D);
213 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
214 glDepthMask(GL_FALSE);
217 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
220 glTexCoord2f(c->max_tx, c->max_ty); glVertex3f(w, h, 0);
221 glTexCoord2f(c->max_tx, c->min_ty); glVertex3f(w, y, 0);
222 glTexCoord2f(c->min_tx, c->min_ty); glVertex3f(x, y, 0);
223 glTexCoord2f(c->min_tx, c->max_ty); glVertex3f(x, h, 0);
225 glNormal3f(0, 0, -1);
226 glTexCoord2f(c->min_tx, c->min_ty); glVertex3f(x, y, -0.05);
227 glTexCoord2f(c->max_tx, c->min_ty); glVertex3f(w, y, -0.05);
228 glTexCoord2f(c->max_tx, c->max_ty); glVertex3f(w, h, -0.05);
229 glTexCoord2f(c->min_tx, c->max_ty); glVertex3f(x, h, -0.05);
233 glDisable(GL_TEXTURE_2D);
234 glDepthMask(GL_TRUE);
236 glBegin(GL_LINE_LOOP);
246 /* This function is responsible for 'zooming back' the square after
247 * a new chunk has been grabbed with getSnapshot(), and positioning
248 * it suitably on the screen. Once positioned (where we begin to rotate),
249 * it just does a glTranslatef() and returns 1
252 static int inposition(Screenflip *c)
259 if (c->curx == 0) c->curx = c->qx;
260 if (c->cury == 0) c->cury = c->qy;
267 if (c->curz > -10 || c->curx > wx + 0.1 || c->curx < wx - 0.1 ||
268 c->cury > wy + 0.1 || c->cury < wy - 0.1) {
287 glTranslatef(0, 0, c->curz);
290 glTranslatef(0, 0, c->curz);
296 static void drawgrid(void)
300 glColor3f(0, 0.7, 0);
302 for (i = 0 ; i <= 50; i+=2) {
303 glVertex3f( -25, -15, i-70);
304 glVertex3f( 25, -15, i-70);
305 glVertex3f( i-25, -15, -70);
306 glVertex3f( i-25, -15, -20);
313 static void display(Screenflip *c, int wire)
317 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
319 gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
324 glTranslatef(5 * sin(c->theta), 5 * sin(c->rho), 10 * cos(c->gamma) - 10);
325 /* randomly change the speed */
326 if (!c->button_down_p && !(random() % 300)) {
328 c->drho = 1/60 - (float)(random() % 100)/3000;
330 c->dtheta = 1/60 - (float)(random() % 100)/3000;
332 c->dgamma = 1/60 - (float)(random() % 100)/3000;
334 gltrackball_rotate (c->trackball);
335 if (rotate) glRotatef(c->rot, c->rx, c->ry, c->rz);
336 /* update variables with each frame */
337 if(!c->button_down_p && !c->fadetime) {
338 c->theta += c->dtheta;
340 c->gamma += c->dgamma;
344 /* dont let our rotation speed get too high */
345 if (c->drot > 5 && c->ddrot > 0)
346 c->ddrot = 0 - (GLfloat)(random() % 100) / 1000;
347 else if (c->drot < -5 && c->ddrot < 0)
348 c->ddrot = (GLfloat)(random() % 100) / 1000;
349 } else { /* reset some paramaters */
350 c->ddrot = 0.05 - (GLfloat)(random() % 100) / 1000;
351 c->theta = c->rho = c->gamma = 0;
355 if (!c->button_down_p && !c->fadetime && (c->rot >= 360 || c->rot <= -360) && !(random() % 7)) { /* rotate change */
356 c->rx = (GLfloat)(random() % 100) / 100;
357 c->ry = (GLfloat)(random() % 100) / 100;
358 c->rz = (GLfloat)(random() % 100) / 100;
360 if (c->odrot * c->drot < 0 && c->tw < c->winw && !(random() % 10)) {
361 c->fadetime = 1; /* randomly fade and get new snapshot */
365 if (c->rot > 360 || c->rot < -360) /* dont overflow rotation! */
367 showscreen(c, frozen, wire);
372 ENTRYPOINT void reshape_screenflip(ModeInfo *mi, int width, int height)
374 Screenflip *c = &screenflip[MI_SCREEN(mi)];
375 glViewport(0,0,(GLint)width, (GLint) height);
376 glMatrixMode(GL_PROJECTION);
378 gluPerspective(45, 1, 2.0, 85);
379 glMatrixMode(GL_MODELVIEW);
385 image_loaded_cb (const char *filename, XRectangle *geometry,
386 int image_width, int image_height,
387 int texture_width, int texture_height,
390 Screenflip *c = (Screenflip *) closure;
392 c->tw = texture_width;
393 c->th = texture_height;
394 c->min_tx = (GLfloat) geometry->x / c->tw;
395 c->min_ty = (GLfloat) geometry->y / c->th;
396 c->max_tx = (GLfloat) (geometry->x + geometry->width) / c->tw;
397 c->max_ty = (GLfloat) (geometry->y + geometry->height) / c->th;
399 c->qx = -QW/2 + ((GLfloat) geometry->x * QW / image_width);
400 c->qy = QH/2 - ((GLfloat) geometry->y * QH / image_height);
401 c->qw = QW * ((GLfloat) geometry->width / image_width);
402 c->qh = QH * ((GLfloat) geometry->height / image_height);
404 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
405 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
406 (c->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
408 c->waiting_for_image_p = False;
409 c->first_image_p = False;
413 static void getSnapshot (ModeInfo *modeinfo)
415 Screenflip *c = &screenflip[MI_SCREEN(modeinfo)];
417 if (MI_IS_WIREFRAME(modeinfo))
420 c->waiting_for_image_p = True;
422 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
423 *c->glx_context, 0, 0, c->mipmap_p, c->texid,
427 ENTRYPOINT void init_screenflip(ModeInfo *mi)
429 int screen = MI_SCREEN(mi);
432 if (screenflip == NULL) {
433 if ((screenflip = (Screenflip *) calloc(MI_NUM_SCREENS(mi),
434 sizeof(Screenflip))) == NULL)
437 c = &screenflip[screen];
438 c->window = MI_WINDOW(mi);
440 c->trackball = gltrackball_init ();
442 if ((c->glx_context = init_GL(mi)) != NULL) {
443 reshape_screenflip(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
447 c->winh = MI_WIN_HEIGHT(mi);
448 c->winw = MI_WIN_WIDTH(mi);
457 c->show_colors[0] = c->show_colors[1] =
458 c->show_colors[2] = c->show_colors[3] = 1;
460 glClearColor(0.0,0.0,0.0,0.0);
462 if (! MI_IS_WIREFRAME(mi))
464 glShadeModel(GL_SMOOTH);
465 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
466 glEnable(GL_DEPTH_TEST);
467 glEnable(GL_CULL_FACE);
469 glDisable(GL_LIGHTING);
472 glGenTextures(1, &c->texid);
474 c->first_image_p = True;
478 ENTRYPOINT void draw_screenflip(ModeInfo *mi)
480 Screenflip *c = &screenflip[MI_SCREEN(mi)];
481 Window w = MI_WINDOW(mi);
482 Display *disp = MI_DISPLAY(mi);
487 /* Wait for the first image; for subsequent images, load them in the
488 background while animating. */
489 if (c->waiting_for_image_p && c->first_image_p)
492 glXMakeCurrent(disp, w, *(c->glx_context));
494 glBindTexture(GL_TEXTURE_2D, c->texid);
499 display(c, MI_IS_WIREFRAME(mi));
501 if(mi->fps_p) do_fps(mi);
503 glXSwapBuffers(disp, w);
506 ENTRYPOINT void release_screenflip(ModeInfo *mi)
508 if (screenflip != NULL) {
509 (void) free((void *) screenflip);
515 XSCREENSAVER_MODULE_2 ("FlipScreen3D", flipscreen3d, screenflip)