18 |
|
|
19 |
|
#include "x11icon.h" |
20 |
|
|
21 |
+ |
#define CTRL(c) ((c)-'@') |
22 |
+ |
|
23 |
|
#define GAMMA 2.2 /* default gamma correction */ |
24 |
|
|
25 |
+ |
#define MOVPCT 10 /* percent distance to move */ |
26 |
+ |
#define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) |
27 |
+ |
|
28 |
|
#define MINWIDTH 480 /* minimum graphics window width */ |
29 |
|
#define MINHEIGHT 400 /* minimum graphics window height */ |
30 |
|
|
35 |
|
#define ourscreen DefaultScreen(ourdisplay) |
36 |
|
#define ourroot RootWindow(ourdisplay,ourscreen) |
37 |
|
#define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\ |
38 |
< |
ButtonPressMask) |
38 |
> |
ButtonPressMask|ButtonReleaseMask) |
39 |
|
|
40 |
|
#define levptr(etype) ((etype *)¤tevent) |
41 |
|
|
49 |
|
static unsigned long ourblack=0, ourwhite=1; |
50 |
|
|
51 |
|
static Display *ourdisplay = NULL; /* our display */ |
47 |
– |
|
52 |
|
static XVisualInfo ourvinfo; /* our visual information */ |
49 |
– |
|
53 |
|
static Window gwind = 0; /* our graphics window */ |
51 |
– |
|
54 |
|
static GC ourgc = 0; /* our graphics context for drawing */ |
53 |
– |
|
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 |
< |
static int heightlocked = 0; /* lock vertical motion */ |
61 |
> |
static int headlocked = 0; /* lock vertical motion */ |
62 |
|
|
63 |
|
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), |
64 |
< |
getevent(), getkey(), fixwindow(); |
64 |
> |
getevent(), getkey(), moveview(), getmove(), fixwindow(); |
65 |
|
static unsigned long true_pixel(); |
66 |
|
|
67 |
|
|
91 |
|
char *gv; |
92 |
|
double gamval = GAMMA; |
93 |
|
int nplanes; |
93 |
– |
int n; |
94 |
|
XSetWindowAttributes ourwinattr; |
95 |
|
XWMHints ourxwmhints; |
96 |
|
XSizeHints oursizhints; |
97 |
+ |
/* set quadtree globals */ |
98 |
+ |
qtDepthEps = 0.02; |
99 |
+ |
qtMinNodesiz = 2; |
100 |
|
/* open display server */ |
101 |
|
ourdisplay = XOpenDisplay(NULL); |
102 |
|
if (ourdisplay == NULL) |
158 |
|
/* map the window and get its size */ |
159 |
|
XMapWindow(ourdisplay, gwind); |
160 |
|
dev_input(); |
161 |
+ |
/* 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 |
|
/* figure out sensible view */ |
168 |
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
169 |
|
DisplayWidth(ourdisplay, ourscreen); |
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); |
169 |
– |
/* allocate our leaf pile */ |
170 |
– |
qtDepthEps = 0.02; |
171 |
– |
n = odev.hres < odev.vres ? odev.hres : odev.vres; |
172 |
– |
qtMinNodesiz = 1; |
173 |
– |
if (!qtAllocLeaves(n*n/(qtMinNodesiz*qtMinNodesiz))) |
174 |
– |
error(SYSTEM, "insufficient memory for leaf storage"); |
178 |
|
} |
179 |
|
|
180 |
|
|
208 |
|
dev_input() /* get X11 input */ |
209 |
|
{ |
210 |
|
inpresflags = 0; |
211 |
+ |
|
212 |
|
do |
213 |
|
getevent(); |
214 |
|
|
369 |
|
getkey(levptr(XKeyPressedEvent)); |
370 |
|
break; |
371 |
|
case ButtonPress: |
372 |
< |
/* getmove(levptr(XButtonPressedEvent)); */ |
372 |
> |
getmove(levptr(XButtonPressedEvent)); |
373 |
|
break; |
374 |
|
} |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
|
static |
379 |
+ |
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 |
+ |
moveview(dx, dy, move) /* move our view */ |
432 |
+ |
int dx, dy, move; |
433 |
+ |
{ |
434 |
+ |
VIEW nv; |
435 |
+ |
double d; |
436 |
+ |
register int i, li; |
437 |
+ |
/* start with old view */ |
438 |
+ |
copystruct(&nv, &odev.v); |
439 |
+ |
/* change view direction */ |
440 |
+ |
if (move) { |
441 |
+ |
if ((li = qtFindLeaf(dx, dy)) < 0) |
442 |
+ |
return(0); /* not on window */ |
443 |
+ |
for (i = 0; i < 3; i++) |
444 |
+ |
nv.vdir[i] = qtL.wp[li][i] - nv.vp[i]; |
445 |
+ |
} 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 |
+ |
XNoOp(ourdisplay); |
484 |
+ |
|
485 |
+ |
while (!XCheckMaskEvent(ourdisplay, |
486 |
+ |
ButtonReleaseMask, levptr(XEvent))) { |
487 |
+ |
|
488 |
+ |
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
489 |
+ |
&rootx, &rooty, &wx, &wy, &statemask)) |
490 |
+ |
break; /* on another screen */ |
491 |
+ |
|
492 |
+ |
if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton))) { |
493 |
+ |
sleep(1); |
494 |
+ |
continue; |
495 |
+ |
} |
496 |
+ |
XClearWindow(ourdisplay, gwind); |
497 |
+ |
qtUpdate(); |
498 |
+ |
draw_grids(); |
499 |
+ |
} |
500 |
+ |
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 |
+ |
dev_flush(); |
507 |
+ |
|
508 |
+ |
qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */ |
509 |
+ |
} |
510 |
+ |
|
511 |
+ |
|
512 |
+ |
static |
513 |
|
getkey(ekey) /* get input key */ |
514 |
|
register XKeyPressedEvent *ekey; |
515 |
|
{ |
521 |
|
return; |
522 |
|
switch (buf[0]) { |
523 |
|
case 'h': /* turn on height motion lock */ |
524 |
< |
heightlocked = 1; |
524 |
> |
headlocked = 1; |
525 |
|
return; |
526 |
|
case 'H': /* turn off height motion lock */ |
527 |
< |
heightlocked = 0; |
527 |
> |
headlocked = 0; |
528 |
|
return; |
529 |
+ |
case CTRL('S'): |
530 |
+ |
case 'p': /* pause computation */ |
531 |
+ |
inpresflags |= DEV_WAIT; |
532 |
+ |
return; |
533 |
+ |
case 'v': /* spit out view */ |
534 |
+ |
fputs(VIEWSTR, stderr); |
535 |
+ |
fprintview(&odev.v, stderr); |
536 |
+ |
fputc('\n', stderr); |
537 |
+ |
return; |
538 |
+ |
case CTRL('Q'): |
539 |
+ |
case '\n': |
540 |
+ |
case '\r': /* resume computation */ |
541 |
+ |
inpresflags |= DEV_RESUME; |
542 |
+ |
return; |
543 |
+ |
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 |
+ |
if (inpresflags & DEV_REDRAW) |
550 |
+ |
return; |
551 |
+ |
XClearWindow(ourdisplay, gwind); |
552 |
+ |
draw_grids(); |
553 |
+ |
XFlush(ourdisplay); |
554 |
+ |
qtCompost(100); /* unload the old tree */ |
555 |
+ |
if (ncolors > 0) |
556 |
+ |
new_ctab(ncolors); |
557 |
+ |
inpresflags |= DEV_REDRAW; /* resend values from server */ |
558 |
+ |
return; |
559 |
+ |
case CTRL('D'): |
560 |
|
case 'Q': |
561 |
|
case 'q': /* quit the program */ |
562 |
|
inpresflags |= DEV_SHUTDOWN; |
573 |
|
register XExposeEvent *eexp; |
574 |
|
{ |
575 |
|
if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ |
407 |
– |
eputs("Resizing window in fixwindow\n"); |
576 |
|
odev.hres = eexp->width; |
577 |
|
odev.vres = eexp->height; |
578 |
|
inpresflags |= DEV_NEWSIZE; |