ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
(Generate patch)

Comparing ray/src/hd/rhd_x11.c (file contents):
Revision 3.10 by gregl, Fri Dec 5 09:38:55 1997 UTC vs.
Revision 3.29 by gwlarson, Fri Nov 13 14:25:52 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10   */
11  
12   #include "standard.h"
13 #include "rhd_qtree.h"
14
13   #include  <X11/Xlib.h>
14   #include  <X11/cursorfont.h>
15   #include  <X11/Xutil.h>
16 <
16 > #include "rhd_qtree.h"
17   #include  "x11icon.h"
18  
19 < #define CTRL(c)         ((c)-'@')
19 > #ifndef RAYQLEN
20 > #define RAYQLEN         50000           /* max. rays to queue before flush */
21 > #endif
22  
23 + #ifndef FEQ
24 + #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
25 + #endif
26 +
27   #define GAMMA           2.2             /* default gamma correction */
28  
29 < #define MOVPCT          10              /* percent distance to move */
29 > #define MOVPCT          7               /* percent distance to move /frame */
30   #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
31 + #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
32 + #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
33  
34   #define MINWIDTH        480             /* minimum graphics window width */
35   #define MINHEIGHT       400             /* minimum graphics window height */
# Line 77 | Line 83 | mytmflags()                    /* figure out tone mapping flags */
83          for (cp = tail; *cp && *cp != '.'; cp++)
84                  ;
85          if (cp-tail == 3 && !strncmp(tail, "x11", 3))
86 <                return(TM_F_CAMERA);
86 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
87          if (cp-tail == 4 && !strncmp(tail, "x11h", 4))
88 <                return(TM_F_HUMAN);
88 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
89          error(USER, "illegal driver name");
90   }
91  
# Line 88 | Line 94 | dev_open(id)                   /* initialize X11 driver */
94   char  *id;
95   {
96          extern char  *getenv();
97 <        char  *gv;
97 >        static RGBPRIMS myprims = STDPRIMS;
98 >        char  *ev;
99          double  gamval = GAMMA;
100 +        RGBPRIMP        dpri = stdprims;
101          int  nplanes;
102          XSetWindowAttributes    ourwinattr;
103          XWMHints  ourxwmhints;
# Line 122 | Line 130 | char  *id;
130                  ourwhite = WhitePixel(ourdisplay,ourscreen);
131          }
132                                          /* set gamma and tone mapping */
133 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
134 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
135 <                gamval = atof(gv);
136 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
133 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
134 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
135 >                gamval = atof(ev);
136 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
137 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
138 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
139 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
140 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
141 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
142 >                dpri = myprims;
143 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
144                  error(SYSTEM, "not enough memory in dev_open");
145                                          /* open window */
146          ourwinattr.background_pixel = ourblack;
# Line 154 | Line 169 | char  *id;
169          oursizhints.min_height = MINHEIGHT;
170          oursizhints.flags = PMinSize;
171          XSetNormalHints(ourdisplay, gwind, &oursizhints);
157                                        /* map the window and get its size */
158        XMapWindow(ourdisplay, gwind);
159        dev_input();
160                                        /* allocate our leaf pile */
161        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
162                        DisplayHeight(ourdisplay,ourscreen) /
163                        (qtMinNodesiz*qtMinNodesiz)))
164                error(SYSTEM, "insufficient memory for leaf storage");
165
172                                          /* figure out sensible view */
173          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
174                          DisplayWidth(ourdisplay, ourscreen);
175          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
176                          DisplayHeight(ourdisplay, ourscreen);
177          copystruct(&odev.v, &stdview);
172        odev.name = id;
178          odev.v.type = VT_PER;
179 <        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
180 <        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
179 >                                        /* map the window and get its size */
180 >        XMapWindow(ourdisplay, gwind);
181 >        dev_input();                    /* sets size and view angles */
182 >                                        /* allocate our leaf pile */
183 >        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
184 >                        DisplayHeight(ourdisplay,ourscreen) * 3 /
185 >                        (qtMinNodesiz*qtMinNodesiz*2)))
186 >                error(SYSTEM, "insufficient memory for leaf storage");
187 >        odev.name = id;
188          odev.ifd = ConnectionNumber(ourdisplay);
189   }
190  
# Line 194 | Line 206 | dev_close()                    /* close our display */
206   }
207  
208  
209 +
210 + dev_clear()                     /* clear our quadtree */
211 + {
212 +        qtCompost(100);
213 +        if (ncolors > 0)
214 +                new_ctab(ncolors);
215 +        rayqleft = 0;                   /* hold off update */
216 + }
217 +
218 +
219 + int
220   dev_view(nv)                    /* assign new driver view */
221   VIEW    *nv;
222   {
223 <        if (nv != &odev.v)
223 >        if (nv->type == VT_PAR ||               /* check view legality */
224 >                        nv->horiz > 160. || nv->vert > 160.) {
225 >                error(COMMAND, "illegal view type/angle");
226 >                nv->type = VT_PER;
227 >                nv->horiz = odev.v.horiz;
228 >                nv->vert = odev.v.vert;
229 >                return(0);
230 >        }
231 >        if (nv->vfore > FTINY) {
232 >                error(COMMAND, "cannot handle fore clipping");
233 >                nv->vfore = 0.;
234 >                return(0);
235 >        }
236 >        if (nv != &odev.v) {
237 >                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
238 >                                !FEQ(nv->vert,odev.v.vert)) {
239 >                        int     dw = DisplayWidth(ourdisplay,ourscreen);
240 >                        int     dh = DisplayHeight(ourdisplay,ourscreen);
241 >
242 >                        dw -= 25;       /* for window frame */
243 >                        dh -= 50;
244 >                        odev.hres = 2.*VIEWDIST/pwidth *
245 >                                        tan(PI/180./2.*nv->horiz);
246 >                        odev.vres = 2.*VIEWDIST/pheight *
247 >                                        tan(PI/180./2.*nv->vert);
248 >                        if (odev.hres > dw) {
249 >                                odev.vres = dw * odev.vres / odev.hres;
250 >                                odev.hres = dw;
251 >                        }
252 >                        if (odev.vres > dh) {
253 >                                odev.hres = dh * odev.hres / odev.vres;
254 >                                odev.vres = dh;
255 >                        }
256 >                        XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
257 >                        dev_input();    /* wait for resize event */
258 >                }
259                  copystruct(&odev.v, nv);
260 +        }
261          qtReplant();
262 +        return(1);
263   }
264  
265  
266 + dev_auxcom(cmd, args)           /* process an auxiliary command */
267 + char    *cmd, *args;
268 + {
269 +        sprintf(errmsg, "%s: unknown command", cmd);
270 +        error(COMMAND, errmsg);
271 + }
272 +
273 +
274 + VIEW *
275 + dev_auxview(n, hvres)           /* return nth auxiliary view */
276 + int     n;
277 + int     hvres[2];
278 + {
279 +        if (n)
280 +                return(NULL);
281 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
282 +        return(&odev.v);
283 + }
284 +
285 +
286   int
287   dev_input()                     /* get X11 input */
288   {
# Line 211 | Line 291 | dev_input()                    /* get X11 input */
291          do
292                  getevent();
293  
294 <        while (XQLength(ourdisplay) > 0);
294 >        while (XPending(ourdisplay) > 0);
295  
296 +        odev.inpready = 0;
297 +
298          return(inpresflags);
299   }
300  
# Line 239 | Line 321 | int
321   dev_flush()                     /* flush output */
322   {
323          qtUpdate();
324 <        return(XPending(ourdisplay));
324 >        rayqleft = RAYQLEN;
325 >        return(odev.inpready = XPending(ourdisplay));
326   }
327  
328  
# Line 379 | Line 462 | ilclip(dp, wp)                 /* clip world coordinates to device *
462   int     dp[2][2];
463   FVECT   wp[2];
464   {
465 <        static FVECT    vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE};
466 <        FVECT   ip[2];
467 <                                /* not exactly right, but who cares? */
468 <        viewloc(ip[0], &odev.v, wp[0]);
469 <        viewloc(ip[1], &odev.v, wp[1]);
465 >        static FVECT    vmin = {0.,0.,0.}, vmax = {1.-FTINY,1.-FTINY,FHUGE};
466 >        FVECT   wpc[2], ip[2];
467 >        double  d, d0, d1;
468 >                                /* check for points behind view */
469 >        d = DOT(odev.v.vp, odev.v.vdir);
470 >        d0 = DOT(wp[0], odev.v.vdir) - d;
471 >        d1 = DOT(wp[1], odev.v.vdir) - d;
472 >                                /* work on copy of world points */
473 >        if (d0 <= d1) {
474 >                VCOPY(wpc[0], wp[0]); VCOPY(wpc[1], wp[1]);
475 >        } else {
476 >                d = d0; d0 = d1; d1 = d;
477 >                VCOPY(wpc[1], wp[0]); VCOPY(wpc[0], wp[1]);
478 >        }
479 >        if (d0 <= FTINY) {
480 >                if (d1 <= FTINY) return(0);
481 >                VSUB(wpc[0], wpc[0], wpc[1]);
482 >                d = .99*d1/(d1-d0);
483 >                VSUM(wpc[0], wpc[1], wpc[0], d);
484 >        }
485 >                                /* get view coordinates and clip to window */
486 >        viewloc(ip[0], &odev.v, wpc[0]);
487 >        viewloc(ip[1], &odev.v, wpc[1]);
488          if (!clip(ip[0], ip[1], vmin, vmax))
489                  return(0);
490          dp[0][0] = ip[0][0]*odev.hres;
# Line 414 | Line 515 | draw_grids()                   /* draw holodeck section grids */
515          static BYTE     gridrgb[3] = {0x0, 0xff, 0xff};
516          unsigned long  pixel;
517  
417        if (!mapped || odev.v.type != VT_PER)
418                return;
518          if (ncolors > 0)
519                  pixel = pixval[get_pixel(gridrgb, xnewcolr)];
520          else
# Line 427 | Line 526 | draw_grids()                   /* draw holodeck section grids */
526  
527  
528   static
529 < moveview(dx, dy, move)          /* move our view */
530 < int     dx, dy, move;
529 > moveview(dx, dy, mov, orb)      /* move our view */
530 > int     dx, dy, mov, orb;
531   {
532          VIEW    nv;
533 +        FVECT   odir, v1;
534          double  d;
535 <        register int    i, li;
535 >        register int    li;
536                                  /* start with old view */
537          copystruct(&nv, &odev.v);
538                                  /* change view direction */
539 <        if (move) {
539 >        if (mov | orb) {
540                  if ((li = qtFindLeaf(dx, dy)) < 0)
541                          return(0);      /* not on window */
542 <                for (i = 0; i < 3; i++)
443 <                        nv.vdir[i] = qtL.wp[li][i] - nv.vp[i];
542 >                VSUM(odir, qtL.wp[li], nv.vp, -1.);
543          } else {
544                  if (viewray(nv.vp, nv.vdir, &odev.v,
545                                  (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
546                          return(0);      /* outside view */
547          }
548 <                                /* move viewpoint */
549 <        if (move > 0)
550 <                for (i = 0; i < 3; i++)
551 <                        nv.vp[i] += MOVPCT/100. * nv.vdir[i];
552 <        else if (move < 0)
553 <                for (i = 0; i < 3; i++)
554 <                        nv.vp[i] -= MOVPCT/100. * nv.vdir[i];
555 <        if (move && headlocked) {
556 <                d = 0;          /* bring head back to same height */
557 <                for (i = 0; i < 3; i++)
558 <                        d += odev.v.vup[i] * (odev.v.vp[i] - nv.vp[i]);
559 <                for (i = 0; i < 3; i++)
560 <                        nv.vp[i] += d * odev.v.vup[i];
548 >        if (orb && mov) {               /* orbit left/right */
549 >                spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
550 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
551 >                spinvector(nv.vdir, nv.vdir, nv.vup, d);
552 >        } else if (orb) {               /* orbit up/down */
553 >                fcross(v1, odir, nv.vup);
554 >                if (normalize(v1) == 0.)
555 >                        return(0);
556 >                spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
557 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
558 >                spinvector(nv.vdir, nv.vdir, v1, d);
559 >        } else if (mov) {               /* move forward/backward */
560 >                d = MOVPCT/100. * mov;
561 >                VSUM(nv.vp, nv.vp, odir, d);
562          }
563 +        if (!mov ^ !orb && headlocked) {        /* restore head height */
564 +                VSUM(v1, odev.v.vp, nv.vp, -1.);
565 +                d = DOT(v1, odev.v.vup);
566 +                VSUM(nv.vp, nv.vp, odev.v.vup, d);
567 +        }
568          if (setview(&nv) != NULL)
569                  return(0);      /* illegal view */
570          dev_view(&nv);
571 <        inpresflags |= DEV_NEWVIEW;
571 >        inpresflags |= DFL(DC_SETVIEW);
572          return(1);
573   }
574  
# Line 472 | Line 577 | static
577   getmove(ebut)                           /* get view change */
578   XButtonPressedEvent     *ebut;
579   {
580 <        int     whichbutton = ebut->button;
580 >        int     movdir = MOVDIR(ebut->button);
581 >        int     movorb = MOVORB(ebut->state);
582          int     oldnodesiz = qtMinNodesiz;
583          Window  rootw, childw;
584          int     rootx, rooty, wx, wy;
# Line 488 | Line 594 | XButtonPressedEvent    *ebut;
594                                  &rootx, &rooty, &wx, &wy, &statemask))
595                          break;          /* on another screen */
596  
597 <                if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton))) {
597 >                if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
598                          sleep(1);
599                          continue;
600                  }
# Line 496 | Line 602 | XButtonPressedEvent    *ebut;
602                  qtUpdate();
603                  draw_grids();
604          }
605 <        if (!(inpresflags & DEV_NEWVIEW)) {     /* do final motion */
606 <                whichbutton = levptr(XButtonReleasedEvent)->button;
605 >        if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
606 >                movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
607                  wx = levptr(XButtonReleasedEvent)->x;
608                  wy = levptr(XButtonReleasedEvent)->y;
609 <                moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
609 >                moveview(wx, odev.vres-1-wy, movdir, movorb);
610          }
611          dev_flush();
612  
# Line 525 | Line 631 | register XKeyPressedEvent  *ekey;
631          case 'H':                       /* turn off height motion lock */
632                  headlocked = 0;
633                  return;
634 <        case CTRL('S'):
634 >        case 'l':                       /* retrieve last view */
635 >                inpresflags |= DFL(DC_LASTVIEW);
636 >                return;
637          case 'p':                       /* pause computation */
638 <                inpresflags |= DEV_WAIT;
638 >                inpresflags |= DFL(DC_PAUSE);
639                  return;
640          case 'v':                       /* spit out view */
641 <                fputs(VIEWSTR, stderr);
534 <                fprintview(&odev.v, stderr);
535 <                fputc('\n', stderr);
641 >                inpresflags |= DFL(DC_GETVIEW);
642                  return;
537        case CTRL('Q'):
643          case '\n':
644          case '\r':                      /* resume computation */
645 <                inpresflags |= DEV_RESUME;
645 >                inpresflags |= DFL(DC_RESUME);
646                  return;
647          case CTRL('R'):                 /* redraw screen */
648                  if (ncolors > 0)
# Line 545 | Line 650 | register XKeyPressedEvent  *ekey;
650                  qtRedraw(0, 0, odev.hres, odev.vres);
651                  return;
652          case CTRL('L'):                 /* refresh from server */
653 <                if (inpresflags & DEV_REDRAW)
653 >                if (inpresflags & DFL(DC_REDRAW))
654                          return;
655                  XClearWindow(ourdisplay, gwind);
656                  draw_grids();
# Line 553 | Line 658 | register XKeyPressedEvent  *ekey;
658                  qtCompost(100);                 /* unload the old tree */
659                  if (ncolors > 0)
660                          new_ctab(ncolors);
661 <                inpresflags |= DEV_REDRAW;      /* resend values from server */
661 >                inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
662 >                rayqleft = 0;                   /* hold off update */
663                  return;
664 <        case CTRL('D'):
665 <        case 'Q':
664 >        case 'K':                       /* kill rtrace process(es) */
665 >                inpresflags |= DFL(DC_KILL);
666 >                break;
667 >        case 'R':                       /* restart rtrace */
668 >                inpresflags |= DFL(DC_RESTART);
669 >                break;
670 >        case 'C':                       /* clobber holodeck */
671 >                inpresflags |= DFL(DC_CLOBBER);
672 >                break;
673          case 'q':                       /* quit the program */
674 <                inpresflags |= DEV_SHUTDOWN;
674 >                inpresflags |= DFL(DC_QUIT);
675                  return;
676          default:
677                  XBell(ourdisplay, 0);
# Line 574 | Line 687 | register XExposeEvent  *eexp;
687          if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
688                  odev.hres = eexp->width;
689                  odev.vres = eexp->height;
577                inpresflags |= DEV_NEWSIZE;
690          }
691          qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
692                          eexp->x + eexp->width, odev.vres - eexp->y);
# Line 588 | Line 700 | register XConfigureEvent  *ersz;
700          if (ersz->width == odev.hres && ersz->height == odev.vres)
701                  return;
702  
591        if (odev.hres != 0 && odev.vres != 0) {
592                odev.v.horiz = 2.*180./PI * atan(
593                        tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
594                odev.v.vert = 2.*180./PI * atan(
595                        tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
596                inpresflags |= DEV_NEWVIEW;
597        }
703          odev.hres = ersz->width;
704          odev.vres = ersz->height;
705  
706 <        inpresflags |= DEV_NEWSIZE;
706 >        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
707 >        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
708 >
709 >        inpresflags |= DFL(DC_SETVIEW);
710   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines