ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.47
Committed: Fri Feb 12 00:53:56 2021 UTC (3 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 3.46: +3 -7 lines
Log Message:
refactor: Created comparison macros for RREAL and FVECT types

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.47 static const char RCSid[] = "$Id: rhd_x11.c,v 3.46 2018/10/05 19:19:16 greg Exp $";
3 gregl 3.1 #endif
4     /*
5     * X11 driver for holodeck display.
6     * Based on rview driver.
7     */
8    
9 schorsch 3.37 #include <stdlib.h>
10     #include <stdio.h>
11 gregl 3.1 #include <X11/Xlib.h>
12     #include <X11/cursorfont.h>
13     #include <X11/Xutil.h>
14 greg 3.32 #include <time.h>
15 schorsch 3.37
16     #include "platform.h"
17     #include "rtmath.h"
18     #include "rterror.h"
19     #include "plocate.h"
20     #include "rhdisp.h"
21 gwlarson 3.29 #include "rhd_qtree.h"
22 greg 3.38 #include "x11icon.h"
23 gregl 3.1
24 gregl 3.20 #ifndef RAYQLEN
25     #define RAYQLEN 50000 /* max. rays to queue before flush */
26     #endif
27    
28 gregl 3.1 #define GAMMA 2.2 /* default gamma correction */
29    
30 gwlarson 3.30 #define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask))
31    
32 gregl 3.13 #define MOVPCT 7 /* percent distance to move /frame */
33 gregl 3.2 #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
34 gregl 3.13 #define MOVDEG (-5) /* degrees to orbit CW/down /frame */
35     #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0)
36 gregl 3.2
37 gregl 3.1 #define MINWIDTH 480 /* minimum graphics window width */
38     #define MINHEIGHT 400 /* minimum graphics window height */
39    
40     #define VIEWDIST 356 /* assumed viewing distance (mm) */
41    
42     #define BORWIDTH 5 /* border width */
43    
44     #define ourscreen DefaultScreen(ourdisplay)
45     #define ourroot RootWindow(ourdisplay,ourscreen)
46     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
47 gregl 3.2 ButtonPressMask|ButtonReleaseMask)
48 gregl 3.1
49     #define levptr(etype) ((etype *)&currentevent)
50    
51     struct driver odev; /* global device driver structure */
52    
53 greg 3.38 TMstruct *tmGlobal; /* global tone-mapping structure */
54    
55 gwlarson 3.30 char odev_args[64]; /* command arguments */
56    
57 gregl 3.1 static XEvent currentevent; /* current event */
58    
59     static int ncolors = 0; /* color table size */
60     static int mapped = 0; /* window is mapped? */
61     static unsigned long *pixval = NULL; /* allocated pixels */
62     static unsigned long ourblack=0, ourwhite=1;
63    
64     static Display *ourdisplay = NULL; /* our display */
65     static XVisualInfo ourvinfo; /* our visual information */
66     static Window gwind = 0; /* our graphics window */
67     static GC ourgc = 0; /* our graphics context for drawing */
68     static Colormap ourmap = 0; /* our color map */
69    
70     static double pwidth, pheight; /* pixel dimensions (mm) */
71    
72     static int inpresflags; /* input result flags */
73    
74 gregl 3.2 static int headlocked = 0; /* lock vertical motion */
75 gregl 3.1
76 schorsch 3.37
77     static int mytmflags(void);
78     static void xnewcolr(int ndx, int r, int g, int b);
79     static int getpixels(void);
80     static void freepixels(void);
81 greg 3.45 static unsigned long true_pixel(uby8 rgb[3]);
82 schorsch 3.37 static void getevent(void);
83     static int ilclip(int dp[2][2], FVECT wp[2]);
84     static void draw3dline(FVECT wp[2]);
85     static void draw_grids(void);
86     static int moveview(int dx, int dy, int mov, int orb);
87     static void getframe(XButtonPressedEvent *ebut);
88     static void waitabit(void);
89     static void getmove(XButtonPressedEvent *ebut);
90 greg 3.45 static void getkey(XKeyPressedEvent *ekey);
91     static void fixwindow(XExposeEvent *eexp);
92     static void resizewindow(XConfigureEvent *ersz);
93 gregl 3.1
94    
95     static int
96 schorsch 3.37 mytmflags(void) /* figure out tone mapping flags */
97 gregl 3.1 {
98     extern char *progname;
99 greg 3.45 char *cp, *tail;
100 gregl 3.1 /* find basic name */
101     for (cp = tail = progname; *cp; cp++)
102     if (*cp == '/')
103     tail = cp+1;
104     for (cp = tail; *cp && *cp != '.'; cp++)
105     ;
106     if (cp-tail == 3 && !strncmp(tail, "x11", 3))
107 gregl 3.20 return(TM_F_CAMERA|TM_F_NOSTDERR);
108 gregl 3.1 if (cp-tail == 4 && !strncmp(tail, "x11h", 4))
109 gregl 3.20 return(TM_F_HUMAN|TM_F_NOSTDERR);
110 gregl 3.1 error(USER, "illegal driver name");
111 schorsch 3.37 return 0; /* pro forma return */
112 gregl 3.1 }
113    
114    
115 greg 3.46 void
116 schorsch 3.37 dev_open( /* initialize X11 driver */
117     char *id
118     )
119 gregl 3.1 {
120 gregl 3.24 static RGBPRIMS myprims = STDPRIMS;
121     char *ev;
122 gregl 3.1 double gamval = GAMMA;
123 gregl 3.24 RGBPRIMP dpri = stdprims;
124 gregl 3.1 int nplanes;
125     XSetWindowAttributes ourwinattr;
126     XWMHints ourxwmhints;
127     XSizeHints oursizhints;
128 gregl 3.2 /* set quadtree globals */
129 gregl 3.4 qtMinNodesiz = 2;
130 gregl 3.1 /* open display server */
131     ourdisplay = XOpenDisplay(NULL);
132     if (ourdisplay == NULL)
133     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
134     /* find a usable visual */
135     nplanes = DisplayPlanes(ourdisplay, ourscreen);
136     if (XMatchVisualInfo(ourdisplay,ourscreen,
137     nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
138     XMatchVisualInfo(ourdisplay,ourscreen,
139     nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
140     ourblack = 0;
141     ourwhite = ourvinfo.red_mask |
142     ourvinfo.green_mask |
143     ourvinfo.blue_mask ;
144     } else {
145     if (nplanes < 4)
146     error(INTERNAL, "not enough colors\n");
147     if (!XMatchVisualInfo(ourdisplay,ourscreen,
148     nplanes,PseudoColor,&ourvinfo) &&
149     !XMatchVisualInfo(ourdisplay,ourscreen,
150     nplanes,GrayScale,&ourvinfo))
151     error(INTERNAL, "unsupported visual type\n");
152     ourblack = BlackPixel(ourdisplay,ourscreen);
153     ourwhite = WhitePixel(ourdisplay,ourscreen);
154     }
155     /* set gamma and tone mapping */
156 gregl 3.24 if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
157     || (ev = getenv("DISPLAY_GAMMA")) != NULL)
158     gamval = atof(ev);
159     if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL &&
160     sscanf(ev, "%f %f %f %f %f %f %f %f",
161     &myprims[RED][CIEX],&myprims[RED][CIEY],
162     &myprims[GRN][CIEX],&myprims[GRN][CIEY],
163     &myprims[BLU][CIEX],&myprims[BLU][CIEY],
164     &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6)
165     dpri = myprims;
166 greg 3.38 tmGlobal = tmInit(mytmflags(), dpri, gamval);
167     if (tmGlobal == NULL)
168 gregl 3.1 error(SYSTEM, "not enough memory in dev_open");
169     /* open window */
170     ourwinattr.background_pixel = ourblack;
171     ourwinattr.border_pixel = ourblack;
172     ourwinattr.event_mask = ourmask;
173     /* this is stupid */
174     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
175     ourvinfo.visual, AllocNone);
176     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
177     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
178     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
179     BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
180     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
181     if (gwind == 0)
182     error(SYSTEM, "cannot create window\n");
183     XStoreName(ourdisplay, gwind, id);
184     /* get graphics context */
185     ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
186     /* set window manager hints */
187     ourxwmhints.flags = InputHint|IconPixmapHint;
188     ourxwmhints.input = True;
189 greg 3.41 ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind,
190     (char *)x11icon_bits, x11icon_width, x11icon_height);
191 gregl 3.1 XSetWMHints(ourdisplay, gwind, &ourxwmhints);
192     oursizhints.min_width = MINWIDTH;
193     oursizhints.min_height = MINHEIGHT;
194     oursizhints.flags = PMinSize;
195     XSetNormalHints(ourdisplay, gwind, &oursizhints);
196 gregl 3.18 /* figure out sensible view */
197     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
198     DisplayWidth(ourdisplay, ourscreen);
199     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
200     DisplayHeight(ourdisplay, ourscreen);
201 schorsch 3.34 odev.v = stdview;
202 gregl 3.18 odev.v.type = VT_PER;
203 gregl 3.1 /* map the window and get its size */
204     XMapWindow(ourdisplay, gwind);
205 gregl 3.18 dev_input(); /* sets size and view angles */
206 gregl 3.2 /* allocate our leaf pile */
207     if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
208 gregl 3.21 DisplayHeight(ourdisplay,ourscreen) * 3 /
209     (qtMinNodesiz*qtMinNodesiz*2)))
210 gregl 3.2 error(SYSTEM, "insufficient memory for leaf storage");
211 gregl 3.1 odev.name = id;
212     odev.ifd = ConnectionNumber(ourdisplay);
213     }
214    
215    
216 greg 3.46 void
217 schorsch 3.37 dev_close(void) /* close our display */
218 gregl 3.1 {
219     freepixels();
220     XFreeGC(ourdisplay, ourgc);
221     XDestroyWindow(ourdisplay, gwind);
222     gwind = 0;
223     ourgc = 0;
224     XCloseDisplay(ourdisplay);
225     ourdisplay = NULL;
226     qtFreeLeaves();
227 greg 3.38 tmDone(tmGlobal);
228 gregl 3.1 odev.v.type = 0;
229     odev.hres = odev.vres = 0;
230     odev.ifd = -1;
231     }
232    
233    
234 greg 3.46 void
235 schorsch 3.37 dev_clear(void) /* clear our quadtree */
236 gregl 3.23 {
237     qtCompost(100);
238     if (ncolors > 0)
239     new_ctab(ncolors);
240     rayqleft = 0; /* hold off update */
241     }
242    
243    
244 greg 3.46 int
245 schorsch 3.37 dev_view( /* assign new driver view */
246     VIEW *nv
247     )
248 gregl 3.1 {
249 gregl 3.13 if (nv->type == VT_PAR || /* check view legality */
250 gregl 3.17 nv->horiz > 160. || nv->vert > 160.) {
251 gregl 3.13 error(COMMAND, "illegal view type/angle");
252     nv->type = VT_PER;
253     nv->horiz = odev.v.horiz;
254     nv->vert = odev.v.vert;
255     return(0);
256     }
257     if (nv->vfore > FTINY) {
258     error(COMMAND, "cannot handle fore clipping");
259     nv->vfore = 0.;
260     return(0);
261     }
262 gregl 3.11 if (nv != &odev.v) {
263 greg 3.47 if (!FABSEQ(nv->horiz,odev.v.horiz) || /* resize window? */
264     !FABSEQ(nv->vert,odev.v.vert)) {
265 gregl 3.13 int dw = DisplayWidth(ourdisplay,ourscreen);
266     int dh = DisplayHeight(ourdisplay,ourscreen);
267    
268     dw -= 25; /* for window frame */
269 gregl 3.16 dh -= 50;
270 gregl 3.12 odev.hres = 2.*VIEWDIST/pwidth *
271     tan(PI/180./2.*nv->horiz);
272     odev.vres = 2.*VIEWDIST/pheight *
273     tan(PI/180./2.*nv->vert);
274 gregl 3.13 if (odev.hres > dw) {
275     odev.vres = dw * odev.vres / odev.hres;
276     odev.hres = dw;
277     }
278     if (odev.vres > dh) {
279     odev.hres = dh * odev.hres / odev.vres;
280     odev.vres = dh;
281     }
282 gregl 3.11 XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres);
283 gregl 3.18 dev_input(); /* wait for resize event */
284 gregl 3.11 }
285 schorsch 3.34 odev.v = *nv;
286 gregl 3.11 }
287 gregl 3.1 qtReplant();
288 gregl 3.13 return(1);
289 gwlarson 3.26 }
290    
291    
292 greg 3.46 void
293 schorsch 3.37 dev_section( /* add octree for geometry rendering */
294 schorsch 3.42 char *gfn,
295     char *pfn
296 schorsch 3.37 )
297 gwlarson 3.31 {
298     /* unimplemented */
299     }
300    
301    
302 greg 3.46 void
303 schorsch 3.37 dev_auxcom( /* process an auxiliary command */
304     char *cmd,
305     char *args
306     )
307 gwlarson 3.26 {
308     sprintf(errmsg, "%s: unknown command", cmd);
309     error(COMMAND, errmsg);
310 gregl 3.1 }
311    
312    
313 greg 3.46 VIEW *
314 schorsch 3.37 dev_auxview( /* return nth auxiliary view */
315     int n,
316     int hvres[2]
317     )
318 gwlarson 3.25 {
319     if (n)
320     return(NULL);
321     hvres[0] = odev.hres; hvres[1] = odev.vres;
322     return(&odev.v);
323     }
324    
325    
326 greg 3.46 int
327 schorsch 3.37 dev_input(void) /* get X11 input */
328 gregl 3.1 {
329     inpresflags = 0;
330 gregl 3.5
331 gregl 3.1 do
332     getevent();
333    
334 gwlarson 3.27 while (XPending(ourdisplay) > 0);
335 gregl 3.1
336 gwlarson 3.27 odev.inpready = 0;
337    
338 gregl 3.1 return(inpresflags);
339     }
340    
341    
342 greg 3.46 void
343 schorsch 3.37 dev_paintr( /* fill a rectangle */
344 greg 3.43 uby8 rgb[3],
345 schorsch 3.37 int xmin,
346     int ymin,
347     int xmax,
348     int ymax
349     )
350 gregl 3.1 {
351     unsigned long pixel;
352    
353     if (!mapped)
354     return;
355     if (ncolors > 0)
356     pixel = pixval[get_pixel(rgb, xnewcolr)];
357     else
358     pixel = true_pixel(rgb);
359     XSetForeground(ourdisplay, ourgc, pixel);
360     XFillRectangle(ourdisplay, gwind,
361     ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin);
362     }
363    
364    
365 greg 3.46 int
366 schorsch 3.37 dev_flush(void) /* flush output */
367 gregl 3.1 {
368     qtUpdate();
369 gregl 3.20 rayqleft = RAYQLEN;
370 gwlarson 3.27 return(odev.inpready = XPending(ourdisplay));
371 gregl 3.1 }
372    
373    
374 schorsch 3.37 static void
375     xnewcolr( /* enter a color into hardware table */
376     int ndx,
377     int r,
378     int g,
379     int b
380     )
381 gregl 3.1 {
382     XColor xcolor;
383    
384     xcolor.pixel = pixval[ndx];
385     xcolor.red = r << 8;
386     xcolor.green = g << 8;
387     xcolor.blue = b << 8;
388     xcolor.flags = DoRed|DoGreen|DoBlue;
389    
390     XStoreColor(ourdisplay, ourmap, &xcolor);
391     }
392    
393    
394     static int
395 schorsch 3.37 getpixels(void) /* get the color map */
396 gregl 3.1 {
397     XColor thiscolor;
398 greg 3.45 int i, j;
399 gregl 3.1
400     if (ncolors > 0)
401     return(ncolors);
402     if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
403     ourmap = DefaultColormap(ourdisplay,ourscreen);
404     goto loop;
405     }
406     newmap:
407     ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
408     loop:
409     for (ncolors = ourvinfo.colormap_size;
410     ncolors > ourvinfo.colormap_size/3;
411     ncolors = ncolors*.937) {
412     pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
413     if (pixval == NULL)
414     return(ncolors = 0);
415     if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
416     break;
417 greg 3.32 free((void *)pixval);
418 gregl 3.1 pixval = NULL;
419     }
420     if (pixval == NULL) {
421     if (ourmap == DefaultColormap(ourdisplay,ourscreen))
422     goto newmap; /* try it with our map */
423     else
424     return(ncolors = 0); /* failed */
425     }
426     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
427     for (i = 0; i < ncolors; i++) { /* reset black and white */
428     if (pixval[i] != ourblack && pixval[i] != ourwhite)
429     continue;
430     thiscolor.pixel = pixval[i];
431     thiscolor.flags = DoRed|DoGreen|DoBlue;
432     XQueryColor(ourdisplay,
433     DefaultColormap(ourdisplay,ourscreen),
434     &thiscolor);
435     XStoreColor(ourdisplay, ourmap, &thiscolor);
436     for (j = i; j+1 < ncolors; j++)
437     pixval[j] = pixval[j+1];
438     ncolors--;
439     i--;
440     }
441     XSetWindowColormap(ourdisplay, gwind, ourmap);
442     return(ncolors);
443     }
444    
445    
446 schorsch 3.37 static void
447     freepixels(void) /* free our pixels */
448 gregl 3.1 {
449     if (ncolors == 0)
450     return;
451     XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
452 greg 3.32 free((void *)pixval);
453 gregl 3.1 pixval = NULL;
454     ncolors = 0;
455     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
456     XFreeColormap(ourdisplay, ourmap);
457     ourmap = 0;
458     }
459    
460    
461     static unsigned long
462 schorsch 3.37 true_pixel( /* return true pixel value for color */
463 greg 3.45 uby8 rgb[3]
464 schorsch 3.37 )
465 gregl 3.1 {
466 greg 3.45 unsigned long rval;
467 gregl 3.1
468     rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
469     rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
470     rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
471     return(rval);
472     }
473    
474    
475 schorsch 3.37 static void
476     getevent(void) /* get next event */
477 gregl 3.1 {
478     XNextEvent(ourdisplay, levptr(XEvent));
479     switch (levptr(XEvent)->type) {
480     case ConfigureNotify:
481     resizewindow(levptr(XConfigureEvent));
482     break;
483     case UnmapNotify:
484     mapped = 0;
485     freepixels();
486     break;
487     case MapNotify:
488     if (ourvinfo.class == PseudoColor ||
489     ourvinfo.class == GrayScale) {
490     if (getpixels() == 0)
491     error(SYSTEM, "cannot allocate colors\n");
492     new_ctab(ncolors);
493     }
494     mapped = 1;
495     break;
496     case Expose:
497     fixwindow(levptr(XExposeEvent));
498     break;
499     case KeyPress:
500     getkey(levptr(XKeyPressedEvent));
501     break;
502     case ButtonPress:
503 gwlarson 3.30 if (FRAMESTATE(levptr(XButtonPressedEvent)->state))
504     getframe(levptr(XButtonPressedEvent));
505 greg 3.45 else
506     switch (levptr(XButtonPressedEvent)->button) {
507     case Button4: /* wheel up */
508     case Button5: /* wheel down */
509     break;
510     default:
511     getmove(levptr(XButtonPressedEvent));
512     break;
513     }
514 gregl 3.1 break;
515     }
516     }
517    
518    
519 schorsch 3.37 static int
520     ilclip( /* clip world coordinates to device */
521     int dp[2][2],
522     FVECT wp[2]
523     )
524 gregl 3.9 {
525 gwlarson 3.28 static FVECT vmin = {0.,0.,0.}, vmax = {1.-FTINY,1.-FTINY,FHUGE};
526     FVECT wpc[2], ip[2];
527     double d, d0, d1;
528     /* check for points behind view */
529     d = DOT(odev.v.vp, odev.v.vdir);
530     d0 = DOT(wp[0], odev.v.vdir) - d;
531     d1 = DOT(wp[1], odev.v.vdir) - d;
532     /* work on copy of world points */
533     if (d0 <= d1) {
534     VCOPY(wpc[0], wp[0]); VCOPY(wpc[1], wp[1]);
535     } else {
536     d = d0; d0 = d1; d1 = d;
537     VCOPY(wpc[1], wp[0]); VCOPY(wpc[0], wp[1]);
538     }
539     if (d0 <= FTINY) {
540     if (d1 <= FTINY) return(0);
541     VSUB(wpc[0], wpc[0], wpc[1]);
542     d = .99*d1/(d1-d0);
543     VSUM(wpc[0], wpc[1], wpc[0], d);
544     }
545     /* get view coordinates and clip to window */
546     viewloc(ip[0], &odev.v, wpc[0]);
547     viewloc(ip[1], &odev.v, wpc[1]);
548 gregl 3.9 if (!clip(ip[0], ip[1], vmin, vmax))
549     return(0);
550     dp[0][0] = ip[0][0]*odev.hres;
551     dp[0][1] = ip[0][1]*odev.vres;
552     dp[1][0] = ip[1][0]*odev.hres;
553     dp[1][1] = ip[1][1]*odev.vres;
554     return(1);
555     }
556    
557    
558 schorsch 3.37 static void
559     draw3dline( /* draw 3d line in world coordinates */
560     FVECT wp[2]
561     )
562 gregl 3.9 {
563     int dp[2][2];
564    
565     if (!ilclip(dp, wp))
566     return;
567     XDrawLine(ourdisplay, gwind, ourgc,
568     dp[0][0], odev.vres-1 - dp[0][1],
569     dp[1][0], odev.vres-1 - dp[1][1]);
570     }
571    
572    
573 schorsch 3.37 static void
574     draw_grids(void) /* draw holodeck section grids */
575 gregl 3.9 {
576 greg 3.43 static uby8 gridrgb[3] = {0x0, 0xff, 0xff};
577 gregl 3.9 unsigned long pixel;
578    
579     if (ncolors > 0)
580     pixel = pixval[get_pixel(gridrgb, xnewcolr)];
581     else
582     pixel = true_pixel(gridrgb);
583     XSetForeground(ourdisplay, ourgc, pixel);
584     /* draw each grid line */
585     gridlines(draw3dline);
586     }
587    
588    
589 schorsch 3.37 static int
590     moveview( /* move our view */
591     int dx,
592     int dy,
593     int mov,
594     int orb
595     )
596 gregl 3.2 {
597     VIEW nv;
598 gregl 3.17 FVECT odir, v1;
599 gregl 3.2 double d;
600 greg 3.45 int li;
601 gregl 3.2 /* start with old view */
602 schorsch 3.34 nv = odev.v;
603 gregl 3.2 /* change view direction */
604 gregl 3.13 if (mov | orb) {
605 gregl 3.4 if ((li = qtFindLeaf(dx, dy)) < 0)
606 gregl 3.2 return(0); /* not on window */
607 gregl 3.17 VSUM(odir, qtL.wp[li], nv.vp, -1.);
608 gregl 3.2 } else {
609     if (viewray(nv.vp, nv.vdir, &odev.v,
610     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
611     return(0); /* outside view */
612     }
613 gregl 3.13 if (orb && mov) { /* orbit left/right */
614 gregl 3.17 spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov);
615     VSUM(nv.vp, qtL.wp[li], odir, -1.);
616     spinvector(nv.vdir, nv.vdir, nv.vup, d);
617 gregl 3.13 } else if (orb) { /* orbit up/down */
618 greg 3.44 if (geodesic(odir, odir, nv.vup,
619     d=MOVDEG*PI/180.*orb, GEOD_RAD) == 0.0)
620 gregl 3.13 return(0);
621 gregl 3.17 VSUM(nv.vp, qtL.wp[li], odir, -1.);
622 greg 3.44 geodesic(nv.vdir, nv.vdir, nv.vup, d, GEOD_RAD);
623 gregl 3.13 } else if (mov) { /* move forward/backward */
624     d = MOVPCT/100. * mov;
625 gregl 3.17 VSUM(nv.vp, nv.vp, odir, d);
626 gregl 3.2 }
627 gregl 3.13 if (!mov ^ !orb && headlocked) { /* restore head height */
628     VSUM(v1, odev.v.vp, nv.vp, -1.);
629     d = DOT(v1, odev.v.vup);
630     VSUM(nv.vp, nv.vp, odev.v.vup, d);
631     }
632 gregl 3.2 if (setview(&nv) != NULL)
633     return(0); /* illegal view */
634     dev_view(&nv);
635 gregl 3.14 inpresflags |= DFL(DC_SETVIEW);
636 gregl 3.2 return(1);
637     }
638    
639    
640 schorsch 3.37 static void
641     getframe( /* get focus frame */
642     XButtonPressedEvent *ebut
643     )
644 gwlarson 3.30 {
645     int startx = ebut->x, starty = ebut->y;
646 greg 3.39 int endx, endy, midx, midy;
647     FVECT v1;
648     int li;
649     /* get mouse drag */
650 gwlarson 3.30 XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent));
651     endx = levptr(XButtonReleasedEvent)->x;
652     endy = levptr(XButtonReleasedEvent)->y;
653 greg 3.39 midx = (startx + endx) >> 1;
654     midy = (starty + endy) >> 1;
655     /* set focus distance */
656     if ((li = qtFindLeaf(midx, midy)) < 0)
657 greg 3.40 return; /* not on window */
658 greg 3.39 VCOPY(v1, qtL.wp[li]);
659     odev.v.vdist = sqrt(dist2(odev.v.vp, v1));
660     /* set frame for rendering */
661     if ((endx == startx) | (endy == starty))
662 gwlarson 3.30 return;
663 greg 3.45 if (endx < startx) {int c = endx; endx = startx; startx = c;}
664     if (endy < starty) {int c = endy; endy = starty; starty = c;}
665 gwlarson 3.30 sprintf(odev_args, "%.3f %.3f %.3f %.3f",
666     (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres,
667     (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres);
668     inpresflags |= DFL(DC_FOCUS);
669     }
670    
671    
672 schorsch 3.37 static void
673     waitabit(void) /* pause a moment */
674 greg 3.32 {
675     struct timespec ts;
676     ts.tv_sec = 0;
677 greg 3.36 ts.tv_nsec = 100000000;
678 greg 3.32 nanosleep(&ts, NULL);
679     }
680    
681    
682 schorsch 3.37 static void
683     getmove( /* get view change */
684     XButtonPressedEvent *ebut
685     )
686 gregl 3.2 {
687 gregl 3.13 int movdir = MOVDIR(ebut->button);
688     int movorb = MOVORB(ebut->state);
689 gregl 3.2 int oldnodesiz = qtMinNodesiz;
690     Window rootw, childw;
691     int rootx, rooty, wx, wy;
692     unsigned int statemask;
693    
694     qtMinNodesiz = 16; /* for quicker update */
695 gregl 3.9 XNoOp(ourdisplay);
696 gregl 3.2
697 gregl 3.9 while (!XCheckMaskEvent(ourdisplay,
698     ButtonReleaseMask, levptr(XEvent))) {
699 greg 3.32 waitabit();
700 gregl 3.2 if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
701     &rootx, &rooty, &wx, &wy, &statemask))
702     break; /* on another screen */
703    
704 gregl 3.13 if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) {
705 gregl 3.2 sleep(1);
706 gregl 3.9 continue;
707     }
708     XClearWindow(ourdisplay, gwind);
709     qtUpdate();
710     draw_grids();
711     }
712 gregl 3.14 if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */
713 gregl 3.13 movdir = MOVDIR(levptr(XButtonReleasedEvent)->button);
714 gregl 3.2 wx = levptr(XButtonReleasedEvent)->x;
715     wy = levptr(XButtonReleasedEvent)->y;
716 gregl 3.13 moveview(wx, odev.vres-1-wy, movdir, movorb);
717 gregl 3.2 }
718 gregl 3.9 dev_flush();
719 gregl 3.2
720     qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */
721     }
722    
723    
724 schorsch 3.37 static void
725     getkey( /* get input key */
726 greg 3.45 XKeyPressedEvent *ekey
727 schorsch 3.37 )
728 gregl 3.1 {
729 gwlarson 3.30 Window rootw, childw;
730     int rootx, rooty, wx, wy;
731     unsigned int statemask;
732 gregl 3.1 int n;
733     char buf[8];
734    
735     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
736     if (n != 1)
737     return;
738     switch (buf[0]) {
739     case 'h': /* turn on height motion lock */
740 gregl 3.2 headlocked = 1;
741 gregl 3.1 return;
742     case 'H': /* turn off height motion lock */
743 gregl 3.2 headlocked = 0;
744 gregl 3.13 return;
745     case 'l': /* retrieve last view */
746 gregl 3.14 inpresflags |= DFL(DC_LASTVIEW);
747 gwlarson 3.30 return;
748     case 'f': /* frame view position */
749     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
750     &rootx, &rooty, &wx, &wy, &statemask))
751     return; /* on another screen */
752     sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres,
753     1.-(wy+.5)/odev.vres);
754     inpresflags |= DFL(DC_FOCUS);
755     return;
756     case 'F': /* unfocus */
757     odev_args[0] = '\0';
758     inpresflags |= DFL(DC_FOCUS);
759 gregl 3.1 return;
760 gregl 3.2 case 'p': /* pause computation */
761 gregl 3.14 inpresflags |= DFL(DC_PAUSE);
762 gregl 3.2 return;
763 gregl 3.7 case 'v': /* spit out view */
764 gregl 3.14 inpresflags |= DFL(DC_GETVIEW);
765 gregl 3.7 return;
766 gregl 3.2 case '\n':
767 gregl 3.5 case '\r': /* resume computation */
768 gregl 3.14 inpresflags |= DFL(DC_RESUME);
769 gregl 3.2 return;
770 gregl 3.7 case CTRL('R'): /* redraw screen */
771     if (ncolors > 0)
772     new_ctab(ncolors);
773     qtRedraw(0, 0, odev.hres, odev.vres);
774     return;
775     case CTRL('L'): /* refresh from server */
776 gregl 3.14 if (inpresflags & DFL(DC_REDRAW))
777 gregl 3.8 return;
778 gregl 3.7 XClearWindow(ourdisplay, gwind);
779 gregl 3.9 draw_grids();
780 gregl 3.8 XFlush(ourdisplay);
781 gregl 3.6 qtCompost(100); /* unload the old tree */
782 gregl 3.3 if (ncolors > 0)
783     new_ctab(ncolors);
784 gregl 3.14 inpresflags |= DFL(DC_REDRAW); /* resend values from server */
785 gregl 3.22 rayqleft = 0; /* hold off update */
786 gregl 3.3 return;
787 gregl 3.15 case 'K': /* kill rtrace process(es) */
788     inpresflags |= DFL(DC_KILL);
789     break;
790     case 'R': /* restart rtrace */
791     inpresflags |= DFL(DC_RESTART);
792     break;
793     case 'C': /* clobber holodeck */
794     inpresflags |= DFL(DC_CLOBBER);
795     break;
796 gregl 3.1 case 'q': /* quit the program */
797 gregl 3.14 inpresflags |= DFL(DC_QUIT);
798 gregl 3.1 return;
799     default:
800     XBell(ourdisplay, 0);
801     return;
802     }
803     }
804    
805    
806 schorsch 3.37 static void
807     fixwindow( /* repair damage to window */
808 greg 3.45 XExposeEvent *eexp
809 schorsch 3.37 )
810 gregl 3.1 {
811     if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
812     odev.hres = eexp->width;
813     odev.vres = eexp->height;
814     }
815     qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
816     eexp->x + eexp->width, odev.vres - eexp->y);
817     }
818    
819    
820 schorsch 3.37 static void
821     resizewindow( /* resize window */
822 greg 3.45 XConfigureEvent *ersz
823 schorsch 3.37 )
824 gregl 3.1 {
825     if (ersz->width == odev.hres && ersz->height == odev.vres)
826     return;
827    
828     odev.hres = ersz->width;
829     odev.vres = ersz->height;
830    
831 gregl 3.12 odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
832     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
833    
834 gregl 3.14 inpresflags |= DFL(DC_SETVIEW);
835 gregl 3.1 }