ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.9
Committed: Tue Nov 25 16:52:51 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.8: +65 -13 lines
Log Message:
created gridlines() function to run through holodeck section grids

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * X11 driver for holodeck display.
9     * Based on rview driver.
10     */
11    
12     #include "standard.h"
13     #include "rhd_qtree.h"
14    
15     #include <X11/Xlib.h>
16     #include <X11/cursorfont.h>
17     #include <X11/Xutil.h>
18    
19     #include "x11icon.h"
20    
21 gregl 3.2 #define CTRL(c) ((c)-'@')
22    
23 gregl 3.1 #define GAMMA 2.2 /* default gamma correction */
24    
25 gregl 3.2 #define MOVPCT 10 /* percent distance to move */
26     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
27    
28 gregl 3.1 #define MINWIDTH 480 /* minimum graphics window width */
29     #define MINHEIGHT 400 /* minimum graphics window height */
30    
31     #define VIEWDIST 356 /* assumed viewing distance (mm) */
32    
33     #define BORWIDTH 5 /* border width */
34    
35     #define ourscreen DefaultScreen(ourdisplay)
36     #define ourroot RootWindow(ourdisplay,ourscreen)
37     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
38 gregl 3.2 ButtonPressMask|ButtonReleaseMask)
39 gregl 3.1
40     #define levptr(etype) ((etype *)&currentevent)
41    
42     struct driver odev; /* global device driver structure */
43    
44     static XEvent currentevent; /* current event */
45    
46     static int ncolors = 0; /* color table size */
47     static int mapped = 0; /* window is mapped? */
48     static unsigned long *pixval = NULL; /* allocated pixels */
49     static unsigned long ourblack=0, ourwhite=1;
50    
51     static Display *ourdisplay = NULL; /* our display */
52     static XVisualInfo ourvinfo; /* our visual information */
53     static Window gwind = 0; /* our graphics window */
54     static GC ourgc = 0; /* our graphics context for drawing */
55     static Colormap ourmap = 0; /* our color map */
56    
57     static double pwidth, pheight; /* pixel dimensions (mm) */
58    
59     static int inpresflags; /* input result flags */
60    
61 gregl 3.2 static int headlocked = 0; /* lock vertical motion */
62 gregl 3.1
63     static int getpixels(), xnewcolr(), freepixels(), resizewindow(),
64 gregl 3.2 getevent(), getkey(), moveview(), getmove(), fixwindow();
65 gregl 3.1 static unsigned long true_pixel();
66    
67    
68     static int
69     mytmflags() /* figure out tone mapping flags */
70     {
71     extern char *progname;
72     register char *cp, *tail;
73     /* find basic name */
74     for (cp = tail = progname; *cp; cp++)
75     if (*cp == '/')
76     tail = cp+1;
77     for (cp = tail; *cp && *cp != '.'; cp++)
78     ;
79     if (cp-tail == 3 && !strncmp(tail, "x11", 3))
80     return(TM_F_CAMERA);
81     if (cp-tail == 4 && !strncmp(tail, "x11h", 4))
82     return(TM_F_HUMAN);
83     error(USER, "illegal driver name");
84     }
85    
86    
87     dev_open(id) /* initialize X11 driver */
88     char *id;
89     {
90     extern char *getenv();
91     char *gv;
92     double gamval = GAMMA;
93     int nplanes;
94     XSetWindowAttributes ourwinattr;
95     XWMHints ourxwmhints;
96     XSizeHints oursizhints;
97 gregl 3.2 /* set quadtree globals */
98     qtDepthEps = 0.02;
99 gregl 3.4 qtMinNodesiz = 2;
100 gregl 3.1 /* open display server */
101     ourdisplay = XOpenDisplay(NULL);
102     if (ourdisplay == NULL)
103     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
104     /* find a usable visual */
105     nplanes = DisplayPlanes(ourdisplay, ourscreen);
106     if (XMatchVisualInfo(ourdisplay,ourscreen,
107     nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
108     XMatchVisualInfo(ourdisplay,ourscreen,
109     nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
110     ourblack = 0;
111     ourwhite = ourvinfo.red_mask |
112     ourvinfo.green_mask |
113     ourvinfo.blue_mask ;
114     } else {
115     if (nplanes < 4)
116     error(INTERNAL, "not enough colors\n");
117     if (!XMatchVisualInfo(ourdisplay,ourscreen,
118     nplanes,PseudoColor,&ourvinfo) &&
119     !XMatchVisualInfo(ourdisplay,ourscreen,
120     nplanes,GrayScale,&ourvinfo))
121     error(INTERNAL, "unsupported visual type\n");
122     ourblack = BlackPixel(ourdisplay,ourscreen);
123     ourwhite = WhitePixel(ourdisplay,ourscreen);
124     }
125     /* set gamma and tone mapping */
126     if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
127     || (gv = getenv("DISPLAY_GAMMA")) != NULL)
128     gamval = atof(gv);
129     if (tmInit(mytmflags(), stdprims, gamval) == NULL)
130     error(SYSTEM, "not enough memory in dev_open");
131     /* open window */
132     ourwinattr.background_pixel = ourblack;
133     ourwinattr.border_pixel = ourblack;
134     ourwinattr.event_mask = ourmask;
135     /* this is stupid */
136     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
137     ourvinfo.visual, AllocNone);
138     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
139     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
140     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
141     BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
142     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
143     if (gwind == 0)
144     error(SYSTEM, "cannot create window\n");
145     XStoreName(ourdisplay, gwind, id);
146     /* get graphics context */
147     ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
148     /* set window manager hints */
149     ourxwmhints.flags = InputHint|IconPixmapHint;
150     ourxwmhints.input = True;
151     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
152     gwind, x11icon_bits, x11icon_width, x11icon_height);
153     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
154     oursizhints.min_width = MINWIDTH;
155     oursizhints.min_height = MINHEIGHT;
156     oursizhints.flags = PMinSize;
157     XSetNormalHints(ourdisplay, gwind, &oursizhints);
158     /* map the window and get its size */
159     XMapWindow(ourdisplay, gwind);
160     dev_input();
161 gregl 3.2 /* allocate our leaf pile */
162     if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
163     DisplayHeight(ourdisplay,ourscreen) /
164     (qtMinNodesiz*qtMinNodesiz)))
165     error(SYSTEM, "insufficient memory for leaf storage");
166    
167 gregl 3.1 /* figure out sensible view */
168     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
169     DisplayWidth(ourdisplay, ourscreen);
170     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
171     DisplayHeight(ourdisplay, ourscreen);
172     copystruct(&odev.v, &stdview);
173     odev.name = id;
174     odev.v.type = VT_PER;
175     odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
176     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
177     odev.ifd = ConnectionNumber(ourdisplay);
178     }
179    
180    
181     dev_close() /* close our display */
182     {
183     freepixels();
184     XFreeGC(ourdisplay, ourgc);
185     XDestroyWindow(ourdisplay, gwind);
186     gwind = 0;
187     ourgc = 0;
188     XCloseDisplay(ourdisplay);
189     ourdisplay = NULL;
190     qtFreeLeaves();
191     tmDone(NULL);
192     odev.v.type = 0;
193     odev.hres = odev.vres = 0;
194     odev.ifd = -1;
195     }
196    
197    
198     dev_view(nv) /* assign new driver view */
199     VIEW *nv;
200     {
201     if (nv != &odev.v)
202     copystruct(&odev.v, nv);
203     qtReplant();
204     }
205    
206    
207     int
208     dev_input() /* get X11 input */
209     {
210     inpresflags = 0;
211 gregl 3.5
212 gregl 3.1 do
213     getevent();
214    
215     while (XQLength(ourdisplay) > 0);
216    
217     return(inpresflags);
218     }
219    
220    
221     dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */
222     BYTE rgb[3];
223     int xmin, ymin, xmax, ymax;
224     {
225     unsigned long pixel;
226    
227     if (!mapped)
228     return;
229     if (ncolors > 0)
230     pixel = pixval[get_pixel(rgb, xnewcolr)];
231     else
232     pixel = true_pixel(rgb);
233     XSetForeground(ourdisplay, ourgc, pixel);
234     XFillRectangle(ourdisplay, gwind,
235     ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin);
236     }
237    
238    
239     int
240     dev_flush() /* flush output */
241     {
242     qtUpdate();
243     return(XPending(ourdisplay));
244     }
245    
246    
247     static
248     xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
249     int ndx;
250     int r, g, b;
251     {
252     XColor xcolor;
253    
254     xcolor.pixel = pixval[ndx];
255     xcolor.red = r << 8;
256     xcolor.green = g << 8;
257     xcolor.blue = b << 8;
258     xcolor.flags = DoRed|DoGreen|DoBlue;
259    
260     XStoreColor(ourdisplay, ourmap, &xcolor);
261     }
262    
263    
264     static int
265     getpixels() /* get the color map */
266     {
267     XColor thiscolor;
268     register int i, j;
269    
270     if (ncolors > 0)
271     return(ncolors);
272     if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
273     ourmap = DefaultColormap(ourdisplay,ourscreen);
274     goto loop;
275     }
276     newmap:
277     ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
278     loop:
279     for (ncolors = ourvinfo.colormap_size;
280     ncolors > ourvinfo.colormap_size/3;
281     ncolors = ncolors*.937) {
282     pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
283     if (pixval == NULL)
284     return(ncolors = 0);
285     if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
286     break;
287     free((char *)pixval);
288     pixval = NULL;
289     }
290     if (pixval == NULL) {
291     if (ourmap == DefaultColormap(ourdisplay,ourscreen))
292     goto newmap; /* try it with our map */
293     else
294     return(ncolors = 0); /* failed */
295     }
296     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
297     for (i = 0; i < ncolors; i++) { /* reset black and white */
298     if (pixval[i] != ourblack && pixval[i] != ourwhite)
299     continue;
300     thiscolor.pixel = pixval[i];
301     thiscolor.flags = DoRed|DoGreen|DoBlue;
302     XQueryColor(ourdisplay,
303     DefaultColormap(ourdisplay,ourscreen),
304     &thiscolor);
305     XStoreColor(ourdisplay, ourmap, &thiscolor);
306     for (j = i; j+1 < ncolors; j++)
307     pixval[j] = pixval[j+1];
308     ncolors--;
309     i--;
310     }
311     XSetWindowColormap(ourdisplay, gwind, ourmap);
312     return(ncolors);
313     }
314    
315    
316     static
317     freepixels() /* free our pixels */
318     {
319     if (ncolors == 0)
320     return;
321     XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
322     free((char *)pixval);
323     pixval = NULL;
324     ncolors = 0;
325     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
326     XFreeColormap(ourdisplay, ourmap);
327     ourmap = 0;
328     }
329    
330    
331     static unsigned long
332     true_pixel(rgb) /* return true pixel value for color */
333     register BYTE rgb[3];
334     {
335     register unsigned long rval;
336    
337     rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
338     rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
339     rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
340     return(rval);
341     }
342    
343    
344     static
345     getevent() /* get next event */
346     {
347     XNextEvent(ourdisplay, levptr(XEvent));
348     switch (levptr(XEvent)->type) {
349     case ConfigureNotify:
350     resizewindow(levptr(XConfigureEvent));
351     break;
352     case UnmapNotify:
353     mapped = 0;
354     freepixels();
355     break;
356     case MapNotify:
357     if (ourvinfo.class == PseudoColor ||
358     ourvinfo.class == GrayScale) {
359     if (getpixels() == 0)
360     error(SYSTEM, "cannot allocate colors\n");
361     new_ctab(ncolors);
362     }
363     mapped = 1;
364     break;
365     case Expose:
366     fixwindow(levptr(XExposeEvent));
367     break;
368     case KeyPress:
369     getkey(levptr(XKeyPressedEvent));
370     break;
371     case ButtonPress:
372 gregl 3.2 getmove(levptr(XButtonPressedEvent));
373 gregl 3.1 break;
374     }
375     }
376    
377    
378     static
379 gregl 3.9 ilclip(dp, wp) /* clip world coordinates to device */
380     int dp[2][2];
381     FVECT wp[2];
382     {
383     static FVECT vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE};
384     FVECT ip[2];
385     /* not exactly right, but who cares? */
386     viewloc(ip[0], &odev.v, wp[0]);
387     viewloc(ip[1], &odev.v, wp[1]);
388     if (!clip(ip[0], ip[1], vmin, vmax))
389     return(0);
390     dp[0][0] = ip[0][0]*odev.hres;
391     dp[0][1] = ip[0][1]*odev.vres;
392     dp[1][0] = ip[1][0]*odev.hres;
393     dp[1][1] = ip[1][1]*odev.vres;
394     return(1);
395     }
396    
397    
398     static
399     draw3dline(wp) /* draw 3d line in world coordinates */
400     FVECT wp[2];
401     {
402     int dp[2][2];
403    
404     if (!ilclip(dp, wp))
405     return;
406     XDrawLine(ourdisplay, gwind, ourgc,
407     dp[0][0], odev.vres-1 - dp[0][1],
408     dp[1][0], odev.vres-1 - dp[1][1]);
409     }
410    
411    
412     static
413     draw_grids() /* draw holodeck section grids */
414     {
415     static BYTE gridrgb[3] = {0x0, 0xff, 0xff};
416     unsigned long pixel;
417    
418     if (!mapped || odev.v.type != VT_PER)
419     return;
420     if (ncolors > 0)
421     pixel = pixval[get_pixel(gridrgb, xnewcolr)];
422     else
423     pixel = true_pixel(gridrgb);
424     XSetForeground(ourdisplay, ourgc, pixel);
425     /* draw each grid line */
426     gridlines(draw3dline);
427     }
428    
429    
430     static
431 gregl 3.2 moveview(dx, dy, move) /* move our view */
432     int dx, dy, move;
433     {
434     VIEW nv;
435     double d;
436 gregl 3.4 register int i, li;
437 gregl 3.2 /* start with old view */
438     copystruct(&nv, &odev.v);
439     /* change view direction */
440     if (move) {
441 gregl 3.4 if ((li = qtFindLeaf(dx, dy)) < 0)
442 gregl 3.2 return(0); /* not on window */
443     for (i = 0; i < 3; i++)
444 gregl 3.4 nv.vdir[i] = qtL.wp[li][i] - nv.vp[i];
445 gregl 3.2 } else {
446     if (viewray(nv.vp, nv.vdir, &odev.v,
447     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
448     return(0); /* outside view */
449     }
450     /* move viewpoint */
451     if (move > 0)
452     for (i = 0; i < 3; i++)
453     nv.vp[i] += MOVPCT/100. * nv.vdir[i];
454     else if (move < 0)
455     for (i = 0; i < 3; i++)
456     nv.vp[i] -= MOVPCT/100. * nv.vdir[i];
457     if (move && headlocked) {
458     d = 0; /* bring head back to same height */
459     for (i = 0; i < 3; i++)
460     d += odev.v.vup[i] * (odev.v.vp[i] - nv.vp[i]);
461     for (i = 0; i < 3; i++)
462     nv.vp[i] += d * odev.v.vup[i];
463     }
464     if (setview(&nv) != NULL)
465     return(0); /* illegal view */
466     dev_view(&nv);
467     inpresflags |= DEV_NEWVIEW;
468     return(1);
469     }
470    
471    
472     static
473     getmove(ebut) /* get view change */
474     XButtonPressedEvent *ebut;
475     {
476     int whichbutton = ebut->button;
477     int oldnodesiz = qtMinNodesiz;
478     Window rootw, childw;
479     int rootx, rooty, wx, wy;
480     unsigned int statemask;
481    
482     qtMinNodesiz = 16; /* for quicker update */
483 gregl 3.9 XNoOp(ourdisplay);
484 gregl 3.2
485 gregl 3.9 while (!XCheckMaskEvent(ourdisplay,
486     ButtonReleaseMask, levptr(XEvent))) {
487    
488 gregl 3.2 if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
489     &rootx, &rooty, &wx, &wy, &statemask))
490     break; /* on another screen */
491    
492 gregl 3.9 if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton))) {
493 gregl 3.2 sleep(1);
494 gregl 3.9 continue;
495     }
496     XClearWindow(ourdisplay, gwind);
497     qtUpdate();
498     draw_grids();
499     }
500 gregl 3.2 if (!(inpresflags & DEV_NEWVIEW)) { /* do final motion */
501     whichbutton = levptr(XButtonReleasedEvent)->button;
502     wx = levptr(XButtonReleasedEvent)->x;
503     wy = levptr(XButtonReleasedEvent)->y;
504     moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
505     }
506 gregl 3.9 dev_flush();
507 gregl 3.2
508     qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */
509     }
510    
511    
512     static
513 gregl 3.1 getkey(ekey) /* get input key */
514     register XKeyPressedEvent *ekey;
515     {
516     int n;
517     char buf[8];
518    
519     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
520     if (n != 1)
521     return;
522     switch (buf[0]) {
523     case 'h': /* turn on height motion lock */
524 gregl 3.2 headlocked = 1;
525 gregl 3.1 return;
526     case 'H': /* turn off height motion lock */
527 gregl 3.2 headlocked = 0;
528 gregl 3.1 return;
529 gregl 3.5 case CTRL('S'):
530 gregl 3.2 case 'p': /* pause computation */
531     inpresflags |= DEV_WAIT;
532     return;
533 gregl 3.7 case 'v': /* spit out view */
534     fputs(VIEWSTR, stderr);
535     fprintview(&odev.v, stderr);
536     fputc('\n', stderr);
537     return;
538 gregl 3.5 case CTRL('Q'):
539 gregl 3.2 case '\n':
540 gregl 3.5 case '\r': /* resume computation */
541     inpresflags |= DEV_RESUME;
542 gregl 3.2 return;
543 gregl 3.7 case CTRL('R'): /* redraw screen */
544     if (ncolors > 0)
545     new_ctab(ncolors);
546     qtRedraw(0, 0, odev.hres, odev.vres);
547     return;
548     case CTRL('L'): /* refresh from server */
549 gregl 3.8 if (inpresflags & DEV_REDRAW)
550     return;
551 gregl 3.7 XClearWindow(ourdisplay, gwind);
552 gregl 3.9 draw_grids();
553 gregl 3.8 XFlush(ourdisplay);
554 gregl 3.6 qtCompost(100); /* unload the old tree */
555 gregl 3.3 if (ncolors > 0)
556     new_ctab(ncolors);
557 gregl 3.6 inpresflags |= DEV_REDRAW; /* resend values from server */
558 gregl 3.3 return;
559 gregl 3.2 case CTRL('D'):
560 gregl 3.1 case 'Q':
561     case 'q': /* quit the program */
562     inpresflags |= DEV_SHUTDOWN;
563     return;
564     default:
565     XBell(ourdisplay, 0);
566     return;
567     }
568     }
569    
570    
571     static
572     fixwindow(eexp) /* repair damage to window */
573     register XExposeEvent *eexp;
574     {
575     if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
576     odev.hres = eexp->width;
577     odev.vres = eexp->height;
578     inpresflags |= DEV_NEWSIZE;
579     }
580     qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
581     eexp->x + eexp->width, odev.vres - eexp->y);
582     }
583    
584    
585     static
586     resizewindow(ersz) /* resize window */
587     register XConfigureEvent *ersz;
588     {
589     if (ersz->width == odev.hres && ersz->height == odev.vres)
590     return;
591    
592     if (odev.hres != 0 && odev.vres != 0) {
593     odev.v.horiz = 2.*180./PI * atan(
594     tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
595     odev.v.vert = 2.*180./PI * atan(
596     tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
597     inpresflags |= DEV_NEWVIEW;
598     }
599     odev.hres = ersz->width;
600     odev.vres = ersz->height;
601    
602     inpresflags |= DEV_NEWSIZE;
603     }