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.
26 #include "screenhack.h"
41 #define MIN(i,j) ((i)<(j)?(i):(j))
43 #define kmax ((minutes?60:24))
44 /* Anzahl der Kugeln */
46 /* Werte in der Sinus-Tabelle */
47 /*-----------------------------------------------------------------*/
48 #define setink(inkcolor) \
49 XSetForeground (dpy,gc,inkcolor)
51 #define drawline(xx1,yy1,xx2,yy2) \
52 XDrawLine(dpy,win,gc,xx1,yy1,xx2,yy2)
54 #define drawseg(segments,nr_segments) \
55 XDrawSegments(dpy,win,gc,segments,nr_segments)
58 #define polyfill(ppts,pcount) \
59 XFillPolygon(dpy,win,gc,ppts,pcount,Convex,CoordModeOrigin)
62 #define frac(argument) argument-floor(argument)
64 #define abs(x) ((x)<0.0 ? -(x) : (x))
67 /* static XColor gray1; */
68 static double r=1.0,g=1.0,b=1.0;
69 static double hue=0.0,sat=0.0,val=1.0;
78 static kugeldat kugeln[100];
80 static double a[3],/*m[3],*/am[3],x[3],y[3],v[3];
81 static double zoom,speed,zaehler,vspeed/*,AE*/;
82 static double vturn/*,aturn*/;
83 /* static XPoint track[sines]; */
84 static double sinus[sines];
85 static double cosinus[sines];
87 static int startx,starty;
88 static double /*magx,magy,*/mag=10;
89 /* static double lastx,lasty,lastz; */
90 /* static int lastcx,lastcy,lastcz; */
91 /* static int move=1; */
94 static double hsvcycl=0.0;
95 static double movef =0.5, wobber=2.0, cycle=6.0;
99 /* static double sec; */
102 static XWindowAttributes xgwa;
107 /* static Font font; */
109 static int screen, scrnWidth = WIDTH, scrnHeight = HEIGHT;
110 static Pixmap buffer;
112 static int fastch=50;
115 # define sum1ton(a) (((a)*(a)+1)/2)
116 # define fastcw sum1ton(fastch)
117 static Pixmap fastcircles;
118 static Pixmap fastmask;
120 static XImage* fastcircles[maxfast];
121 static XImage* fastmask[maxfast];
123 static int fastdraw=0;
126 static int scrnW2,scrnH2;
127 /* static unsigned short flags = 0; */
128 /* static char *text; */
129 static XColor colors[64];
130 static struct tm *zeit;
138 struct timeval time1;
139 struct timezone zone1;
142 gettimeofday(&time1,&zone1);
143 zeit=localtime(&time1.tv_sec);
145 return (zeit->tm_sec+60*(zeit->tm_min+60*(zeit->tm_hour))
146 + time1.tv_usec*1.0E-6);
149 /* --------------------COLORMAP---------------------*/
152 hsv2rgb (double h, double s, double v, double *r, double *g, double *b)
154 h/=360.0; h=6*(h-floor(h));
172 case 0: *r=v; *g=t; *b=w; break;
173 case 1: *r=u; *g=v; *b=w; break;
174 case 2: *r=w; *g=v; *b=t; break;
175 case 3: *r=w; *g=u; *b=v; break;
176 case 4: *r=t; *g=w; *b=v; break;
177 case 5: *r=v; *g=w; *b=u; break;
181 printf("HSV: %f %f %f to\nRGB: %f %f %f\n",h,s,v,*r,*g,*b);
186 changeColor (double r, double g, double b)
193 colors[n1].red =1023+ n*(int)(1024.*r);
194 colors[n1].blue =1023+ n*(int)(1024.*b);
195 colors[n1].green =1023+ n*(int)(1024.*g);
200 XStoreColors (dpy, cmap, colors, 12);
204 initColor (double r, double g, double b)
207 unsigned long pixels[12];
210 cmap = xgwa.colormap;
212 if(hsvcycl!=0.0 && XAllocColorCells(dpy, cmap, 0, &dummy, 0, pixels, 12))
216 colors[n1].pixel=pixels[n1];
217 colors[n1].flags=DoRed | DoGreen | DoBlue;
227 colors[n1].red =1023+ n*(int)(1024.*r);
228 colors[n1].blue =1023+ n*(int)(1024.*b);
229 colors[n1].green =1023+ n*(int)(1024.*g);
231 if (!(XAllocColor (dpy, cmap, &colors[n1]))) {
232 (void) fprintf (stderr, "Error: Cannot allocate colors\n");
241 /* ----------------WINDOW-------------------*/
250 XGetWindowAttributes (dpy, win, &xgwa);
251 scrnWidth = xgwa.width;
252 scrnHeight = xgwa.height;
254 cycle = 60.0 / get_float_resource ("cycle", "Float");
255 movef = get_float_resource ("move", "Float") / 2;
256 wobber *= get_float_resource ("wobble", "Float");
259 double magfac = get_float_resource ("mag", "Float");
261 fastch=(int)(fastch*magfac);
264 if (get_boolean_resource ("minutes", "Boolean")) {
265 minutes=1; maxk+=60-24;
268 timewait = get_integer_resource ("wait", "Integer");
269 fastch = get_integer_resource ("fast", "Integer");
270 cycl = get_boolean_resource ("colcycle", "Integer");
271 hsvcycl = get_float_resource ("hsvcycle", "Integer");
274 char *s = get_string_resource ("rgb", "RGB");
279 if (3 == sscanf (s, "%lf %lf %lf %c", &rr, &gg, &bb, &dummy))
280 r = rr, g = gg, b = bb;
284 s = get_string_resource ("hsv", "HSV");
288 if (3 == sscanf (s, "%lf %lf %lf", &hh, &ss, &vv, &dummy)) {
289 hue = hh, sat = ss, val = vv;
290 hsv2rgb(hue,sat,val,&r,&g,&b);
300 printf("Set options:\ndisplay: '%s'\ngeometry: '%s'\n",display,geometry);
301 printf("move\t%.2f\nwobber\t%.2f\nmag\t%.2f\ncycle\t%.4f\n",
302 movef,wobber,mag/10,cycle);
303 printf("nice\t%i\nfast\t%i\nmarks\t%i\nwait\t%i\n",niced,fastch,maxk,timewait);
306 xgc=( XGCValues *) malloc(sizeof(XGCValues) );
307 xorgc=( XGCValues *) malloc(sizeof(XGCValues) );
308 xandgc=( XGCValues *) malloc(sizeof(XGCValues) );
310 screen = screen_number (xgwa.screen);
314 gc = XCreateGC (dpy, win, 0, xgc);
315 xorgc->function =GXor;
316 orgc = XCreateGC (dpy, win, GCFunction, xorgc);
317 xandgc->function =GXandInverted;
318 andgc = XCreateGC (dpy, win, GCFunction, xandgc);
320 buffer = XCreatePixmap (dpy, win, scrnWidth, scrnHeight,
324 printf("Time 3D drawing ");
327 puts("fast by Pixmap copy");
329 puts("fast by XImage copy");
337 fastcircles = XCreatePixmap (dpy, win, fastcw, fastch+1, xgwa.depth);
338 fastmask = XCreatePixmap (dpy, win, fastcw, fastch+1, xgwa.depth);
341 setink(BlackPixel (dpy, screen));
342 XFillRectangle (dpy, buffer , gc, 0, 0, scrnWidth, scrnHeight);
347 XFillRectangle (dpy, fastcircles, gc, 0, 0, fastcw, fastch+1);
348 XFillRectangle (dpy, fastmask , gc, 0, 0, fastcw, fastch+1);
353 static void fill_kugel(int i, Pixmap buf, int setcol);
356 /*------------------------------------------------------------------*/
364 for(i=0; i<fastch; i++)
367 kugeln[i].r1=-((double) i)/2 -1;
368 kugeln[i].x1=sum1ton(i);
369 kugeln[i].y1=((double) i)/2 +1;
371 fill_kugel(i,fastcircles,1);
372 setink((1<<MIN(24,xgwa.depth))-1);
373 fill_kugel(i,fastmask,0);
375 kugeln[i].r1=-((double) i)/2 -1;
376 kugeln[i].x1=kugeln[i].y1=((double) i)/2 +1;
378 fill_kugel(i,buffer,1);
379 fastcircles[i]=XGetImage(dpy,buffer,0,0,i+2,i+2,(1<<planes)-1,ZPixmap);
381 setink((1<<MIN(24,xgwa.depth))-1);
382 fill_kugel(i,buffer,0);
383 fastmask[i]=XGetImage(dpy,buffer,0,0,i+2,i+2,(1<<planes)-1,ZPixmap);
386 XFillRectangle (dpy, buffer , gc, 0, 0, scrnWidth, scrnHeight);
393 /* Zeiger zeichnen */
396 zeiger(double dist,double rad, double z, double sec, int *q)
399 double gratio=sqrt(2.0/(1.0+sqrt(5.0)));
405 kugeln[n].x=dist*cos(sec);
406 kugeln[n].y=-dist*sin(sec);
417 /*-----------------------------------------------------------------*
419 *-----------------------------------------------------------------*/
424 double i,l,/*xs,*/ys,zs,mod;
425 double /*persec,*/sec,min,hour;
428 sec=TWOPI*modf(k/60,&mod);
429 min=TWOPI*modf(k/3600,&mod);
430 hour=TWOPI*modf(k/43200,&mod);
432 l=TWOPI*modf(k/300,&mod);
437 kugeln[n].x=4.0*sin(i);
438 kugeln[n].y=4.0*cos(i);
439 kugeln[n].z=wobber* /* (sin(floor(2+2*l/(PI))*i)*sin(2*l)); */
440 cos((i-sec)*floor(2+5*l/(PI)))*sin(5*l);
443 kugeln[n].r=/* (1.0+0.3*cos(floor(2+2*l/(PI))*i)*sin(2*l))* */
444 ((n % 5!=0) ? 0.3 : 0.6)*
445 ((n % 15 ==0) ? 1.25 : .75);
449 kugeln[n].r=/* (1.0+0.3*cos(floor(2+2*l/(PI))*i)*sin(2*l))* */
450 ((n & 1) ? 0.5 : 1.0)*
451 ((n % 6==0) ? 1.25 : .75);
459 kugeln[n].r=2.0+cos(TWOPI*modf(k,&mod))/2;
462 zeiger(2.0,0.75,-2.0,sec,&n);
463 zeiger(1.0,1.0,-1.5,min,&n);
464 zeiger(0.0,1.5,-1.0,hour,&n);
468 ys=kugeln[n].y*cos(movef*sin(cycle*sec))+
469 kugeln[n].z*sin(movef*sin(cycle*sec));
470 zs=-kugeln[n].y*sin(movef*sin(cycle*sec))+
471 kugeln[n].z*cos(movef*sin(cycle*sec));
476 /*------------------------------------------------------------------*/
478 t3d_sort(int l, int r)
488 while(kugeln[i].d>x) i++;
489 while(x>kugeln[j].d) j--;
492 ex=kugeln[i];kugeln[i]=kugeln[j];kugeln[j]=ex;
497 if (l<j) t3d_sort(l,j);
498 if (i<r) t3d_sort (i,r);
501 /*------------------------------------------------------------------*/
503 fill_kugel(int i, Pixmap buf, int setcol)
506 int m,col,inc=1,inr=3,d;
507 d=(int)((abs(kugeln[i].r1)*2));
511 if(fastdraw && d<fastch)
514 XCopyArea(dpy, fastmask, buf, andgc, sum1ton(d)-(d+1)/2, 1,d,d,
515 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2);
516 XCopyArea(dpy, fastcircles, buf, orgc, sum1ton(d)-(d+1)/2, 1,d,d,
517 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2);
519 XPutImage(dpy, buf, andgc, fastmask[d-1], 0, 0,
520 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2, d, d);
521 XPutImage(dpy, buf, orgc, fastcircles[d-1], 0, 0,
522 (int)(kugeln[i].x1)-d/2, (int)(kugeln[i].y1)-d/2, d, d);
528 if(abs(kugeln[i].r1)<6.0) inr=9;
530 for (m=0;m<=28;m+=inr)
532 ra=kugeln[i].r1*sqrt(1-m*m/(28.0*28.0));
534 printf("Radius: %f\n",ra);
537 else if(-ra< 6.0) inc=8;
538 else if(-ra<20.0) inc=4;
539 else if(-ra<40.0) inc=2;
545 if (col>33) col=33; col/=3;
546 setink(colors[col].pixel);
552 for (n=0,nr=0;n<=sines-1;n+=inc,nr++)
554 track[nr].x=kugeln[i].x1+(int)(ra*sinus[n])+(kugeln[i].r1-ra)/2;
555 track[nr].y=kugeln[i].y1+(int)(ra*cosinus[n])+(kugeln[i].r1-ra)/2;
557 XFillPolygon(dpy,buf,gc,track,nr,Convex,CoordModeOrigin);
559 #else /* Use XFillArc */
560 XFillArc(dpy, buf, gc,
561 (int)(kugeln[i].x1+(kugeln[i].r1+ra)/2),
562 (int)(kugeln[i].y1+(kugeln[i].r1+ra)/2),
563 (int)-(2*ra+1), (int)-(2*ra+1), 0, 360*64);
569 /*------------------------------------------------------------------*/
593 for (i=0.0;n<sines;i+=TWOPI/sines,n++)
599 /*------------------------------------------------------------------*/
603 vektorprodukt(double feld1[], double feld2[], double feld3[])
605 feld3[0]=feld1[1]*feld2[2]-feld1[2]*feld2[1];
606 feld3[1]=feld1[2]*feld2[0]-feld1[0]*feld2[2];
607 feld3[2]=feld1[0]*feld2[1]-feld1[1]*feld2[0];
609 /*------------------------------------------------------------------*/
611 turn(double feld1[], double feld2[], double winkel)
614 double s,ca,sa,sx1,sx2,sx3;
616 vektorprodukt(feld1,feld2,temp);
618 s=feld1[0]*feld2[0]+feld1[1]*feld2[1]+feld1[2]*feld2[2];
623 sa=sin(winkel);ca=cos(winkel);
624 feld1[0]=ca*(feld1[0]-sx1)+sa*temp[0]+sx1;
625 feld1[1]=ca*(feld1[1]-sx2)+sa*temp[1]+sx2;
626 feld1[2]=ca*(feld1[2]-sx3)+sa*temp[2]+sx3;
628 /*------------------------------------------------------------------*/
630 /* 1: Blickrichtung v;3:Ebenenmittelpunkt m
631 double feld1[],feld3[]; */
639 zaehler=norm*norm*zoom;
641 /*------------------------------------------------------------------*/
645 double c1[3],c2[3],k[3],x1,y1;
646 double cno,cnorm/*,magnit*/;
651 c1[0]=kugeln[i].x-a[0];
652 c1[1]=kugeln[i].y-a[1];
653 c1[2]=kugeln[i].z-a[2];
654 cnorm=sqrt(c1[0]*c1[0]+c1[1]*c1[1]+c1[2]*c1[2]);
660 cno=c2[0]*v[0]+c2[1]*v[1]+c2[2]*v[2];
662 if (cno<0) kugeln[i].d=-20.0;
665 kugeln[i].r1=(mag*zoom*kugeln[i].r/cnorm);
671 vektorprodukt(c2,c1,k);
674 x1=(startx+(x[0]*k[0]+x[1]*k[1]+x[2]*k[2])*mag);
675 y1=(starty-(y[0]*k[0]+y[1]*k[1]+y[2]*k[2])*mag);
677 && (x1<scrnWidth+2000.0)
679 && (y1<scrnHeight+2000.0))
681 kugeln[i].x1=(int)x1;
682 kugeln[i].y1=(int)y1;
693 /*---------- event-handler ----------------*/
697 while (XEventsQueued (dpy, QueuedAfterReading))
701 XNextEvent (dpy, &event);
704 case ConfigureNotify:
705 if (event.xconfigure.width != scrnWidth ||
706 event.xconfigure.height != scrnHeight)
708 XFreePixmap (dpy, buffer);
709 scrnWidth = event.xconfigure.width;
710 scrnHeight = event.xconfigure.height;
711 buffer = XCreatePixmap (dpy, win, scrnWidth, scrnHeight,
721 KeySym kpr=XKeycodeToKeysym(dpy,event.xkey.keycode,0);
722 if (kpr=='s') /* s */
730 /* printf("%i\n",event.xkey.keycode);*/
731 if (kpr=='z') mag*=1.02;
732 if (kpr=='x') mag/=1.02;
740 struct timeval timeout;
741 timeout.tv_sec=timewait/1000000;
742 timeout.tv_usec=timewait%1000000;
743 (void)select(0,0,0,0,&timeout);
748 /*-------------------------------------------------*/
750 char *progclass = "T3D";
752 char *defaults [] = {
753 ".background: black",
754 ".foreground: white",
767 XrmOptionDescRec options [] = {
768 { "-move", ".move", XrmoptionSepArg, 0 },
769 { "-wobble", ".wobble", XrmoptionSepArg, 0 },
770 { "-cycle", ".cycle", XrmoptionSepArg, 0 },
771 { "-mag", ".mag", XrmoptionSepArg, 0 },
772 { "-minutes", ".minutes", XrmoptionSepArg, 0 },
773 { "-timewait", ".timewait", XrmoptionSepArg, 0 },
774 { "-fast", ".fast", XrmoptionSepArg, 0 },
775 { "-colcycle", ".colcycle", XrmoptionSepArg, 0 },
776 { "-hsvcycle", ".hsvcycle", XrmoptionSepArg, 0 },
777 { "-rgb", ".rgb", XrmoptionSepArg, 0 },
778 { "-hsv", ".hsv", XrmoptionSepArg, 0 },
784 screenhack (Display *d, Window w)
786 Window junk_win,in_win;
788 int px,py,junk,kb/*,wai*/;
789 /* int act,act1,tc;*/
791 /* double var=0.0; */
792 int color=0/*, dir=1*/;
800 zeit=(struct tm *)malloc(sizeof(struct tm));
810 vektorprodukt(x,y,v);
813 setink (BlackPixel (dpy, screen));
814 XFillRectangle (dpy, win, gc, 0, 0, scrnWidth, scrnHeight);
815 XQueryPointer (dpy, win, &junk_win, &junk_win, &junk, &junk,
821 /*--------------- Zeichenteil --------------*/
825 vektorprodukt(x,y,v);
827 vnorm=sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
828 v[0]=v[0]*norm/vnorm;
829 v[1]=v[1]*norm/vnorm;
830 v[2]=v[2]*norm/vnorm;
831 vnorm=sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
832 x[0]=x[0]*norm/vnorm;
833 x[1]=x[1]*norm/vnorm;
834 x[2]=x[2]*norm/vnorm;
835 vnorm=sqrt(y[0]*y[0]+y[1]*y[1]+y[2]*y[2]);
836 y[0]=y[0]*norm/vnorm;
837 y[1]=y[1]*norm/vnorm;
838 y[2]=y[2]*norm/vnorm;
847 color=(int)(64.0*(dtime/60-floor(dtime/60)))-32;
852 setink(colors[color/3].pixel);
855 setink(BlackPixel (dpy, screen));
857 XFillRectangle(dpy,buffer,gc,0,0,scrnWidth,scrnHeight);
867 fill_kugel(i,buffer,1);
873 /* manipulate(gettime());
875 if (var>=TWOPI) var=PI/500; */
881 dtime=hsvcycl*dtime/10.0+hue/360.0;
882 dtime=360*(dtime-floor(dtime));
884 hsv2rgb(dtime,sat,val,&r,&g,&b);
888 XCopyArea (dpy, buffer, win, gc, 0, 0, scrnWidth, scrnHeight, 0, 0);
891 /*-------------------------------------------------*/
896 (void)(XQueryPointer (dpy, win, &junk_win, &in_win, &junk, &junk,
899 if ((px>0)&&(px<scrnWidth)&&(py>0)&&(py<scrnHeight) )
901 if ((px !=startx)&&(kb&Button2Mask))
903 /* printf("y=(%f,%f,%f)",y[0],y[1],y[2]);*/
904 turn(y,x,((double)(px-startx))/(8000*mag));
905 /* printf("->(%f,%f,%f)\n",y[0],y[1],y[2]);*/
907 if ((py !=starty)&&(kb&Button2Mask))
909 /* printf("x=(%f,%f,%f)",x[0],x[1],x[2]);*/
910 turn(x,y,((double)(py-starty))/(-8000*mag));
911 /* printf("->(%f,%f,%f)\n",x[0],x[1],x[2]);*/
913 if ((kb&Button1Mask))
915 if (vturn==0.0) vturn=.005; else if (vturn<2) vturn+=.01;
916 turn(x,v,.002*vturn);
917 turn(y,v,.002*vturn);
919 if ((kb&Button3Mask))
921 if (vturn==0.0) vturn=.005; else if (vturn<2) vturn+=.01;
922 turn(x,v,-.002*vturn);
923 turn(y,v,-.002*vturn);
926 if (!(kb&Button1Mask)&&!(kb&Button3Mask))
929 speed=speed+speed*vspeed;
930 if ((speed<0.0000001) &&(vspeed>0.000001)) speed=0.000001;
932 if (speed>0.01) speed=.01;
933 a[0]=a[0]+speed*v[0];
934 a[1]=a[1]+speed*v[1];
935 a[2]=a[2]+speed*v[2];