--- ray/src/hd/rhd_x11.c 1997/12/12 11:13:15 3.15 +++ ray/src/hd/rhd_x11.c 2003/07/27 22:12:02 3.35 @@ -1,29 +1,31 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhd_x11.c,v 3.35 2003/07/27 22:12:02 schorsch Exp $"; #endif - /* * X11 driver for holodeck display. * Based on rview driver. */ #include "standard.h" -#include "rhd_qtree.h" - #include #include #include - +#include +#include "rhd_qtree.h" #include "x11icon.h" +#ifndef RAYQLEN +#define RAYQLEN 50000 /* max. rays to queue before flush */ +#endif + #ifndef FEQ #define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) #endif #define GAMMA 2.2 /* default gamma correction */ +#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) + #define MOVPCT 7 /* percent distance to move /frame */ #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) #define MOVDEG (-5) /* degrees to orbit CW/down /frame */ @@ -45,6 +47,8 @@ static char SCCSid[] = "$SunId$ SGI"; struct driver odev; /* global device driver structure */ +char odev_args[64]; /* command arguments */ + static XEvent currentevent; /* current event */ static int ncolors = 0; /* color table size */ @@ -64,7 +68,7 @@ static int inpresflags; /* input result flags */ static int headlocked = 0; /* lock vertical motion */ -static int getpixels(), xnewcolr(), freepixels(), resizewindow(), +static int getpixels(), xnewcolr(), freepixels(), resizewindow(), getframe(), getevent(), getkey(), moveview(), getmove(), fixwindow(); static unsigned long true_pixel(); @@ -81,9 +85,9 @@ mytmflags() /* figure out tone mapping flags */ for (cp = tail; *cp && *cp != '.'; cp++) ; if (cp-tail == 3 && !strncmp(tail, "x11", 3)) - return(TM_F_CAMERA); + return(TM_F_CAMERA|TM_F_NOSTDERR); if (cp-tail == 4 && !strncmp(tail, "x11h", 4)) - return(TM_F_HUMAN); + return(TM_F_HUMAN|TM_F_NOSTDERR); error(USER, "illegal driver name"); } @@ -92,8 +96,10 @@ dev_open(id) /* initialize X11 driver */ char *id; { extern char *getenv(); - char *gv; + static RGBPRIMS myprims = STDPRIMS; + char *ev; double gamval = GAMMA; + RGBPRIMP dpri = stdprims; int nplanes; XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; @@ -126,10 +132,17 @@ char *id; ourwhite = WhitePixel(ourdisplay,ourscreen); } /* set gamma and tone mapping */ - if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL - || (gv = getenv("DISPLAY_GAMMA")) != NULL) - gamval = atof(gv); - if (tmInit(mytmflags(), stdprims, gamval) == NULL) + if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL + || (ev = getenv("DISPLAY_GAMMA")) != NULL) + gamval = atof(ev); + if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL && + sscanf(ev, "%f %f %f %f %f %f %f %f", + &myprims[RED][CIEX],&myprims[RED][CIEY], + &myprims[GRN][CIEX],&myprims[GRN][CIEY], + &myprims[BLU][CIEX],&myprims[BLU][CIEY], + &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) + dpri = myprims; + if (tmInit(mytmflags(), dpri, gamval) == NULL) error(SYSTEM, "not enough memory in dev_open"); /* open window */ ourwinattr.background_pixel = ourblack; @@ -158,25 +171,22 @@ char *id; oursizhints.min_height = MINHEIGHT; oursizhints.flags = PMinSize; XSetNormalHints(ourdisplay, gwind, &oursizhints); - /* map the window and get its size */ - XMapWindow(ourdisplay, gwind); - dev_input(); - /* allocate our leaf pile */ - if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * - DisplayHeight(ourdisplay,ourscreen) / - (qtMinNodesiz*qtMinNodesiz))) - error(SYSTEM, "insufficient memory for leaf storage"); - /* figure out sensible view */ pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / DisplayWidth(ourdisplay, ourscreen); pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / DisplayHeight(ourdisplay, ourscreen); - copystruct(&odev.v, &stdview); - odev.name = id; + odev.v = stdview; odev.v.type = VT_PER; - odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); - odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); + /* map the window and get its size */ + XMapWindow(ourdisplay, gwind); + dev_input(); /* sets size and view angles */ + /* allocate our leaf pile */ + if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * + DisplayHeight(ourdisplay,ourscreen) * 3 / + (qtMinNodesiz*qtMinNodesiz*2))) + error(SYSTEM, "insufficient memory for leaf storage"); + odev.name = id; odev.ifd = ConnectionNumber(ourdisplay); } @@ -198,12 +208,22 @@ dev_close() /* close our display */ } + +dev_clear() /* clear our quadtree */ +{ + qtCompost(100); + if (ncolors > 0) + new_ctab(ncolors); + rayqleft = 0; /* hold off update */ +} + + int dev_view(nv) /* assign new driver view */ VIEW *nv; { if (nv->type == VT_PAR || /* check view legality */ - nv->horiz > 160. || nv->vert > 160.) { + nv->horiz > 160. || nv->vert > 160.) { error(COMMAND, "illegal view type/angle"); nv->type = VT_PER; nv->horiz = odev.v.horiz; @@ -222,7 +242,7 @@ VIEW *nv; int dh = DisplayHeight(ourdisplay,ourscreen); dw -= 25; /* for window frame */ - dh -= 100; + dh -= 50; odev.hres = 2.*VIEWDIST/pwidth * tan(PI/180./2.*nv->horiz); odev.vres = 2.*VIEWDIST/pheight * @@ -236,14 +256,42 @@ VIEW *nv; odev.vres = dh; } XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); + dev_input(); /* wait for resize event */ } - copystruct(&odev.v, nv); + odev.v = *nv; } qtReplant(); return(1); } +dev_section(ofn) /* add octree for geometry rendering */ +char *ofn; +{ + /* unimplemented */ +} + + +dev_auxcom(cmd, args) /* process an auxiliary command */ +char *cmd, *args; +{ + sprintf(errmsg, "%s: unknown command", cmd); + error(COMMAND, errmsg); +} + + +VIEW * +dev_auxview(n, hvres) /* return nth auxiliary view */ +int n; +int hvres[2]; +{ + if (n) + return(NULL); + hvres[0] = odev.hres; hvres[1] = odev.vres; + return(&odev.v); +} + + int dev_input() /* get X11 input */ { @@ -252,8 +300,10 @@ dev_input() /* get X11 input */ do getevent(); - while (XQLength(ourdisplay) > 0); + while (XPending(ourdisplay) > 0); + odev.inpready = 0; + return(inpresflags); } @@ -280,7 +330,8 @@ int dev_flush() /* flush output */ { qtUpdate(); - return(XPending(ourdisplay)); + rayqleft = RAYQLEN; + return(odev.inpready = XPending(ourdisplay)); } @@ -324,7 +375,7 @@ loop: return(ncolors = 0); if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) break; - free((char *)pixval); + free((void *)pixval); pixval = NULL; } if (pixval == NULL) { @@ -359,7 +410,7 @@ freepixels() /* free our pixels */ if (ncolors == 0) return; XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); - free((char *)pixval); + free((void *)pixval); pixval = NULL; ncolors = 0; if (ourmap != DefaultColormap(ourdisplay,ourscreen)) @@ -409,7 +460,10 @@ getevent() /* get next event */ getkey(levptr(XKeyPressedEvent)); break; case ButtonPress: - getmove(levptr(XButtonPressedEvent)); + if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) + getframe(levptr(XButtonPressedEvent)); + else + getmove(levptr(XButtonPressedEvent)); break; } } @@ -420,11 +474,29 @@ ilclip(dp, wp) /* clip world coordinates to device * int dp[2][2]; FVECT wp[2]; { - static FVECT vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE}; - FVECT ip[2]; - /* not exactly right, but who cares? */ - viewloc(ip[0], &odev.v, wp[0]); - viewloc(ip[1], &odev.v, wp[1]); + static FVECT vmin = {0.,0.,0.}, vmax = {1.-FTINY,1.-FTINY,FHUGE}; + FVECT wpc[2], ip[2]; + double d, d0, d1; + /* check for points behind view */ + d = DOT(odev.v.vp, odev.v.vdir); + d0 = DOT(wp[0], odev.v.vdir) - d; + d1 = DOT(wp[1], odev.v.vdir) - d; + /* work on copy of world points */ + if (d0 <= d1) { + VCOPY(wpc[0], wp[0]); VCOPY(wpc[1], wp[1]); + } else { + d = d0; d0 = d1; d1 = d; + VCOPY(wpc[1], wp[0]); VCOPY(wpc[0], wp[1]); + } + if (d0 <= FTINY) { + if (d1 <= FTINY) return(0); + VSUB(wpc[0], wpc[0], wpc[1]); + d = .99*d1/(d1-d0); + VSUM(wpc[0], wpc[1], wpc[0], d); + } + /* get view coordinates and clip to window */ + viewloc(ip[0], &odev.v, wpc[0]); + viewloc(ip[1], &odev.v, wpc[1]); if (!clip(ip[0], ip[1], vmin, vmax)) return(0); dp[0][0] = ip[0][0]*odev.hres; @@ -455,8 +527,6 @@ draw_grids() /* draw holodeck section grids */ static BYTE gridrgb[3] = {0x0, 0xff, 0xff}; unsigned long pixel; - if (!mapped || odev.v.type != VT_PER) - return; if (ncolors > 0) pixel = pixval[get_pixel(gridrgb, xnewcolr)]; else @@ -472,33 +542,35 @@ moveview(dx, dy, mov, orb) /* move our view */ int dx, dy, mov, orb; { VIEW nv; - FVECT v1; + FVECT odir, v1; double d; register int li; /* start with old view */ - copystruct(&nv, &odev.v); + nv = odev.v; /* change view direction */ if (mov | orb) { if ((li = qtFindLeaf(dx, dy)) < 0) return(0); /* not on window */ - VSUM(nv.vdir, qtL.wp[li], nv.vp, -1.); + VSUM(odir, qtL.wp[li], nv.vp, -1.); } else { if (viewray(nv.vp, nv.vdir, &odev.v, (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) return(0); /* outside view */ } if (orb && mov) { /* orbit left/right */ - spinvector(nv.vdir, nv.vdir, nv.vup, MOVDEG*PI/180.*mov); - VSUM(nv.vp, qtL.wp[li], nv.vdir, -1.); + spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); + VSUM(nv.vp, qtL.wp[li], odir, -1.); + spinvector(nv.vdir, nv.vdir, nv.vup, d); } else if (orb) { /* orbit up/down */ - fcross(v1, nv.vdir, nv.vup); + fcross(v1, odir, nv.vup); if (normalize(v1) == 0.) return(0); - spinvector(nv.vdir, nv.vdir, v1, MOVDEG*PI/180.*orb); - VSUM(nv.vp, qtL.wp[li], nv.vdir, -1.); + spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); + VSUM(nv.vp, qtL.wp[li], odir, -1.); + spinvector(nv.vdir, nv.vdir, v1, d); } else if (mov) { /* move forward/backward */ d = MOVPCT/100. * mov; - VSUM(nv.vp, nv.vp, nv.vdir, d); + VSUM(nv.vp, nv.vp, odir, d); } if (!mov ^ !orb && headlocked) { /* restore head height */ VSUM(v1, odev.v.vp, nv.vp, -1.); @@ -514,6 +586,39 @@ int dx, dy, mov, orb; static +getframe(ebut) /* get focus frame */ +XButtonPressedEvent *ebut; +{ + int startx = ebut->x, starty = ebut->y; + int endx, endy; + + XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); + endx = levptr(XButtonReleasedEvent)->x; + endy = levptr(XButtonReleasedEvent)->y; + if ((endx == startx) | (endy == starty)) { + XBell(ourdisplay, 0); + return; + } + if (endx < startx) {register int c = endx; endx = startx; startx = c;} + if (endy < starty) {register int c = endy; endy = starty; starty = c;} + sprintf(odev_args, "%.3f %.3f %.3f %.3f", + (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, + (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); + inpresflags |= DFL(DC_FOCUS); +} + + +static +waitabit() /* pause a moment */ +{ + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 50000000; + nanosleep(&ts, NULL); +} + + +static getmove(ebut) /* get view change */ XButtonPressedEvent *ebut; { @@ -529,7 +634,7 @@ XButtonPressedEvent *ebut; while (!XCheckMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent))) { - + waitabit(); if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, &rootx, &rooty, &wx, &wy, &statemask)) break; /* on another screen */ @@ -558,6 +663,9 @@ static getkey(ekey) /* get input key */ register XKeyPressedEvent *ekey; { + Window rootw, childw; + int rootx, rooty, wx, wy; + unsigned int statemask; int n; char buf[8]; @@ -574,6 +682,18 @@ register XKeyPressedEvent *ekey; case 'l': /* retrieve last view */ inpresflags |= DFL(DC_LASTVIEW); return; + case 'f': /* frame view position */ + if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, + &rootx, &rooty, &wx, &wy, &statemask)) + return; /* on another screen */ + sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, + 1.-(wy+.5)/odev.vres); + inpresflags |= DFL(DC_FOCUS); + return; + case 'F': /* unfocus */ + odev_args[0] = '\0'; + inpresflags |= DFL(DC_FOCUS); + return; case 'p': /* pause computation */ inpresflags |= DFL(DC_PAUSE); return; @@ -599,6 +719,7 @@ register XKeyPressedEvent *ekey; if (ncolors > 0) new_ctab(ncolors); inpresflags |= DFL(DC_REDRAW); /* resend values from server */ + rayqleft = 0; /* hold off update */ return; case 'K': /* kill rtrace process(es) */ inpresflags |= DFL(DC_KILL);