ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.19
Committed: Wed Dec 24 16:56:00 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.18: +1 -1 lines
Log Message:
allow (slightly wrong) grid lines to be drawn even if !VT_PER

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