--- ray/src/hd/rhd_x11.c 1997/12/08 19:03:18 3.11 +++ ray/src/hd/rhd_x11.c 1998/01/06 13:07:57 3.24 @@ -18,16 +18,20 @@ static char SCCSid[] = "$SunId$ SGI"; #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 CTRL(c) ((c)-'@') - #define GAMMA 2.2 /* default gamma correction */ -#define MOVPCT 10 /* percent distance to move */ +#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 */ +#define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) #define MINWIDTH 480 /* minimum graphics window width */ #define MINHEIGHT 400 /* minimum graphics window height */ @@ -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.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,21 +208,60 @@ 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.) { + error(COMMAND, "illegal view type/angle"); + nv->type = VT_PER; + nv->horiz = odev.v.horiz; + nv->vert = odev.v.vert; + return(0); + } + if (nv->vfore > FTINY) { + error(COMMAND, "cannot handle fore clipping"); + nv->vfore = 0.; + return(0); + } if (nv != &odev.v) { if (!FEQ(nv->horiz,odev.v.horiz) || /* resize window? */ !FEQ(nv->vert,odev.v.vert)) { - odev.hres = 2.*VIEWDIST*tan(PI/180./2.*nv->horiz) / - pwidth; - odev.vres = 2.*VIEWDIST*tan(PI/180./2.*nv->vert) / - pheight; + int dw = DisplayWidth(ourdisplay,ourscreen); + int dh = DisplayHeight(ourdisplay,ourscreen); + + dw -= 25; /* for window frame */ + dh -= 50; + odev.hres = 2.*VIEWDIST/pwidth * + tan(PI/180./2.*nv->horiz); + odev.vres = 2.*VIEWDIST/pheight * + tan(PI/180./2.*nv->vert); + if (odev.hres > dw) { + odev.vres = dw * odev.vres / odev.hres; + odev.hres = dw; + } + if (odev.vres > dh) { + odev.hres = dh * odev.hres / odev.vres; + odev.vres = dh; + } XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); + dev_input(); /* wait for resize event */ } copystruct(&odev.v, nv); } qtReplant(); + return(1); } @@ -252,6 +301,7 @@ int dev_flush() /* flush output */ { qtUpdate(); + rayqleft = RAYQLEN; return(XPending(ourdisplay)); } @@ -427,7 +477,7 @@ draw_grids() /* draw holodeck section grids */ static BYTE gridrgb[3] = {0x0, 0xff, 0xff}; unsigned long pixel; - if (!mapped || odev.v.type != VT_PER) + if (!mapped) return; if (ncolors > 0) pixel = pixval[get_pixel(gridrgb, xnewcolr)]; @@ -440,43 +490,49 @@ draw_grids() /* draw holodeck section grids */ static -moveview(dx, dy, move) /* move our view */ -int dx, dy, move; +moveview(dx, dy, mov, orb) /* move our view */ +int dx, dy, mov, orb; { VIEW nv; + FVECT odir, v1; double d; - register int i, li; + register int li; /* start with old view */ copystruct(&nv, &odev.v); /* change view direction */ - if (move) { + if (mov | orb) { if ((li = qtFindLeaf(dx, dy)) < 0) return(0); /* not on window */ - for (i = 0; i < 3; i++) - nv.vdir[i] = qtL.wp[li][i] - nv.vp[i]; + 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 */ } - /* move viewpoint */ - if (move > 0) - for (i = 0; i < 3; i++) - nv.vp[i] += MOVPCT/100. * nv.vdir[i]; - else if (move < 0) - for (i = 0; i < 3; i++) - nv.vp[i] -= MOVPCT/100. * nv.vdir[i]; - if (move && headlocked) { - d = 0; /* bring head back to same height */ - for (i = 0; i < 3; i++) - d += odev.v.vup[i] * (odev.v.vp[i] - nv.vp[i]); - for (i = 0; i < 3; i++) - nv.vp[i] += d * odev.v.vup[i]; + if (orb && mov) { /* orbit left/right */ + 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, odir, nv.vup); + if (normalize(v1) == 0.) + return(0); + 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, odir, d); } + if (!mov ^ !orb && headlocked) { /* restore head height */ + VSUM(v1, odev.v.vp, nv.vp, -1.); + d = DOT(v1, odev.v.vup); + VSUM(nv.vp, nv.vp, odev.v.vup, d); + } if (setview(&nv) != NULL) return(0); /* illegal view */ dev_view(&nv); - inpresflags |= DEV_NEWVIEW; + inpresflags |= DFL(DC_SETVIEW); return(1); } @@ -485,7 +541,8 @@ static getmove(ebut) /* get view change */ XButtonPressedEvent *ebut; { - int whichbutton = ebut->button; + int movdir = MOVDIR(ebut->button); + int movorb = MOVORB(ebut->state); int oldnodesiz = qtMinNodesiz; Window rootw, childw; int rootx, rooty, wx, wy; @@ -501,7 +558,7 @@ XButtonPressedEvent *ebut; &rootx, &rooty, &wx, &wy, &statemask)) break; /* on another screen */ - if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton))) { + if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { sleep(1); continue; } @@ -509,11 +566,11 @@ XButtonPressedEvent *ebut; qtUpdate(); draw_grids(); } - if (!(inpresflags & DEV_NEWVIEW)) { /* do final motion */ - whichbutton = levptr(XButtonReleasedEvent)->button; + if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ + movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); wx = levptr(XButtonReleasedEvent)->x; wy = levptr(XButtonReleasedEvent)->y; - moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton)); + moveview(wx, odev.vres-1-wy, movdir, movorb); } dev_flush(); @@ -538,17 +595,18 @@ register XKeyPressedEvent *ekey; case 'H': /* turn off height motion lock */ headlocked = 0; return; - case CTRL('S'): + case 'l': /* retrieve last view */ + inpresflags |= DFL(DC_LASTVIEW); + return; case 'p': /* pause computation */ - inpresflags |= DEV_WAIT; + inpresflags |= DFL(DC_PAUSE); return; case 'v': /* spit out view */ - inpresflags |= DEV_PUTVIEW; + inpresflags |= DFL(DC_GETVIEW); return; - case CTRL('Q'): case '\n': case '\r': /* resume computation */ - inpresflags |= DEV_RESUME; + inpresflags |= DFL(DC_RESUME); return; case CTRL('R'): /* redraw screen */ if (ncolors > 0) @@ -556,7 +614,7 @@ register XKeyPressedEvent *ekey; qtRedraw(0, 0, odev.hres, odev.vres); return; case CTRL('L'): /* refresh from server */ - if (inpresflags & DEV_REDRAW) + if (inpresflags & DFL(DC_REDRAW)) return; XClearWindow(ourdisplay, gwind); draw_grids(); @@ -564,12 +622,20 @@ register XKeyPressedEvent *ekey; qtCompost(100); /* unload the old tree */ if (ncolors > 0) new_ctab(ncolors); - inpresflags |= DEV_REDRAW; /* resend values from server */ + inpresflags |= DFL(DC_REDRAW); /* resend values from server */ + rayqleft = 0; /* hold off update */ return; - case CTRL('D'): - case 'Q': + case 'K': /* kill rtrace process(es) */ + inpresflags |= DFL(DC_KILL); + break; + case 'R': /* restart rtrace */ + inpresflags |= DFL(DC_RESTART); + break; + case 'C': /* clobber holodeck */ + inpresflags |= DFL(DC_CLOBBER); + break; case 'q': /* quit the program */ - inpresflags |= DEV_SHUTDOWN; + inpresflags |= DFL(DC_QUIT); return; default: XBell(ourdisplay, 0); @@ -585,7 +651,6 @@ register XExposeEvent *eexp; if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ odev.hres = eexp->width; odev.vres = eexp->height; - inpresflags |= DEV_NEWSIZE; } qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height, eexp->x + eexp->width, odev.vres - eexp->y); @@ -599,13 +664,11 @@ register XConfigureEvent *ersz; if (ersz->width == odev.hres && ersz->height == odev.vres) return; - if (odev.hres != 0 && odev.vres != 0) { - 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); - inpresflags |= DEV_NEWVIEW; - } odev.hres = ersz->width; odev.vres = ersz->height; - inpresflags |= DEV_NEWSIZE; + 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); + + inpresflags |= DFL(DC_SETVIEW); }