From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / t3d.c
1 /* t3d -- Flying Balls Clock Demo
2    by Bernd Paysan , paysan@informatik.tu-muenchen.de
3
4    Copy, modify, and distribute T3D either under GPL  version 2 or newer, 
5    or under the standard MIT/X license notice.
6
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 !
11
12   4-Jan-99 jwz@jwz.org -- adapted to xscreensaver framework, to take advantage
13                           of the command-line options provided by screenhack.c.
14 */
15
16 #ifndef HAVE_JWXYZ
17 # define FASTDRAW
18 # define FASTCOPY
19 #endif /* !HAVE_JWXYZ */
20
21 #include <stdio.h>
22 #include <math.h>
23 #include <time.h> /* for localtime() and gettimeofday() */
24
25 #include "screenhack.h"
26
27
28 #define   WIDTH      200
29 #define   HEIGHT     200
30 #define   norm       20.0
31
32 #define   ROOT       0x1
33 #define PI M_PI
34 #define TWOPI 2*M_PI
35
36 #define MIN(i,j) ((i)<(j)?(i):(j))
37
38 #define kmax ((st->minutes?60:24))
39 /* Anzahl der Kugeln */
40 #define sines 52
41 /* Werte in der Sinus-Tabelle */
42 /*-----------------------------------------------------------------*/
43 #define setink(inkcolor) \
44         XSetForeground (st->dpy,st->gc,inkcolor)
45
46 #define drawline(xx1,yy1,xx2,yy2) \
47         XDrawLine(st->dpy,st->win,st->gc,xx1,yy1,xx2,yy2) 
48
49 #define drawseg(segments,nr_segments) \
50         XDrawSegments(st->dpy,st->win,st->gc,segments,nr_segments) 
51
52
53 #define polyfill(ppts,pcount) \
54         XFillPolygon(st->dpy,st->win,st->gc,ppts,pcount,Convex,CoordModeOrigin)
55
56
57 #define frac(argument) argument-floor(argument)
58
59 #undef ABS
60 #define ABS(x) ((x)<0.0 ? -(x) : (x))
61
62 typedef struct {
63   double x,y,z,r,d,r1;
64   int x1,y1;
65 } kugeldat;
66
67 /* Felder fuer 3D */
68
69
70 struct state {
71   Display *dpy;
72   Window window;
73
74   int maxk;
75   int timewait;
76
77   Colormap cmap;
78   double r,g,b;
79   double hue,sat,val;
80
81   kugeldat kugeln[100];
82
83   double  a[3],am[3],x[3],y[3],v[3];
84   double zoom,speed,zaehler,vspeed;
85   double vturn;
86   double sinus[sines];
87   double cosinus[sines];
88
89   int startx,starty;
90   double mag;
91   int minutes;
92   int cycl;
93   double hsvcycl;
94   double movef, wobber, cycle;
95
96   XWindowAttributes xgwa;
97   GC    gc;
98   GC orgc;
99   GC andgc;
100   int   scrnWidth, scrnHeight;
101   Pixmap  buffer;
102   int fastch;
103
104   int scrnW2,scrnH2;
105   XColor colors[64];
106   struct tm *zeit;
107
108   int planes;
109
110   Window junk_win,in_win;
111   int px,py,junk;
112   unsigned int kb;
113
114   int fastdraw;
115   int draw_color;
116
117 # ifdef FASTDRAW
118 #  ifdef FASTCOPY
119 #   define sum1ton(a) (((a)*(a)+1)/2)
120 #   define fastcw sum1ton(st->fastch)
121     Pixmap fastcircles;
122     Pixmap fastmask;
123 #  else /* !FASTCOPY */
124     XImage* fastcircles[maxfast];
125     XImage* fastmask[maxfast];
126 #  endif /* !FASTCOPY */
127 # endif /* FASTDRAW */
128 };
129
130
131
132 #define maxfast 100
133
134 /* compute time */
135
136 static double
137 gettime (struct state *st)
138 {
139   struct timeval time1;
140   struct tm *zeit;
141   time_t lt;
142
143 #ifdef GETTIMEOFDAY_TWO_ARGS
144   struct timezone zone1;
145   gettimeofday(&time1,&zone1);
146 #else
147   gettimeofday(&time1);
148 #endif
149   lt = time1.tv_sec;    /* avoid type cast lossage */
150   zeit=localtime(&lt);
151   
152   return (zeit->tm_sec+60*(zeit->tm_min+60*(zeit->tm_hour))
153           + time1.tv_usec*1.0E-6);
154 }
155
156 /* --------------------COLORMAP---------------------*/ 
157
158 static void
159 hsv2rgb (double h, double s, double v, double *r, double *g, double *b)
160 {
161   h/=360.0;     h=6*(h-floor(h));
162
163   if(s==0.0)
164     {
165       *r=*g=*b=v;
166     }
167   else
168     {   int i=(int)h;
169         double t,u,w;
170         
171         h=h-floor(h);
172         
173         u=v*(s*(1.0-h));
174         w=v*(1.0-s);
175         t=v*(s*h+1.0-s);
176         
177         switch(i)
178           {
179           case 0:       *r=v;   *g=t;   *b=w;   break;
180           case 1:       *r=u;   *g=v;   *b=w;   break;
181           case 2:       *r=w;   *g=v;   *b=t;   break;
182           case 3:       *r=w;   *g=u;   *b=v;   break;
183           case 4:       *r=t;   *g=w;   *b=v;   break;
184           case 5:       *r=v;   *g=w;   *b=u;   break;
185           }
186       }
187 #ifdef PRTDBX
188   printf("HSV: %f %f %f to\nRGB: %f %f %f\n",h,s,v,*r,*g,*b);
189 #endif
190 }
191
192 static void
193 changeColor (struct state *st, double r, double g, double b)
194 {
195   int n,n1;
196   
197   n1=0;
198   for(n=30;n<64;n+=3)
199     {
200       st->colors[n1].red   =1023+ n*(int)(1024.*r);
201       st->colors[n1].blue  =1023+ n*(int)(1024.*b);
202       st->colors[n1].green =1023+ n*(int)(1024.*g);
203       
204       n1++;
205     }
206   
207   XStoreColors (st->dpy, st->cmap, st->colors, 12);
208 }
209
210 static void 
211 initColor (struct state *st, double r, double g, double b)
212 {
213   int n,n1;
214   unsigned long pixels[12];
215   unsigned long dummy;
216   
217   st->cmap = st->xgwa.colormap;
218   
219   if(st->hsvcycl!=0.0 && XAllocColorCells(st->dpy, st->cmap, 0, &dummy, 0, pixels, 12))
220     {
221       for(n1=0;n1<12;n1++)
222         {
223           st->colors[n1].pixel=pixels[n1];
224           st->colors[n1].flags=DoRed | DoGreen | DoBlue;
225         }
226       
227       changeColor(st,r,g,b);
228     }
229   else
230     {
231       n1=0;
232       for(n=30;n<64;n+=3)
233         {
234           st->colors[n1].red   =1023+ n*(int)(1024.*r);
235           st->colors[n1].blue  =1023+ n*(int)(1024.*b);
236           st->colors[n1].green =1023+ n*(int)(1024.*g);
237           
238           if (!(XAllocColor (st->dpy, st->cmap, &st->colors[n1]))) {
239             fprintf (stderr, "Error:  Cannot allocate colors\n");
240             exit (1);
241           }
242           
243           n1++;
244         }
245     }
246 }
247
248 /* ----------------WINDOW-------------------*/
249
250 static void
251 initialize (struct state *st)
252 {
253   XGCValues *xgc;
254   XGCValues *xorgc;
255   XGCValues *xandgc;
256
257   st->maxk=34;
258   st->r = st->g = st->b = 1;
259   st->hue = st->sat = 0;
260   st->val = 1;
261   st->mag = 10;
262   st->movef = 0.5;
263   st->wobber = 2;
264   st->cycle = 6;
265   st->scrnWidth = WIDTH;
266   st->scrnHeight = HEIGHT;
267
268
269   XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
270   st->scrnWidth = st->xgwa.width;
271   st->scrnHeight = st->xgwa.height;
272
273   {
274     float f = get_float_resource (st->dpy, "cycle", "Float");
275     if (f <= 0 || f > 60) f = 6.0;
276     st->cycle = 60.0 / f;
277   }
278   st->movef = get_float_resource (st->dpy, "move", "Float");
279   st->wobber = get_float_resource (st->dpy, "wobble", "Float");
280
281   {
282     double magfac = get_float_resource (st->dpy, "mag", "Float");
283     st->mag *= magfac;
284     st->fastch=(int)(st->fastch*magfac);
285   }
286
287   if (get_boolean_resource (st->dpy, "minutes", "Boolean")) {
288     st->minutes=1; st->maxk+=60-24;
289   }
290
291   st->timewait = get_integer_resource (st->dpy, "delay", "Integer");
292   st->fastch = get_integer_resource (st->dpy, "fast", "Integer");
293   st->cycl = get_boolean_resource (st->dpy, "colcycle", "Integer");
294   st->hsvcycl = get_float_resource (st->dpy, "hsvcycle", "Integer");
295
296   {
297     char *s = get_string_resource (st->dpy, "rgb", "RGB");
298     char dummy;
299     if (s && *s)
300       {
301         double rr, gg, bb;
302         if (3 == sscanf (s, "%lf %lf %lf %c", &rr, &gg, &bb, &dummy))
303           st->r = rr, st->g = gg, st->b = bb;
304       }
305     if (s) free (s);
306
307     s = get_string_resource (st->dpy, "hsv", "HSV");
308     if (s && *s)
309       {
310         double hh, ss, vv;
311         if (3 == sscanf (s, "%lf %lf %lf %c", &hh, &ss, &vv, &dummy)) {
312           st->hue = hh, st->sat = ss, st->val = vv;
313           hsv2rgb(st->hue,st->sat,st->val,&st->r,&st->g,&st->b);
314         }
315       }
316     if (s) free (s);
317   }
318
319   if (st->fastch > maxfast)
320     st->fastch=maxfast;
321   
322   xgc=( XGCValues *) malloc(sizeof(XGCValues) );
323   xorgc=( XGCValues *) malloc(sizeof(XGCValues) );
324   xandgc=( XGCValues *) malloc(sizeof(XGCValues) );
325
326   st->planes=st->xgwa.depth;
327
328 #ifdef HAVE_JWXYZ
329 # define GXandInverted GXcopy  /* #### this can't be right, right? */
330 #endif
331  st->gc = XCreateGC (st->dpy, st->window, 0,  xgc);
332   xorgc->function =GXor;
333   st->orgc = XCreateGC (st->dpy, st->window, GCFunction,  xorgc);
334   xandgc->function =GXandInverted;
335   st->andgc = XCreateGC (st->dpy, st->window, GCFunction,  xandgc);
336   
337   st->buffer = XCreatePixmap (st->dpy, st->window, st->scrnWidth, st->scrnHeight,
338                           st->xgwa.depth); 
339   
340 #ifdef DEBUG
341   printf("Time 3D drawing ");
342 #ifdef FASTDRAW
343 #       ifdef FASTCOPY
344   puts("fast by Pixmap copy");
345 #       else
346   puts("fast by XImage copy");
347 #       endif
348 #else
349   puts("slow");
350 #endif
351 #endif /* DEBUG */
352   
353 #ifdef FASTCOPY
354   st->fastcircles = XCreatePixmap (st->dpy, st->window, fastcw, st->fastch+1, st->xgwa.depth);
355   st->fastmask    = XCreatePixmap (st->dpy, st->window, fastcw, st->fastch+1, st->xgwa.depth);
356 #endif
357   
358   setink(BlackPixelOfScreen (st->xgwa.screen));
359   XFillRectangle (st->dpy, st->buffer     , st->gc, 0, 0, st->scrnWidth, st->scrnHeight);       
360   
361 #ifdef FASTCOPY
362   
363   setink(0);
364   XFillRectangle (st->dpy, st->fastcircles, st->gc, 0, 0, fastcw, st->fastch+1);
365   XFillRectangle (st->dpy, st->fastmask   , st->gc, 0, 0, fastcw, st->fastch+1);
366   
367 #endif
368
369 #ifdef PRTDBX
370   printf("move\t%.2f\nwobber\t%.2f\nmag\t%.2f\ncycle\t%.4f\n",
371          st->movef,st->wobber,st->mag/10,st->cycle);
372   printf("fast\t%i\nmarks\t%i\nwait\t%i\n",st->fastch,st->maxk,st->timewait);
373 #endif
374  
375 }
376
377 static void fill_kugel(struct state *st, int i, Pixmap buf, int setcol);
378
379
380 /*------------------------------------------------------------------*/
381 static void 
382 init_kugel(struct state *st)
383 {
384   
385 #ifdef FASTDRAW
386   int i;
387
388   for(i=0; i<st->fastch; i++)
389     {
390 #       ifdef FASTCOPY
391       st->kugeln[i].r1=-((double) i)/2 -1;
392       st->kugeln[i].x1=sum1ton(i);
393       st->kugeln[i].y1=((double) i)/2 +1;
394       
395       fill_kugel(st,i,st->fastcircles,1);
396       setink((1<<MIN(24,st->xgwa.depth))-1);
397       fill_kugel(st,i,st->fastmask,0);
398 #       else
399       st->kugeln[i].r1=-((double) i)/2 -1;
400       st->kugeln[i].x1=st->kugeln[i].y1=((double) i)/2 +1;
401       
402       fill_kugel(i,st->buffer,1);
403       st->fastcircles[i]=XGetImage(st->dpy,st->buffer,0,0,i+2,i+2,(1<<st->planes)-1,ZPixmap);
404       
405       setink((1<<MIN(24,st->xgwa.depth))-1);
406       fill_kugel(i,st->buffer,0);
407       st->fastmask[i]=XGetImage(st->dpy,st->buffer,0,0,i+2,i+2,(1<<st->planes)-1,ZPixmap);
408       
409       setink(0);
410       XFillRectangle (st->dpy, st->buffer     , st->gc, 0, 0, st->scrnWidth, st->scrnHeight);   
411 #       endif
412     }
413   st->fastdraw=1;
414 #endif
415 }
416
417 /* Zeiger zeichnen */
418
419 static void
420 zeiger(struct state *st, double dist,double rad, double z, double sec, int *q)
421 {
422   int i,n;
423   double gratio=sqrt(2.0/(1.0+sqrt(5.0)));
424   
425   n = *q;
426   
427   for(i=0;i<3;i++)
428     {
429       st->kugeln[n].x=dist*cos(sec);
430       st->kugeln[n].y=-dist*sin(sec);
431       st->kugeln[n].z=z;
432       st->kugeln[n].r=rad;
433       n++;
434
435       dist += rad;
436       rad = rad*gratio;
437     }
438   *q = n;
439 }
440
441 /*-----------------------------------------------------------------*
442  *                           Uhr zeichnen                          *
443  *-----------------------------------------------------------------*/
444
445 static void
446 manipulate(struct state *st, double k)
447 {
448   double i,l,/*xs,*/ys,zs,mod;
449   double /*persec,*/sec,min,hour;
450   int n;
451   
452   sec=TWOPI*modf(k/60,&mod);
453   min=TWOPI*modf(k/3600,&mod);
454   hour=TWOPI*modf(k/43200,&mod);
455   
456   l=TWOPI*modf(k/300,&mod);
457   i=0.0;
458   for (n=0;n<kmax;n++)
459     {
460       
461       st->kugeln[n].x=4.0*sin(i);
462       st->kugeln[n].y=4.0*cos(i);
463       st->kugeln[n].z=st->wobber* /* (sin(floor(2+2*l/(PI))*i)*sin(2*l)); */
464         cos((i-sec)*floor(2+5*l/(PI)))*sin(5*l);
465       if(st->minutes)
466         {
467           st->kugeln[n].r=/* (1.0+0.3*cos(floor(2+2*l/(PI))*i)*sin(2*l))* */
468             ((n % 5!=0) ? 0.3 : 0.6)*
469               ((n % 15 ==0) ? 1.25 : .75);
470         }
471       else
472         {
473           st->kugeln[n].r=/* (1.0+0.3*cos(floor(2+2*l/(PI))*i)*sin(2*l))* */
474             ((n & 1) ? 0.5 : 1.0)*
475               ((n % 6==0) ? 1.25 : .75);
476         }
477       i+=TWOPI/kmax;
478     }
479
480   st->kugeln[n].x=0.0;
481   st->kugeln[n].y=0.0;
482   st->kugeln[n].z=0.0;
483   st->kugeln[n].r=2.0+cos(TWOPI*modf(k,&mod))/2;
484   n++;
485   
486   zeiger(st,2.0,0.75,-2.0,sec,&n);
487   zeiger(st,1.0,1.0,-1.5,min,&n);
488   zeiger(st,0.0,1.5,-1.0,hour,&n);
489   
490   for(n=0;n<st->maxk;n++)
491     {
492       ys=st->kugeln[n].y*cos(st->movef*sin(st->cycle*sec))+
493         st->kugeln[n].z*sin(st->movef*sin(st->cycle*sec));
494       zs=-st->kugeln[n].y*sin(st->movef*sin(st->cycle*sec))+
495         st->kugeln[n].z*cos(st->movef*sin(st->cycle*sec));
496       st->kugeln[n].y=ys;
497       st->kugeln[n].z=zs;
498     }
499 }
500 /*------------------------------------------------------------------*/
501 static void
502 t3d_sort(struct state *st, int l, int r)
503 {
504   int i,j;
505   kugeldat ex;
506   double x;
507   
508   i=l;j=r;
509   x=st->kugeln[(l+r)/2].d;
510   while(1)
511     {
512       while(st->kugeln[i].d>x) i++;
513       while(x>st->kugeln[j].d) j--;
514       if (i<=j)
515         {
516           ex=st->kugeln[i];st->kugeln[i]=st->kugeln[j];st->kugeln[j]=ex;
517           i++;j--;
518         }
519       if (i>j) break;
520     }
521   if (l<j) t3d_sort(st,l,j);
522   if (i<r) t3d_sort(st,i,r);
523 }
524
525 /*------------------------------------------------------------------*/
526 static void
527 fill_kugel(struct state *st, int i, Pixmap buf, int setcol)
528 {
529   double ra;
530   int m,col,inc=1,inr=3,d;
531   d=(int)((ABS(st->kugeln[i].r1)*2));
532   if (d==0) d=1;
533   
534 #ifdef FASTDRAW
535   if(st->fastdraw && d<st->fastch)
536     {
537 #       ifdef FASTCOPY
538       XCopyArea(st->dpy, st->fastmask, buf, st->andgc, sum1ton(d)-(d+1)/2, 1,d,d,
539                 (int)(st->kugeln[i].x1)-d/2, (int)(st->kugeln[i].y1)-d/2);
540       XCopyArea(st->dpy, st->fastcircles, buf, st->orgc, sum1ton(d)-(d+1)/2, 1,d,d,
541                 (int)(st->kugeln[i].x1)-d/2, (int)(st->kugeln[i].y1)-d/2);
542 #       else
543       XPutImage(st->dpy, buf, st->andgc, st->fastmask[d-1], 0, 0,
544                 (int)(st->kugeln[i].x1)-d/2, (int)(st->kugeln[i].y1)-d/2, d, d);
545       XPutImage(st->dpy, buf, st->orgc, st->fastcircles[d-1], 0, 0,
546                 (int)(st->kugeln[i].x1)-d/2, (int)(st->kugeln[i].y1)-d/2, d, d);
547 #       endif
548     }
549   else
550 #endif
551     {
552       if(ABS(st->kugeln[i].r1)<6.0) inr=9;
553       
554       for (m=0;m<=28;m+=inr)
555         {
556           ra=st->kugeln[i].r1*sqrt(1-m*m/(28.0*28.0));
557 #ifdef PRTDBX
558           printf("Radius: %f\n",ra);
559 #endif
560           if(-ra< 3.0) inc=14;
561           else if(-ra< 6.0) inc=8;
562           else if(-ra<20.0) inc=4;
563           else if(-ra<40.0) inc=2;
564           if(setcol)
565             {
566               if (m==27)
567                 col=33;
568               else
569                 col=(int)(m);
570
571               if (col>33)
572                 col=33;
573
574               col/=3;
575               setink(st->colors[col].pixel);
576             }
577
578 #ifdef USE_POLYGON
579           {
580             int n, nr;
581           for (n=0,nr=0;n<=sines-1;n+=inc,nr++)
582             {
583               track[nr].x=st->kugeln[i].x1+(int)(ra*st->sinus[n])+(st->kugeln[i].r1-ra)/2;
584               track[nr].y=st->kugeln[i].y1+(int)(ra*st->cosinus[n])+(st->kugeln[i].r1-ra)/2;
585             }
586           XFillPolygon(st->dpy,buf,st->gc,track,nr,Convex,CoordModeOrigin);
587           }
588 #else /* Use XFillArc */
589           XFillArc(st->dpy, buf, st->gc,
590                    (int)(st->kugeln[i].x1+(st->kugeln[i].r1+ra)/2),
591                    (int)(st->kugeln[i].y1+(st->kugeln[i].r1+ra)/2),
592                    (int)-(2*ra+1), (int)-(2*ra+1), 0, 360*64);
593 #endif
594         }
595     }
596 }
597
598 /*------------------------------------------------------------------*/
599
600 static void
601 init_3d(struct state *st)
602 {
603   double i;
604   int n=0;
605   
606   st->a[0]=0.0;
607   st->a[1]=0.0;
608   st->a[2]=-10.0;
609   
610   st->x[0]=10.0;
611   st->x[1]=0.0;
612   st->x[2]=0.0;
613   
614   st->y[0]=0.0;
615   st->y[1]=10.0;
616   st->y[2]=0.0;
617   
618   
619   st->zoom=-10.0;
620   st->speed=.0;
621   
622   for (i=0.0;n<sines;i+=TWOPI/sines,n++)
623     {
624       st->sinus[n]=sin(i);
625       st->cosinus[n]=cos(i);
626     }
627 }
628 /*------------------------------------------------------------------*/
629
630
631 static void
632 vektorprodukt(double feld1[], double feld2[], double feld3[])
633 {
634   feld3[0]=feld1[1]*feld2[2]-feld1[2]*feld2[1];
635   feld3[1]=feld1[2]*feld2[0]-feld1[0]*feld2[2];
636   feld3[2]=feld1[0]*feld2[1]-feld1[1]*feld2[0];
637 }
638
639
640 /*------------------------------------------------------------------*/
641 static void
642 turn(double feld1[], double feld2[], double winkel)
643 {
644   double temp[3];
645   double s,ca,sa,sx1,sx2,sx3;
646   
647   vektorprodukt(feld1,feld2,temp);
648   
649   s=feld1[0]*feld2[0]+feld1[1]*feld2[1]+feld1[2]*feld2[2];
650   
651   sx1=s*feld2[0];
652   sx2=s*feld2[1];
653   sx3=s*feld2[2];
654   sa=sin(winkel);ca=cos(winkel);
655   feld1[0]=ca*(feld1[0]-sx1)+sa*temp[0]+sx1;
656   feld1[1]=ca*(feld1[1]-sx2)+sa*temp[1]+sx2;
657   feld1[2]=ca*(feld1[2]-sx3)+sa*temp[2]+sx3;
658 }
659 /*------------------------------------------------------------------*/
660
661 /* 1: Blickrichtung v;3:Ebenenmittelpunkt m 
662    double feld1[],feld3[]; */
663 static void 
664 viewpoint(struct state *st)
665 {
666   st->am[0]=-st->zoom*st->v[0];
667   st->am[1]=-st->zoom*st->v[1];
668   st->am[2]=-st->zoom*st->v[2];
669   
670   st->zaehler=norm*norm*st->zoom;
671 }
672
673 /*------------------------------------------------------------------*/
674 static void 
675 projektion(struct state *st)
676 {
677   double c1[3],c2[3],k[3],x1,y1;
678   double cno,cnorm/*,magnit*/;
679   int i;
680   
681   for (i=0;i<st->maxk;i++)
682     {
683       c1[0]=st->kugeln[i].x-st->a[0];
684       c1[1]=st->kugeln[i].y-st->a[1];
685       c1[2]=st->kugeln[i].z-st->a[2];
686       cnorm=sqrt(c1[0]*c1[0]+c1[1]*c1[1]+c1[2]*c1[2]);
687       
688       c2[0]=c1[0];
689       c2[1]=c1[1];
690       c2[2]=c1[2];
691       
692       cno=c2[0]*st->v[0]+c2[1]*st->v[1]+c2[2]*st->v[2];
693       st->kugeln[i].d=cnorm;
694       if (cno<0) st->kugeln[i].d=-20.0;
695       
696       
697       st->kugeln[i].r1=(st->mag*st->zoom*st->kugeln[i].r/cnorm);
698       
699       c2[0]=st->v[0]/cno;
700       c2[1]=st->v[1]/cno;
701       c2[2]=st->v[2]/cno;
702       
703       vektorprodukt(c2,c1,k);
704
705       
706       x1=(st->startx+(st->x[0]*k[0]+st->x[1]*k[1]+st->x[2]*k[2])*st->mag);
707       y1=(st->starty-(st->y[0]*k[0]+st->y[1]*k[1]+st->y[2]*k[2])*st->mag);
708       if(   (x1>-2000.0)
709          && (x1<st->scrnWidth+2000.0)
710          && (y1>-2000.0)
711          && (y1<st->scrnHeight+2000.0))
712         {
713           st->kugeln[i].x1=(int)x1;
714           st->kugeln[i].y1=(int)y1;
715         }
716       else
717         {
718           st->kugeln[i].x1=0;
719           st->kugeln[i].y1=0;
720           st->kugeln[i].d=-20.0;
721         }
722     }
723 }
724
725
726 static void *
727 t3d_init (Display *dpy, Window window)
728 {
729   struct state *st = (struct state *) calloc (1, sizeof(*st));
730   st->dpy = dpy;
731   st->window = window;
732   st->dpy = dpy;
733   st->window = window;
734   initialize(st);
735   
736   initColor(st,st->r,st->g,st->b);
737   init_3d(st);
738   st->zeit=(struct tm *)malloc(sizeof(struct tm));
739   init_kugel(st);
740   
741   st->startx=st->scrnWidth/2;
742   st->starty=st->scrnHeight/2;
743   st->scrnH2=st->startx;
744   st->scrnW2=st->starty;
745   st->vspeed=0;
746   
747   
748   vektorprodukt(st->x,st->y,st->v);
749   viewpoint(st/*m,v*/);
750   
751   setink (BlackPixelOfScreen(st->xgwa.screen));
752   XFillRectangle (st->dpy, st->window, st->gc, 0, 0, st->scrnWidth, st->scrnHeight);
753   XQueryPointer (st->dpy, st->window, &st->junk_win, &st->junk_win, &st->junk, &st->junk,
754                  &st->px, &st->py, &st->kb);
755   
756   return st;
757 }
758
759 static unsigned long
760 t3d_draw (Display *d, Window w, void *closure)
761 {
762   struct state *st = (struct state *) closure;
763   double dtime;
764   double vnorm;
765
766         
767   /*--------------- Zeichenteil --------------*/
768
769   vektorprodukt(st->x,st->y,st->v);
770         
771   vnorm=sqrt(st->v[0]*st->v[0]+st->v[1]*st->v[1]+st->v[2]*st->v[2]);
772   st->v[0]=st->v[0]*norm/vnorm;
773   st->v[1]=st->v[1]*norm/vnorm;
774   st->v[2]=st->v[2]*norm/vnorm;
775   vnorm=sqrt(st->x[0]*st->x[0]+st->x[1]*st->x[1]+st->x[2]*st->x[2]);
776   st->x[0]=st->x[0]*norm/vnorm;
777   st->x[1]=st->x[1]*norm/vnorm;
778   st->x[2]=st->x[2]*norm/vnorm;
779   vnorm=sqrt(st->y[0]*st->y[0]+st->y[1]*st->y[1]+st->y[2]*st->y[2]);
780   st->y[0]=st->y[0]*norm/vnorm;
781   st->y[1]=st->y[1]*norm/vnorm;
782   st->y[2]=st->y[2]*norm/vnorm;
783         
784   projektion(st);
785   t3d_sort (st,0,st->maxk-1);
786         
787   dtime=gettime(st);
788         
789   if(st->cycl)
790     {
791       st->draw_color=(int)(64.0*(dtime/60-floor(dtime/60)))-32;
792             
793       if(st->draw_color<0)
794         st->draw_color=-st->draw_color;
795             
796       setink(st->colors[st->draw_color/3].pixel);
797     }
798   else
799     setink(BlackPixelOfScreen (st->xgwa.screen));
800         
801   XFillRectangle(st->dpy,st->buffer,st->gc,0,0,st->scrnWidth,st->scrnHeight);
802         
803   {
804     int i;
805           
806     manipulate(st,dtime);
807           
808     for (i=0;i<st->maxk;i++)
809       {
810         if (st->kugeln[i].d>0.0)
811           fill_kugel(st,i,st->buffer,1);
812       }
813   }
814
815   /* manipulate(gettime());
816      var+=PI/500;
817      if (var>=TWOPI) var=PI/500; */
818         
819   if(st->hsvcycl!=0.0)
820     {
821       dtime=st->hsvcycl*dtime/10.0+st->hue/360.0;
822       dtime=360*(dtime-floor(dtime));
823             
824       hsv2rgb(dtime,st->sat,st->val,&st->r,&st->g,&st->b);
825       changeColor(st,st->r,st->g,st->b);
826     }
827
828   XCopyArea (st->dpy, st->buffer, st->window, st->gc, 0, 0, st->scrnWidth, st->scrnHeight, 0, 0);
829         
830         
831   /*-------------------------------------------------*/
832         
833   XQueryPointer (st->dpy, st->window, &st->junk_win, &st->in_win, &st->junk, &st->junk,
834                         &st->px, &st->py, &st->kb);
835         
836   if ((st->px>0)&&(st->px<st->scrnWidth)&&(st->py>0)&&(st->py<st->scrnHeight) )         
837     {
838       if ((st->px !=st->startx)&&(st->kb&Button2Mask))
839         {
840           /* printf("y=(%f,%f,%f)",y[0],y[1],y[2]);*/
841           turn(st->y,st->x,((double)(st->px-st->startx))/(8000*st->mag));
842           /* printf("->(%f,%f,%f)\n",y[0],y[1],y[2]);*/
843         }
844       if ((st->py !=st->starty)&&(st->kb&Button2Mask)) 
845         {
846           /* printf("x=(%f,%f,%f)",x[0],x[1],x[2]);*/
847           turn(st->x,st->y,((double)(st->py-st->starty))/(-8000*st->mag));
848           /* printf("->(%f,%f,%f)\n",x[0],x[1],x[2]);*/
849         }
850       if ((st->kb&Button1Mask)) 
851         {
852           if (st->vturn==0.0) st->vturn=.005; else if (st->vturn<2)  st->vturn+=.01;
853           turn(st->x,st->v,.002*st->vturn);
854           turn(st->y,st->v,.002*st->vturn); 
855         }
856       if ((st->kb&Button3Mask)) 
857         {
858           if (st->vturn==0.0) st->vturn=.005; else if (st->vturn<2) st->vturn+=.01;
859           turn(st->x,st->v,-.002*st->vturn);
860           turn(st->y,st->v,-.002*st->vturn);
861         }
862     }
863   if (!(st->kb&Button1Mask)&&!(st->kb&Button3Mask)) 
864     st->vturn=0;
865         
866   st->speed=st->speed+st->speed*st->vspeed;
867   if ((st->speed<0.0000001) &&(st->vspeed>0.000001)) st->speed=0.000001;
868   st->vspeed=.1*st->vspeed;
869   if (st->speed>0.01) st->speed=.01;
870   st->a[0]=st->a[0]+st->speed*st->v[0];
871   st->a[1]=st->a[1]+st->speed*st->v[1];
872   st->a[2]=st->a[2]+st->speed*st->v[2];
873
874   return st->timewait;
875 }
876
877 static void
878 t3d_reshape (Display *dpy, Window window, void *closure, 
879                  unsigned int w, unsigned int h)
880 {
881   struct state *st = (struct state *) closure;
882   if (w != st->scrnWidth ||
883       h != st->scrnHeight)
884     {
885       XFreePixmap (st->dpy, st->buffer); 
886       st->scrnWidth = w;
887       st->scrnHeight = h;
888       st->buffer = XCreatePixmap (st->dpy, st->window, st->scrnWidth, st->scrnHeight, st->xgwa.depth);
889               
890       st->startx=st->scrnWidth/2;
891       st->starty=st->scrnHeight/2;
892       st->scrnH2=st->startx;
893       st->scrnW2=st->starty;
894     }
895 }
896
897 static Bool
898 t3d_event (Display *dpy, Window window, void *closure, XEvent *event)
899 {
900   struct state *st = (struct state *) closure;
901   if (event->type == KeyPress)
902     {
903       KeySym keysym;
904       char kpr = 0;
905       XLookupString (&event->xkey, &kpr, 1, &keysym, 0);
906       switch (kpr)
907         {
908         case 's': case 'S':
909           st->vspeed = 0.5;
910           return True;
911         case 'a': case 'A':
912           st->vspeed = -0.3;
913           return True;
914         case '0':
915           st->speed = 0;
916           st->vspeed = 0;
917           return True;
918         case 'z': case 'Z':
919           st->mag *= 1.02;
920           return True;
921         case 'x': case 'X':
922           st->mag /= 1.02;
923           return True;
924         default:
925           break;
926         }
927     }
928   return False;
929 }
930
931 static void
932 t3d_free (Display *dpy, Window window, void *closure)
933 {
934 }
935
936
937
938
939 /*-------------------------------------------------*/
940
941 static const char *t3d_defaults [] = {
942   ".background: black",
943   ".foreground: white",
944   "*move:       0.5",
945   "*wobble:     2.0",
946   "*cycle:      10.0",
947   "*mag:        1.0",
948   "*minutes:    False",
949   "*delay:      40000",
950   "*fast:       50",
951   "*colcycle:   False",
952   "*hsvcycle:   0.0",
953   "*rgb:        ",
954   "*hsv:        ",
955   0
956 };
957
958 static XrmOptionDescRec t3d_options [] = {
959   { "-move",            ".move",        XrmoptionSepArg, 0 },
960   { "-wobble",          ".wobble",      XrmoptionSepArg, 0 },
961   { "-cycle",           ".cycle",       XrmoptionSepArg, 0 },
962   { "-mag",             ".mag",         XrmoptionSepArg, 0 },
963   { "-minutes",         ".minutes",     XrmoptionNoArg, "True" },
964   { "-no-minutes",      ".minutes",     XrmoptionNoArg, "False" },
965   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
966   { "-fast",            ".fast",        XrmoptionSepArg, 0 },
967   { "-colcycle",        ".colcycle",    XrmoptionSepArg, 0 },
968   { "-hsvcycle",        ".hsvcycle",    XrmoptionSepArg, 0 },
969   { "-rgb",             ".rgb",         XrmoptionSepArg, 0 },
970   { "-hsv",             ".hsv",         XrmoptionSepArg, 0 },
971   { 0, 0, 0, 0 }
972 };
973
974
975 XSCREENSAVER_MODULE ("T3D", t3d)