http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / hacks / glx / blinkbox.c
1 /* blinkbox, Copyright (c) 2003 Jeremy English <jenglish@myself.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or
9  * implied warranty.
10  */
11
12 /* motion blur added March 2005 by John Boero <jlboero@cs.uwm.edu>
13  */
14
15 #define DEFAULTS        "*delay:        30000            \n" \
16                         "*wireframe:    False            \n" \
17
18 # define refresh_ball 0
19 # define release_ball 0
20 # define ball_handle_event 0
21 #undef countof
22 #define countof(x) (sizeof((x))/sizeof((*x)))
23
24 #include "xlockmore.h"
25 #include "sphere.h"
26 #include <ctype.h>
27
28 #ifdef USE_GL /* whole file */
29
30 #define MAX_COUNT 20
31 #define ALPHA_AMT 0.05
32
33 /* this should be between 1 and 8 */
34 #define DEF_BOXSIZE  "2"
35 #define DEF_DISSOLVE "False"
36 #define DEF_FADE     "True"
37 #define DEF_BLUR     "True"
38
39
40 typedef struct{
41   GLfloat x,y,z;
42 } Tdpos;
43
44 typedef struct{
45   int hit;
46   Tdpos pos;
47   int counter;
48   GLfloat color[3];
49   GLfloat rot[4];
50   int des_count;
51   int alpha_count;
52 }Side;
53
54 struct Bounding_box {
55   Tdpos top;
56   Tdpos bottom;
57 };
58
59 struct Ball {
60   GLfloat x;
61   GLfloat y;
62   GLfloat z;
63   int d;
64 };
65
66 struct bscale {
67   GLfloat wh; /*width Height*/
68   GLfloat d; /*depth*/
69 };
70
71 static const struct Bounding_box bbox = {{14,14,20},{-14,-14,-20}};
72
73 typedef struct {
74   GLXContext *glx_context;
75
76   struct Ball ball;
77
78   struct bscale bscale;
79
80   Tdpos mo;  /*motion*/
81   Tdpos moh; /*hold motion value*/
82
83   Tdpos bpos;
84
85   GLuint ballList;
86   GLuint boxList;
87   GLfloat des_amt;
88
89   /*sides*/
90   Side lside;/*Red*/
91   Side rside;/*Green*/
92   Side tside;/*Blue*/
93   Side bside;/*Orange*/
94   Side fside;/*Yellow*/
95   Side aside;/*Purple*/
96   Side *sp;
97
98 } blinkboxstruct;
99
100 static blinkboxstruct *blinkbox = (blinkboxstruct *) NULL;
101
102
103 /* lights */
104 static const float LightDiffuse[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
105 static const float LightPosition[]=  { 20.0f, 100.0f, 20.0f, 1.0f };
106
107 static Bool do_dissolve;
108 static Bool do_fade;
109 static Bool do_blur;
110 static float bscale_wh;
111
112 static XrmOptionDescRec opts[] = {
113   { "-boxsize",  ".boxsize",  XrmoptionSepArg, 0      },
114   { "-dissolve", ".dissolve", XrmoptionNoArg, "True"  },
115   { "+dissolve", ".dissolve", XrmoptionNoArg, "False" },
116   { "-fade",     ".fade",     XrmoptionNoArg, "True"  },
117   { "+fade",     ".fade",     XrmoptionNoArg, "False" },
118   { "-blur",     ".blur",     XrmoptionNoArg, "True"  },
119   { "+blur",     ".blur",     XrmoptionNoArg, "False" }
120
121 };
122
123 static argtype vars[] = {
124   {&bscale_wh,   "boxsize",   "Boxsize",  DEF_BOXSIZE,  t_Float},
125   {&do_dissolve, "dissolve",  "Dissolve", DEF_DISSOLVE, t_Bool},
126   {&do_fade,     "fade",      "Fade",     DEF_FADE,     t_Bool},
127   {&do_blur,     "blur",      "Blur",     DEF_BLUR,     t_Bool},
128 };
129
130 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
131
132 static void
133 swap(GLfloat *a, GLfloat *b)
134 {
135   GLfloat t = *a;
136   *a = *b;
137   *b = t;
138 }
139
140 static float
141 get_rand(void)
142 {
143   GLfloat j = 1+(random() % 2);
144   return (j);
145 }
146
147 static void
148 swap_mov(GLfloat *a, GLfloat *b)
149 {
150   int j;
151   swap(a,b);
152   j = get_rand();
153   if (*a < 0)
154     *a = j * -1;
155   else
156     *a = j;
157 }
158
159 static void
160 cp_b_pos(blinkboxstruct *bp, Tdpos *s_pos)
161 {
162   s_pos->x = bp->ball.x;
163   s_pos->y = bp->ball.y;
164   s_pos->z = bp->ball.z;
165 }
166
167 static void
168 hit_side(blinkboxstruct *bp)
169 {
170   if ((bp->ball.x - bp->ball.d) <= bbox.bottom.x){
171     bp->lside.hit = 1;
172     bp->lside.counter   = MAX_COUNT;
173     bp->lside.des_count = 1;
174     bp->lside.alpha_count = 0;
175     cp_b_pos(bp, &bp->lside.pos);
176     swap_mov(&bp->mo.x,&bp->moh.x);
177   }else
178   if ((bp->ball.x + bp->ball.d) >= bbox.top.x){
179     bp->rside.hit = 1;
180     bp->rside.counter = MAX_COUNT;
181     bp->rside.des_count = 1;
182     bp->rside.alpha_count = 0;
183     cp_b_pos(bp, &bp->rside.pos);
184     swap_mov(&bp->mo.x,&bp->moh.x);
185   }
186 }
187
188 static void
189 hit_top_bottom(blinkboxstruct *bp)
190 {
191   if ((bp->ball.y - bp->ball.d) <= bbox.bottom.y){
192     bp->bside.hit = 1;
193     bp->bside.counter = MAX_COUNT;
194     bp->bside.des_count = 1;
195     bp->bside.alpha_count = 0;
196     cp_b_pos(bp, &bp->bside.pos);
197     swap_mov(&bp->mo.y,&bp->moh.y);
198   }else
199   if ((bp->ball.y + bp->ball.d) >= bbox.top.y){
200     bp->tside.hit = 1;
201     bp->tside.counter = MAX_COUNT;
202     bp->tside.des_count = 1;
203     bp->tside.alpha_count = 0;
204     cp_b_pos(bp, &bp->tside.pos);
205     swap_mov(&bp->mo.y,&bp->moh.y);
206   }
207 }
208
209 static void
210 hit_front_back(blinkboxstruct *bp)
211 {
212   if ((bp->ball.z - bp->ball.d) <= bbox.bottom.z){
213     bp->aside.hit = 1;
214     bp->aside.counter = MAX_COUNT;
215     bp->aside.des_count = 1;
216     bp->aside.alpha_count = 0;
217     cp_b_pos(bp, &bp->aside.pos);
218     swap_mov(&bp->mo.z,&bp->moh.z);
219   }else
220   if((bp->ball.z + bp->ball.d) >= bbox.top.z){
221     bp->fside.hit = 1;
222     bp->fside.counter = MAX_COUNT;
223     bp->fside.des_count = 1;
224     bp->fside.alpha_count = 0;
225     cp_b_pos(bp, &bp->fside.pos);
226     swap_mov(&bp->mo.z,&bp->moh.z);
227   }
228 }
229
230 ENTRYPOINT void
231 reshape_ball (ModeInfo *mi, int width, int height)
232 {
233   GLfloat h = (GLfloat) height / (GLfloat) width;
234
235   glViewport (0, 0, (GLint) width, (GLint) height);
236   glMatrixMode(GL_PROJECTION);
237   glLoadIdentity();
238   gluPerspective (30.0, 1/h, 1.0, 100.0);
239
240   glMatrixMode(GL_MODELVIEW);
241   glLoadIdentity();
242   gluLookAt( 0.0, 0.0, 40.0,
243              0.0, 0.0, 0.0,
244              0.0, 2.0,  10.0);
245
246 }
247
248 static void
249 unit_cube(int wire)
250 {
251   glBegin((wire)?GL_LINE_LOOP:GL_QUADS);
252   glNormal3f( 0.0f, -1.0f, 0.0f);
253   glVertex3f(-1.0f, -1.0f, -1.0f);
254   glVertex3f( 1.0f, -1.0f, -1.0f);
255   glVertex3f( 1.0f, -1.0f,  1.0f);
256   glVertex3f(-1.0f, -1.0f,  1.0f);
257   glNormal3f( 0.0f,  0.0f,  1.0f);
258   glVertex3f(-1.0f, -1.0f,  1.0f);
259   glVertex3f( 1.0f, -1.0f,  1.0f);
260   glVertex3f( 1.0f,  1.0f,  1.0f);
261   glVertex3f(-1.0f,  1.0f,  1.0f);
262   glNormal3f( 0.0f,  0.0f, -1.0f);
263   glVertex3f(-1.0f, -1.0f, -1.0f);
264   glVertex3f(-1.0f,  1.0f, -1.0f);
265   glVertex3f( 1.0f,  1.0f, -1.0f);
266   glVertex3f( 1.0f, -1.0f, -1.0f);
267   glNormal3f( 1.0f,  0.0f,  0.0f);
268   glVertex3f( 1.0f, -1.0f, -1.0f);
269   glVertex3f( 1.0f,  1.0f, -1.0f);
270   glVertex3f( 1.0f,  1.0f,  1.0f);
271   glVertex3f( 1.0f, -1.0f,  1.0f);
272   glNormal3f( -1.0f, 0.0f,  0.0f);
273   glVertex3f(-1.0f, -1.0f, -1.0f);
274   glVertex3f(-1.0f, -1.0f,  1.0f);
275   glVertex3f(-1.0f,  1.0f,  1.0f);
276   glVertex3f(-1.0f,  1.0f, -1.0f);
277   glNormal3f( 1.0f,  1.0f,  0.0f);
278   glVertex3f(-1.0f,  1.0f, -1.0f);
279   glVertex3f(-1.0f,  1.0f,  1.0f);
280   glVertex3f( 1.0f,  1.0f,  1.0f);
281   glVertex3f( 1.0f,  1.0f, -1.0f);
282   glEnd();
283 }
284
285 ENTRYPOINT void
286 init_ball (ModeInfo *mi)
287 {
288   int wire = MI_IS_WIREFRAME(mi);
289   blinkboxstruct *bp;
290   
291   if(blinkbox == NULL) {
292     if((blinkbox = (blinkboxstruct *) calloc(MI_NUM_SCREENS(mi),
293                                              sizeof (blinkboxstruct))) == NULL)
294       return;
295   }
296   bp = &blinkbox[MI_SCREEN(mi)];
297
298   if ((bp->glx_context = init_GL(mi)) != NULL) {
299     reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
300     glDrawBuffer(GL_BACK);
301   }
302   else
303     MI_CLEARWINDOW(mi);
304
305   bp->ball.d = 1;
306   bp->bscale.wh = bscale_wh;
307   bp->bscale.d = 0.25;
308
309   bp->mo.x = 1;
310   bp->mo.y = 1;
311   bp->mo.z = 1;
312
313   bp->moh.x = -1.0;
314   bp->moh.y = -1.5;
315   bp->moh.z = -1.5;
316
317   bp->bpos.x = 1;
318   bp->bpos.y = 1;
319   bp->bpos.z = 1;
320
321   bp->des_amt = 1;
322
323   bp->lside.counter = MAX_COUNT;
324   bp->rside.counter = MAX_COUNT;
325   bp->tside.counter = MAX_COUNT;
326   bp->bside.counter = MAX_COUNT;
327   bp->fside.counter = MAX_COUNT;
328   bp->aside.counter = MAX_COUNT;
329
330   bp->lside.color[0] = 1;
331   bp->rside.color[1] = 1;
332   bp->tside.color[2] = 1;
333
334   bp->bside.color[0] = 1;
335   bp->bside.color[1] = 0.5;
336
337   bp->fside.color[0] = 1;
338   bp->fside.color[1] = 1;
339
340   bp->aside.color[0] = 0.5;
341   bp->aside.color[2] = 1;
342
343   bp->lside.rot[0] = 90;
344   bp->rside.rot[0] = 90;
345   bp->tside.rot[0] = 90;
346   bp->bside.rot[0] = 90;
347   bp->fside.rot[0] = 90;
348   bp->aside.rot[0] = 90;
349
350   bp->lside.rot[2] = 1;
351   bp->rside.rot[2] = 1;
352   bp->tside.rot[1] = 1;
353   bp->bside.rot[1] = 1;
354   bp->fside.rot[3] = 1;
355   bp->aside.rot[3] = 1;
356
357   bp->lside.des_count = 1;
358   bp->rside.des_count = 1;
359   bp->tside.des_count = 1;
360   bp->bside.des_count = 1;
361   bp->fside.des_count = 1;
362   bp->aside.des_count = 1;
363
364   bp->lside.alpha_count = 1;
365   bp->rside.alpha_count = 1;
366   bp->tside.alpha_count = 1;
367   bp->bside.alpha_count = 1;
368   bp->fside.alpha_count = 1;
369   bp->aside.alpha_count = 1;
370
371
372 #define SPHERE_SLICES 12  /* how densely to render spheres */
373 #define SPHERE_STACKS 16
374
375   bp->sp = malloc(sizeof(*bp->sp));
376   if(bp->sp == NULL){
377     fprintf(stderr,"Could not allocate memory\n");
378     exit(1);
379   }
380   if( (bp->bscale.wh < 1) ||
381       (bp->bscale.wh > 8) ) {
382     fprintf(stderr,"Boxsize out of range. Using default\n");
383     bp->bscale.wh = 2;
384   }
385   if (do_dissolve){
386     bp->des_amt = bp->bscale.wh / MAX_COUNT;
387   }
388
389   reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
390   bp->ballList = glGenLists(1);
391   glNewList(bp->ballList, GL_COMPILE);
392   unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
393   glEndList ();
394
395   bp->boxList = glGenLists(1);
396   glNewList(bp->boxList, GL_COMPILE);
397   unit_cube(wire);
398   glEndList();
399
400   if (wire) return;
401
402   glEnable(GL_COLOR_MATERIAL);
403   glShadeModel(GL_SMOOTH);
404   glClearDepth(1.0f);
405   glEnable(GL_DEPTH_TEST);
406   glDepthFunc(GL_LEQUAL);
407   glEnable(GL_LIGHTING);
408   glClearDepth(1);
409   glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
410   glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
411   glEnable(GL_LIGHT1);
412   if (do_fade || do_blur) {
413     glEnable(GL_BLEND);
414     glDisable(GL_DEPTH_TEST);
415   }
416 }
417
418 static void
419 CheckBoxPos(blinkboxstruct *bp, 
420             GLfloat bot_x, GLfloat top_x, GLfloat bot_y, GLfloat top_y)
421 {
422   /*Make sure it's inside of the bounding box*/
423   bp->bpos.x = ((bp->bpos.x - bp->bscale.wh) < bot_x) ? bot_x + bp->bscale.wh : bp->bpos.x;
424   bp->bpos.x = ((bp->bpos.x + bp->bscale.wh) > top_x) ? top_x - bp->bscale.wh : bp->bpos.x;
425   bp->bpos.y = ((bp->bpos.y - bp->bscale.wh) < bot_y) ? bot_y + bp->bscale.wh : bp->bpos.y;
426   bp->bpos.y = ((bp->bpos.y + bp->bscale.wh) > top_y) ? top_y - bp->bscale.wh : bp->bpos.y;
427 }
428
429 ENTRYPOINT void
430 draw_ball (ModeInfo *mi)
431 {
432    blinkboxstruct *bp = &blinkbox[MI_SCREEN(mi)];
433
434    Display *dpy = MI_DISPLAY(mi);
435    Window window = MI_WINDOW(mi);
436    int i = 0;
437
438    if (! bp->glx_context)
439      return;
440    mi->polygon_count = 0;
441    glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
442
443    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
444
445    hit_top_bottom(bp);
446    hit_front_back(bp);
447    hit_side(bp);
448
449    glRotated(0.25,0,0,1);
450    glRotated(0.25,0,1,0);
451    glRotated(0.25,1,0,0);
452
453
454    glPushMatrix();
455    glScalef(0.5,0.5,0.5);
456
457    glColor3f(1,1,1);
458    glPushMatrix();
459
460    if (!do_blur || MI_IS_WIREFRAME(mi)) {
461      glTranslatef(bp->ball.x += bp->mo.x,
462                   bp->ball.y += bp->mo.y,
463                   bp->ball.z += bp->mo.z);
464
465      glScalef(2,2,2);
466      glCallList(bp->ballList);
467      mi->polygon_count += SPHERE_SLICES*SPHERE_STACKS;
468
469    } else {
470
471 #    define blur_detail 24.0
472      float ball_alpha = 1 / blur_detail;
473
474      glBlendFunc(GL_SRC_ALPHA,GL_ONE);
475      glTranslatef(bp->ball.x, bp->ball.y, bp->ball.z);
476    
477      for (i = 0; i < blur_detail; ++i) {
478        glTranslatef(bp->mo.x / blur_detail,
479                     bp->mo.y / blur_detail,
480                     bp->mo.z / blur_detail);
481
482        /* comment the following line for quick but boring linear blur */
483        ball_alpha = sin((M_PI / blur_detail) * i) / blur_detail;
484      
485        glColor4f(1, 1, 1, ball_alpha);
486
487        glScalef(2, 2, 2);
488        glCallList(bp->ballList);
489        mi->polygon_count += SPHERE_SLICES*SPHERE_STACKS;
490        glScalef(.5, .5, .5);
491      }
492      i = 0;
493    
494      bp->ball.x += bp->mo.x;
495      bp->ball.y += bp->mo.y;
496      bp->ball.z += bp->mo.z;
497    }
498    
499    glPopMatrix();
500
501    while(i < 6){
502     switch(i){
503       case 0:{
504                bp->sp = &bp->lside;
505                bp->bpos.x = bp->lside.pos.z*-1;
506                bp->bpos.y = bp->lside.pos.y;
507                bp->bpos.z = bbox.bottom.x - bp->bscale.d;
508                if (bp->sp->hit)
509                 CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
510                break;
511              }
512       case 1:{
513                bp->sp = &bp->rside;
514                bp->bpos.x = bp->rside.pos.z*-1;
515                bp->bpos.y = bp->rside.pos.y;
516                bp->bpos.z = bbox.top.x + bp->bscale.d;
517                if (bp->sp->hit)
518                 CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
519                break;
520              }
521       case 2:{
522                bp->sp = &bp->tside;
523                bp->bpos.x = bp->tside.pos.x;
524                bp->bpos.y = bp->tside.pos.z;
525                bp->bpos.z = bbox.bottom.y - bp->bscale.d;
526                if (bp->sp->hit)
527                 CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
528                break;
529              }
530       case 3:{
531                bp->sp = &bp->bside;
532                bp->bpos.x = bp->bside.pos.x;
533                bp->bpos.y = bp->bside.pos.z;
534                bp->bpos.z = bbox.top.y + bp->bscale.d;
535                if (bp->sp->hit)
536                 CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
537                break;
538              }
539       case 4:{
540                bp->sp = &bp->fside;
541                bp->bpos.x = bp->fside.pos.y;
542                bp->bpos.y = bp->fside.pos.x*-1;
543                bp->bpos.z = bbox.top.z + bp->bscale.d;
544                if (bp->sp->hit)
545                 CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
546                break;
547              }
548       case 5:{
549                bp->sp = &bp->aside;
550                bp->bpos.x = bp->aside.pos.y;
551                bp->bpos.y = bp->aside.pos.x*-1;
552                bp->bpos.z = bbox.bottom.z + bp->bscale.d;
553                if (bp->sp->hit)
554                 CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
555                break;
556              }
557     }
558     if(bp->sp->hit){
559       if(do_fade){
560         glColor4f(bp->sp->color[0],bp->sp->color[1],bp->sp->color[2],1-(ALPHA_AMT * bp->sp->alpha_count));
561       }else{
562         glColor3fv(bp->sp->color);
563       }
564       glBlendFunc(GL_SRC_ALPHA,GL_ONE);
565       glPushMatrix();
566       glRotatef(bp->sp->rot[0],bp->sp->rot[1],bp->sp->rot[2],bp->sp->rot[3]);
567       glTranslatef(bp->bpos.x,bp->bpos.y,bp->bpos.z);
568       if (do_dissolve) {
569          glScalef(bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.d);
570       }else{
571         glScalef(bp->bscale.wh,bp->bscale.wh,bp->bscale.d);
572       }
573       glCallList(bp->boxList);
574       mi->polygon_count += 6;
575       glPopMatrix();
576       bp->sp->counter--;
577       bp->sp->des_count++;
578       bp->sp->alpha_count++;
579       if(!bp->sp->counter)
580       {
581         bp->sp->hit = 0;
582       }
583     }
584     i++;
585   }
586
587
588    glPopMatrix();
589   if (mi->fps_p) do_fps (mi);
590    glFinish();
591    glXSwapBuffers(dpy, window);
592
593 }
594
595 XSCREENSAVER_MODULE_2 ("BlinkBox", blinkbox, ball)
596
597 #endif /* USE_GL */