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.1 by gregl, Wed Nov 19 18:01:03 1997 UTC vs.
Revision 3.27 by gwlarson, Wed Aug 19 17:43:08 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 18 | Line 18 | static char SCCSid[] = "$SunId$ SGI";
18  
19   #include  "x11icon.h"
20  
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          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 */
38  
# Line 30 | Line 43 | static char SCCSid[] = "$SunId$ SGI";
43   #define  ourscreen      DefaultScreen(ourdisplay)
44   #define  ourroot        RootWindow(ourdisplay,ourscreen)
45   #define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\
46 <                        ButtonPressMask)
46 >                        ButtonPressMask|ButtonReleaseMask)
47  
48   #define  levptr(etype)  ((etype *)&currentevent)
49  
# Line 44 | 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 */
47
60   static XVisualInfo  ourvinfo;           /* our visual information */
49
61   static Window  gwind = 0;               /* our graphics window */
51
62   static GC  ourgc = 0;                   /* our graphics context for drawing */
53
63   static Colormap ourmap = 0;             /* our color map */
64  
65   static double   pwidth, pheight;        /* pixel dimensions (mm) */
66  
67   static int      inpresflags;            /* input result flags */
68  
69 < static int      heightlocked = 0;       /* lock vertical motion */
69 > static int      headlocked = 0;         /* lock vertical motion */
70  
71   static int  getpixels(), xnewcolr(), freepixels(), resizewindow(),
72 <                getevent(), getkey(), fixwindow();
72 >                getevent(), getkey(), moveview(), getmove(), fixwindow();
73   static unsigned long  true_pixel();
74  
75  
# Line 76 | 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 87 | 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;
93        int  n;
104          XSetWindowAttributes    ourwinattr;
105          XWMHints  ourxwmhints;
106          XSizeHints      oursizhints;
107 +                                        /* set quadtree globals */
108 +        qtMinNodesiz = 2;
109                                          /* open display server */
110          ourdisplay = XOpenDisplay(NULL);
111          if (ourdisplay == NULL)
# Line 120 | 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 152 | Line 171 | char  *id;
171          oursizhints.min_height = MINHEIGHT;
172          oursizhints.flags = PMinSize;
173          XSetNormalHints(ourdisplay, gwind, &oursizhints);
155                                        /* map the window and get its size */
156        XMapWindow(ourdisplay, gwind);
157        dev_input();
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);
164        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);
183 <        odev.ifd = ConnectionNumber(ourdisplay);
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 <        qtDepthEps = 0.02;
186 <        n = odev.hres < odev.vres ? odev.hres : odev.vres;
187 <        qtMinNodesiz = 1;
173 <        if (!qtAllocLeaves(n*n/(qtMinNodesiz*qtMinNodesiz)))
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  
193  
# Line 192 | 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 + dev_auxcom(cmd, args)           /* process an auxiliary command */
269 + char    *cmd, *args;
270 + {
271 +        sprintf(errmsg, "%s: unknown command", cmd);
272 +        error(COMMAND, errmsg);
273 + }
274 +
275 +
276 + VIEW *
277 + dev_auxview(n, hvres)           /* return nth auxiliary view */
278 + int     n;
279 + int     hvres[2];
280 + {
281 +        if (n)
282 +                return(NULL);
283 +        hvres[0] = odev.hres; hvres[1] = odev.vres;
284 +        return(&odev.v);
285 + }
286 +
287 +
288   int
289   dev_input()                     /* get X11 input */
290   {
291          inpresflags = 0;
292 +
293          do
294                  getevent();
295  
296 <        while (XQLength(ourdisplay) > 0);
296 >        while (XPending(ourdisplay) > 0);
297  
298 +        odev.inpready = 0;
299 +
300          return(inpresflags);
301   }
302  
# Line 236 | Line 323 | int
323   dev_flush()                     /* flush output */
324   {
325          qtUpdate();
326 <        return(XPending(ourdisplay));
326 >        rayqleft = RAYQLEN;
327 >        return(odev.inpready = XPending(ourdisplay));
328   }
329  
330  
# Line 365 | Line 453 | getevent()                     /* get next event */
453                  getkey(levptr(XKeyPressedEvent));
454                  break;
455          case ButtonPress:
456 <                /* getmove(levptr(XButtonPressedEvent)); */
456 >                getmove(levptr(XButtonPressedEvent));
457                  break;
458          }
459   }
460  
461  
462   static
463 + ilclip(dp, wp)                  /* clip world coordinates to device */
464 + int     dp[2][2];
465 + FVECT   wp[2];
466 + {
467 +        static FVECT    vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE};
468 +        FVECT   ip[2];
469 +                                /* not exactly right, but who cares? */
470 +        viewloc(ip[0], &odev.v, wp[0]);
471 +        viewloc(ip[1], &odev.v, wp[1]);
472 +        if (!clip(ip[0], ip[1], vmin, vmax))
473 +                return(0);
474 +        dp[0][0] = ip[0][0]*odev.hres;
475 +        dp[0][1] = ip[0][1]*odev.vres;
476 +        dp[1][0] = ip[1][0]*odev.hres;
477 +        dp[1][1] = ip[1][1]*odev.vres;
478 +        return(1);
479 + }
480 +
481 +
482 + static
483 + draw3dline(wp)                  /* draw 3d line in world coordinates */
484 + FVECT   wp[2];
485 + {
486 +        int     dp[2][2];
487 +
488 +        if (!ilclip(dp, wp))
489 +                return;
490 +        XDrawLine(ourdisplay, gwind, ourgc,
491 +                        dp[0][0], odev.vres-1 - dp[0][1],
492 +                        dp[1][0], odev.vres-1 - dp[1][1]);
493 + }
494 +
495 +
496 + static
497 + draw_grids()                    /* draw holodeck section grids */
498 + {
499 +        static BYTE     gridrgb[3] = {0x0, 0xff, 0xff};
500 +        unsigned long  pixel;
501 +
502 +        if (!mapped)
503 +                return;
504 +        if (ncolors > 0)
505 +                pixel = pixval[get_pixel(gridrgb, xnewcolr)];
506 +        else
507 +                pixel = true_pixel(gridrgb);
508 +        XSetForeground(ourdisplay, ourgc, pixel);
509 +                                        /* draw each grid line */
510 +        gridlines(draw3dline);
511 + }
512 +
513 +
514 + static
515 + moveview(dx, dy, mov, orb)      /* move our view */
516 + int     dx, dy, mov, orb;
517 + {
518 +        VIEW    nv;
519 +        FVECT   odir, v1;
520 +        double  d;
521 +        register int    li;
522 +                                /* start with old view */
523 +        copystruct(&nv, &odev.v);
524 +                                /* change view direction */
525 +        if (mov | orb) {
526 +                if ((li = qtFindLeaf(dx, dy)) < 0)
527 +                        return(0);      /* not on window */
528 +                VSUM(odir, qtL.wp[li], nv.vp, -1.);
529 +        } else {
530 +                if (viewray(nv.vp, nv.vdir, &odev.v,
531 +                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
532 +                        return(0);      /* outside view */
533 +        }
534 +        if (orb && mov) {               /* orbit left/right */
535 +                spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
536 +                VSUM(nv.vp, qtL.wp[li], odir, -1.);
537 +                spinvector(nv.vdir, nv.vdir, nv.vup, d);
538 +        } else if (orb) {               /* orbit up/down */
539 +                fcross(v1, odir, nv.vup);
540 +                if (normalize(v1) == 0.)
541 +                        return(0);
542 +                spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb);
543 +                VSUM(nv.vp, qtL.wp[li], odir, -1.);
544 +                spinvector(nv.vdir, nv.vdir, v1, d);
545 +        } else if (mov) {               /* move forward/backward */
546 +                d = MOVPCT/100. * mov;
547 +                VSUM(nv.vp, nv.vp, odir, d);
548 +        }
549 +        if (!mov ^ !orb && headlocked) {        /* restore head height */
550 +                VSUM(v1, odev.v.vp, nv.vp, -1.);
551 +                d = DOT(v1, odev.v.vup);
552 +                VSUM(nv.vp, nv.vp, odev.v.vup, d);
553 +        }
554 +        if (setview(&nv) != NULL)
555 +                return(0);      /* illegal view */
556 +        dev_view(&nv);
557 +        inpresflags |= DFL(DC_SETVIEW);
558 +        return(1);
559 + }
560 +
561 +
562 + static
563 + getmove(ebut)                           /* get view change */
564 + XButtonPressedEvent     *ebut;
565 + {
566 +        int     movdir = MOVDIR(ebut->button);
567 +        int     movorb = MOVORB(ebut->state);
568 +        int     oldnodesiz = qtMinNodesiz;
569 +        Window  rootw, childw;
570 +        int     rootx, rooty, wx, wy;
571 +        unsigned int    statemask;
572 +
573 +        qtMinNodesiz = 16;              /* for quicker update */
574 +        XNoOp(ourdisplay);
575 +
576 +        while (!XCheckMaskEvent(ourdisplay,
577 +                        ButtonReleaseMask, levptr(XEvent))) {
578 +
579 +                if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
580 +                                &rootx, &rooty, &wx, &wy, &statemask))
581 +                        break;          /* on another screen */
582 +
583 +                if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
584 +                        sleep(1);
585 +                        continue;
586 +                }
587 +                XClearWindow(ourdisplay, gwind);
588 +                qtUpdate();
589 +                draw_grids();
590 +        }
591 +        if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
592 +                movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
593 +                wx = levptr(XButtonReleasedEvent)->x;
594 +                wy = levptr(XButtonReleasedEvent)->y;
595 +                moveview(wx, odev.vres-1-wy, movdir, movorb);
596 +        }
597 +        dev_flush();
598 +
599 +        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
600 + }
601 +
602 +
603 + static
604   getkey(ekey)                            /* get input key */
605   register XKeyPressedEvent  *ekey;
606   {
# Line 383 | Line 612 | register XKeyPressedEvent  *ekey;
612                  return;
613          switch (buf[0]) {
614          case 'h':                       /* turn on height motion lock */
615 <                heightlocked = 1;
615 >                headlocked = 1;
616                  return;
617          case 'H':                       /* turn off height motion lock */
618 <                heightlocked = 0;
618 >                headlocked = 0;
619                  return;
620 <        case 'Q':
620 >        case 'l':                       /* retrieve last view */
621 >                inpresflags |= DFL(DC_LASTVIEW);
622 >                return;
623 >        case 'p':                       /* pause computation */
624 >                inpresflags |= DFL(DC_PAUSE);
625 >                return;
626 >        case 'v':                       /* spit out view */
627 >                inpresflags |= DFL(DC_GETVIEW);
628 >                return;
629 >        case '\n':
630 >        case '\r':                      /* resume computation */
631 >                inpresflags |= DFL(DC_RESUME);
632 >                return;
633 >        case CTRL('R'):                 /* redraw screen */
634 >                if (ncolors > 0)
635 >                        new_ctab(ncolors);
636 >                qtRedraw(0, 0, odev.hres, odev.vres);
637 >                return;
638 >        case CTRL('L'):                 /* refresh from server */
639 >                if (inpresflags & DFL(DC_REDRAW))
640 >                        return;
641 >                XClearWindow(ourdisplay, gwind);
642 >                draw_grids();
643 >                XFlush(ourdisplay);
644 >                qtCompost(100);                 /* unload the old tree */
645 >                if (ncolors > 0)
646 >                        new_ctab(ncolors);
647 >                inpresflags |= DFL(DC_REDRAW);  /* resend values from server */
648 >                rayqleft = 0;                   /* hold off update */
649 >                return;
650 >        case 'K':                       /* kill rtrace process(es) */
651 >                inpresflags |= DFL(DC_KILL);
652 >                break;
653 >        case 'R':                       /* restart rtrace */
654 >                inpresflags |= DFL(DC_RESTART);
655 >                break;
656 >        case 'C':                       /* clobber holodeck */
657 >                inpresflags |= DFL(DC_CLOBBER);
658 >                break;
659          case 'q':                       /* quit the program */
660 <                inpresflags |= DEV_SHUTDOWN;
660 >                inpresflags |= DFL(DC_QUIT);
661                  return;
662          default:
663                  XBell(ourdisplay, 0);
# Line 404 | Line 671 | fixwindow(eexp)                                /* repair damage to window */
671   register XExposeEvent  *eexp;
672   {
673          if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
407 eputs("Resizing window in fixwindow\n");
674                  odev.hres = eexp->width;
675                  odev.vres = eexp->height;
410                inpresflags |= DEV_NEWSIZE;
676          }
677          qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
678                          eexp->x + eexp->width, odev.vres - eexp->y);
# Line 421 | Line 686 | register XConfigureEvent  *ersz;
686          if (ersz->width == odev.hres && ersz->height == odev.vres)
687                  return;
688  
424        if (odev.hres != 0 && odev.vres != 0) {
425                odev.v.horiz = 2.*180./PI * atan(
426                        tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
427                odev.v.vert = 2.*180./PI * atan(
428                        tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
429                inpresflags |= DEV_NEWVIEW;
430        }
689          odev.hres = ersz->width;
690          odev.vres = ersz->height;
691  
692 <        inpresflags |= DEV_NEWSIZE;
692 >        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
693 >        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
694 >
695 >        inpresflags |= DFL(DC_SETVIEW);
696   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines