ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.21
Committed: Thu Jan 1 14:48:46 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.20: +2 -2 lines
Log Message:
increased leaf allocation by 50%

File Contents

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