1 /* t3d -- Flying Balls Clock Demo
2 by Bernd Paysan , paysan@informatik.tu-muenchen.de
4 Copy, modify, and distribute T3D either under GPL version 2 or newer,
5 or under the standard MIT/X license notice.
7 partly based on flying balls demo by Georg Acher,
8 acher@informatik.tu-muenchen.de
9 (developed on HP9000/720 (55 MIPS,20 MFLOPS) )
10 NO warranty at all ! Complaints to /dev/null !
12 4-Jan-99 jwz@jwz.org -- adapted to xscreensaver framework, to take advantage
13 of the command-line options provided by screenhack.c.
22 #include <time.h> /* for localtime() and gettimeofday() */
24 #include "screenhack.h"
39 #define MIN(i,j) ((i)<(j)?(i):(j))
41 #define kmax ((minutes?60:24))
42 /* Anzahl der Kugeln */
44 /* Werte in der Sinus-Tabelle */
45 /*-----------------------------------------------------------------*/
46 #define setink(inkcolor) \
47 XSetForeground (dpy,gc,inkcolor)
49 #define drawline(xx1,yy1,xx2,yy2) \
50 XDrawLine(dpy,win,gc,xx1,yy1,xx2,yy2)
52 #define drawseg(segments,nr_segments) \
53 XDrawSegments(dpy,win,gc,segments,nr_segments)
56 #define polyfill(ppts,pcount) \
57 XFillPolygon(dpy,win,gc,ppts,pcount,Convex,CoordModeOrigin)
60 #define frac(argument) argument-floor(argument)
62 #define abs(x) ((x)<0.0 ? -(x) : (x))
65 /* static XColor gray1; */
66 static double r=1.0,g=1.0,b=1.0;
67 static double hue=0.0,sat=0.0,val=1.0;
76 static kugeldat kugeln[100];
78 static double a[3],/*m[3],*/am[3],x[3],y[3],v[3];
79 static double zoom,speed,zaehler,vspeed/*,AE*/;
80 static double vturn/*,aturn*/;
81 /* static XPoint track[sines]; */
82 static double sinus[sines];
83 static double cosinus[sines];
85 static int startx,starty;
86 static double /*magx,magy,*/mag=10;
87 /* static double lastx,lasty,lastz; */
88 /* static int lastcx,lastcy,lastcz; */
89 /* static int move=1; */
92 static double hsvcycl=0.0;
93 static double movef =0.5, wobber=2.0, cycle=6.0;
97 /* static double sec; */
100 static XWindowAttributes xgwa;
105 /* static Font font; */
107 static int screen, scrnWidth = WIDTH, scrnHeight = HEIGHT;
108 static Pixmap buffer;
110 static int fastch=50;
113 # define sum1ton(a) (((a)*(a)+1)/2)
114 # define fastcw sum1ton(fastch)
115 static Pixmap fastcircles;
116 static Pixmap fastmask;
118 static XImage* fastcircles[maxfast];
119 static XImage* fastmask[maxfast];
121 static int fastdraw=0;
124 static int scrnW2,scrnH2;
125 /* static unsigned short flags = 0; */
126 /* static char *text; */
127 static XColor colors[64];
128 static struct tm *zeit;
136 struct timeval time1;
140 #ifdef GETTIMEOFDAY_TWO_ARGS
141 struct timezone zone1;
142 gettimeofday(&time1,&zone1);
144 gettimeofday(&time1);
146 lt = time1.tv_sec; /* avoid type cast lossage */
149 return (zeit->tm_sec+60*(zeit->tm_min+60*(zeit->tm_hour))
150 + time1.tv_usec*1.0E-6);
153 /* --------------------COLORMAP---------------------*/
156 hsv2rgb (double h, double s, double v, double *r, double *g, double *b)
158 h/=360.0; h=6*(h-floor(h));
176 case 0: *r=v; *g=t; *b=w; break;
177 case 1: *r=u; *g=v; *b=w; break;
178 case 2: *r=w; *g=v; *b=t; break;
179 case 3: *r=w; *g=u; *b=v; break;
180 case 4: *r=t; *g=w; *b=v; break;
181 case 5: *r=v; *g=w; *b=u; break;
185 printf("HSV: %f %f %f to\nRGB: %f %f %f\n",h,s,v,*r,*g,*b);
190 changeColor (double r, double g, double b)
197 colors[n1].red =1023+ n*(int)(1024.*r);
198 colors[n1].blue =1023+ n*(int)(1024.*b);
199 colors[n1].green =1023+ n*(int)(1024.*g);
204 XStoreColors (dpy, cmap, colors, 12);
208 initColor (double r, double g, double b)
211 unsigned long pixels[12];
214 cmap = xgwa.colormap;
216 if(hsvcycl!=0.0 && XAllocColorCells(dpy, cmap, 0, &dummy, 0, pixels, 12))
220 colors[n1].pixel=pixels[n1];
221 colors[n1].flags=DoRed | DoGreen | DoBlue;
231 colors[n1].red =1023+ n*(int)(1024.*r);
232 colors[n1].blue =1023+ n*(int)(1024.*b);
233 colors[n1].green =1023+ n*(int)(1024.*g);
235 if (!(XAllocColor (dpy, cmap, &colors[n1]))) {
236 (void) fprintf (stderr, "Error: Cannot allocate colors\n");
245 /* ----------------WINDOW-------------------*/
254 XGetWindowAttributes (dpy, win, &xgwa);
255 scrnWidth = xgwa.width;
256 scrnHeight = xgwa.height;
259 float f = get_float_resource ("cycle", "Float");
260 if (f <= 0 || f > 60) f = 6.0;
263 movef = get_float_resource ("move", "Float");
264 wobber = get_float_resource ("wobble", "Float");
267 double magfac = get_float_resource ("mag", "Float");
269 fastch=(int)(fastch*magfac);
272 if (get_boolean_resource ("minutes", "Boolean")) {
273 minutes=1; maxk+=60-24;
276 timewait = get_integer_resource ("delay", "Integer");
277 fastch = get_integer_resource ("fast", "Integer");
278 cycl = get_boolean_resource ("colcycle", "Integer");
279 hsvcycl = get_float_resource ("hsvcycle", "Integer");
282 char *s = get_string_resource ("rgb", "RGB");
287 if (3 == sscanf (s, "%lf %lf %lf %c", &rr, &gg, &bb, &dummy))
288 r = rr, g = gg, b = bb;
292 s = get_string_resource ("hsv", "HSV");
296 if (3 == sscanf (s, "%lf %lf %lf %c", &hh, &ss, &vv, &dummy)) {
297 hue = hh, sat = ss, val = vv;
298 hsv2rgb(hue,sat,val,&r,&g,&b);
307 xgc=( XGCValues *) malloc(sizeof(XGCValues) );
308 xorgc=( XGCValues *) malloc(sizeof(XGCValues) );
309 xandgc=( XGCValues *) malloc(sizeof(XGCValues) );
311 screen = screen_number (xgwa.screen);
315 gc = XCreateGC (dpy, win, 0, xgc);
316 xorgc->function =GXor;
317 orgc = XCreateGC (dpy, win, GCFunction, xorgc);
318 xandgc->function =GXandInverted;
319 andgc = XCreateGC (dpy, win, GCFunction, xandgc);
321 buffer = XCreatePixmap (dpy, win, scrnWidth, scrnHeight,
325 printf("Time 3D drawing ");
328 puts("fast by Pixmap copy");
330 puts("fast by XImage copy");
338 fastcircles = XCreatePixmap (dpy, win, fastcw, fastch+1, xgwa.depth);
339 fastmask = XCreatePixmap (dpy, win, fastcw, fastch+1, xgwa.depth);
342 setink(BlackPixel (dpy, screen));
343 XFillRectangle (dpy, buffer , gc, 0, 0, scrnWidth, scrnHeight);
348 XFillRectangle (dpy, fastcircles, gc, 0, 0, fastcw, fastch+1);
349 XFillRectangle (dpy, fastmask , gc, 0, 0, fastcw, fastch+1);
354 printf("move\t%.2f\nwobber\t%.2f\nmag\t%.2f\ncycle\t%.4f\n",
355 movef,wobber,mag/10,cycle);
356 printf("fast\t%i\nmarks\t%i\nwait\t%i\n",fastch,maxk,timewait);
361 static void fill_kugel(int i, Pixmap buf, int setcol);
364 /*------------------------------------------------------------------*/
372 for(i=0; i<fastch; i++)
375 kugeln[i].r1=-((double) i)/2 -1;
376 kugeln[i].x1=sum1ton(i);
377 kugeln[i].y1=((double) i)/2 +1;
379 fill_kugel(i,fastcircles,1);
380 setink((1<<MIN(24,xgwa.depth))-1);
381 fill_kugel(i,fastmask,0);
383 kugeln[i].r1=-((double) i)/2 -1;
384 kugeln[i].x1=kugeln[i].y1=((double) i)/2 +1;
386 fill_kugel(i,buffer,1);
387 fastcircles[i]=XGetImage(dpy,buffer,0,0,i+2,i+2,(1<<planes)-1,ZPixmap);
389 setink((1<<MIN(24,xgwa.depth))-1);
390 fill_kugel(i,buffer,0);
391 fastmask[i]=XGetImage(dpy,buffer,0,0,i+2,i+2,(1<<planes)-1,ZPixmap);
394 XFillRectangle (dpy, buffer , gc, 0, 0, scrnWidth, scrnHeight);
401 /* Zeiger zeichnen */
404 zeiger(double dist,double rad, double z, double sec, int *q)
407 double gratio=sqrt(2.0/(1.0+sqrt(5.0)));
413 kugeln[n].x=dist*cos(sec);
414 kugeln[n].y=-dist*sin(sec);
425 /*-----------------------------------------------------------------*
427 *-----------------------------------------------------------------*/
432 double i,l,/*xs,*/ys,zs,mod;
433 double /*persec,*/sec,min,hour;
436 sec=TWOPI*modf(k/60,&mod);
437 min=TWOPI*modf(k/3600,&mod);
438 hour=TWOPI*modf(k/43200,&mod);
440 l=TWOPI*modf(k/300,&mod);
445 kugeln[n].x=4.0*sin(i);
446 kugeln[n].y=4.0*cos(i);
447 kugeln[n].z=wobber* /* (sin(floor(2+2*l/(PI))*i)*sin(2*l)); */
448 cos((i-sec)*floor(2+5*l/(PI)))*sin(5*l);
451 kugeln[n].r=/* (1.0+0.3*cos(floor(2+2*l/(PI))*i)*sin(2*l))* */
452 ((n % 5!=0) ? 0.3 : 0.6)*
453 ((n % 15 ==0) ? 1.25 : .75);
457 kugeln[n].r=/* (1.0+0.3*cos(floor(2+2*l/(PI))*i)*sin(2*l))* */
458 ((n & 1) ? 0.5 : 1.0)*
459 ((n % 6==0) ? 1.25 : .75);
467 kugeln[n].r=2.0+cos(TWOPI*modf(k,&mod))/2;
470 zeiger(2.0,0.75,-2.0,sec,&n);
471 zeiger(1.0,1.0,-1.5,min,&n);
472 zeiger(0.0,1.5,-1.0,hour,&n);
476 ys=kugeln[n].y*cos(movef*sin(cycle*sec))+
477 kugeln[n].z*sin(movef*sin(cycle*sec));
478 zs=-kugeln[n].y*sin(movef*sin(cycle*sec))+
479 kugeln[n].z*cos(movef*sin(cycle*sec));
484 /*------------------------------------------------------------------*/
486 t3d_sort(int l, int r)
496 while(kugeln[i].d>x) i++;
497 while(x>kugeln[j].d) j--;
500 ex=kugeln[i];kugeln[i]=kugeln[j];kugeln[j]=ex;
505 if (l<j) t3d_sort(l,j);
506 if (i<r) t3d_sort (i,r);
509 /*------------------------------------------------------------------*/
511 fill_kugel(int i, Pixmap buf, int setcol)
514 int m,col,inc=1,inr=3,d;
515 d=(int)((abs(kugeln[i].r1)*2));
519 if(fastdraw && d<fastch)
522 XCopyArea(dpy, fastmask, buf, andgc, sum1ton(d)-(d+1)/2, 1,d,d,
523 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2);
524 XCopyArea(dpy, fastcircles, buf, orgc, sum1ton(d)-(d+1)/2, 1,d,d,
525 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2);
527 XPutImage(dpy, buf, andgc, fastmask[d-1], 0, 0,
528 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2, d, d);
529 XPutImage(dpy, buf, orgc, fastcircles[d-1], 0, 0,
530 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2, d, d);
536 if(abs(kugeln[i].r1)<6.0) inr=9;
538 for (m=0;m<=28;m+=inr)
540 ra=kugeln[i].r1*sqrt(1-m*m/(28.0*28.0));
542 printf("Radius: %f\n",ra);
545 else if(-ra< 6.0) inc=8;
546 else if(-ra<20.0) inc=4;
547 else if(-ra<40.0) inc=2;
553 if (col>33) col=33; col/=3;
554 setink(colors[col].pixel);
560 for (n=0,nr=0;n<=sines-1;n+=inc,nr++)
562 track[nr].x=kugeln[i].x1+(int)(ra*sinus[n])+(kugeln[i].r1-ra)/2;
563 track[nr].y=kugeln[i].y1+(int)(ra*cosinus[n])+(kugeln[i].r1-ra)/2;
565 XFillPolygon(dpy,buf,gc,track,nr,Convex,CoordModeOrigin);
567 #else /* Use XFillArc */
568 XFillArc(dpy, buf, gc,
569 (int)(kugeln[i].x1+(kugeln[i].r1+ra)/2),
570 (int)(kugeln[i].y1+(kugeln[i].r1+ra)/2),
571 (int)-(2*ra+1), (int)-(2*ra+1), 0, 360*64);
577 /*------------------------------------------------------------------*/
601 for (i=0.0;n<sines;i+=TWOPI/sines,n++)
607 /*------------------------------------------------------------------*/
611 vektorprodukt(double feld1[], double feld2[], double feld3[])
613 feld3[0]=feld1[1]*feld2[2]-feld1[2]*feld2[1];
614 feld3[1]=feld1[2]*feld2[0]-feld1[0]*feld2[2];
615 feld3[2]=feld1[0]*feld2[1]-feld1[1]*feld2[0];
617 /*------------------------------------------------------------------*/
619 turn(double feld1[], double feld2[], double winkel)
622 double s,ca,sa,sx1,sx2,sx3;
624 vektorprodukt(feld1,feld2,temp);
626 s=feld1[0]*feld2[0]+feld1[1]*feld2[1]+feld1[2]*feld2[2];
631 sa=sin(winkel);ca=cos(winkel);
632 feld1[0]=ca*(feld1[0]-sx1)+sa*temp[0]+sx1;
633 feld1[1]=ca*(feld1[1]-sx2)+sa*temp[1]+sx2;
634 feld1[2]=ca*(feld1[2]-sx3)+sa*temp[2]+sx3;
636 /*------------------------------------------------------------------*/
638 /* 1: Blickrichtung v;3:Ebenenmittelpunkt m
639 double feld1[],feld3[]; */
647 zaehler=norm*norm*zoom;
649 /*------------------------------------------------------------------*/
653 double c1[3],c2[3],k[3],x1,y1;
654 double cno,cnorm/*,magnit*/;
659 c1[0]=kugeln[i].x-a[0];
660 c1[1]=kugeln[i].y-a[1];
661 c1[2]=kugeln[i].z-a[2];
662 cnorm=sqrt(c1[0]*c1[0]+c1[1]*c1[1]+c1[2]*c1[2]);
668 cno=c2[0]*v[0]+c2[1]*v[1]+c2[2]*v[2];
670 if (cno<0) kugeln[i].d=-20.0;
673 kugeln[i].r1=(mag*zoom*kugeln[i].r/cnorm);
679 vektorprodukt(c2,c1,k);
682 x1=(startx+(x[0]*k[0]+x[1]*k[1]+x[2]*k[2])*mag);
683 y1=(starty-(y[0]*k[0]+y[1]*k[1]+y[2]*k[2])*mag);
685 && (x1<scrnWidth+2000.0)
687 && (y1<scrnHeight+2000.0))
689 kugeln[i].x1=(int)x1;
690 kugeln[i].y1=(int)y1;
701 /*---------- event-handler ----------------*/
705 while (XEventsQueued (dpy, QueuedAfterReading))
709 XNextEvent (dpy, &event);
712 case ConfigureNotify:
713 if (event.xconfigure.width != scrnWidth ||
714 event.xconfigure.height != scrnHeight)
716 XFreePixmap (dpy, buffer);
717 scrnWidth = event.xconfigure.width;
718 scrnHeight = event.xconfigure.height;
719 buffer = XCreatePixmap (dpy, win, scrnWidth, scrnHeight,
731 KeySym kpr=XKeycodeToKeysym(dpy,event.xkey.keycode,0);
756 screenhack_handle_event (dpy, &event);
761 case ButtonPress: case ButtonRelease:
765 screenhack_handle_event (dpy, &event);
771 struct timeval timeout;
772 timeout.tv_sec=timewait/1000000;
773 timeout.tv_usec=timewait%1000000;
774 (void)select(0,0,0,0,&timeout);
779 /*-------------------------------------------------*/
781 char *progclass = "T3D";
783 char *defaults [] = {
784 ".background: black",
785 ".foreground: white",
798 XrmOptionDescRec options [] = {
799 { "-move", ".move", XrmoptionSepArg, 0 },
800 { "-wobble", ".wobble", XrmoptionSepArg, 0 },
801 { "-cycle", ".cycle", XrmoptionSepArg, 0 },
802 { "-mag", ".mag", XrmoptionSepArg, 0 },
803 { "-minutes", ".minutes", XrmoptionSepArg, 0 },
804 { "-delay", ".delay", XrmoptionSepArg, 0 },
805 { "-fast", ".fast", XrmoptionSepArg, 0 },
806 { "-colcycle", ".colcycle", XrmoptionSepArg, 0 },
807 { "-hsvcycle", ".hsvcycle", XrmoptionSepArg, 0 },
808 { "-rgb", ".rgb", XrmoptionSepArg, 0 },
809 { "-hsv", ".hsv", XrmoptionSepArg, 0 },
815 screenhack (Display *d, Window w)
817 Window junk_win,in_win;
819 int px,py,junk/*,wai*/;
821 /* int act,act1,tc;*/
823 /* double var=0.0; */
824 int color=0/*, dir=1*/;
832 zeit=(struct tm *)malloc(sizeof(struct tm));
842 vektorprodukt(x,y,v);
845 setink (BlackPixel (dpy, screen));
846 XFillRectangle (dpy, win, gc, 0, 0, scrnWidth, scrnHeight);
847 XQueryPointer (dpy, win, &junk_win, &junk_win, &junk, &junk,
853 /*--------------- Zeichenteil --------------*/
857 vektorprodukt(x,y,v);
859 vnorm=sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
860 v[0]=v[0]*norm/vnorm;
861 v[1]=v[1]*norm/vnorm;
862 v[2]=v[2]*norm/vnorm;
863 vnorm=sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
864 x[0]=x[0]*norm/vnorm;
865 x[1]=x[1]*norm/vnorm;
866 x[2]=x[2]*norm/vnorm;
867 vnorm=sqrt(y[0]*y[0]+y[1]*y[1]+y[2]*y[2]);
868 y[0]=y[0]*norm/vnorm;
869 y[1]=y[1]*norm/vnorm;
870 y[2]=y[2]*norm/vnorm;
879 color=(int)(64.0*(dtime/60-floor(dtime/60)))-32;
884 setink(colors[color/3].pixel);
887 setink(BlackPixel (dpy, screen));
889 XFillRectangle(dpy,buffer,gc,0,0,scrnWidth,scrnHeight);
899 fill_kugel(i,buffer,1);
905 /* manipulate(gettime());
907 if (var>=TWOPI) var=PI/500; */
913 dtime=hsvcycl*dtime/10.0+hue/360.0;
914 dtime=360*(dtime-floor(dtime));
916 hsv2rgb(dtime,sat,val,&r,&g,&b);
920 XCopyArea (dpy, buffer, win, gc, 0, 0, scrnWidth, scrnHeight, 0, 0);
923 /*-------------------------------------------------*/
928 (void)(XQueryPointer (dpy, win, &junk_win, &in_win, &junk, &junk,
931 if ((px>0)&&(px<scrnWidth)&&(py>0)&&(py<scrnHeight) )
933 if ((px !=startx)&&(kb&Button2Mask))
935 /* printf("y=(%f,%f,%f)",y[0],y[1],y[2]);*/
936 turn(y,x,((double)(px-startx))/(8000*mag));
937 /* printf("->(%f,%f,%f)\n",y[0],y[1],y[2]);*/
939 if ((py !=starty)&&(kb&Button2Mask))
941 /* printf("x=(%f,%f,%f)",x[0],x[1],x[2]);*/
942 turn(x,y,((double)(py-starty))/(-8000*mag));
943 /* printf("->(%f,%f,%f)\n",x[0],x[1],x[2]);*/
945 if ((kb&Button1Mask))
947 if (vturn==0.0) vturn=.005; else if (vturn<2) vturn+=.01;
948 turn(x,v,.002*vturn);
949 turn(y,v,.002*vturn);
951 if ((kb&Button3Mask))
953 if (vturn==0.0) vturn=.005; else if (vturn<2) vturn+=.01;
954 turn(x,v,-.002*vturn);
955 turn(y,v,-.002*vturn);
958 if (!(kb&Button1Mask)&&!(kb&Button3Mask))
961 speed=speed+speed*vspeed;
962 if ((speed<0.0000001) &&(vspeed>0.000001)) speed=0.000001;
964 if (speed>0.01) speed=.01;
965 a[0]=a[0]+speed*v[0];
966 a[1]=a[1]+speed*v[1];
967 a[2]=a[2]+speed*v[2];