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.7 by gregl, Mon Nov 24 15:17:06 1997 UTC vs.
Revision 3.25 by gwlarson, Thu May 14 13:06:32 1998 UTC

# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ SGI";
18  
19   #include  "x11icon.h"
20  
21 < #define CTRL(c)         ((c)-'@')
21 > #ifndef RAYQLEN
22 > #define RAYQLEN         50000           /* max. rays to queue before flush */
23 > #endif
24  
25 + #ifndef FEQ
26 + #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
27 + #endif
28 +
29   #define GAMMA           2.2             /* default gamma correction */
30  
31 < #define MOVPCT          10              /* percent distance to move */
31 > #define MOVPCT          7               /* percent distance to move /frame */
32   #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
33 + #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */
34 + #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
35  
36   #define MINWIDTH        480             /* minimum graphics window width */
37   #define MINHEIGHT       400             /* minimum graphics window height */
# Line 49 | Line 57 | static unsigned long  *pixval = NULL;  /* allocated pix
57   static unsigned long  ourblack=0, ourwhite=1;
58  
59   static Display  *ourdisplay = NULL;     /* our display */
52
60   static XVisualInfo  ourvinfo;           /* our visual information */
54
61   static Window  gwind = 0;               /* our graphics window */
56
62   static GC  ourgc = 0;                   /* our graphics context for drawing */
58
63   static Colormap ourmap = 0;             /* our color map */
64  
65   static double   pwidth, pheight;        /* pixel dimensions (mm) */
# Line 81 | Line 85 | mytmflags()                    /* figure out tone mapping flags */
85          for (cp = tail; *cp && *cp != '.'; cp++)
86                  ;
87          if (cp-tail == 3 && !strncmp(tail, "x11", 3))
88 <                return(TM_F_CAMERA);
88 >                return(TM_F_CAMERA|TM_F_NOSTDERR);
89          if (cp-tail == 4 && !strncmp(tail, "x11h", 4))
90 <                return(TM_F_HUMAN);
90 >                return(TM_F_HUMAN|TM_F_NOSTDERR);
91          error(USER, "illegal driver name");
92   }
93  
# Line 92 | Line 96 | dev_open(id)                   /* initialize X11 driver */
96   char  *id;
97   {
98          extern char  *getenv();
99 <        char  *gv;
99 >        static RGBPRIMS myprims = STDPRIMS;
100 >        char  *ev;
101          double  gamval = GAMMA;
102 +        RGBPRIMP        dpri = stdprims;
103          int  nplanes;
104          XSetWindowAttributes    ourwinattr;
105          XWMHints  ourxwmhints;
106          XSizeHints      oursizhints;
107                                          /* set quadtree globals */
102        qtDepthEps = 0.02;
108          qtMinNodesiz = 2;
109                                          /* open display server */
110          ourdisplay = XOpenDisplay(NULL);
# Line 127 | Line 132 | char  *id;
132                  ourwhite = WhitePixel(ourdisplay,ourscreen);
133          }
134                                          /* set gamma and tone mapping */
135 <        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
136 <                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
137 <                gamval = atof(gv);
138 <        if (tmInit(mytmflags(), stdprims, gamval) == NULL)
135 >        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
136 >                        || (ev = getenv("DISPLAY_GAMMA")) != NULL)
137 >                gamval = atof(ev);
138 >        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
139 >                        sscanf(ev, "%f %f %f %f %f %f %f %f",
140 >                                &myprims[RED][CIEX],&myprims[RED][CIEY],
141 >                                &myprims[GRN][CIEX],&myprims[GRN][CIEY],
142 >                                &myprims[BLU][CIEX],&myprims[BLU][CIEY],
143 >                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
144 >                dpri = myprims;
145 >        if (tmInit(mytmflags(), dpri, gamval) == NULL)
146                  error(SYSTEM, "not enough memory in dev_open");
147                                          /* open window */
148          ourwinattr.background_pixel = ourblack;
# Line 159 | Line 171 | char  *id;
171          oursizhints.min_height = MINHEIGHT;
172          oursizhints.flags = PMinSize;
173          XSetNormalHints(ourdisplay, gwind, &oursizhints);
162                                        /* map the window and get its size */
163        XMapWindow(ourdisplay, gwind);
164        dev_input();
165                                        /* allocate our leaf pile */
166        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
167                        DisplayHeight(ourdisplay,ourscreen) /
168                        (qtMinNodesiz*qtMinNodesiz)))
169                error(SYSTEM, "insufficient memory for leaf storage");
170
174                                          /* figure out sensible view */
175          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
176                          DisplayWidth(ourdisplay, ourscreen);
177          pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
178                          DisplayHeight(ourdisplay, ourscreen);
179          copystruct(&odev.v, &stdview);
177        odev.name = id;
180          odev.v.type = VT_PER;
181 <        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
182 <        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
181 >                                        /* map the window and get its size */
182 >        XMapWindow(ourdisplay, gwind);
183 >        dev_input();                    /* sets size and view angles */
184 >                                        /* allocate our leaf pile */
185 >        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
186 >                        DisplayHeight(ourdisplay,ourscreen) * 3 /
187 >                        (qtMinNodesiz*qtMinNodesiz*2)))
188 >                error(SYSTEM, "insufficient memory for leaf storage");
189 >        odev.name = id;
190          odev.ifd = ConnectionNumber(ourdisplay);
191   }
192  
# Line 199 | Line 208 | dev_close()                    /* close our display */
208   }
209  
210  
211 +
212 + dev_clear()                     /* clear our quadtree */
213 + {
214 +        qtCompost(100);
215 +        if (ncolors > 0)
216 +                new_ctab(ncolors);
217 +        rayqleft = 0;                   /* hold off update */
218 + }
219 +
220 +
221 + int
222   dev_view(nv)                    /* assign new driver view */
223   VIEW    *nv;
224   {
225 <        if (nv != &odev.v)
225 >        if (nv->type == VT_PAR ||               /* check view legality */
226 >                        nv->horiz > 160. || nv->vert > 160.) {
227 >                error(COMMAND, "illegal view type/angle");
228 >                nv->type = VT_PER;
229 >                nv->horiz = odev.v.horiz;
230 >                nv->vert = odev.v.vert;
231 >                return(0);
232 >        }
233 >        if (nv->vfore > FTINY) {
234 >                error(COMMAND, "cannot handle fore clipping");
235 >                nv->vfore = 0.;
236 >                return(0);
237 >        }
238 >        if (nv != &odev.v) {
239 >                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
240 >                                !FEQ(nv->vert,odev.v.vert)) {
241 >                        int     dw = DisplayWidth(ourdisplay,ourscreen);
242 >                        int     dh = DisplayHeight(ourdisplay,ourscreen);
243 >
244 >                        dw -= 25;       /* for window frame */
245 >                        dh -= 50;
246 >                        odev.hres = 2.*VIEWDIST/pwidth *
247 >                                        tan(PI/180./2.*nv->horiz);
248 >                        odev.vres = 2.*VIEWDIST/pheight *
249 >                                        tan(PI/180./2.*nv->vert);
250 >                        if (odev.hres > dw) {
251 >                                odev.vres = dw * odev.vres / odev.hres;
252 >                                odev.hres = dw;
253 >                        }
254 >                        if (odev.vres > dh) {
255 >                                odev.hres = dh * odev.hres / odev.vres;
256 >                                odev.vres = dh;
257 >                        }
258 >                        XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
259 >                        dev_input();    /* wait for resize event */
260 >                }
261                  copystruct(&odev.v, nv);
262 +        }
263          qtReplant();
264 +        return(1);
265   }
266  
267  
268 + VIEW *
269 + dev_auxview(n, hvres)           /* return nth auxiliary view */
270 + int     n;
271 + int     hvres[2];
272 + {
273 +        if (n)
274 +                return(NULL);
275 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
276 +        return(&odev.v);
277 + }
278 +
279 +
280   int
281   dev_input()                     /* get X11 input */
282   {
# Line 244 | Line 313 | int
313   dev_flush()                     /* flush output */
314   {
315          qtUpdate();
316 +        rayqleft = RAYQLEN;
317          return(XPending(ourdisplay));
318   }
319  
# Line 380 | Line 450 | getevent()                     /* get next event */
450  
451  
452   static
453 < moveview(dx, dy, move)          /* move our view */
454 < int     dx, dy, move;
453 > ilclip(dp, wp)                  /* clip world coordinates to device */
454 > int     dp[2][2];
455 > FVECT   wp[2];
456   {
457 +        static FVECT    vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE};
458 +        FVECT   ip[2];
459 +                                /* not exactly right, but who cares? */
460 +        viewloc(ip[0], &odev.v, wp[0]);
461 +        viewloc(ip[1], &odev.v, wp[1]);
462 +        if (!clip(ip[0], ip[1], vmin, vmax))
463 +                return(0);
464 +        dp[0][0] = ip[0][0]*odev.hres;
465 +        dp[0][1] = ip[0][1]*odev.vres;
466 +        dp[1][0] = ip[1][0]*odev.hres;
467 +        dp[1][1] = ip[1][1]*odev.vres;
468 +        return(1);
469 + }
470 +
471 +
472 + static
473 + draw3dline(wp)                  /* draw 3d line in world coordinates */
474 + FVECT   wp[2];
475 + {
476 +        int     dp[2][2];
477 +
478 +        if (!ilclip(dp, wp))
479 +                return;
480 +        XDrawLine(ourdisplay, gwind, ourgc,
481 +                        dp[0][0], odev.vres-1 - dp[0][1],
482 +                        dp[1][0], odev.vres-1 - dp[1][1]);
483 + }
484 +
485 +
486 + static
487 + draw_grids()                    /* draw holodeck section grids */
488 + {
489 +        static BYTE     gridrgb[3] = {0x0, 0xff, 0xff};
490 +        unsigned long  pixel;
491 +
492 +        if (!mapped)
493 +                return;
494 +        if (ncolors > 0)
495 +                pixel = pixval[get_pixel(gridrgb, xnewcolr)];
496 +        else
497 +                pixel = true_pixel(gridrgb);
498 +        XSetForeground(ourdisplay, ourgc, pixel);
499 +                                        /* draw each grid line */
500 +        gridlines(draw3dline);
501 + }
502 +
503 +
504 + static
505 + moveview(dx, dy, mov, orb)      /* move our view */
506 + int     dx, dy, mov, orb;
507 + {
508          VIEW    nv;
509 +        FVECT   odir, v1;
510          double  d;
511 <        register int    i, li;
511 >        register int    li;
512                                  /* start with old view */
513          copystruct(&nv, &odev.v);
514                                  /* change view direction */
515 <        if (move) {
515 >        if (mov | orb) {
516                  if ((li = qtFindLeaf(dx, dy)) < 0)
517                          return(0);      /* not on window */
518 <                for (i = 0; i < 3; i++)
396 <                        nv.vdir[i] = qtL.wp[li][i] - nv.vp[i];
518 >                VSUM(odir, qtL.wp[li], nv.vp, -1.);
519          } else {
520                  if (viewray(nv.vp, nv.vdir, &odev.v,
521                                  (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
522                          return(0);      /* outside view */
523          }
524 <                                /* move viewpoint */
525 <        if (move > 0)
526 <                for (i = 0; i < 3; i++)
527 <                        nv.vp[i] += MOVPCT/100. * nv.vdir[i];
528 <        else if (move < 0)
529 <                for (i = 0; i < 3; i++)
530 <                        nv.vp[i] -= MOVPCT/100. * nv.vdir[i];
531 <        if (move && headlocked) {
532 <                d = 0;          /* bring head back to same height */
533 <                for (i = 0; i < 3; i++)
534 <                        d += odev.v.vup[i] * (odev.v.vp[i] - nv.vp[i]);
535 <                for (i = 0; i < 3; i++)
536 <                        nv.vp[i] += d * odev.v.vup[i];
524 >        if (orb && mov) {               /* orbit left/right */
525 >                spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
526 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
527 >                spinvector(nv.vdir, nv.vdir, nv.vup, d);
528 >        } else if (orb) {               /* orbit up/down */
529 >                fcross(v1, odir, nv.vup);
530 >                if (normalize(v1) == 0.)
531 >                        return(0);
532 >                spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
533 >                VSUM(nv.vp, qtL.wp[li], odir, -1.);
534 >                spinvector(nv.vdir, nv.vdir, v1, d);
535 >        } else if (mov) {               /* move forward/backward */
536 >                d = MOVPCT/100. * mov;
537 >                VSUM(nv.vp, nv.vp, odir, d);
538          }
539 +        if (!mov ^ !orb && headlocked) {        /* restore head height */
540 +                VSUM(v1, odev.v.vp, nv.vp, -1.);
541 +                d = DOT(v1, odev.v.vup);
542 +                VSUM(nv.vp, nv.vp, odev.v.vup, d);
543 +        }
544          if (setview(&nv) != NULL)
545                  return(0);      /* illegal view */
546          dev_view(&nv);
547 <        inpresflags |= DEV_NEWVIEW;
547 >        inpresflags |= DFL(DC_SETVIEW);
548          return(1);
549   }
550  
# Line 425 | Line 553 | static
553   getmove(ebut)                           /* get view change */
554   XButtonPressedEvent     *ebut;
555   {
556 <        int     whichbutton = ebut->button;
556 >        int     movdir = MOVDIR(ebut->button);
557 >        int     movorb = MOVORB(ebut->state);
558          int     oldnodesiz = qtMinNodesiz;
559          Window  rootw, childw;
560          int     rootx, rooty, wx, wy;
561          unsigned int    statemask;
562  
563          qtMinNodesiz = 16;              /* for quicker update */
564 +        XNoOp(ourdisplay);
565  
566 <        do {
566 >        while (!XCheckMaskEvent(ourdisplay,
567 >                        ButtonReleaseMask, levptr(XEvent))) {
568 >
569                  if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
570                                  &rootx, &rooty, &wx, &wy, &statemask))
571                          break;          /* on another screen */
572  
573 <                if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton)))
573 >                if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
574                          sleep(1);
575 <                else
576 <                        qtUpdate();
577 <
578 <        } while (!XCheckMaskEvent(ourdisplay,
579 <                        ButtonReleaseMask, levptr(XEvent)));
580 <
581 <        if (!(inpresflags & DEV_NEWVIEW)) {     /* do final motion */
582 <                whichbutton = levptr(XButtonReleasedEvent)->button;
575 >                        continue;
576 >                }
577 >                XClearWindow(ourdisplay, gwind);
578 >                qtUpdate();
579 >                draw_grids();
580 >        }
581 >        if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
582 >                movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
583                  wx = levptr(XButtonReleasedEvent)->x;
584                  wy = levptr(XButtonReleasedEvent)->y;
585 <                moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
585 >                moveview(wx, odev.vres-1-wy, movdir, movorb);
586          }
587 +        dev_flush();
588  
589          qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
590   }
# Line 474 | Line 607 | register XKeyPressedEvent  *ekey;
607          case 'H':                       /* turn off height motion lock */
608                  headlocked = 0;
609                  return;
610 <        case CTRL('S'):
610 >        case 'l':                       /* retrieve last view */
611 >                inpresflags |= DFL(DC_LASTVIEW);
612 >                return;
613          case 'p':                       /* pause computation */
614 <                inpresflags |= DEV_WAIT;
614 >                inpresflags |= DFL(DC_PAUSE);
615                  return;
616          case 'v':                       /* spit out view */
617 <                fputs(VIEWSTR, stderr);
483 <                fprintview(&odev.v, stderr);
484 <                fputc('\n', stderr);
617 >                inpresflags |= DFL(DC_GETVIEW);
618                  return;
486        case CTRL('Q'):
619          case '\n':
620          case '\r':                      /* resume computation */
621 <                inpresflags |= DEV_RESUME;
621 >                inpresflags |= DFL(DC_RESUME);
622                  return;
623          case CTRL('R'):                 /* redraw screen */
624                  if (ncolors > 0)
# Line 494 | Line 626 | register XKeyPressedEvent  *ekey;
626                  qtRedraw(0, 0, odev.hres, odev.vres);
627                  return;
628          case CTRL('L'):                 /* refresh from server */
629 +                if (inpresflags & DFL(DC_REDRAW))
630 +                        return;
631                  XClearWindow(ourdisplay, gwind);
632 +                draw_grids();
633 +                XFlush(ourdisplay);
634                  qtCompost(100);                 /* unload the old tree */
635                  if (ncolors > 0)
636                          new_ctab(ncolors);
637 <                inpresflags |= DEV_REDRAW;      /* resend values from server */
637 >                inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
638 >                rayqleft = 0;                   /* hold off update */
639                  return;
640 <        case CTRL('D'):
641 <        case 'Q':
640 >        case 'K':                       /* kill rtrace process(es) */
641 >                inpresflags |= DFL(DC_KILL);
642 >                break;
643 >        case 'R':                       /* restart rtrace */
644 >                inpresflags |= DFL(DC_RESTART);
645 >                break;
646 >        case 'C':                       /* clobber holodeck */
647 >                inpresflags |= DFL(DC_CLOBBER);
648 >                break;
649          case 'q':                       /* quit the program */
650 <                inpresflags |= DEV_SHUTDOWN;
650 >                inpresflags |= DFL(DC_QUIT);
651                  return;
652          default:
653                  XBell(ourdisplay, 0);
# Line 519 | Line 663 | register XExposeEvent  *eexp;
663          if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
664                  odev.hres = eexp->width;
665                  odev.vres = eexp->height;
522                inpresflags |= DEV_NEWSIZE;
666          }
667          qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
668                          eexp->x + eexp->width, odev.vres - eexp->y);
# Line 533 | Line 676 | register XConfigureEvent  *ersz;
676          if (ersz->width == odev.hres && ersz->height == odev.vres)
677                  return;
678  
536        if (odev.hres != 0 && odev.vres != 0) {
537                odev.v.horiz = 2.*180./PI * atan(
538                        tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
539                odev.v.vert = 2.*180./PI * atan(
540                        tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
541                inpresflags |= DEV_NEWVIEW;
542        }
679          odev.hres = ersz->width;
680          odev.vres = ersz->height;
681  
682 <        inpresflags |= DEV_NEWSIZE;
682 >        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
683 >        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
684 >
685 >        inpresflags |= DFL(DC_SETVIEW);
686   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines