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.13 by gregl, Thu Dec 11 09:37:43 1997 UTC

# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ SGI";
18  
19   #include  "x11icon.h"
20  
21 + #ifndef FEQ
22 + #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY)
23 + #endif
24 +
25 + #define CTRL(c)         ((c)-'@')
26 +
27   #define GAMMA           2.2             /* default gamma correction */
28  
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 */
36  
# Line 30 | Line 41 | static char SCCSid[] = "$SunId$ SGI";
41   #define  ourscreen      DefaultScreen(ourdisplay)
42   #define  ourroot        RootWindow(ourdisplay,ourscreen)
43   #define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\
44 <                        ButtonPressMask)
44 >                        ButtonPressMask|ButtonReleaseMask)
45  
46   #define  levptr(etype)  ((etype *)&currentevent)
47  
# Line 44 | Line 55 | static unsigned long  *pixval = NULL;  /* allocated pix
55   static unsigned long  ourblack=0, ourwhite=1;
56  
57   static Display  *ourdisplay = NULL;     /* our display */
47
58   static XVisualInfo  ourvinfo;           /* our visual information */
49
59   static Window  gwind = 0;               /* our graphics window */
51
60   static GC  ourgc = 0;                   /* our graphics context for drawing */
53
61   static Colormap ourmap = 0;             /* our color map */
62  
63   static double   pwidth, pheight;        /* pixel dimensions (mm) */
64  
65   static int      inpresflags;            /* input result flags */
66  
67 < static int      heightlocked = 0;       /* lock vertical motion */
67 > static int      headlocked = 0;         /* lock vertical motion */
68  
69   static int  getpixels(), xnewcolr(), freepixels(), resizewindow(),
70 <                getevent(), getkey(), fixwindow();
70 >                getevent(), getkey(), moveview(), getmove(), fixwindow();
71   static unsigned long  true_pixel();
72  
73  
# Line 90 | Line 97 | char  *id;
97          char  *gv;
98          double  gamval = GAMMA;
99          int  nplanes;
93        int  n;
100          XSetWindowAttributes    ourwinattr;
101          XWMHints  ourxwmhints;
102          XSizeHints      oursizhints;
103 +                                        /* set quadtree globals */
104 +        qtMinNodesiz = 2;
105                                          /* open display server */
106          ourdisplay = XOpenDisplay(NULL);
107          if (ourdisplay == NULL)
# Line 155 | Line 163 | char  *id;
163                                          /* map the window and get its size */
164          XMapWindow(ourdisplay, gwind);
165          dev_input();
166 +                                        /* allocate our leaf pile */
167 +        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
168 +                        DisplayHeight(ourdisplay,ourscreen) /
169 +                        (qtMinNodesiz*qtMinNodesiz)))
170 +                error(SYSTEM, "insufficient memory for leaf storage");
171 +
172                                          /* figure out sensible view */
173          pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
174                          DisplayWidth(ourdisplay, ourscreen);
# Line 166 | Line 180 | char  *id;
180          odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
181          odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
182          odev.ifd = ConnectionNumber(ourdisplay);
169                                        /* allocate our leaf pile */
170        qtDepthEps = 0.02;
171        n = odev.hres < odev.vres ? odev.hres : odev.vres;
172        qtMinNodesiz = 1;
173        if (!qtAllocLeaves(n*n/(qtMinNodesiz*qtMinNodesiz)))
174                error(SYSTEM, "insufficient memory for leaf storage");
183   }
184  
185  
# Line 192 | Line 200 | dev_close()                    /* close our display */
200   }
201  
202  
203 + int
204   dev_view(nv)                    /* assign new driver view */
205   VIEW    *nv;
206   {
207 <        if (nv != &odev.v)
207 >        if (nv->type == VT_PAR ||               /* check view legality */
208 >                nv->horiz > 160. || nv->vert > 160.) {
209 >                error(COMMAND, "illegal view type/angle");
210 >                nv->type = VT_PER;
211 >                nv->horiz = odev.v.horiz;
212 >                nv->vert = odev.v.vert;
213 >                return(0);
214 >        }
215 >        if (nv->vfore > FTINY) {
216 >                error(COMMAND, "cannot handle fore clipping");
217 >                nv->vfore = 0.;
218 >                return(0);
219 >        }
220 >        if (nv != &odev.v) {
221 >                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */
222 >                                !FEQ(nv->vert,odev.v.vert)) {
223 >                        int     dw = DisplayWidth(ourdisplay,ourscreen);
224 >                        int     dh = DisplayHeight(ourdisplay,ourscreen);
225 >
226 >                        dw -= 25;       /* for window frame */
227 >                        dh -= 100;
228 >                        odev.hres = 2.*VIEWDIST/pwidth *
229 >                                        tan(PI/180./2.*nv->horiz);
230 >                        odev.vres = 2.*VIEWDIST/pheight *
231 >                                        tan(PI/180./2.*nv->vert);
232 >                        if (odev.hres > dw) {
233 >                                odev.vres = dw * odev.vres / odev.hres;
234 >                                odev.hres = dw;
235 >                        }
236 >                        if (odev.vres > dh) {
237 >                                odev.hres = dh * odev.hres / odev.vres;
238 >                                odev.vres = dh;
239 >                        }
240 >                        XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
241 >                }
242                  copystruct(&odev.v, nv);
243 +        }
244          qtReplant();
245 +        return(1);
246   }
247  
248  
# Line 205 | Line 250 | int
250   dev_input()                     /* get X11 input */
251   {
252          inpresflags = 0;
253 +
254          do
255                  getevent();
256  
# Line 365 | Line 411 | getevent()                     /* get next event */
411                  getkey(levptr(XKeyPressedEvent));
412                  break;
413          case ButtonPress:
414 <                /* getmove(levptr(XButtonPressedEvent)); */
414 >                getmove(levptr(XButtonPressedEvent));
415                  break;
416          }
417   }
418  
419  
420   static
421 + ilclip(dp, wp)                  /* clip world coordinates to device */
422 + int     dp[2][2];
423 + FVECT   wp[2];
424 + {
425 +        static FVECT    vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE};
426 +        FVECT   ip[2];
427 +                                /* not exactly right, but who cares? */
428 +        viewloc(ip[0], &odev.v, wp[0]);
429 +        viewloc(ip[1], &odev.v, wp[1]);
430 +        if (!clip(ip[0], ip[1], vmin, vmax))
431 +                return(0);
432 +        dp[0][0] = ip[0][0]*odev.hres;
433 +        dp[0][1] = ip[0][1]*odev.vres;
434 +        dp[1][0] = ip[1][0]*odev.hres;
435 +        dp[1][1] = ip[1][1]*odev.vres;
436 +        return(1);
437 + }
438 +
439 +
440 + static
441 + draw3dline(wp)                  /* draw 3d line in world coordinates */
442 + FVECT   wp[2];
443 + {
444 +        int     dp[2][2];
445 +
446 +        if (!ilclip(dp, wp))
447 +                return;
448 +        XDrawLine(ourdisplay, gwind, ourgc,
449 +                        dp[0][0], odev.vres-1 - dp[0][1],
450 +                        dp[1][0], odev.vres-1 - dp[1][1]);
451 + }
452 +
453 +
454 + static
455 + draw_grids()                    /* draw holodeck section grids */
456 + {
457 +        static BYTE     gridrgb[3] = {0x0, 0xff, 0xff};
458 +        unsigned long  pixel;
459 +
460 +        if (!mapped || odev.v.type != VT_PER)
461 +                return;
462 +        if (ncolors > 0)
463 +                pixel = pixval[get_pixel(gridrgb, xnewcolr)];
464 +        else
465 +                pixel = true_pixel(gridrgb);
466 +        XSetForeground(ourdisplay, ourgc, pixel);
467 +                                        /* draw each grid line */
468 +        gridlines(draw3dline);
469 + }
470 +
471 +
472 + static
473 + moveview(dx, dy, mov, orb)      /* move our view */
474 + int     dx, dy, mov, orb;
475 + {
476 +        VIEW    nv;
477 +        FVECT   v1;
478 +        double  d;
479 +        register int    li;
480 +                                /* start with old view */
481 +        copystruct(&nv, &odev.v);
482 +                                /* change view direction */
483 +        if (mov | orb) {
484 +                if ((li = qtFindLeaf(dx, dy)) < 0)
485 +                        return(0);      /* not on window */
486 +                VSUM(nv.vdir, qtL.wp[li], nv.vp, -1.);
487 +        } else {
488 +                if (viewray(nv.vp, nv.vdir, &odev.v,
489 +                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
490 +                        return(0);      /* outside view */
491 +        }
492 +        if (orb && mov) {               /* orbit left/right */
493 +                spinvector(nv.vdir, nv.vdir, nv.vup, MOVDEG*PI/180.*mov);
494 +                VSUM(nv.vp, qtL.wp[li], nv.vdir, -1.);
495 +        } else if (orb) {               /* orbit up/down */
496 +                fcross(v1, nv.vdir, nv.vup);
497 +                if (normalize(v1) == 0.)
498 +                        return(0);
499 +                spinvector(nv.vdir, nv.vdir, v1, MOVDEG*PI/180.*orb);
500 +                VSUM(nv.vp, qtL.wp[li], nv.vdir, -1.);
501 +        } else if (mov) {               /* move forward/backward */
502 +                d = MOVPCT/100. * mov;
503 +                VSUM(nv.vp, nv.vp, nv.vdir, d);
504 +        }
505 +        if (!mov ^ !orb && headlocked) {        /* restore head height */
506 +                VSUM(v1, odev.v.vp, nv.vp, -1.);
507 +                d = DOT(v1, odev.v.vup);
508 +                VSUM(nv.vp, nv.vp, odev.v.vup, d);
509 +        }
510 +        if (setview(&nv) != NULL)
511 +                return(0);      /* illegal view */
512 +        dev_view(&nv);
513 +        inpresflags |= DEV_NEWVIEW;
514 +        return(1);
515 + }
516 +
517 +
518 + static
519 + getmove(ebut)                           /* get view change */
520 + XButtonPressedEvent     *ebut;
521 + {
522 +        int     movdir = MOVDIR(ebut->button);
523 +        int     movorb = MOVORB(ebut->state);
524 +        int     oldnodesiz = qtMinNodesiz;
525 +        Window  rootw, childw;
526 +        int     rootx, rooty, wx, wy;
527 +        unsigned int    statemask;
528 +
529 +        qtMinNodesiz = 16;              /* for quicker update */
530 +        XNoOp(ourdisplay);
531 +
532 +        while (!XCheckMaskEvent(ourdisplay,
533 +                        ButtonReleaseMask, levptr(XEvent))) {
534 +
535 +                if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
536 +                                &rootx, &rooty, &wx, &wy, &statemask))
537 +                        break;          /* on another screen */
538 +
539 +                if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
540 +                        sleep(1);
541 +                        continue;
542 +                }
543 +                XClearWindow(ourdisplay, gwind);
544 +                qtUpdate();
545 +                draw_grids();
546 +        }
547 +        if (!(inpresflags & DEV_NEWVIEW)) {     /* do final motion */
548 +                movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
549 +                wx = levptr(XButtonReleasedEvent)->x;
550 +                wy = levptr(XButtonReleasedEvent)->y;
551 +                moveview(wx, odev.vres-1-wy, movdir, movorb);
552 +        }
553 +        dev_flush();
554 +
555 +        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */
556 + }
557 +
558 +
559 + static
560   getkey(ekey)                            /* get input key */
561   register XKeyPressedEvent  *ekey;
562   {
# Line 383 | Line 568 | register XKeyPressedEvent  *ekey;
568                  return;
569          switch (buf[0]) {
570          case 'h':                       /* turn on height motion lock */
571 <                heightlocked = 1;
571 >                headlocked = 1;
572                  return;
573          case 'H':                       /* turn off height motion lock */
574 <                heightlocked = 0;
574 >                headlocked = 0;
575                  return;
576 +        case 'l':                       /* retrieve last view */
577 +                inpresflags |= DEV_LASTVIEW;
578 +                return;
579 +        case CTRL('S'):
580 +        case 'p':                       /* pause computation */
581 +                inpresflags |= DEV_WAIT;
582 +                return;
583 +        case 'v':                       /* spit out view */
584 +                inpresflags |= DEV_PUTVIEW;
585 +                return;
586 +        case CTRL('Q'):
587 +        case '\n':
588 +        case '\r':                      /* resume computation */
589 +                inpresflags |= DEV_RESUME;
590 +                return;
591 +        case CTRL('R'):                 /* redraw screen */
592 +                if (ncolors > 0)
593 +                        new_ctab(ncolors);
594 +                qtRedraw(0, 0, odev.hres, odev.vres);
595 +                return;
596 +        case CTRL('L'):                 /* refresh from server */
597 +                if (inpresflags & DEV_REDRAW)
598 +                        return;
599 +                XClearWindow(ourdisplay, gwind);
600 +                draw_grids();
601 +                XFlush(ourdisplay);
602 +                qtCompost(100);                 /* unload the old tree */
603 +                if (ncolors > 0)
604 +                        new_ctab(ncolors);
605 +                inpresflags |= DEV_REDRAW;      /* resend values from server */
606 +                return;
607 +        case CTRL('D'):
608          case 'Q':
609          case 'q':                       /* quit the program */
610                  inpresflags |= DEV_SHUTDOWN;
# Line 404 | Line 621 | fixwindow(eexp)                                /* repair damage to window */
621   register XExposeEvent  *eexp;
622   {
623          if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
407 eputs("Resizing window in fixwindow\n");
624                  odev.hres = eexp->width;
625                  odev.vres = eexp->height;
626                  inpresflags |= DEV_NEWSIZE;
# Line 421 | Line 637 | register XConfigureEvent  *ersz;
637          if (ersz->width == odev.hres && ersz->height == odev.vres)
638                  return;
639  
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        }
640          odev.hres = ersz->width;
641          odev.vres = ersz->height;
642  
643 <        inpresflags |= DEV_NEWSIZE;
643 >        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
644 >        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
645 >
646 >        inpresflags |= DEV_NEWSIZE|DEV_NEWVIEW;
647   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines