| 1 | 
#ifndef lint | 
| 2 | 
static const char       RCSid[] = "$Id: rhd_glx1.c,v 3.1 2003/02/22 02:07:24 greg Exp $"; | 
| 3 | 
#endif | 
| 4 | 
/* | 
| 5 | 
 * OpenGL GLX driver for holodeck display. | 
| 6 | 
 * Based on x11 driver. | 
| 7 | 
 */ | 
| 8 | 
 | 
| 9 | 
#include "standard.h" | 
| 10 | 
 | 
| 11 | 
#include  <GL/glx.h> | 
| 12 | 
 | 
| 13 | 
#include "rhd_qtree.h" | 
| 14 | 
 | 
| 15 | 
#include  "x11icon.h" | 
| 16 | 
 | 
| 17 | 
#ifndef RAYQLEN | 
| 18 | 
#define RAYQLEN         50000           /* max. rays to queue before flush */ | 
| 19 | 
#endif | 
| 20 | 
 | 
| 21 | 
#ifndef FEQ | 
| 22 | 
#define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) | 
| 23 | 
#endif | 
| 24 | 
 | 
| 25 | 
#ifndef MAXCONE | 
| 26 | 
#define MAXCONE         16              /* number of different cone sizes */ | 
| 27 | 
#endif | 
| 28 | 
#ifndef MAXVERT | 
| 29 | 
#define MAXVERT         64              /* maximum number of cone vertices */ | 
| 30 | 
#endif | 
| 31 | 
#ifndef MINVERT | 
| 32 | 
#define MINVERT         4               /* minimum number of cone vertices */ | 
| 33 | 
#endif | 
| 34 | 
#ifndef DEPTHFACT | 
| 35 | 
#define DEPTHFACT       16.             /* multiplier for depth tests */ | 
| 36 | 
#endif | 
| 37 | 
 | 
| 38 | 
#define GAMMA           1.4             /* default gamma correction */ | 
| 39 | 
 | 
| 40 | 
#define MOVPCT          7               /* percent distance to move /frame */ | 
| 41 | 
#define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) | 
| 42 | 
#define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */ | 
| 43 | 
#define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) | 
| 44 | 
 | 
| 45 | 
#define MINWIDTH        480             /* minimum graphics window width */ | 
| 46 | 
#define MINHEIGHT       400             /* minimum graphics window height */ | 
| 47 | 
 | 
| 48 | 
#define VIEWDIST        356             /* assumed viewing distance (mm) */ | 
| 49 | 
 | 
| 50 | 
#define BORWIDTH        5               /* border width */ | 
| 51 | 
 | 
| 52 | 
#define  ourscreen      DefaultScreen(ourdisplay) | 
| 53 | 
#define  ourroot        RootWindow(ourdisplay,ourscreen) | 
| 54 | 
#define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\ | 
| 55 | 
                        ButtonPressMask|ButtonReleaseMask) | 
| 56 | 
 | 
| 57 | 
#define  levptr(etype)  ((etype *)¤tevent) | 
| 58 | 
 | 
| 59 | 
struct driver   odev;                   /* global device driver structure */ | 
| 60 | 
 | 
| 61 | 
char odev_args[64];                     /* command arguments */ | 
| 62 | 
 | 
| 63 | 
static XEvent  currentevent;            /* current event */ | 
| 64 | 
 | 
| 65 | 
static int  mapped = 0;                 /* window is mapped? */ | 
| 66 | 
static unsigned long  ourblack=0, ourwhite=~0; | 
| 67 | 
 | 
| 68 | 
static Display  *ourdisplay = NULL;     /* our display */ | 
| 69 | 
static XVisualInfo  *ourvinf;           /* our visual information */ | 
| 70 | 
static Window  gwind = 0;               /* our graphics window */ | 
| 71 | 
static GLXContext       gctx;           /* our GLX context */ | 
| 72 | 
 | 
| 73 | 
static double   pwidth, pheight;        /* pixel dimensions (mm) */ | 
| 74 | 
 | 
| 75 | 
static double   curzmax = 1e4;          /* current depth upper limit */ | 
| 76 | 
static double   nxtzmax = 0.;           /* maximum (finite) depth so far */ | 
| 77 | 
 | 
| 78 | 
static struct { | 
| 79 | 
        double  rad;            /* cone radius */ | 
| 80 | 
        int     nverts;         /* number of vertices */ | 
| 81 | 
        FVECT   *va;            /* allocated vertex array */ | 
| 82 | 
} cone[MAXCONE];        /* precomputed cones for drawing */ | 
| 83 | 
 | 
| 84 | 
static int      inpresflags;            /* input result flags */ | 
| 85 | 
 | 
| 86 | 
static int      headlocked = 0;         /* lock vertical motion */ | 
| 87 | 
 | 
| 88 | 
static int  resizewindow(), getevent(), getkey(), moveview(), | 
| 89 | 
                initcones(), freecones(), | 
| 90 | 
                getmove(), fixwindow(), mytmflags(); | 
| 91 | 
 | 
| 92 | 
 | 
| 93 | 
dev_open(id)                    /* initialize X11 driver */ | 
| 94 | 
char  *id; | 
| 95 | 
{ | 
| 96 | 
        extern char     *getenv(); | 
| 97 | 
        static RGBPRIMS myprims = STDPRIMS; | 
| 98 | 
        static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, | 
| 99 | 
                                GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, | 
| 100 | 
                                GLX_DEPTH_SIZE,15, None}; | 
| 101 | 
        char    *ev; | 
| 102 | 
        double  gamval = GAMMA; | 
| 103 | 
        RGBPRIMP        dpri = stdprims; | 
| 104 | 
        XSetWindowAttributes    ourwinattr; | 
| 105 | 
        XWMHints        ourxwmhints; | 
| 106 | 
        XSizeHints      oursizhints; | 
| 107 | 
                                        /* set quadtree globals */ | 
| 108 | 
        qtMinNodesiz = 3; | 
| 109 | 
        qtDepthEps = 0.07; | 
| 110 | 
                                        /* open display server */ | 
| 111 | 
        ourdisplay = XOpenDisplay(NULL); | 
| 112 | 
        if (ourdisplay == NULL) | 
| 113 | 
                error(USER, "cannot open X-windows; DISPLAY variable set?\n"); | 
| 114 | 
                                        /* find a usable visual */ | 
| 115 | 
        ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); | 
| 116 | 
        if (ourvinf == NULL) | 
| 117 | 
                error(USER, "no suitable visuals available"); | 
| 118 | 
                                        /* get a context */ | 
| 119 | 
        gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); | 
| 120 | 
                                        /* set gamma and tone mapping */ | 
| 121 | 
        if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL | 
| 122 | 
                        || (ev = getenv("DISPLAY_GAMMA")) != NULL) | 
| 123 | 
                gamval = atof(ev); | 
| 124 | 
        if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL && | 
| 125 | 
                        sscanf(ev, "%f %f %f %f %f %f %f %f", | 
| 126 | 
                                &myprims[RED][CIEX],&myprims[RED][CIEY], | 
| 127 | 
                                &myprims[GRN][CIEX],&myprims[GRN][CIEY], | 
| 128 | 
                                &myprims[BLU][CIEX],&myprims[BLU][CIEY], | 
| 129 | 
                                &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) | 
| 130 | 
                dpri = myprims; | 
| 131 | 
        if (tmInit(mytmflags(), dpri, gamval) == NULL) | 
| 132 | 
                error(SYSTEM, "not enough memory in dev_open"); | 
| 133 | 
                                        /* open window */ | 
| 134 | 
        ourwinattr.background_pixel = ourblack; | 
| 135 | 
        ourwinattr.border_pixel = ourblack; | 
| 136 | 
        ourwinattr.event_mask = ourmask; | 
| 137 | 
                                        /* this is stupid */ | 
| 138 | 
        ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, | 
| 139 | 
                                ourvinf->visual, AllocNone); | 
| 140 | 
        gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, | 
| 141 | 
                DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, | 
| 142 | 
                DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, | 
| 143 | 
                BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual, | 
| 144 | 
                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr); | 
| 145 | 
        if (gwind == 0) | 
| 146 | 
                error(SYSTEM, "cannot create window\n"); | 
| 147 | 
        XStoreName(ourdisplay, gwind, id); | 
| 148 | 
                                        /* set window manager hints */ | 
| 149 | 
        ourxwmhints.flags = InputHint|IconPixmapHint; | 
| 150 | 
        ourxwmhints.input = True; | 
| 151 | 
        ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, | 
| 152 | 
                        gwind, x11icon_bits, x11icon_width, x11icon_height); | 
| 153 | 
        XSetWMHints(ourdisplay, gwind, &ourxwmhints); | 
| 154 | 
        oursizhints.min_width = MINWIDTH; | 
| 155 | 
        oursizhints.min_height = MINHEIGHT; | 
| 156 | 
        oursizhints.flags = PMinSize; | 
| 157 | 
        XSetNormalHints(ourdisplay, gwind, &oursizhints); | 
| 158 | 
                                        /* set GLX context */ | 
| 159 | 
        glXMakeCurrent(ourdisplay, gwind, gctx); | 
| 160 | 
        glEnable(GL_DEPTH_TEST); | 
| 161 | 
        glDepthFunc(GL_LEQUAL); | 
| 162 | 
        glShadeModel(GL_FLAT); | 
| 163 | 
        glDisable(GL_DITHER); | 
| 164 | 
        glDisable(GL_CULL_FACE); | 
| 165 | 
        glMatrixMode(GL_PROJECTION); | 
| 166 | 
        glOrtho(0., 1., 0., 1., -.01, 1.01); | 
| 167 | 
        glTranslated(0., 0., -1.01); | 
| 168 | 
                                        /* figure out sensible view */ | 
| 169 | 
        pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / | 
| 170 | 
                        DisplayWidth(ourdisplay, ourscreen); | 
| 171 | 
        pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / | 
| 172 | 
                        DisplayHeight(ourdisplay, ourscreen); | 
| 173 | 
        odev.v = stdview; | 
| 174 | 
        odev.v.type = VT_PER; | 
| 175 | 
                                        /* map the window */ | 
| 176 | 
        XMapWindow(ourdisplay, gwind); | 
| 177 | 
        dev_input();                    /* sets size and view angles */ | 
| 178 | 
                                        /* allocate our leaf pile */ | 
| 179 | 
        if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * | 
| 180 | 
                        DisplayHeight(ourdisplay,ourscreen) * 3 / | 
| 181 | 
                        (qtMinNodesiz*qtMinNodesiz*2))) | 
| 182 | 
                error(SYSTEM, "insufficient memory for value storage"); | 
| 183 | 
        odev.name = id; | 
| 184 | 
        odev.ifd = ConnectionNumber(ourdisplay); | 
| 185 | 
                                        /* initialize cone array */ | 
| 186 | 
        initcones(); | 
| 187 | 
} | 
| 188 | 
 | 
| 189 | 
 | 
| 190 | 
dev_close()                     /* close our display and free resources */ | 
| 191 | 
{ | 
| 192 | 
        glXMakeCurrent(ourdisplay, None, NULL); | 
| 193 | 
        glXDestroyContext(ourdisplay, gctx); | 
| 194 | 
        XDestroyWindow(ourdisplay, gwind); | 
| 195 | 
        gwind = 0; | 
| 196 | 
        XCloseDisplay(ourdisplay); | 
| 197 | 
        ourdisplay = NULL; | 
| 198 | 
        qtFreeLeaves(); | 
| 199 | 
        tmDone(NULL); | 
| 200 | 
        freecones(); | 
| 201 | 
        odev.v.type = 0; | 
| 202 | 
        odev.hres = odev.vres = 0; | 
| 203 | 
        odev.ifd = -1; | 
| 204 | 
} | 
| 205 | 
 | 
| 206 | 
 | 
| 207 | 
dev_clear()                     /* clear our quadtree */ | 
| 208 | 
{ | 
| 209 | 
        qtCompost(100); | 
| 210 | 
        glClear(GL_DEPTH_BUFFER_BIT); | 
| 211 | 
        rayqleft = 0;                   /* hold off update */ | 
| 212 | 
} | 
| 213 | 
 | 
| 214 | 
 | 
| 215 | 
int | 
| 216 | 
dev_view(nv)                    /* assign new driver view */ | 
| 217 | 
register VIEW   *nv; | 
| 218 | 
{ | 
| 219 | 
        if (nv->type == VT_PAR ||               /* check view legality */ | 
| 220 | 
                        nv->horiz > 160. || nv->vert > 160.) { | 
| 221 | 
                error(COMMAND, "illegal view type/angle"); | 
| 222 | 
                nv->type = odev.v.type; | 
| 223 | 
                nv->horiz = odev.v.horiz; | 
| 224 | 
                nv->vert = odev.v.vert; | 
| 225 | 
                return(0); | 
| 226 | 
        } | 
| 227 | 
        if (nv->vfore > FTINY) { | 
| 228 | 
                error(COMMAND, "cannot handle fore clipping"); | 
| 229 | 
                nv->vfore = 0.; | 
| 230 | 
                return(0); | 
| 231 | 
        } | 
| 232 | 
        if (nv != &odev.v) { | 
| 233 | 
                if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */ | 
| 234 | 
                                !FEQ(nv->vert,odev.v.vert)) { | 
| 235 | 
                        int     dw = DisplayWidth(ourdisplay,ourscreen); | 
| 236 | 
                        int     dh = DisplayHeight(ourdisplay,ourscreen); | 
| 237 | 
 | 
| 238 | 
                        dw -= 25;       /* for window frame */ | 
| 239 | 
                        dh -= 50; | 
| 240 | 
                        odev.hres = 2.*VIEWDIST/pwidth * | 
| 241 | 
                                        tan(PI/180./2.*nv->horiz); | 
| 242 | 
                        odev.vres = 2.*VIEWDIST/pheight * | 
| 243 | 
                                        tan(PI/180./2.*nv->vert); | 
| 244 | 
                        if (odev.hres > dw) { | 
| 245 | 
                                odev.vres = dw * odev.vres / odev.hres; | 
| 246 | 
                                odev.hres = dw; | 
| 247 | 
                        } | 
| 248 | 
                        if (odev.vres > dh) { | 
| 249 | 
                                odev.hres = dh * odev.hres / odev.vres; | 
| 250 | 
                                odev.vres = dh; | 
| 251 | 
                        } | 
| 252 | 
                        XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); | 
| 253 | 
                        dev_input();    /* get resize event */ | 
| 254 | 
                } | 
| 255 | 
                odev.v = *nv; | 
| 256 | 
        } | 
| 257 | 
        if (nxtzmax > FTINY) { | 
| 258 | 
                curzmax = nxtzmax; | 
| 259 | 
                nxtzmax = 0.; | 
| 260 | 
        } | 
| 261 | 
        glClear(GL_DEPTH_BUFFER_BIT); | 
| 262 | 
        qtReplant(); | 
| 263 | 
        return(1); | 
| 264 | 
} | 
| 265 | 
 | 
| 266 | 
 | 
| 267 | 
dev_section(ofn)                /* add octree for geometry rendering */ | 
| 268 | 
char    *ofn; | 
| 269 | 
{ | 
| 270 | 
        /* unimplemented */ | 
| 271 | 
} | 
| 272 | 
 | 
| 273 | 
 | 
| 274 | 
dev_auxcom(cmd, args)           /* process an auxiliary command */ | 
| 275 | 
char    *cmd, *args; | 
| 276 | 
{ | 
| 277 | 
        sprintf(errmsg, "%s: unknown command", cmd); | 
| 278 | 
        error(COMMAND, errmsg); | 
| 279 | 
} | 
| 280 | 
 | 
| 281 | 
 | 
| 282 | 
VIEW * | 
| 283 | 
dev_auxview(n, hvres)           /* return nth auxiliary view */ | 
| 284 | 
int     n; | 
| 285 | 
int     hvres[2]; | 
| 286 | 
{ | 
| 287 | 
        if (n) | 
| 288 | 
                return(NULL); | 
| 289 | 
        hvres[0] = odev.hres; hvres[1] = odev.vres; | 
| 290 | 
        return(&odev.v); | 
| 291 | 
} | 
| 292 | 
 | 
| 293 | 
 | 
| 294 | 
int | 
| 295 | 
dev_input()                     /* get X11 input */ | 
| 296 | 
{ | 
| 297 | 
        inpresflags = 0; | 
| 298 | 
 | 
| 299 | 
        do | 
| 300 | 
                getevent(); | 
| 301 | 
 | 
| 302 | 
        while (XQLength(ourdisplay) > 0); | 
| 303 | 
 | 
| 304 | 
        return(inpresflags); | 
| 305 | 
} | 
| 306 | 
 | 
| 307 | 
 | 
| 308 | 
int | 
| 309 | 
dev_flush()                     /* flush output */ | 
| 310 | 
{ | 
| 311 | 
        qtUpdate(); | 
| 312 | 
        glFlush(); | 
| 313 | 
        rayqleft = RAYQLEN; | 
| 314 | 
        return(XPending(ourdisplay)); | 
| 315 | 
} | 
| 316 | 
 | 
| 317 | 
 | 
| 318 | 
dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */ | 
| 319 | 
BYTE    rgb[3]; | 
| 320 | 
FVECT   ip; | 
| 321 | 
double  rad; | 
| 322 | 
{ | 
| 323 | 
        register int    ci, j; | 
| 324 | 
        double  apexh, basez; | 
| 325 | 
                                        /* is window mapped? */ | 
| 326 | 
        if (!mapped) | 
| 327 | 
                return; | 
| 328 | 
                                        /* compute apex height (0. to 1.) */ | 
| 329 | 
        if (ip[2] > 1e6) | 
| 330 | 
                apexh = 1. - 1./DEPTHFACT; | 
| 331 | 
        else { | 
| 332 | 
                if (ip[2] > nxtzmax) | 
| 333 | 
                        nxtzmax = ip[2]; | 
| 334 | 
                if (ip[2] >= curzmax) | 
| 335 | 
                        apexh = 1. - 1./DEPTHFACT; | 
| 336 | 
                else | 
| 337 | 
                        apexh = 1. - ip[2]/(curzmax*DEPTHFACT); | 
| 338 | 
        } | 
| 339 | 
        rad *= 1.25;                    /* find conservative cone match */ | 
| 340 | 
        for (ci = 0; ci < MAXCONE-1; ci++) | 
| 341 | 
                if (cone[ci].rad >= rad) | 
| 342 | 
                        break; | 
| 343 | 
                                        /* draw it */ | 
| 344 | 
        glColor3ub(rgb[0], rgb[1], rgb[2]); | 
| 345 | 
        glBegin(GL_TRIANGLE_FAN); | 
| 346 | 
        glVertex3d(ip[0], ip[1], apexh);        /* start with apex */ | 
| 347 | 
        basez = apexh*cone[ci].va[0][2];        /* base z's all the same */ | 
| 348 | 
        for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */ | 
| 349 | 
                glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1], | 
| 350 | 
                                basez); | 
| 351 | 
                                                /* connect last to first */ | 
| 352 | 
        glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez); | 
| 353 | 
        glEnd();                                /* all done */ | 
| 354 | 
} | 
| 355 | 
 | 
| 356 | 
 | 
| 357 | 
static int | 
| 358 | 
mytmflags()                     /* figure out tone mapping flags */ | 
| 359 | 
{ | 
| 360 | 
        extern char     *progname; | 
| 361 | 
        register char   *cp, *tail; | 
| 362 | 
                                        /* find basic name */ | 
| 363 | 
        for (cp = tail = progname; *cp; cp++) | 
| 364 | 
                if (*cp == '/') | 
| 365 | 
                        tail = cp+1; | 
| 366 | 
        for (cp = tail; *cp && *cp != '.'; cp++) | 
| 367 | 
                ; | 
| 368 | 
        if (cp-tail == 4 && !strncmp(tail, "glx1", 4)) | 
| 369 | 
                return(TM_F_CAMERA|TM_F_NOSTDERR); | 
| 370 | 
        if (cp-tail == 5 && !strncmp(tail, "glx1h", 5)) | 
| 371 | 
                return(TM_F_HUMAN|TM_F_NOSTDERR); | 
| 372 | 
        error(USER, "illegal driver name"); | 
| 373 | 
} | 
| 374 | 
 | 
| 375 | 
 | 
| 376 | 
static | 
| 377 | 
initcones()                     /* initialize cone vertices */ | 
| 378 | 
{ | 
| 379 | 
        register int    i, j; | 
| 380 | 
        double  minrad, d; | 
| 381 | 
 | 
| 382 | 
        if (cone[0].nverts) | 
| 383 | 
                freecones(); | 
| 384 | 
        minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) + | 
| 385 | 
                                        DisplayHeight(ourdisplay,ourscreen)); | 
| 386 | 
        for (i = 0; i < MAXCONE; i++) { | 
| 387 | 
                d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */ | 
| 388 | 
                cone[i].rad = minrad + (1.-minrad)*d; | 
| 389 | 
                cone[i].nverts = MINVERT + 0.5 + (MAXVERT-MINVERT)*d; | 
| 390 | 
                cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT)); | 
| 391 | 
                if (cone[i].va == NULL) | 
| 392 | 
                        error(SYSTEM, "out of memory in initcones"); | 
| 393 | 
                for (j = cone[i].nverts; j--; ) { | 
| 394 | 
                        d = 2.*PI * (j+.5) / (cone[i].nverts); | 
| 395 | 
                        cone[i].va[j][0] = cos(d) * cone[i].rad; | 
| 396 | 
                        cone[i].va[j][1] = sin(d) * cone[i].rad; | 
| 397 | 
                        cone[i].va[j][2] = 1. - cone[i].rad; | 
| 398 | 
                } | 
| 399 | 
        } | 
| 400 | 
} | 
| 401 | 
 | 
| 402 | 
 | 
| 403 | 
static | 
| 404 | 
freecones()                     /* free cone vertices */ | 
| 405 | 
{ | 
| 406 | 
        register int    i; | 
| 407 | 
 | 
| 408 | 
        for (i = MAXCONE; i--; ) | 
| 409 | 
                if (cone[i].nverts) { | 
| 410 | 
                        free((void *)cone[i].va); | 
| 411 | 
                        cone[i].va = NULL; | 
| 412 | 
                        cone[i].nverts = 0; | 
| 413 | 
                } | 
| 414 | 
} | 
| 415 | 
 | 
| 416 | 
 | 
| 417 | 
static | 
| 418 | 
getevent()                      /* get next event */ | 
| 419 | 
{ | 
| 420 | 
        XNextEvent(ourdisplay, levptr(XEvent)); | 
| 421 | 
        switch (levptr(XEvent)->type) { | 
| 422 | 
        case ConfigureNotify: | 
| 423 | 
                resizewindow(levptr(XConfigureEvent)); | 
| 424 | 
                break; | 
| 425 | 
        case UnmapNotify: | 
| 426 | 
                mapped = 0; | 
| 427 | 
                break; | 
| 428 | 
        case MapNotify: | 
| 429 | 
                mapped = 1; | 
| 430 | 
                break; | 
| 431 | 
        case Expose: | 
| 432 | 
                fixwindow(levptr(XExposeEvent)); | 
| 433 | 
                break; | 
| 434 | 
        case KeyPress: | 
| 435 | 
                getkey(levptr(XKeyPressedEvent)); | 
| 436 | 
                break; | 
| 437 | 
        case ButtonPress: | 
| 438 | 
                getmove(levptr(XButtonPressedEvent)); | 
| 439 | 
                break; | 
| 440 | 
        } | 
| 441 | 
} | 
| 442 | 
 | 
| 443 | 
 | 
| 444 | 
static | 
| 445 | 
draw3dline(wp)                  /* draw 3d line in world coordinates */ | 
| 446 | 
register FVECT  wp[2]; | 
| 447 | 
{ | 
| 448 | 
        glVertex3d(wp[0][0], wp[0][1], wp[0][2]); | 
| 449 | 
        glVertex3d(wp[1][0], wp[1][1], wp[1][2]); | 
| 450 | 
} | 
| 451 | 
 | 
| 452 | 
 | 
| 453 | 
static | 
| 454 | 
draw_grids()                    /* draw holodeck section grids */ | 
| 455 | 
{ | 
| 456 | 
        static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00}; | 
| 457 | 
        double  xmin, xmax, ymin, ymax, zmin, zmax; | 
| 458 | 
        double  d, cx, sx, crad; | 
| 459 | 
        FVECT   vx, vy; | 
| 460 | 
        register int    i, j; | 
| 461 | 
                                        /* can we even do it? */ | 
| 462 | 
        if (!mapped || odev.v.type != VT_PER) | 
| 463 | 
                return; | 
| 464 | 
                                        /* compute view frustum */ | 
| 465 | 
        if (normalize(odev.v.vdir) == 0.0) | 
| 466 | 
                return; | 
| 467 | 
        zmin = 0.01; | 
| 468 | 
        zmax = 10000.; | 
| 469 | 
        if (odev.v.vfore > FTINY) | 
| 470 | 
                zmin = odev.v.vfore; | 
| 471 | 
        if (odev.v.vaft > FTINY) | 
| 472 | 
                zmax = odev.v.vaft; | 
| 473 | 
        xmax = zmin * tan(PI/180./2. * odev.v.horiz); | 
| 474 | 
        xmin = -xmax; | 
| 475 | 
        d = odev.v.hoff * (xmax - xmin); | 
| 476 | 
        xmin += d; xmax += d; | 
| 477 | 
        ymax = zmin * tan(PI/180./2. * odev.v.vert); | 
| 478 | 
        ymin = -ymax; | 
| 479 | 
        d = odev.v.voff * (ymax - ymin); | 
| 480 | 
        ymin += d; ymax += d; | 
| 481 | 
                                        /* set view matrix */ | 
| 482 | 
        glMatrixMode(GL_PROJECTION); | 
| 483 | 
        glPushMatrix(); | 
| 484 | 
        glLoadIdentity(); | 
| 485 | 
        glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); | 
| 486 | 
        gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], | 
| 487 | 
                odev.v.vp[0] + odev.v.vdir[0], | 
| 488 | 
                odev.v.vp[1] + odev.v.vdir[1], | 
| 489 | 
                odev.v.vp[2] + odev.v.vdir[2], | 
| 490 | 
                odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); | 
| 491 | 
        glDisable(GL_DEPTH_TEST);       /* write no depth values */ | 
| 492 | 
        glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]); | 
| 493 | 
        glBegin(GL_LINES);              /* draw each grid line */ | 
| 494 | 
        gridlines(draw3dline); | 
| 495 | 
        glEnd(); | 
| 496 | 
        glEnable(GL_DEPTH_TEST);        /* restore rendering params */ | 
| 497 | 
        glPopMatrix(); | 
| 498 | 
} | 
| 499 | 
 | 
| 500 | 
 | 
| 501 | 
static | 
| 502 | 
moveview(dx, dy, mov, orb)      /* move our view */ | 
| 503 | 
int     dx, dy, mov, orb; | 
| 504 | 
{ | 
| 505 | 
        VIEW    nv; | 
| 506 | 
        FVECT   odir, v1; | 
| 507 | 
        double  d; | 
| 508 | 
        register int    li; | 
| 509 | 
                                /* start with old view */ | 
| 510 | 
        nv = odev.v; | 
| 511 | 
                                /* change view direction */ | 
| 512 | 
        if (mov | orb) { | 
| 513 | 
                if ((li = qtFindLeaf(dx, dy)) < 0) | 
| 514 | 
                        return(0);      /* not on window */ | 
| 515 | 
                VSUM(odir, qtL.wp[li], nv.vp, -1.); | 
| 516 | 
        } else { | 
| 517 | 
                if (viewray(nv.vp, nv.vdir, &odev.v, | 
| 518 | 
                                (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) | 
| 519 | 
                        return(0);      /* outside view */ | 
| 520 | 
        } | 
| 521 | 
        if (orb && mov) {               /* orbit left/right */ | 
| 522 | 
                spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); | 
| 523 | 
                VSUM(nv.vp, qtL.wp[li], odir, -1.); | 
| 524 | 
                spinvector(nv.vdir, nv.vdir, nv.vup, d); | 
| 525 | 
        } else if (orb) {               /* orbit up/down */ | 
| 526 | 
                fcross(v1, odir, nv.vup); | 
| 527 | 
                if (normalize(v1) == 0.) | 
| 528 | 
                        return(0); | 
| 529 | 
                spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); | 
| 530 | 
                VSUM(nv.vp, qtL.wp[li], odir, -1.); | 
| 531 | 
                spinvector(nv.vdir, nv.vdir, v1, d); | 
| 532 | 
        } else if (mov) {               /* move forward/backward */ | 
| 533 | 
                d = MOVPCT/100. * mov; | 
| 534 | 
                VSUM(nv.vp, nv.vp, odir, d); | 
| 535 | 
        } | 
| 536 | 
        if (!mov ^ !orb && headlocked) {        /* restore head height */ | 
| 537 | 
                VSUM(v1, odev.v.vp, nv.vp, -1.); | 
| 538 | 
                d = DOT(v1, odev.v.vup); | 
| 539 | 
                VSUM(nv.vp, nv.vp, odev.v.vup, d); | 
| 540 | 
        } | 
| 541 | 
        if (setview(&nv) != NULL) | 
| 542 | 
                return(0);      /* illegal view */ | 
| 543 | 
        dev_view(&nv); | 
| 544 | 
        inpresflags |= DFL(DC_SETVIEW); | 
| 545 | 
        return(1); | 
| 546 | 
} | 
| 547 | 
 | 
| 548 | 
 | 
| 549 | 
static | 
| 550 | 
getmove(ebut)                           /* get view change */ | 
| 551 | 
XButtonPressedEvent     *ebut; | 
| 552 | 
{ | 
| 553 | 
        int     movdir = MOVDIR(ebut->button); | 
| 554 | 
        int     movorb = MOVORB(ebut->state); | 
| 555 | 
        int     oldnodesiz = qtMinNodesiz; | 
| 556 | 
        Window  rootw, childw; | 
| 557 | 
        int     rootx, rooty, wx, wy; | 
| 558 | 
        unsigned int    statemask; | 
| 559 | 
 | 
| 560 | 
        qtMinNodesiz = 24;              /* accelerate update rate */ | 
| 561 | 
        XNoOp(ourdisplay); | 
| 562 | 
 | 
| 563 | 
        while (!XCheckMaskEvent(ourdisplay, | 
| 564 | 
                        ButtonReleaseMask, levptr(XEvent))) { | 
| 565 | 
 | 
| 566 | 
                if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, | 
| 567 | 
                                &rootx, &rooty, &wx, &wy, &statemask)) | 
| 568 | 
                        break;          /* on another screen */ | 
| 569 | 
 | 
| 570 | 
                if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { | 
| 571 | 
                        sleep(1); | 
| 572 | 
                        continue; | 
| 573 | 
                } | 
| 574 | 
                glClear(GL_COLOR_BUFFER_BIT); | 
| 575 | 
                qtUpdate(); | 
| 576 | 
                draw_grids(); | 
| 577 | 
                glFlush(); | 
| 578 | 
        } | 
| 579 | 
        if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ | 
| 580 | 
                movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); | 
| 581 | 
                wx = levptr(XButtonReleasedEvent)->x; | 
| 582 | 
                wy = levptr(XButtonReleasedEvent)->y; | 
| 583 | 
                moveview(wx, odev.vres-1-wy, movdir, movorb); | 
| 584 | 
        } | 
| 585 | 
        dev_flush(); | 
| 586 | 
 | 
| 587 | 
        qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */ | 
| 588 | 
} | 
| 589 | 
 | 
| 590 | 
 | 
| 591 | 
static | 
| 592 | 
getkey(ekey)                            /* get input key */ | 
| 593 | 
register XKeyPressedEvent  *ekey; | 
| 594 | 
{ | 
| 595 | 
        int  n; | 
| 596 | 
        char    buf[8]; | 
| 597 | 
 | 
| 598 | 
        n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); | 
| 599 | 
        if (n != 1) | 
| 600 | 
                return; | 
| 601 | 
        switch (buf[0]) { | 
| 602 | 
        case 'h':                       /* turn on height motion lock */ | 
| 603 | 
                headlocked = 1; | 
| 604 | 
                return; | 
| 605 | 
        case 'H':                       /* turn off height motion lock */ | 
| 606 | 
                headlocked = 0; | 
| 607 | 
                return; | 
| 608 | 
        case 'l':                       /* retrieve last view */ | 
| 609 | 
                inpresflags |= DFL(DC_LASTVIEW); | 
| 610 | 
                return; | 
| 611 | 
        case 'p':                       /* pause computation */ | 
| 612 | 
                inpresflags |= DFL(DC_PAUSE); | 
| 613 | 
                return; | 
| 614 | 
        case 'v':                       /* spit out view */ | 
| 615 | 
                inpresflags |= DFL(DC_GETVIEW); | 
| 616 | 
                return; | 
| 617 | 
        case '\n': | 
| 618 | 
        case '\r':                      /* resume computation */ | 
| 619 | 
                inpresflags |= DFL(DC_RESUME); | 
| 620 | 
                return; | 
| 621 | 
        case CTRL('R'):                 /* redraw screen */ | 
| 622 | 
                if (nxtzmax > FTINY) { | 
| 623 | 
                        curzmax = nxtzmax; | 
| 624 | 
                        nxtzmax = 0.; | 
| 625 | 
                } | 
| 626 | 
                glClear(GL_DEPTH_BUFFER_BIT); | 
| 627 | 
                qtRedraw(0, 0, odev.hres, odev.vres); | 
| 628 | 
                return; | 
| 629 | 
        case CTRL('L'):                 /* refresh from server */ | 
| 630 | 
                if (inpresflags & DFL(DC_REDRAW)) | 
| 631 | 
                        return; | 
| 632 | 
                if (nxtzmax > FTINY) { | 
| 633 | 
                        curzmax = nxtzmax; | 
| 634 | 
                        nxtzmax = 0.; | 
| 635 | 
                } | 
| 636 | 
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
| 637 | 
                draw_grids(); | 
| 638 | 
                glFlush(); | 
| 639 | 
                qtCompost(100);                 /* get rid of old values */ | 
| 640 | 
                inpresflags |= DFL(DC_REDRAW);  /* resend values from server */ | 
| 641 | 
                rayqleft = 0;                   /* hold off update */ | 
| 642 | 
                return; | 
| 643 | 
        case 'K':                       /* kill rtrace process(es) */ | 
| 644 | 
                inpresflags |= DFL(DC_KILL); | 
| 645 | 
                break; | 
| 646 | 
        case 'R':                       /* restart rtrace */ | 
| 647 | 
                inpresflags |= DFL(DC_RESTART); | 
| 648 | 
                break; | 
| 649 | 
        case 'C':                       /* clobber holodeck */ | 
| 650 | 
                inpresflags |= DFL(DC_CLOBBER); | 
| 651 | 
                break; | 
| 652 | 
        case 'q':                       /* quit the program */ | 
| 653 | 
                inpresflags |= DFL(DC_QUIT); | 
| 654 | 
                return; | 
| 655 | 
        default: | 
| 656 | 
                XBell(ourdisplay, 0); | 
| 657 | 
                return; | 
| 658 | 
        } | 
| 659 | 
} | 
| 660 | 
 | 
| 661 | 
 | 
| 662 | 
static | 
| 663 | 
fixwindow(eexp)                         /* repair damage to window */ | 
| 664 | 
register XExposeEvent  *eexp; | 
| 665 | 
{ | 
| 666 | 
        int     xmin, xmax, ymin, ymax; | 
| 667 | 
 | 
| 668 | 
        if (odev.hres == 0 || odev.vres == 0)   /* first exposure */ | 
| 669 | 
                resizewindow((XConfigureEvent *)eexp); | 
| 670 | 
        xmin = eexp->x; xmax = eexp->x + eexp->width; | 
| 671 | 
        ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; | 
| 672 | 
                                                /* clear portion of depth */ | 
| 673 | 
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | 
| 674 | 
        glDepthFunc(GL_ALWAYS); | 
| 675 | 
        glBegin(GL_POLYGON); | 
| 676 | 
        glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.); | 
| 677 | 
        glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.); | 
| 678 | 
        glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.); | 
| 679 | 
        glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.); | 
| 680 | 
        glEnd(); | 
| 681 | 
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | 
| 682 | 
        glDepthFunc(GL_LEQUAL); | 
| 683 | 
        qtRedraw(xmin, ymin, xmax, ymax); | 
| 684 | 
} | 
| 685 | 
 | 
| 686 | 
 | 
| 687 | 
static | 
| 688 | 
resizewindow(ersz)                      /* resize window */ | 
| 689 | 
register XConfigureEvent  *ersz; | 
| 690 | 
{ | 
| 691 | 
        glViewport(0, 0, ersz->width, ersz->height); | 
| 692 | 
 | 
| 693 | 
        if (ersz->width == odev.hres && ersz->height == odev.vres) | 
| 694 | 
                return; | 
| 695 | 
 | 
| 696 | 
        odev.hres = ersz->width; | 
| 697 | 
        odev.vres = ersz->height; | 
| 698 | 
 | 
| 699 | 
        odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); | 
| 700 | 
        odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); | 
| 701 | 
 | 
| 702 | 
        inpresflags |= DFL(DC_SETVIEW); | 
| 703 | 
} |