--- ray/src/hd/rhd_x11.c 1997/11/19 18:01:03 3.1 +++ ray/src/hd/rhd_x11.c 1997/12/12 11:13:15 3.15 @@ -18,8 +18,17 @@ static char SCCSid[] = "$SunId$ SGI"; #include "x11icon.h" +#ifndef FEQ +#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) +#endif + #define GAMMA 2.2 /* default gamma correction */ +#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 */ @@ -30,7 +39,7 @@ static char SCCSid[] = "$SunId$ SGI"; #define ourscreen DefaultScreen(ourdisplay) #define ourroot RootWindow(ourdisplay,ourscreen) #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\ - ButtonPressMask) + ButtonPressMask|ButtonReleaseMask) #define levptr(etype) ((etype *)¤tevent) @@ -44,23 +53,19 @@ static unsigned long *pixval = NULL; /* allocated pix static unsigned long ourblack=0, ourwhite=1; static Display *ourdisplay = NULL; /* our display */ - static XVisualInfo ourvinfo; /* our visual information */ - static Window gwind = 0; /* our graphics window */ - static GC ourgc = 0; /* our graphics context for drawing */ - static Colormap ourmap = 0; /* our color map */ static double pwidth, pheight; /* pixel dimensions (mm) */ static int inpresflags; /* input result flags */ -static int heightlocked = 0; /* lock vertical motion */ +static int headlocked = 0; /* lock vertical motion */ static int getpixels(), xnewcolr(), freepixels(), resizewindow(), - getevent(), getkey(), fixwindow(); + getevent(), getkey(), moveview(), getmove(), fixwindow(); static unsigned long true_pixel(); @@ -90,10 +95,11 @@ char *id; char *gv; double gamval = GAMMA; int nplanes; - int n; XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; XSizeHints oursizhints; + /* set quadtree globals */ + qtMinNodesiz = 2; /* open display server */ ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) @@ -155,6 +161,12 @@ char *id; /* 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); @@ -166,12 +178,6 @@ char *id; 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); odev.ifd = ConnectionNumber(ourdisplay); - /* allocate our leaf pile */ - qtDepthEps = 0.02; - n = odev.hres < odev.vres ? odev.hres : odev.vres; - qtMinNodesiz = 1; - if (!qtAllocLeaves(n*n/(qtMinNodesiz*qtMinNodesiz))) - error(SYSTEM, "insufficient memory for leaf storage"); } @@ -192,12 +198,49 @@ dev_close() /* close our display */ } +int dev_view(nv) /* assign new driver view */ VIEW *nv; { - if (nv != &odev.v) + 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)) { + int dw = DisplayWidth(ourdisplay,ourscreen); + int dh = DisplayHeight(ourdisplay,ourscreen); + + dw -= 25; /* for window frame */ + dh -= 100; + 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); + } copystruct(&odev.v, nv); + } qtReplant(); + return(1); } @@ -205,6 +248,7 @@ int dev_input() /* get X11 input */ { inpresflags = 0; + do getevent(); @@ -365,13 +409,152 @@ getevent() /* get next event */ getkey(levptr(XKeyPressedEvent)); break; case ButtonPress: - /* getmove(levptr(XButtonPressedEvent)); */ + getmove(levptr(XButtonPressedEvent)); break; } } static +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]); + if (!clip(ip[0], ip[1], vmin, vmax)) + return(0); + dp[0][0] = ip[0][0]*odev.hres; + dp[0][1] = ip[0][1]*odev.vres; + dp[1][0] = ip[1][0]*odev.hres; + dp[1][1] = ip[1][1]*odev.vres; + return(1); +} + + +static +draw3dline(wp) /* draw 3d line in world coordinates */ +FVECT wp[2]; +{ + int dp[2][2]; + + if (!ilclip(dp, wp)) + return; + XDrawLine(ourdisplay, gwind, ourgc, + dp[0][0], odev.vres-1 - dp[0][1], + dp[1][0], odev.vres-1 - dp[1][1]); +} + + +static +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 + pixel = true_pixel(gridrgb); + XSetForeground(ourdisplay, ourgc, pixel); + /* draw each grid line */ + gridlines(draw3dline); +} + + +static +moveview(dx, dy, mov, orb) /* move our view */ +int dx, dy, mov, orb; +{ + VIEW nv; + FVECT v1; + double d; + register int li; + /* start with old view */ + copystruct(&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.); + } 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.); + } else if (orb) { /* orbit up/down */ + fcross(v1, nv.vdir, 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.); + } else if (mov) { /* move forward/backward */ + d = MOVPCT/100. * mov; + VSUM(nv.vp, nv.vp, nv.vdir, 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 |= DFL(DC_SETVIEW); + return(1); +} + + +static +getmove(ebut) /* get view change */ +XButtonPressedEvent *ebut; +{ + int movdir = MOVDIR(ebut->button); + int movorb = MOVORB(ebut->state); + int oldnodesiz = qtMinNodesiz; + Window rootw, childw; + int rootx, rooty, wx, wy; + unsigned int statemask; + + qtMinNodesiz = 16; /* for quicker update */ + XNoOp(ourdisplay); + + while (!XCheckMaskEvent(ourdisplay, + ButtonReleaseMask, levptr(XEvent))) { + + if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, + &rootx, &rooty, &wx, &wy, &statemask)) + break; /* on another screen */ + + if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { + sleep(1); + continue; + } + XClearWindow(ourdisplay, gwind); + qtUpdate(); + draw_grids(); + } + 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, movorb); + } + dev_flush(); + + qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */ +} + + +static getkey(ekey) /* get input key */ register XKeyPressedEvent *ekey; { @@ -383,14 +566,51 @@ register XKeyPressedEvent *ekey; return; switch (buf[0]) { case 'h': /* turn on height motion lock */ - heightlocked = 1; + headlocked = 1; return; case 'H': /* turn off height motion lock */ - heightlocked = 0; + headlocked = 0; return; - case 'Q': + case 'l': /* retrieve last view */ + inpresflags |= DFL(DC_LASTVIEW); + return; + case 'p': /* pause computation */ + inpresflags |= DFL(DC_PAUSE); + return; + case 'v': /* spit out view */ + inpresflags |= DFL(DC_GETVIEW); + return; + case '\n': + case '\r': /* resume computation */ + inpresflags |= DFL(DC_RESUME); + return; + case CTRL('R'): /* redraw screen */ + if (ncolors > 0) + new_ctab(ncolors); + qtRedraw(0, 0, odev.hres, odev.vres); + return; + case CTRL('L'): /* refresh from server */ + if (inpresflags & DFL(DC_REDRAW)) + return; + XClearWindow(ourdisplay, gwind); + draw_grids(); + XFlush(ourdisplay); + qtCompost(100); /* unload the old tree */ + if (ncolors > 0) + new_ctab(ncolors); + inpresflags |= DFL(DC_REDRAW); /* resend values from server */ + return; + 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); @@ -404,10 +624,8 @@ fixwindow(eexp) /* repair damage to window */ register XExposeEvent *eexp; { if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ -eputs("Resizing window in fixwindow\n"); 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); @@ -421,15 +639,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( - tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres ); - odev.v.vert = 2.*180./PI * atan( - tan(PI/180./2.*odev.v.vert) * ersz->height/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); }