summaryrefslogtreecommitdiffstats
path: root/hacks/glx/flipscreen3d.c
blob: e4db5df1bf4870536f5b8adbda2ca637e776f9f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * flipscreen3d - takes snapshots of the screen and flips it around
 *
 * version 1.0 - Oct 24, 2001
 *
 * Copyright (C) 2001 Ben Buxton (bb@cactii.net)
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 */

#ifdef STANDALONE
#define DEFAULTS "*delay:     20000 \n" \
                 "*showFPS:   False \n" \
                 "*wireframe: False \n" \
                 "*useSHM:    True  \n" \
		 "*suppressRotationAnimation: True\n" \

# define free_screenflip 0
# define release_screenflip 0
# include "xlockmore.h"                         /* from the xscreensaver distribution */
# include "gltrackball.h"
#else  /* !STANDALONE */
# include "xlock.h"                                     /* from the xlockmore distribution */
#endif /* !STANDALONE */

/* lifted from lament.c */
#define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
#define RANDSIGN() ((random() & 1) ? 1 : -1)


#ifdef USE_GL

/* Should be in <GL/glext.h> */
# ifndef  GL_TEXTURE_MAX_ANISOTROPY_EXT
#  define GL_TEXTURE_MAX_ANISOTROPY_EXT     0x84FE
# endif
# ifndef  GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
#  define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
# endif

#define DEF_ROTATE "True"
static int rotate;

#define QW 12
#define QH 12

#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))


static XrmOptionDescRec opts[] = {
  {"+rotate", ".screenflip.rotate", XrmoptionNoArg, "false" },
  {"-rotate", ".screenflip.rotate", XrmoptionNoArg, "true" },
};


static argtype vars[] = {
  {&rotate, "rotate", "Rotate", DEF_ROTATE, t_Bool},
};



ENTRYPOINT ModeSpecOpt screenflip_opts = {countof(opts), opts, countof(vars), vars, NULL};


#ifdef USE_MODULES
ModStruct   screenflip_description =
{"screenflip", "init_screenflip", "draw_screenflip", NULL,
 "draw_screenflip", "init_screenflip", NULL, &screenflip_opts,
 1000, 1, 2, 1, 4, 1.0, "",
 "Screenflips", 0, NULL};

#endif


typedef struct {
  GLXContext *glx_context;
  Window window;

  int winw, winh;
  int tw, th; /* texture width, height */
  GLfloat min_tx, min_ty;
  GLfloat max_tx, max_ty;
  GLfloat qx, qy, qw, qh; /* the quad we'll draw */

  int regrab;
  int fadetime; /* fade before regrab */

  trackball_state *trackball;
  Bool button_down_p;

  GLfloat show_colors[4];
  GLfloat stretch_val_x, stretch_val_y;
  GLfloat stretch_val_dx, stretch_val_dy;

  GLfloat curx, cury, curz;

  GLfloat rx, ry, rz;
  GLfloat rot, drot, odrot, ddrot, orot;
  float theta, rho, dtheta, drho, gamma, dgamma;

  GLuint texid;
  Bool mipmap_p;
  Bool waiting_for_image_p;
  Bool first_image_p;

  GLfloat anisotropic;

} Screenflip;

static Screenflip *screenflip = NULL;

#include "grab-ximage.h"

static const GLfloat viewer[] = {0.0, 0.0, 15.0};


static void getSnapshot (ModeInfo *);


ENTRYPOINT Bool
screenflip_handle_event (ModeInfo *mi, XEvent *event)
{
  Screenflip *c = &screenflip[MI_SCREEN(mi)];

  if (gltrackball_event_handler (event, c->trackball,
                                 MI_WIDTH (mi), MI_HEIGHT (mi),
                                 &c->button_down_p))
    return True;
  else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
    {
      if (!c->waiting_for_image_p)
        {
          getSnapshot (mi);
          return True;
        }
    }

  return False;
}


/* draw the texture mapped quad (actually two back to back)*/
static void showscreen(Screenflip *c, int frozen, int wire)
{
  GLfloat x, y, w, h;

  if (c->fadetime) {
/*    r -= 0.02; g -= 0.02; b -= 0.02; */
    c->show_colors[3] -= 0.02;
    if (c->show_colors[3] < 0) {
      c->regrab = 1;
      c->fadetime = 0;
    }
  } else if (c->show_colors[3] < 0) {
    c->show_colors[0] = c->show_colors[1] = 
      c->show_colors[2] = c->show_colors[3] = 1;
    c->stretch_val_x = c->stretch_val_y = 
      c->stretch_val_dx = c->stretch_val_dy = 0;
  }
  if (c->stretch_val_dx == 0 && !frozen && !(random() % 25))
    c->stretch_val_dx = (float)(random() % 100) / 5000;
  if (c->stretch_val_dy == 0 && !frozen && !(random() % 25))
    c->stretch_val_dy = (float)(random() % 100) / 5000;
    
  x = c->qx;
  y = c->qy;
  w = c->qx+c->qw;
  h = c->qy-c->qh;

  if (!frozen) {
     w *= sin (c->stretch_val_x) + 1;
     x *= sin (c->stretch_val_x) + 1;
     if (!c->button_down_p) {
     if (!c->fadetime) c->stretch_val_x += c->stretch_val_dx;
     if (c->stretch_val_x > 2*M_PI && !(random() % 5))
       c->stretch_val_dx = (float)(random() % 100) / 5000;
     else
       c->stretch_val_x -= 2*M_PI;
     }

     if (!c->button_down_p && !c->fadetime) c->stretch_val_y += c->stretch_val_dy;
     h *= sin (c->stretch_val_y) / 2 + 1;
     y *= sin (c->stretch_val_y) / 2 + 1;
     if (!c->button_down_p) {
     if (c->stretch_val_y > 2*M_PI && !(random() % 5))
       c->stretch_val_dy = (float)(random() % 100) / 5000;
     else
       c->stretch_val_y -= 2*M_PI;
     }
  }

  glColor4f(c->show_colors[0], c->show_colors[1], 
            c->show_colors[2], c->show_colors[3]);

  if (!wire)
    {
      glEnable(GL_TEXTURE_2D);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glDepthMask(GL_FALSE);
    }

  glBegin(wire ? GL_LINE_LOOP : GL_QUADS);

  glNormal3f(0, 0, 1);
  glTexCoord2f(c->max_tx, c->max_ty); glVertex3f(w, h, 0);
  glTexCoord2f(c->max_tx, c->min_ty); glVertex3f(w, y, 0);
  glTexCoord2f(c->min_tx, c->min_ty); glVertex3f(x, y, 0);
  glTexCoord2f(c->min_tx, c->max_ty); glVertex3f(x, h, 0);

  glNormal3f(0, 0, -1);
  glTexCoord2f(c->min_tx, c->min_ty); glVertex3f(x, y, -0.05);
  glTexCoord2f(c->max_tx, c->min_ty); glVertex3f(w, y, -0.05);
  glTexCoord2f(c->max_tx, c->max_ty); glVertex3f(w, h, -0.05);
  glTexCoord2f(c->min_tx, c->max_ty); glVertex3f(x, h, -0.05);
  glEnd();


  glDisable(GL_TEXTURE_2D);
  glDepthMask(GL_TRUE);

  glBegin(GL_LINE_LOOP);
   glVertex3f(x, y, 0);
   glVertex3f(x, h, 0);
   glVertex3f(w, h, 0);
   glVertex3f(w, y, 0);
 glEnd();
  glDisable(GL_BLEND);

}

/* This function is responsible for 'zooming back' the square after
 * a new chunk has been grabbed with getSnapshot(), and positioning
 * it suitably on the screen. Once positioned (where we begin to rotate),
 * it just does a glTranslatef() and returns 1
 */

static int inposition(Screenflip *c)
{
  GLfloat wx;
  GLfloat wy;
  wx = 0 - (c->qw/2);
  wy = (c->qh/2);

  if (c->curx == 0) c->curx = c->qx;
  if (c->cury == 0) c->cury = c->qy;
  if (c->regrab) {
     c->curz = 0;
     c->curx = c->qx;
     c->cury = c->qy;
     c->regrab = 0;
  }
  if (c->curz > -10 || c->curx > wx + 0.1 || c->curx < wx - 0.1 ||
         c->cury > wy + 0.1 || c->cury < wy - 0.1) {
    if (c->curz > -10)
      c->curz -= 0.05;
    if (c->curx > wx) {
       c->qx -= 0.02;
       c->curx -= 0.02;
    }
    if (c->curx < wx) {
       c->qx += 0.02;
       c->curx += 0.02;
    }
    if (c->cury > wy) {
       c->qy -= 0.02;
       c->cury -= 0.02;
    }
    if (c->cury < wy) {
       c->qy += 0.02;
       c->cury += 0.02;
    }
    glTranslatef(0, 0, c->curz);
    return 0;
  }
  glTranslatef(0, 0, c->curz);
  return 1;

}

#if 0
static void drawgrid(void)
{
  int i;

  glColor3f(0, 0.7, 0);
  glBegin(GL_LINES);
  for (i = 0 ; i <= 50; i+=2) {
      glVertex3f( -25, -15, i-70);
      glVertex3f( 25, -15, i-70);
      glVertex3f( i-25, -15, -70);
      glVertex3f( i-25, -15, -20);
  }
  glEnd();
}
#endif


static void display(Screenflip *c, int wire)
{
  int frozen;
  GLfloat rot = current_device_rotation();

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  glPushMatrix();

/*
  glRotatef(rot, 0, 0, 1);
  if ((rot >  45 && rot <  135) ||
      (rot < -45 && rot > -135))
    {
      GLfloat s = c->winw / (GLfloat) c->winh;
      glScalef (s, 1/s, 1);
    }
*/

  if (inposition(c)) {
    frozen = 0;
    glTranslatef(5 * sin(c->theta), 5 * sin(c->rho), 10 * cos(c->gamma) - 10);
/* randomly change the speed */
    if (!c->button_down_p && !(random() % 300)) {
      if (random() % 2)
        c->drho = 1/60 - (float)(random() % 100)/3000;
      if (random() % 2)
        c->dtheta = 1/60 - (float)(random() % 100)/3000;
      if (random() % 2)
        c->dgamma = 1/60 - (float)(random() % 100)/3000;
    }
    glRotatef(rot, 0, 0, 1);
    gltrackball_rotate (c->trackball);
    glRotatef(-rot, 0, 0, 1);
    if (rotate) glRotatef(c->rot, c->rx, c->ry, c->rz);
/* update variables with each frame */
    if(!c->button_down_p && !c->fadetime) {
      c->theta += c->dtheta;
      c->rho += c->drho;
      c->gamma += c->dgamma;
      c->rot += c->drot;
      c->drot += c->ddrot;
    }
/* dont let our rotation speed get too high */
    if (c->drot > 5 && c->ddrot > 0)
        c->ddrot = 0 - (GLfloat)(random() % 100) / 1000;
    else if (c->drot < -5 && c->ddrot < 0)
        c->ddrot = (GLfloat)(random() % 100) / 1000;
  } else { /* reset some paramaters */
    c->ddrot = 0.05 - (GLfloat)(random() % 100) / 1000;
    c->theta = c->rho = c->gamma = 0;
    c->rot = 0;
    frozen = 1;
  }
  if (!c->button_down_p && !c->fadetime && (c->rot >= 360 || c->rot <= -360) && !(random() % 7)) { /* rotate  change */
    c->rx = (GLfloat)(random() % 100) / 100;
    c->ry = (GLfloat)(random() % 100) / 100;
    c->rz = (GLfloat)(random() % 100) / 100;
  }
  if (c->odrot * c->drot < 0 && c->tw < c->winw && !(random() % 10)) {
    c->fadetime = 1;                /* randomly fade and get new snapshot */
  }
  c->orot = c->rot;
  c->odrot = c->drot;
  if (c->rot > 360 || c->rot < -360) /* dont overflow rotation! */
    c->rot -= c->rot;
  showscreen(c, frozen, wire);
  glPopMatrix();
  glFlush();
}

ENTRYPOINT void reshape_screenflip(ModeInfo *mi, int width, int height)
{
 Screenflip *c = &screenflip[MI_SCREEN(mi)];
 glViewport(0,0,(GLint)width, (GLint) height);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45, 1, 2.0, 85);
 glMatrixMode(GL_MODELVIEW);
 c->winw = width;
 c->winh = height;
}

static void
image_loaded_cb (const char *filename, XRectangle *geometry,
                 int image_width, int image_height, 
                 int texture_width, int texture_height,
                 void *closure)
{
  Screenflip *c = (Screenflip *) closure;

  c->tw = texture_width;
  c->th = texture_height;
  c->min_tx = (GLfloat) geometry->x / c->tw;
  c->min_ty = (GLfloat) geometry->y / c->th;
  c->max_tx = (GLfloat) (geometry->x + geometry->width)  / c->tw;
  c->max_ty = (GLfloat) (geometry->y + geometry->height) / c->th;

  c->qx = -QW/2 + ((GLfloat) geometry->x * QW / image_width);
  c->qy =  QH/2 - ((GLfloat) geometry->y * QH / image_height);
  c->qw =  QW   * ((GLfloat) geometry->width  / image_width);
  c->qh =  QH   * ((GLfloat) geometry->height / image_height);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   (c->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));

  if (c->anisotropic >= 1.0)
    glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 
                     c->anisotropic);

  c->waiting_for_image_p = False;
  c->first_image_p = False;
}


static void getSnapshot (ModeInfo *modeinfo)
{
  Screenflip *c = &screenflip[MI_SCREEN(modeinfo)];

  if (MI_IS_WIREFRAME(modeinfo))
    return;

  c->waiting_for_image_p = True;
  c->mipmap_p = True;
  load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
                      *c->glx_context, 0, 0, c->mipmap_p, c->texid,
                      image_loaded_cb, c);
}

ENTRYPOINT void init_screenflip(ModeInfo *mi)
{
  int screen = MI_SCREEN(mi);
  Screenflip *c;

 MI_INIT(mi, screenflip);
 c = &screenflip[screen];
 c->window = MI_WINDOW(mi);

 c->trackball = gltrackball_init (False);

 if ((c->glx_context = init_GL(mi)) != NULL) {
      reshape_screenflip(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
 } else {
     MI_CLEARWINDOW(mi);
 }
 c->winh = MI_WIN_HEIGHT(mi);
 c->winw = MI_WIN_WIDTH(mi);
 c->qw = QW;
 c->qh = QH;
 c->qx = -6;
 c->qy = 6;

 c->rx = c->ry = 1;
 c->odrot = 1;

 c->show_colors[0] = c->show_colors[1] = 
   c->show_colors[2] = c->show_colors[3] = 1;

 if (! MI_IS_WIREFRAME(mi))
   {
     glShadeModel(GL_SMOOTH);
     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_CULL_FACE);
     glCullFace(GL_BACK);
     glDisable(GL_LIGHTING);
   }

 if (strstr ((char *) glGetString(GL_EXTENSIONS),
             "GL_EXT_texture_filter_anisotropic"))
   glGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &c->anisotropic);
 else
   c->anisotropic = 0.0;

 glGenTextures(1, &c->texid);

 c->first_image_p = True;
 getSnapshot(mi);
}

ENTRYPOINT void draw_screenflip(ModeInfo *mi)
{
  Screenflip *c = &screenflip[MI_SCREEN(mi)];
  Window w = MI_WINDOW(mi);
  Display *disp = MI_DISPLAY(mi);

  if (!c->glx_context)
      return;

  /* Wait for the first image; for subsequent images, load them in the
     background while animating. */
  if (c->waiting_for_image_p && c->first_image_p)
    return;

  glXMakeCurrent(disp, w, *(c->glx_context));

  glBindTexture(GL_TEXTURE_2D, c->texid);

  if (c->regrab)
    getSnapshot(mi);

  display(c, MI_IS_WIREFRAME(mi));

  if(mi->fps_p) do_fps(mi);
  glFinish(); 
  glXSwapBuffers(disp, w);
}

XSCREENSAVER_MODULE_2 ("FlipScreen3D", flipscreen3d, screenflip)

#endif