X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Ft3d.c;fp=hacks%2Ft3d.c;h=ae0396e514ae94222b364dfbc7cbc9940ffae1c6;hb=f54438ea00f152166e68073e98000fd3a00f65cd;hp=0000000000000000000000000000000000000000;hpb=f65151994eba80ecabcdac6eef6fa0dde7e2d45b;p=xscreensaver diff --git a/hacks/t3d.c b/hacks/t3d.c new file mode 100644 index 00000000..ae0396e5 --- /dev/null +++ b/hacks/t3d.c @@ -0,0 +1,937 @@ +/* t3d -- Flying Balls Clock Demo + by Bernd Paysan , paysan@informatik.tu-muenchen.de + + Copy, modify, and distribute T3D either under GPL version 2 or newer, + or under the standard MIT/X license notice. + + partly based on flying balls demo by Georg Acher, + acher@informatik.tu-muenchen.de + (developed on HP9000/720 (55 MIPS,20 MFLOPS) ) + NO warranty at all ! Complaints to /dev/null ! + + 4-Jan-99 jwz@jwz.org -- adapted to xscreensaver framework, to take advantage + of the command-line options provided by screenhack.c. +*/ + +#undef FASTDRAW +#undef USE_POLYGON + +#ifdef FASTDRAW +# define FASTCOPY +#endif + +#include +#include + +#include "screenhack.h" + + +static int maxk=34; + +#define WIDTH 200 +#define HEIGHT 200 +#define norm 20.0 + +int timewait=40000; + +#define ROOT 0x1 +#define PI M_PI +#define TWOPI 2*M_PI + +#define MIN(i,j) ((i)<(j)?(i):(j)) + +#define kmax ((minutes?60:24)) +/* Anzahl der Kugeln */ +#define sines 52 +/* Werte in der Sinus-Tabelle */ +/*-----------------------------------------------------------------*/ +#define setink(inkcolor) \ + XSetForeground (dpy,gc,inkcolor) + +#define drawline(xx1,yy1,xx2,yy2) \ + XDrawLine(dpy,win,gc,xx1,yy1,xx2,yy2) + +#define drawseg(segments,nr_segments) \ + XDrawSegments(dpy,win,gc,segments,nr_segments) + + +#define polyfill(ppts,pcount) \ + XFillPolygon(dpy,win,gc,ppts,pcount,Convex,CoordModeOrigin) + + +#define frac(argument) argument-floor(argument) + +#define abs(x) ((x)<0.0 ? -(x) : (x)) + +static Colormap cmap; +/* static XColor gray1; */ +static double r=1.0,g=1.0,b=1.0; +static double hue=0.0,sat=0.0,val=1.0; + +typedef struct { + double x,y,z,r,d,r1; + int x1,y1; +} kugeldat; + +/* Felder fuer 3D */ + +static kugeldat kugeln[100]; + +static double a[3],/*m[3],*/am[3],x[3],y[3],v[3]; +static double zoom,speed,zaehler,vspeed/*,AE*/; +static double vturn/*,aturn*/; +/* static XPoint track[sines]; */ +static double sinus[sines]; +static double cosinus[sines]; + +static int startx,starty; +static double /*magx,magy,*/mag=10; +/* static double lastx,lasty,lastz; */ +/* static int lastcx,lastcy,lastcz; */ +/* static int move=1; */ +static int minutes=0; +static int cycl=0; +static double hsvcycl=0.0; +static double movef =0.5, wobber=2.0, cycle=6.0; + +/* time */ + +/* static double sec; */ + +/* Windows */ +static XWindowAttributes xgwa; +static GC gc; +static GC orgc; +static GC andgc; +static Window win; +/* static Font font; */ +static Display *dpy; +static int screen, scrnWidth = WIDTH, scrnHeight = HEIGHT; +static Pixmap buffer; +#define maxfast 100 +static int fastch=50; +#ifdef FASTDRAW +# ifdef FASTCOPY +# define sum1ton(a) (((a)*(a)+1)/2) +# define fastcw sum1ton(fastch) + static Pixmap fastcircles; + static Pixmap fastmask; +# else + static XImage* fastcircles[maxfast]; + static XImage* fastmask[maxfast]; +# endif +static int fastdraw=0; +#endif + +static int scrnW2,scrnH2; +/* static unsigned short flags = 0; */ +/* static char *text; */ +static XColor colors[64]; +static struct tm *zeit; + +static int planes; +/* compute time */ + +static double +gettime (void) +{ + struct timeval time1; + struct timezone zone1; + struct tm *zeit; + + gettimeofday(&time1,&zone1); + zeit=localtime(&time1.tv_sec); + + return (zeit->tm_sec+60*(zeit->tm_min+60*(zeit->tm_hour)) + + time1.tv_usec*1.0E-6); +} + +/* --------------------COLORMAP---------------------*/ + +static void +hsv2rgb (double h, double s, double v, double *r, double *g, double *b) +{ + h/=360.0; h=6*(h-floor(h)); + + if(s==0.0) + { + *r=*g=*b=v; + } + else + { int i=(int)h; + double t,u,w; + + h=h-floor(h); + + u=v*(s*(1.0-h)); + w=v*(1.0-s); + t=v*(s*h+1.0-s); + + switch(i) + { + case 0: *r=v; *g=t; *b=w; break; + case 1: *r=u; *g=v; *b=w; break; + case 2: *r=w; *g=v; *b=t; break; + case 3: *r=w; *g=u; *b=v; break; + case 4: *r=t; *g=w; *b=v; break; + case 5: *r=v; *g=w; *b=u; break; + } + } +#ifdef PRTDBX + printf("HSV: %f %f %f to\nRGB: %f %f %f\n",h,s,v,*r,*g,*b); +#endif +} + +static void +changeColor (double r, double g, double b) +{ + int n,n1; + + n1=0; + for(n=30;n<64;n+=3) + { + colors[n1].red =1023+ n*(int)(1024.*r); + colors[n1].blue =1023+ n*(int)(1024.*b); + colors[n1].green =1023+ n*(int)(1024.*g); + + n1++; + } + + XStoreColors (dpy, cmap, colors, 12); +} + +static void +initColor (double r, double g, double b) +{ + int n,n1; + unsigned long pixels[12]; + long dummy; + + cmap = xgwa.colormap; + + if(hsvcycl!=0.0 && XAllocColorCells(dpy, cmap, 0, &dummy, 0, pixels, 12)) + { + for(n1=0;n1<12;n1++) + { + colors[n1].pixel=pixels[n1]; + colors[n1].flags=DoRed | DoGreen | DoBlue; + } + + changeColor(r,g,b); + } + else + { + n1=0; + for(n=30;n<64;n+=3) + { + colors[n1].red =1023+ n*(int)(1024.*r); + colors[n1].blue =1023+ n*(int)(1024.*b); + colors[n1].green =1023+ n*(int)(1024.*g); + + if (!(XAllocColor (dpy, cmap, &colors[n1]))) { + (void) fprintf (stderr, "Error: Cannot allocate colors\n"); + exit (1); + } + + n1++; + } + } +} + +/* ----------------WINDOW-------------------*/ + +static void +initialize (void) +{ + XGCValues *xgc; + XGCValues *xorgc; + XGCValues *xandgc; + + XGetWindowAttributes (dpy, win, &xgwa); + scrnWidth = xgwa.width; + scrnHeight = xgwa.height; + + cycle = 60.0 / get_float_resource ("cycle", "Float"); + movef = get_float_resource ("move", "Float") / 2; + wobber *= get_float_resource ("wobble", "Float"); + + { + double magfac = get_float_resource ("mag", "Float"); + mag *= magfac; + fastch=(int)(fastch*magfac); + } + + if (get_boolean_resource ("minutes", "Boolean")) { + minutes=1; maxk+=60-24; + } + + timewait = get_integer_resource ("wait", "Integer"); + fastch = get_integer_resource ("fast", "Integer"); + cycl = get_boolean_resource ("colcycle", "Integer"); + hsvcycl = get_float_resource ("hsvcycle", "Integer"); + + { + char *s = get_string_resource ("rgb", "RGB"); + char dummy; + if (s && *s) + { + double rr, gg, bb; + if (3 == sscanf (s, "%lf %lf %lf %c", &rr, &gg, &bb, &dummy)) + r = rr, g = gg, b = bb; + } + if (s) free (s); + + s = get_string_resource ("hsv", "HSV"); + if (s && *s) + { + double hh, ss, vv; + if (3 == sscanf (s, "%lf %lf %lf", &hh, &ss, &vv, &dummy)) { + hue = hh, sat = ss, val = vv; + hsv2rgb(hue,sat,val,&r,&g,&b); + } + } + if (s) free (s); + } + + if (fastch>maxfast) + fastch=maxfast; + +#ifdef PRTDBX + printf("Set options:\ndisplay: '%s'\ngeometry: '%s'\n",display,geometry); + printf("move\t%.2f\nwobber\t%.2f\nmag\t%.2f\ncycle\t%.4f\n", + movef,wobber,mag/10,cycle); + printf("nice\t%i\nfast\t%i\nmarks\t%i\nwait\t%i\n",niced,fastch,maxk,timewait); +#endif + + xgc=( XGCValues *) malloc(sizeof(XGCValues) ); + xorgc=( XGCValues *) malloc(sizeof(XGCValues) ); + xandgc=( XGCValues *) malloc(sizeof(XGCValues) ); + + screen = screen_number (xgwa.screen); + + planes=xgwa.depth; + + gc = XCreateGC (dpy, win, 0, xgc); + xorgc->function =GXor; + orgc = XCreateGC (dpy, win, GCFunction, xorgc); + xandgc->function =GXandInverted; + andgc = XCreateGC (dpy, win, GCFunction, xandgc); + + buffer = XCreatePixmap (dpy, win, scrnWidth, scrnHeight, + xgwa.depth); + +#ifdef DEBUG + printf("Time 3D drawing "); +#ifdef FASTDRAW +# ifdef FASTCOPY + puts("fast by Pixmap copy"); +# else + puts("fast by XImage copy"); +# endif +#else + puts("slow"); +#endif +#endif /* DEBUG */ + +#ifdef FASTCOPY + fastcircles = XCreatePixmap (dpy, win, fastcw, fastch+1, xgwa.depth); + fastmask = XCreatePixmap (dpy, win, fastcw, fastch+1, xgwa.depth); +#endif + + setink(BlackPixel (dpy, screen)); + XFillRectangle (dpy, buffer , gc, 0, 0, scrnWidth, scrnHeight); + +#ifdef FASTCOPY + + setink(0); + XFillRectangle (dpy, fastcircles, gc, 0, 0, fastcw, fastch+1); + XFillRectangle (dpy, fastmask , gc, 0, 0, fastcw, fastch+1); + +#endif +} + +static void fill_kugel(int i, Pixmap buf, int setcol); + + +/*------------------------------------------------------------------*/ +static void +init_kugel(void) +{ + +#ifdef FASTDRAW + int i; + + for(i=0; ix) i++; + while(x>kugeln[j].d) j--; + if (i<=j) + { + ex=kugeln[i];kugeln[i]=kugeln[j];kugeln[j]=ex; + i++;j--; + } + if (i>j) break; + } + if (l33) col=33; col/=3; + setink(colors[col].pixel); + } + +#ifdef USE_POLYGON + { + int n, nr; + for (n=0,nr=0;n<=sines-1;n+=inc,nr++) + { + track[nr].x=kugeln[i].x1+(int)(ra*sinus[n])+(kugeln[i].r1-ra)/2; + track[nr].y=kugeln[i].y1+(int)(ra*cosinus[n])+(kugeln[i].r1-ra)/2; + } + XFillPolygon(dpy,buf,gc,track,nr,Convex,CoordModeOrigin); + } +#else /* Use XFillArc */ + XFillArc(dpy, buf, gc, + (int)(kugeln[i].x1+(kugeln[i].r1+ra)/2), + (int)(kugeln[i].y1+(kugeln[i].r1+ra)/2), + (int)-(2*ra+1), (int)-(2*ra+1), 0, 360*64); +#endif + } + } +} + +/*------------------------------------------------------------------*/ + +static void +init_3d(void) +{ + double i; + int n=0; + + a[0]=0.0; + a[1]=0.0; + a[2]=-10.0; + + x[0]=10.0; + x[1]=0.0; + x[2]=0.0; + + y[0]=0.0; + y[1]=10.0; + y[2]=0.0; + + + zoom=-10.0; + speed=.0; + + for (i=0.0;n-2000.0) + && (x1-2000.0) + && (y10.0) + fill_kugel(i,buffer,1); + } + } + + XSync(dpy,0); + + /* manipulate(gettime()); + var+=PI/500; + if (var>=TWOPI) var=PI/500; */ + + /*event_handler();*/ + + if(hsvcycl!=0.0) + { + dtime=hsvcycl*dtime/10.0+hue/360.0; + dtime=360*(dtime-floor(dtime)); + + hsv2rgb(dtime,sat,val,&r,&g,&b); + changeColor(r,g,b); + } + + XCopyArea (dpy, buffer, win, gc, 0, 0, scrnWidth, scrnHeight, 0, 0); + + + /*-------------------------------------------------*/ + XSync(dpy,0); + + event_handler(); + + (void)(XQueryPointer (dpy, win, &junk_win, &in_win, &junk, &junk, + &px, &py, &kb)); + + if ((px>0)&&(px0)&&(py(%f,%f,%f)\n",y[0],y[1],y[2]);*/ + } + if ((py !=starty)&&(kb&Button2Mask)) + { + /* printf("x=(%f,%f,%f)",x[0],x[1],x[2]);*/ + turn(x,y,((double)(py-starty))/(-8000*mag)); + /* printf("->(%f,%f,%f)\n",x[0],x[1],x[2]);*/ + } + if ((kb&Button1Mask)) + { + if (vturn==0.0) vturn=.005; else if (vturn<2) vturn+=.01; + turn(x,v,.002*vturn); + turn(y,v,.002*vturn); + } + if ((kb&Button3Mask)) + { + if (vturn==0.0) vturn=.005; else if (vturn<2) vturn+=.01; + turn(x,v,-.002*vturn); + turn(y,v,-.002*vturn); + } + } + if (!(kb&Button1Mask)&&!(kb&Button3Mask)) + vturn=0; + + speed=speed+speed*vspeed; + if ((speed<0.0000001) &&(vspeed>0.000001)) speed=0.000001; + vspeed=.1*vspeed; + if (speed>0.01) speed=.01; + a[0]=a[0]+speed*v[0]; + a[1]=a[1]+speed*v[1]; + a[2]=a[2]+speed*v[2]; + } +}