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