2 * screenflip - 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
17 #include <X11/Intrinsic.h>
21 # define PROGCLASS "Screenflip"
22 # define HACK_INIT init_screenflip
23 # define HACK_DRAW draw_screenflip
24 # define HACK_RESHAPE reshape_screenflip
25 # define screenflip_opts xlockmore_opts
26 /* insert defaults here */
28 #define DEFAULTS "*delay: 20000 \n" \
29 "*showFPS: False \n" \
32 # include "xlockmore.h" /* from the xscreensaver distribution */
33 #else /* !STANDALONE */
34 # include "xlock.h" /* from the xlockmore distribution */
35 #endif /* !STANDALONE */
37 /* lifted from lament.c */
38 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
39 #define RANDSIGN() ((random() & 1) ? 1 : -1)
49 int tw, th; /* texture width, height */
51 GLfloat max_tx, max_ty;
55 GLfloat qw = QW, qh = QH; /* q? are for the quad we'll draw */
56 GLfloat qx = -6 , qy = 6;
59 #define countof(x) (sizeof((x))/sizeof((*x)))
62 static XrmOptionDescRec opts[] = {
63 {"+rotate", ".screenflip.rotate", XrmoptionNoArg, (caddr_t) "false" },
64 {"-rotate", ".screenflip.rotate", XrmoptionNoArg, (caddr_t) "true" },
68 static argtype vars[] = {
69 {(caddr_t *) &rotate, "rotate", "Rotate", "True", t_Bool},
74 ModeSpecOpt screenflip_opts = {countof(opts), opts, countof(vars), vars, NULL};
78 ModStruct screenflip_description =
79 {"screenflip", "init_screenflip", "draw_screenflip", "release_screenflip",
80 "draw_screenflip", "init_screenflip", NULL, &screenflip_opts,
81 1000, 1, 2, 1, 4, 1.0, "",
82 "Screenflips", 0, NULL};
88 GLXContext *glx_context;
92 static Screenflip *screenflip = NULL;
98 #include "grab-ximage.h"
101 #define M_PI 3.14159265
104 static GLfloat viewer[] = {0.0, 0.0, 15.0};
107 int fadetime = 0; /* fade before regrab */
110 /* draw the texture mapped quad (actually two back to back)*/
111 void showscreen(int frozen)
113 static GLfloat r = 1, g = 1, b = 1, a = 1;
116 /* static int stretch; */
117 static GLfloat stretch_val_x = 0, stretch_val_y = 0;
118 static GLfloat stretch_val_dx = 0, stretch_val_dy = 0;
119 /* static int stretch_x = 0, stretch_y = 0; */
122 /* r -= 0.02; g -= 0.02; b -= 0.02; */
130 stretch_val_x = stretch_val_y = stretch_val_dx = stretch_val_dy = 0;
132 if (stretch_val_dx == 0 && !frozen && !(random() % 25))
133 stretch_val_dx = (float)(random() % 100) / 5000;
134 if (stretch_val_dy == 0 && !frozen && !(random() % 25))
135 stretch_val_dy = (float)(random() % 100) / 5000;
143 w *= sin (stretch_val_x) + 1;
144 x *= sin (stretch_val_x) + 1;
145 if (!fadetime) stretch_val_x += stretch_val_dx;
146 if (stretch_val_x > 2*M_PI && !(random() % 5))
147 stretch_val_dx = (float)(random() % 100) / 5000;
149 stretch_val_x -= 2*M_PI;
151 if (!fadetime) stretch_val_y += stretch_val_dy;
152 h *= sin (stretch_val_y) / 2 + 1;
153 y *= sin (stretch_val_y) / 2 + 1;
154 if (stretch_val_y > 2*M_PI && !(random() % 5))
155 stretch_val_dy = (float)(random() % 100) / 5000;
157 stretch_val_y -= 2*M_PI;
160 glColor4f(r, g, b, a);
162 glEnable(GL_TEXTURE_2D);
164 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165 glDepthMask(GL_FALSE);
171 glTexCoord2f(0, max_ty);
174 glTexCoord2f(max_tx, max_ty);
177 glTexCoord2f(max_tx, 0);
183 glNormal3f(0, 0, -1);
185 glTexCoord2f(0, max_ty);
186 glVertex3f(x, y, -0.05);
189 glVertex3f(x, h, -0.05);
191 glTexCoord2f(max_tx, 0);
192 glVertex3f(w, h, -0.05);
194 glTexCoord2f(max_tx, max_ty);
195 glVertex3f(w, y, -0.05);
200 glDisable(GL_TEXTURE_2D);
201 glDepthMask(GL_TRUE);
203 glBegin(GL_LINE_LOOP);
213 /* This function is responsible for 'zooming back' the square after
214 * a new chunk has been grabbed with getSnapshot(), and positioning
215 * it suitably on the screen. Once positioned (where we begin to rotate),
216 * it just does a glTranslatef() and returns 1
221 static GLfloat curx, cury, curz = 0;
227 if (curx == 0) curx = qx;
228 if (cury == 0) cury = qy;
235 if (curz > -10 || curx > wx + 0.1 || curx < wx - 0.1 ||
236 cury > wy + 0.1 || cury < wy - 0.1) {
255 glTranslatef(0, 0, curz);
258 glTranslatef(0, 0, curz);
267 glColor3f(0, 0.7, 0);
269 for (i = 0 ; i <= 50; i+=2) {
270 glVertex3f( -25, -15, i-70);
271 glVertex3f( 25, -15, i-70);
272 glVertex3f( i-25, -15, -70);
273 glVertex3f( i-25, -15, -20);
280 static GLfloat rx=1, ry=1, rz=0;
281 static GLfloat rot = 0;
282 static GLfloat drot = 0;
283 static GLfloat odrot = 1;
284 static GLfloat ddrot = 0;
285 static float theta = 0, rho = 0, dtheta = 0, drho = 0, gamma = 0, dgamma = 0;
289 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
291 gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
296 glTranslatef(5 * sin(theta), 5 * sin(rho), 10 * cos(gamma) - 10);
297 /* randomly change the speed */
298 if (!(random() % 300)) {
300 drho = 1/60 - (float)(random() % 100)/3000;
302 dtheta = 1/60 - (float)(random() % 100)/3000;
304 dgamma = 1/60 - (float)(random() % 100)/3000;
306 if (rotate) glRotatef(rot, rx, ry, rz);
307 /* update variables with each frame */
315 /* dont let our rotation speed get too high */
316 if (drot > 5 && ddrot > 0)
317 ddrot = 0 - (GLfloat)(random() % 100) / 1000;
318 else if (drot < -5 && ddrot < 0)
319 ddrot = (GLfloat)(random() % 100) / 1000;
320 } else { /* reset some paramaters */
321 ddrot = 0.05 - (GLfloat)(random() % 100) / 1000;
322 theta = rho = gamma = 0;
326 if (!fadetime && (rot >= 360 || rot <= -360) && !(random() % 7)) { /* rotate change */
327 rx = (GLfloat)(random() % 100) / 100;
328 ry = (GLfloat)(random() % 100) / 100;
329 rz = (GLfloat)(random() % 100) / 100;
331 if (odrot * drot < 0 && tw < winw && !(random() % 10)) {
332 fadetime = 1; /* randomly fade and get new snapshot */
336 if (rot > 360 || rot < -360) /* dont overflow rotation! */
343 void reshape_screenflip(ModeInfo *mi, int width, int height)
345 glViewport(0,0,(GLint)width, (GLint) height);
346 glMatrixMode(GL_PROJECTION);
348 gluPerspective(45, 1, 2.0, 85);
349 glMatrixMode(GL_MODELVIEW);
354 void getSnapshot (ModeInfo *modeinfo)
358 ximage = screen_to_ximage (modeinfo->xgwa.screen, modeinfo->window);
361 tw = modeinfo->xgwa.width;
362 th = modeinfo->xgwa.height;
367 qw *= (GLfloat)tw/winw;
368 qh *= (GLfloat)th/winh;
370 max_tx = (GLfloat) tw / (GLfloat) ximage->width;
371 max_ty = (GLfloat) th / (GLfloat) ximage->height;
374 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
376 GL_LINEAR_MIPMAP_LINEAR);
377 if (gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
378 ximage->width, ximage->height,
379 GL_RGBA, GL_UNSIGNED_BYTE, ximage->data)) {
385 XDestroyImage (ximage);
388 void init_screenflip(ModeInfo *mi)
390 int screen = MI_SCREEN(mi);
393 if (screenflip == NULL) {
394 if ((screenflip = (Screenflip *) calloc(MI_NUM_SCREENS(mi),
395 sizeof(Screenflip))) == NULL)
398 c = &screenflip[screen];
399 c->window = MI_WINDOW(mi);
401 if ((c->glx_context = init_GL(mi)) != NULL) {
402 reshape_screenflip(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
406 winh = MI_WIN_HEIGHT(mi);
407 winw = MI_WIN_WIDTH(mi);
408 glClearColor(0.0,0.0,0.0,0.0);
409 glShadeModel(GL_SMOOTH);
410 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
411 glEnable(GL_DEPTH_TEST);
412 glEnable(GL_CULL_FACE);
413 glCullFace(GL_FRONT);
414 glDisable(GL_LIGHTING);
419 void draw_screenflip(ModeInfo *mi)
421 Screenflip *c = &screenflip[MI_SCREEN(mi)];
422 Window w = MI_WINDOW(mi);
423 Display *disp = MI_DISPLAY(mi);
428 glXMakeCurrent(disp, w, *(c->glx_context));
435 if(mi->fps_p) do_fps(mi);
437 glXSwapBuffers(disp, w);
440 void release_screenflip(ModeInfo *mi)
442 if (screenflip != NULL) {
443 (void) free((void *) screenflip);