ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.4
Committed: Fri Nov 21 13:35:58 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.3: +7 -5 lines
Log Message:
changed definition of global leaf pile to make tone mapping work better

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    
53     static XVisualInfo ourvinfo; /* our visual information */
54    
55     static Window gwind = 0; /* our graphics window */
56    
57     static GC ourgc = 0; /* our graphics context for drawing */
58    
59     static Colormap ourmap = 0; /* our color map */
60    
61     static double pwidth, pheight; /* pixel dimensions (mm) */
62    
63     static int inpresflags; /* input result flags */
64    
65 gregl 3.2 static int headlocked = 0; /* lock vertical motion */
66 gregl 3.1
67     static int getpixels(), xnewcolr(), freepixels(), resizewindow(),
68 gregl 3.2 getevent(), getkey(), moveview(), getmove(), fixwindow();
69 gregl 3.1 static unsigned long true_pixel();
70    
71    
72     static int
73     mytmflags() /* figure out tone mapping flags */
74     {
75     extern char *progname;
76     register char *cp, *tail;
77     /* find basic name */
78     for (cp = tail = progname; *cp; cp++)
79     if (*cp == '/')
80     tail = cp+1;
81     for (cp = tail; *cp && *cp != '.'; cp++)
82     ;
83     if (cp-tail == 3 && !strncmp(tail, "x11", 3))
84     return(TM_F_CAMERA);
85     if (cp-tail == 4 && !strncmp(tail, "x11h", 4))
86     return(TM_F_HUMAN);
87     error(USER, "illegal driver name");
88     }
89    
90    
91     dev_open(id) /* initialize X11 driver */
92     char *id;
93     {
94     extern char *getenv();
95     char *gv;
96     double gamval = GAMMA;
97     int nplanes;
98     XSetWindowAttributes ourwinattr;
99     XWMHints ourxwmhints;
100     XSizeHints oursizhints;
101 gregl 3.2 /* set quadtree globals */
102     qtDepthEps = 0.02;
103 gregl 3.4 qtMinNodesiz = 2;
104 gregl 3.1 /* open display server */
105     ourdisplay = XOpenDisplay(NULL);
106     if (ourdisplay == NULL)
107     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
108     /* find a usable visual */
109     nplanes = DisplayPlanes(ourdisplay, ourscreen);
110     if (XMatchVisualInfo(ourdisplay,ourscreen,
111     nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
112     XMatchVisualInfo(ourdisplay,ourscreen,
113     nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
114     ourblack = 0;
115     ourwhite = ourvinfo.red_mask |
116     ourvinfo.green_mask |
117     ourvinfo.blue_mask ;
118     } else {
119     if (nplanes < 4)
120     error(INTERNAL, "not enough colors\n");
121     if (!XMatchVisualInfo(ourdisplay,ourscreen,
122     nplanes,PseudoColor,&ourvinfo) &&
123     !XMatchVisualInfo(ourdisplay,ourscreen,
124     nplanes,GrayScale,&ourvinfo))
125     error(INTERNAL, "unsupported visual type\n");
126     ourblack = BlackPixel(ourdisplay,ourscreen);
127     ourwhite = WhitePixel(ourdisplay,ourscreen);
128     }
129     /* set gamma and tone mapping */
130     if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
131     || (gv = getenv("DISPLAY_GAMMA")) != NULL)
132     gamval = atof(gv);
133     if (tmInit(mytmflags(), stdprims, gamval) == NULL)
134     error(SYSTEM, "not enough memory in dev_open");
135     /* open window */
136     ourwinattr.background_pixel = ourblack;
137     ourwinattr.border_pixel = ourblack;
138     ourwinattr.event_mask = ourmask;
139     /* this is stupid */
140     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
141     ourvinfo.visual, AllocNone);
142     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
143     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
144     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
145     BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
146     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
147     if (gwind == 0)
148     error(SYSTEM, "cannot create window\n");
149     XStoreName(ourdisplay, gwind, id);
150     /* get graphics context */
151     ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
152     /* set window manager hints */
153     ourxwmhints.flags = InputHint|IconPixmapHint;
154     ourxwmhints.input = True;
155     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
156     gwind, x11icon_bits, x11icon_width, x11icon_height);
157     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
158     oursizhints.min_width = MINWIDTH;
159     oursizhints.min_height = MINHEIGHT;
160     oursizhints.flags = PMinSize;
161     XSetNormalHints(ourdisplay, gwind, &oursizhints);
162     /* map the window and get its size */
163     XMapWindow(ourdisplay, gwind);
164     dev_input();
165 gregl 3.2 /* 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    
171 gregl 3.1 /* figure out sensible view */
172     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
173     DisplayWidth(ourdisplay, ourscreen);
174     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
175     DisplayHeight(ourdisplay, ourscreen);
176     copystruct(&odev.v, &stdview);
177     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);
181     odev.ifd = ConnectionNumber(ourdisplay);
182     }
183    
184    
185     dev_close() /* close our display */
186     {
187     freepixels();
188     XFreeGC(ourdisplay, ourgc);
189     XDestroyWindow(ourdisplay, gwind);
190     gwind = 0;
191     ourgc = 0;
192     XCloseDisplay(ourdisplay);
193     ourdisplay = NULL;
194     qtFreeLeaves();
195     tmDone(NULL);
196     odev.v.type = 0;
197     odev.hres = odev.vres = 0;
198     odev.ifd = -1;
199     }
200    
201    
202     dev_view(nv) /* assign new driver view */
203     VIEW *nv;
204     {
205     if (nv != &odev.v)
206     copystruct(&odev.v, nv);
207     qtReplant();
208     }
209    
210    
211     int
212     dev_input() /* get X11 input */
213     {
214     inpresflags = 0;
215     do
216     getevent();
217    
218     while (XQLength(ourdisplay) > 0);
219    
220     return(inpresflags);
221     }
222    
223    
224     dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */
225     BYTE rgb[3];
226     int xmin, ymin, xmax, ymax;
227     {
228     unsigned long pixel;
229    
230     if (!mapped)
231     return;
232     if (ncolors > 0)
233     pixel = pixval[get_pixel(rgb, xnewcolr)];
234     else
235     pixel = true_pixel(rgb);
236     XSetForeground(ourdisplay, ourgc, pixel);
237     XFillRectangle(ourdisplay, gwind,
238     ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin);
239     }
240    
241    
242     int
243     dev_flush() /* flush output */
244     {
245     qtUpdate();
246     return(XPending(ourdisplay));
247     }
248    
249    
250     static
251     xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
252     int ndx;
253     int r, g, b;
254     {
255     XColor xcolor;
256    
257     xcolor.pixel = pixval[ndx];
258     xcolor.red = r << 8;
259     xcolor.green = g << 8;
260     xcolor.blue = b << 8;
261     xcolor.flags = DoRed|DoGreen|DoBlue;
262    
263     XStoreColor(ourdisplay, ourmap, &xcolor);
264     }
265    
266    
267     static int
268     getpixels() /* get the color map */
269     {
270     XColor thiscolor;
271     register int i, j;
272    
273     if (ncolors > 0)
274     return(ncolors);
275     if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
276     ourmap = DefaultColormap(ourdisplay,ourscreen);
277     goto loop;
278     }
279     newmap:
280     ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
281     loop:
282     for (ncolors = ourvinfo.colormap_size;
283     ncolors > ourvinfo.colormap_size/3;
284     ncolors = ncolors*.937) {
285     pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
286     if (pixval == NULL)
287     return(ncolors = 0);
288     if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
289     break;
290     free((char *)pixval);
291     pixval = NULL;
292     }
293     if (pixval == NULL) {
294     if (ourmap == DefaultColormap(ourdisplay,ourscreen))
295     goto newmap; /* try it with our map */
296     else
297     return(ncolors = 0); /* failed */
298     }
299     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
300     for (i = 0; i < ncolors; i++) { /* reset black and white */
301     if (pixval[i] != ourblack && pixval[i] != ourwhite)
302     continue;
303     thiscolor.pixel = pixval[i];
304     thiscolor.flags = DoRed|DoGreen|DoBlue;
305     XQueryColor(ourdisplay,
306     DefaultColormap(ourdisplay,ourscreen),
307     &thiscolor);
308     XStoreColor(ourdisplay, ourmap, &thiscolor);
309     for (j = i; j+1 < ncolors; j++)
310     pixval[j] = pixval[j+1];
311     ncolors--;
312     i--;
313     }
314     XSetWindowColormap(ourdisplay, gwind, ourmap);
315     return(ncolors);
316     }
317    
318    
319     static
320     freepixels() /* free our pixels */
321     {
322     if (ncolors == 0)
323     return;
324     XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
325     free((char *)pixval);
326     pixval = NULL;
327     ncolors = 0;
328     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
329     XFreeColormap(ourdisplay, ourmap);
330     ourmap = 0;
331     }
332    
333    
334     static unsigned long
335     true_pixel(rgb) /* return true pixel value for color */
336     register BYTE rgb[3];
337     {
338     register unsigned long rval;
339    
340     rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
341     rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
342     rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
343     return(rval);
344     }
345    
346    
347     static
348     getevent() /* get next event */
349     {
350     XNextEvent(ourdisplay, levptr(XEvent));
351     switch (levptr(XEvent)->type) {
352     case ConfigureNotify:
353     resizewindow(levptr(XConfigureEvent));
354     break;
355     case UnmapNotify:
356     mapped = 0;
357     freepixels();
358     break;
359     case MapNotify:
360     if (ourvinfo.class == PseudoColor ||
361     ourvinfo.class == GrayScale) {
362     if (getpixels() == 0)
363     error(SYSTEM, "cannot allocate colors\n");
364     new_ctab(ncolors);
365     }
366     mapped = 1;
367     break;
368     case Expose:
369     fixwindow(levptr(XExposeEvent));
370     break;
371     case KeyPress:
372     getkey(levptr(XKeyPressedEvent));
373     break;
374     case ButtonPress:
375 gregl 3.2 getmove(levptr(XButtonPressedEvent));
376 gregl 3.1 break;
377     }
378     }
379    
380    
381     static
382 gregl 3.2 moveview(dx, dy, move) /* move our view */
383     int dx, dy, move;
384     {
385     VIEW nv;
386     double d;
387 gregl 3.4 register int i, li;
388 gregl 3.2 /* start with old view */
389     copystruct(&nv, &odev.v);
390     /* change view direction */
391     if (move) {
392 gregl 3.4 if ((li = qtFindLeaf(dx, dy)) < 0)
393 gregl 3.2 return(0); /* not on window */
394     for (i = 0; i < 3; i++)
395 gregl 3.4 nv.vdir[i] = qtL.wp[li][i] - nv.vp[i];
396 gregl 3.2 } else {
397     if (viewray(nv.vp, nv.vdir, &odev.v,
398     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
399     return(0); /* outside view */
400     }
401     /* move viewpoint */
402     if (move > 0)
403     for (i = 0; i < 3; i++)
404     nv.vp[i] += MOVPCT/100. * nv.vdir[i];
405     else if (move < 0)
406     for (i = 0; i < 3; i++)
407     nv.vp[i] -= MOVPCT/100. * nv.vdir[i];
408     if (move && headlocked) {
409     d = 0; /* bring head back to same height */
410     for (i = 0; i < 3; i++)
411     d += odev.v.vup[i] * (odev.v.vp[i] - nv.vp[i]);
412     for (i = 0; i < 3; i++)
413     nv.vp[i] += d * odev.v.vup[i];
414     }
415     if (setview(&nv) != NULL)
416     return(0); /* illegal view */
417     dev_view(&nv);
418     inpresflags |= DEV_NEWVIEW;
419     return(1);
420     }
421    
422    
423     static
424     getmove(ebut) /* get view change */
425     XButtonPressedEvent *ebut;
426     {
427     int whichbutton = ebut->button;
428     int oldnodesiz = qtMinNodesiz;
429     Window rootw, childw;
430     int rootx, rooty, wx, wy;
431     unsigned int statemask;
432    
433     qtMinNodesiz = 16; /* for quicker update */
434 gregl 3.4 qtCompost(50);
435 gregl 3.2
436     do {
437     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
438     &rootx, &rooty, &wx, &wy, &statemask))
439     break; /* on another screen */
440    
441     if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton)))
442     sleep(1);
443 gregl 3.4 else
444     qtUpdate();
445 gregl 3.2
446     } while (!XCheckMaskEvent(ourdisplay,
447     ButtonReleaseMask, levptr(XEvent)));
448    
449     if (!(inpresflags & DEV_NEWVIEW)) { /* do final motion */
450     whichbutton = levptr(XButtonReleasedEvent)->button;
451     wx = levptr(XButtonReleasedEvent)->x;
452     wy = levptr(XButtonReleasedEvent)->y;
453     moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
454     }
455    
456     qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */
457     }
458    
459    
460     static
461 gregl 3.1 getkey(ekey) /* get input key */
462     register XKeyPressedEvent *ekey;
463     {
464     int n;
465     char buf[8];
466    
467     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
468     if (n != 1)
469     return;
470     switch (buf[0]) {
471     case 'h': /* turn on height motion lock */
472 gregl 3.2 headlocked = 1;
473 gregl 3.1 return;
474     case 'H': /* turn off height motion lock */
475 gregl 3.2 headlocked = 0;
476 gregl 3.1 return;
477 gregl 3.2 case CTRL('Z'):
478     case 'p': /* pause computation */
479     inpresflags |= DEV_WAIT;
480     return;
481     case '\n':
482     case '\r': /* release */
483     return;
484 gregl 3.3 case CTRL('R'): /* redraw */
485     if (ncolors > 0)
486     new_ctab(ncolors);
487     qtRedraw(0, 0, odev.hres, odev.vres);
488     return;
489 gregl 3.2 case CTRL('D'):
490 gregl 3.1 case 'Q':
491     case 'q': /* quit the program */
492     inpresflags |= DEV_SHUTDOWN;
493     return;
494     default:
495     XBell(ourdisplay, 0);
496     return;
497     }
498     }
499    
500    
501     static
502     fixwindow(eexp) /* repair damage to window */
503     register XExposeEvent *eexp;
504     {
505     if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
506     odev.hres = eexp->width;
507     odev.vres = eexp->height;
508     inpresflags |= DEV_NEWSIZE;
509     }
510     qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
511     eexp->x + eexp->width, odev.vres - eexp->y);
512     }
513    
514    
515     static
516     resizewindow(ersz) /* resize window */
517     register XConfigureEvent *ersz;
518     {
519     if (ersz->width == odev.hres && ersz->height == odev.vres)
520     return;
521    
522     if (odev.hres != 0 && odev.vres != 0) {
523     odev.v.horiz = 2.*180./PI * atan(
524     tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
525     odev.v.vert = 2.*180./PI * atan(
526     tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
527     inpresflags |= DEV_NEWVIEW;
528     }
529     odev.hres = ersz->width;
530     odev.vres = ersz->height;
531    
532     inpresflags |= DEV_NEWSIZE;
533     }