| 1 | gregl | 3.1 | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * OpenGL GLX driver for holodeck display. | 
| 9 | gregl | 3.2 | * Based on x11 driver. | 
| 10 | gregl | 3.1 | */ | 
| 11 |  |  |  | 
| 12 |  |  | #include "standard.h" | 
| 13 | gregl | 3.4 | #include "rhd_qtree.h" | 
| 14 | gregl | 3.1 |  | 
| 15 |  |  | #include  <GL/glx.h> | 
| 16 |  |  |  | 
| 17 |  |  | #include  "x11icon.h" | 
| 18 |  |  |  | 
| 19 | gregl | 3.10 | #ifndef RAYQLEN | 
| 20 |  |  | #define RAYQLEN         50000           /* max. rays to queue before flush */ | 
| 21 |  |  | #endif | 
| 22 |  |  |  | 
| 23 | gregl | 3.1 | #ifndef FEQ | 
| 24 |  |  | #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) | 
| 25 |  |  | #endif | 
| 26 |  |  |  | 
| 27 | gregl | 3.4 | #ifndef MAXCONE | 
| 28 |  |  | #define MAXCONE         16              /* number of different cone sizes */ | 
| 29 | gregl | 3.1 | #endif | 
| 30 | gregl | 3.4 | #ifndef MAXVERT | 
| 31 |  |  | #define MAXVERT         32              /* maximum number of cone vertices */ | 
| 32 | gregl | 3.1 | #endif | 
| 33 | gregl | 3.4 | #ifndef MINVERT | 
| 34 |  |  | #define MINVERT         4               /* minimum number of cone vertices */ | 
| 35 | gregl | 3.1 | #endif | 
| 36 | gregl | 3.4 | #ifndef DEPTHFACT | 
| 37 |  |  | #define DEPTHFACT       16.             /* multiplier for depth tests */ | 
| 38 | gregl | 3.1 | #endif | 
| 39 |  |  |  | 
| 40 | gregl | 3.2 | #define GAMMA           1.4             /* default gamma correction */ | 
| 41 | gregl | 3.1 |  | 
| 42 |  |  | #define MOVPCT          7               /* percent distance to move /frame */ | 
| 43 |  |  | #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) | 
| 44 |  |  | #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */ | 
| 45 |  |  | #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) | 
| 46 |  |  |  | 
| 47 |  |  | #define MINWIDTH        480             /* minimum graphics window width */ | 
| 48 |  |  | #define MINHEIGHT       400             /* minimum graphics window height */ | 
| 49 |  |  |  | 
| 50 |  |  | #define VIEWDIST        356             /* assumed viewing distance (mm) */ | 
| 51 |  |  |  | 
| 52 |  |  | #define BORWIDTH        5               /* border width */ | 
| 53 |  |  |  | 
| 54 |  |  | #define  ourscreen      DefaultScreen(ourdisplay) | 
| 55 |  |  | #define  ourroot        RootWindow(ourdisplay,ourscreen) | 
| 56 |  |  | #define  ourmask        (StructureNotifyMask|ExposureMask|KeyPressMask|\ | 
| 57 |  |  | ButtonPressMask|ButtonReleaseMask) | 
| 58 |  |  |  | 
| 59 |  |  | #define  levptr(etype)  ((etype *)¤tevent) | 
| 60 |  |  |  | 
| 61 |  |  | struct driver   odev;                   /* global device driver structure */ | 
| 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 | gregl | 3.4 | static double   curzmax = 1e4;          /* current depth upper limit */ | 
| 76 |  |  | static double   nxtzmax = 0.;           /* maximum (finite) depth so far */ | 
| 77 | gregl | 3.1 |  | 
| 78 | gregl | 3.4 | 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 | gregl | 3.1 | static int      inpresflags;            /* input result flags */ | 
| 85 |  |  |  | 
| 86 |  |  | static int      headlocked = 0;         /* lock vertical motion */ | 
| 87 |  |  |  | 
| 88 |  |  | static int  resizewindow(), getevent(), getkey(), moveview(), | 
| 89 | gregl | 3.4 | initcones(), freecones(), | 
| 90 |  |  | getmove(), fixwindow(), mytmflags(); | 
| 91 | gregl | 3.1 |  | 
| 92 |  |  |  | 
| 93 |  |  | dev_open(id)                    /* initialize X11 driver */ | 
| 94 |  |  | char  *id; | 
| 95 |  |  | { | 
| 96 |  |  | extern char     *getenv(); | 
| 97 | gregl | 3.15 | static RGBPRIMS myprims = STDPRIMS; | 
| 98 | gregl | 3.1 | static int      atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, | 
| 99 |  |  | GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, | 
| 100 | gregl | 3.4 | GLX_DEPTH_SIZE,15, None}; | 
| 101 | gregl | 3.15 | char    *ev; | 
| 102 | gregl | 3.1 | double  gamval = GAMMA; | 
| 103 | gregl | 3.15 | RGBPRIMP        dpri = stdprims; | 
| 104 | gregl | 3.1 | XSetWindowAttributes    ourwinattr; | 
| 105 |  |  | XWMHints        ourxwmhints; | 
| 106 |  |  | XSizeHints      oursizhints; | 
| 107 | gregl | 3.4 | /* set quadtree globals */ | 
| 108 |  |  | qtMinNodesiz = 3; | 
| 109 | gregl | 3.9 | qtDepthEps = 0.07; | 
| 110 | gregl | 3.1 | /* 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 | gregl | 3.15 | 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 | gregl | 3.1 | 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 | gregl | 3.2 | /* 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 | gregl | 3.4 | glDisable(GL_CULL_FACE); | 
| 165 |  |  | glMatrixMode(GL_PROJECTION); | 
| 166 |  |  | glOrtho(0., 1., 0., 1., -.01, 1.01); | 
| 167 |  |  | glTranslated(0., 0., -1.01); | 
| 168 | gregl | 3.1 | /* 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 |  |  | copystruct(&odev.v, &stdview); | 
| 174 |  |  | odev.v.type = VT_PER; | 
| 175 | gregl | 3.2 | /* map the window */ | 
| 176 |  |  | XMapWindow(ourdisplay, gwind); | 
| 177 |  |  | dev_input();                    /* sets size and view angles */ | 
| 178 | gregl | 3.4 | /* allocate our leaf pile */ | 
| 179 | gregl | 3.9 | if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * | 
| 180 | gregl | 3.11 | DisplayHeight(ourdisplay,ourscreen) * 3 / | 
| 181 |  |  | (qtMinNodesiz*qtMinNodesiz*2))) | 
| 182 | gregl | 3.2 | error(SYSTEM, "insufficient memory for value storage"); | 
| 183 | gregl | 3.1 | odev.name = id; | 
| 184 |  |  | odev.ifd = ConnectionNumber(ourdisplay); | 
| 185 | gregl | 3.4 | /* initialize cone array */ | 
| 186 |  |  | initcones(); | 
| 187 | gregl | 3.1 | } | 
| 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 | gregl | 3.4 | qtFreeLeaves(); | 
| 199 | gregl | 3.1 | tmDone(NULL); | 
| 200 | gregl | 3.4 | freecones(); | 
| 201 | gregl | 3.1 | odev.v.type = 0; | 
| 202 |  |  | odev.hres = odev.vres = 0; | 
| 203 |  |  | odev.ifd = -1; | 
| 204 |  |  | } | 
| 205 |  |  |  | 
| 206 |  |  |  | 
| 207 | gregl | 3.14 | 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 | gregl | 3.1 | int | 
| 216 |  |  | dev_view(nv)                    /* assign new driver view */ | 
| 217 |  |  | register VIEW   *nv; | 
| 218 |  |  | { | 
| 219 | gregl | 3.4 | if (nv->type == VT_PAR ||               /* check view legality */ | 
| 220 |  |  | nv->horiz > 160. || nv->vert > 160.) { | 
| 221 | gregl | 3.1 | error(COMMAND, "illegal view type/angle"); | 
| 222 | gregl | 3.4 | nv->type = odev.v.type; | 
| 223 | gregl | 3.1 | nv->horiz = odev.v.horiz; | 
| 224 |  |  | nv->vert = odev.v.vert; | 
| 225 |  |  | return(0); | 
| 226 |  |  | } | 
| 227 | gregl | 3.4 | if (nv->vfore > FTINY) { | 
| 228 |  |  | error(COMMAND, "cannot handle fore clipping"); | 
| 229 |  |  | nv->vfore = 0.; | 
| 230 |  |  | return(0); | 
| 231 |  |  | } | 
| 232 | gregl | 3.1 | 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 | gregl | 3.2 | dev_input();    /* get resize event */ | 
| 254 | gregl | 3.1 | } | 
| 255 |  |  | copystruct(&odev.v, nv); | 
| 256 |  |  | } | 
| 257 | gregl | 3.4 | if (nxtzmax > FTINY) { | 
| 258 |  |  | curzmax = nxtzmax; | 
| 259 |  |  | nxtzmax = 0.; | 
| 260 |  |  | } | 
| 261 |  |  | glClear(GL_DEPTH_BUFFER_BIT); | 
| 262 |  |  | qtReplant(); | 
| 263 | gregl | 3.1 | return(1); | 
| 264 |  |  | } | 
| 265 |  |  |  | 
| 266 |  |  |  | 
| 267 |  |  | int | 
| 268 |  |  | dev_input()                     /* get X11 input */ | 
| 269 |  |  | { | 
| 270 |  |  | inpresflags = 0; | 
| 271 |  |  |  | 
| 272 |  |  | do | 
| 273 |  |  | getevent(); | 
| 274 |  |  |  | 
| 275 |  |  | while (XQLength(ourdisplay) > 0); | 
| 276 |  |  |  | 
| 277 |  |  | return(inpresflags); | 
| 278 |  |  | } | 
| 279 |  |  |  | 
| 280 |  |  |  | 
| 281 | gregl | 3.4 | int | 
| 282 |  |  | dev_flush()                     /* flush output */ | 
| 283 | gregl | 3.1 | { | 
| 284 | gregl | 3.4 | qtUpdate(); | 
| 285 |  |  | glFlush(); | 
| 286 | gregl | 3.10 | rayqleft = RAYQLEN; | 
| 287 | gregl | 3.4 | return(XPending(ourdisplay)); | 
| 288 | gregl | 3.1 | } | 
| 289 |  |  |  | 
| 290 |  |  |  | 
| 291 | gregl | 3.4 | dev_cone(rgb, ip, rad)          /* render a cone in view coordinates */ | 
| 292 |  |  | BYTE    rgb[3]; | 
| 293 |  |  | FVECT   ip; | 
| 294 |  |  | double  rad; | 
| 295 | gregl | 3.1 | { | 
| 296 | gregl | 3.4 | register int    ci, j; | 
| 297 |  |  | double  apexh, basez; | 
| 298 | gregl | 3.12 | /* is window mapped? */ | 
| 299 |  |  | if (!mapped) | 
| 300 |  |  | return; | 
| 301 | gregl | 3.4 | /* compute apex height (0. to 1.) */ | 
| 302 |  |  | if (ip[2] > 1e6) | 
| 303 |  |  | apexh = 1. - 1./DEPTHFACT; | 
| 304 |  |  | else { | 
| 305 |  |  | if (ip[2] > nxtzmax) | 
| 306 |  |  | nxtzmax = ip[2]; | 
| 307 |  |  | if (ip[2] >= curzmax) | 
| 308 |  |  | apexh = 1. - 1./DEPTHFACT; | 
| 309 |  |  | else | 
| 310 |  |  | apexh = 1. - ip[2]/(curzmax*DEPTHFACT); | 
| 311 | gregl | 3.1 | } | 
| 312 | gregl | 3.5 | rad *= 1.25;                    /* find conservative cone match */ | 
| 313 | gregl | 3.4 | for (ci = 0; ci < MAXCONE-1; ci++) | 
| 314 |  |  | if (cone[ci].rad >= rad) | 
| 315 |  |  | break; | 
| 316 |  |  | /* draw it */ | 
| 317 |  |  | glColor3ub(rgb[0], rgb[1], rgb[2]); | 
| 318 |  |  | glBegin(GL_TRIANGLE_FAN); | 
| 319 |  |  | glVertex3d(ip[0], ip[1], apexh);        /* start with apex */ | 
| 320 |  |  | basez = apexh*cone[ci].va[0][2];        /* base z's all the same */ | 
| 321 |  |  | for (j = 0; j < cone[ci].nverts; j++)   /* draw each face */ | 
| 322 |  |  | glVertex3d(ip[0]+cone[ci].va[j][0], ip[1]+cone[ci].va[j][1], | 
| 323 |  |  | basez); | 
| 324 |  |  | /* connect last to first */ | 
| 325 |  |  | glVertex3d(ip[0]+cone[ci].va[0][0], ip[1]+cone[ci].va[0][1], basez); | 
| 326 |  |  | glEnd();                                /* all done */ | 
| 327 | gregl | 3.1 | } | 
| 328 |  |  |  | 
| 329 |  |  |  | 
| 330 |  |  | static int | 
| 331 |  |  | mytmflags()                     /* figure out tone mapping flags */ | 
| 332 |  |  | { | 
| 333 |  |  | extern char     *progname; | 
| 334 |  |  | register char   *cp, *tail; | 
| 335 |  |  | /* find basic name */ | 
| 336 |  |  | for (cp = tail = progname; *cp; cp++) | 
| 337 |  |  | if (*cp == '/') | 
| 338 |  |  | tail = cp+1; | 
| 339 |  |  | for (cp = tail; *cp && *cp != '.'; cp++) | 
| 340 |  |  | ; | 
| 341 |  |  | if (cp-tail == 3 && !strncmp(tail, "glx", 3)) | 
| 342 | gregl | 3.10 | return(TM_F_CAMERA|TM_F_NOSTDERR); | 
| 343 | gregl | 3.1 | if (cp-tail == 4 && !strncmp(tail, "glxh", 4)) | 
| 344 | gregl | 3.10 | return(TM_F_HUMAN|TM_F_NOSTDERR); | 
| 345 | gregl | 3.1 | error(USER, "illegal driver name"); | 
| 346 |  |  | } | 
| 347 |  |  |  | 
| 348 |  |  |  | 
| 349 |  |  | static | 
| 350 | gregl | 3.4 | initcones()                     /* initialize cone vertices */ | 
| 351 | gregl | 3.1 | { | 
| 352 |  |  | register int    i, j; | 
| 353 | gregl | 3.4 | double  minrad, d; | 
| 354 | gregl | 3.1 |  | 
| 355 | gregl | 3.4 | if (cone[0].nverts) | 
| 356 |  |  | freecones(); | 
| 357 |  |  | minrad = 2.*qtMinNodesiz/(double)(DisplayWidth(ourdisplay,ourscreen) + | 
| 358 |  |  | DisplayHeight(ourdisplay,ourscreen)); | 
| 359 |  |  | for (i = 0; i < MAXCONE; i++) { | 
| 360 |  |  | d = (double)i/(MAXCONE-1); d *= d;      /* x^2 distribution */ | 
| 361 |  |  | cone[i].rad = minrad + (1.-minrad)*d; | 
| 362 |  |  | cone[i].nverts = MINVERT + (MAXVERT-MINVERT)*d; | 
| 363 |  |  | cone[i].va = (FVECT *)malloc(cone[i].nverts*sizeof(FVECT)); | 
| 364 |  |  | if (cone[i].va == NULL) | 
| 365 |  |  | error(SYSTEM, "out of memory in initcones"); | 
| 366 |  |  | for (j = cone[i].nverts; j--; ) { | 
| 367 |  |  | d = 2.*PI * (j+.5) / (cone[i].nverts); | 
| 368 |  |  | cone[i].va[j][0] = cos(d) * cone[i].rad; | 
| 369 |  |  | cone[i].va[j][1] = sin(d) * cone[i].rad; | 
| 370 |  |  | cone[i].va[j][2] = 1. - cone[i].rad; | 
| 371 | gregl | 3.2 | } | 
| 372 |  |  | } | 
| 373 |  |  | } | 
| 374 |  |  |  | 
| 375 |  |  |  | 
| 376 |  |  | static | 
| 377 | gregl | 3.4 | freecones()                     /* free cone vertices */ | 
| 378 | gregl | 3.2 | { | 
| 379 | gregl | 3.4 | register int    i; | 
| 380 | gregl | 3.2 |  | 
| 381 | gregl | 3.4 | for (i = MAXCONE; i--; ) | 
| 382 |  |  | if (cone[i].nverts) { | 
| 383 |  |  | free((char *)cone[i].va); | 
| 384 |  |  | cone[i].va = NULL; | 
| 385 |  |  | cone[i].nverts = 0; | 
| 386 | gregl | 3.2 | } | 
| 387 |  |  | } | 
| 388 |  |  |  | 
| 389 |  |  |  | 
| 390 |  |  | static | 
| 391 | gregl | 3.1 | getevent()                      /* get next event */ | 
| 392 |  |  | { | 
| 393 |  |  | XNextEvent(ourdisplay, levptr(XEvent)); | 
| 394 |  |  | switch (levptr(XEvent)->type) { | 
| 395 |  |  | case ConfigureNotify: | 
| 396 |  |  | resizewindow(levptr(XConfigureEvent)); | 
| 397 |  |  | break; | 
| 398 |  |  | case UnmapNotify: | 
| 399 |  |  | mapped = 0; | 
| 400 |  |  | break; | 
| 401 |  |  | case MapNotify: | 
| 402 |  |  | mapped = 1; | 
| 403 |  |  | break; | 
| 404 |  |  | case Expose: | 
| 405 |  |  | fixwindow(levptr(XExposeEvent)); | 
| 406 |  |  | break; | 
| 407 |  |  | case KeyPress: | 
| 408 |  |  | getkey(levptr(XKeyPressedEvent)); | 
| 409 |  |  | break; | 
| 410 |  |  | case ButtonPress: | 
| 411 |  |  | getmove(levptr(XButtonPressedEvent)); | 
| 412 |  |  | break; | 
| 413 |  |  | } | 
| 414 |  |  | } | 
| 415 |  |  |  | 
| 416 |  |  |  | 
| 417 |  |  | static | 
| 418 |  |  | draw3dline(wp)                  /* draw 3d line in world coordinates */ | 
| 419 |  |  | register FVECT  wp[2]; | 
| 420 |  |  | { | 
| 421 |  |  | glVertex3d(wp[0][0], wp[0][1], wp[0][2]); | 
| 422 |  |  | glVertex3d(wp[1][0], wp[1][1], wp[1][2]); | 
| 423 |  |  | } | 
| 424 |  |  |  | 
| 425 |  |  |  | 
| 426 |  |  | static | 
| 427 |  |  | draw_grids()                    /* draw holodeck section grids */ | 
| 428 |  |  | { | 
| 429 | gregl | 3.2 | static BYTE     gridrgba[4] = {0x0, 0xff, 0xff, 0x00}; | 
| 430 | gregl | 3.4 | double  xmin, xmax, ymin, ymax, zmin, zmax; | 
| 431 |  |  | double  d, cx, sx, crad; | 
| 432 |  |  | FVECT   vx, vy; | 
| 433 |  |  | register int    i, j; | 
| 434 |  |  | /* can we even do it? */ | 
| 435 |  |  | if (!mapped || odev.v.type != VT_PER) | 
| 436 | gregl | 3.1 | return; | 
| 437 | gregl | 3.4 | /* compute view frustum */ | 
| 438 |  |  | if (normalize(odev.v.vdir) == 0.0) | 
| 439 |  |  | return; | 
| 440 |  |  | zmin = 0.01; | 
| 441 |  |  | zmax = 10000.; | 
| 442 |  |  | if (odev.v.vfore > FTINY) | 
| 443 |  |  | zmin = odev.v.vfore; | 
| 444 |  |  | if (odev.v.vaft > FTINY) | 
| 445 |  |  | zmax = odev.v.vaft; | 
| 446 |  |  | xmax = zmin * tan(PI/180./2. * odev.v.horiz); | 
| 447 |  |  | xmin = -xmax; | 
| 448 |  |  | d = odev.v.hoff * (xmax - xmin); | 
| 449 |  |  | xmin += d; xmax += d; | 
| 450 |  |  | ymax = zmin * tan(PI/180./2. * odev.v.vert); | 
| 451 |  |  | ymin = -ymax; | 
| 452 |  |  | d = odev.v.voff * (ymax - ymin); | 
| 453 |  |  | ymin += d; ymax += d; | 
| 454 |  |  | /* set view matrix */ | 
| 455 |  |  | glMatrixMode(GL_PROJECTION); | 
| 456 |  |  | glPushMatrix(); | 
| 457 |  |  | glLoadIdentity(); | 
| 458 |  |  | glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); | 
| 459 |  |  | gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], | 
| 460 |  |  | odev.v.vp[0] + odev.v.vdir[0], | 
| 461 |  |  | odev.v.vp[1] + odev.v.vdir[1], | 
| 462 |  |  | odev.v.vp[2] + odev.v.vdir[2], | 
| 463 |  |  | odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); | 
| 464 |  |  | glDisable(GL_DEPTH_TEST);       /* write no depth values */ | 
| 465 | gregl | 3.1 | glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]); | 
| 466 | gregl | 3.4 | glBegin(GL_LINES);              /* draw each grid line */ | 
| 467 | gregl | 3.1 | gridlines(draw3dline); | 
| 468 |  |  | glEnd(); | 
| 469 | gregl | 3.4 | glEnable(GL_DEPTH_TEST);        /* restore rendering params */ | 
| 470 |  |  | glPopMatrix(); | 
| 471 | gregl | 3.1 | } | 
| 472 |  |  |  | 
| 473 |  |  |  | 
| 474 |  |  | static | 
| 475 |  |  | moveview(dx, dy, mov, orb)      /* move our view */ | 
| 476 |  |  | int     dx, dy, mov, orb; | 
| 477 |  |  | { | 
| 478 |  |  | VIEW    nv; | 
| 479 |  |  | FVECT   odir, v1; | 
| 480 |  |  | double  d; | 
| 481 |  |  | register int    li; | 
| 482 |  |  | /* start with old view */ | 
| 483 |  |  | copystruct(&nv, &odev.v); | 
| 484 |  |  | /* change view direction */ | 
| 485 |  |  | if (mov | orb) { | 
| 486 | gregl | 3.4 | if ((li = qtFindLeaf(dx, dy)) < 0) | 
| 487 | gregl | 3.1 | return(0);      /* not on window */ | 
| 488 | gregl | 3.4 | VSUM(odir, qtL.wp[li], nv.vp, -1.); | 
| 489 | gregl | 3.1 | } else { | 
| 490 |  |  | if (viewray(nv.vp, nv.vdir, &odev.v, | 
| 491 |  |  | (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) | 
| 492 |  |  | return(0);      /* outside view */ | 
| 493 |  |  | } | 
| 494 |  |  | if (orb && mov) {               /* orbit left/right */ | 
| 495 |  |  | spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); | 
| 496 | gregl | 3.4 | VSUM(nv.vp, qtL.wp[li], odir, -1.); | 
| 497 | gregl | 3.1 | spinvector(nv.vdir, nv.vdir, nv.vup, d); | 
| 498 |  |  | } else if (orb) {               /* orbit up/down */ | 
| 499 |  |  | fcross(v1, odir, nv.vup); | 
| 500 |  |  | if (normalize(v1) == 0.) | 
| 501 |  |  | return(0); | 
| 502 |  |  | spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); | 
| 503 | gregl | 3.4 | VSUM(nv.vp, qtL.wp[li], odir, -1.); | 
| 504 | gregl | 3.1 | spinvector(nv.vdir, nv.vdir, v1, d); | 
| 505 |  |  | } else if (mov) {               /* move forward/backward */ | 
| 506 |  |  | d = MOVPCT/100. * mov; | 
| 507 |  |  | VSUM(nv.vp, nv.vp, odir, d); | 
| 508 |  |  | } | 
| 509 |  |  | if (!mov ^ !orb && headlocked) {        /* restore head height */ | 
| 510 |  |  | VSUM(v1, odev.v.vp, nv.vp, -1.); | 
| 511 |  |  | d = DOT(v1, odev.v.vup); | 
| 512 |  |  | VSUM(nv.vp, nv.vp, odev.v.vup, d); | 
| 513 |  |  | } | 
| 514 |  |  | if (setview(&nv) != NULL) | 
| 515 |  |  | return(0);      /* illegal view */ | 
| 516 |  |  | dev_view(&nv); | 
| 517 |  |  | inpresflags |= DFL(DC_SETVIEW); | 
| 518 |  |  | return(1); | 
| 519 |  |  | } | 
| 520 |  |  |  | 
| 521 |  |  |  | 
| 522 |  |  | static | 
| 523 |  |  | getmove(ebut)                           /* get view change */ | 
| 524 |  |  | XButtonPressedEvent     *ebut; | 
| 525 |  |  | { | 
| 526 |  |  | int     movdir = MOVDIR(ebut->button); | 
| 527 |  |  | int     movorb = MOVORB(ebut->state); | 
| 528 | gregl | 3.4 | int     oldnodesiz = qtMinNodesiz; | 
| 529 | gregl | 3.1 | Window  rootw, childw; | 
| 530 |  |  | int     rootx, rooty, wx, wy; | 
| 531 |  |  | unsigned int    statemask; | 
| 532 |  |  |  | 
| 533 | gregl | 3.4 | qtMinNodesiz = 24;              /* accelerate update rate */ | 
| 534 | gregl | 3.1 | XNoOp(ourdisplay); | 
| 535 |  |  |  | 
| 536 |  |  | while (!XCheckMaskEvent(ourdisplay, | 
| 537 |  |  | ButtonReleaseMask, levptr(XEvent))) { | 
| 538 |  |  |  | 
| 539 |  |  | if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, | 
| 540 |  |  | &rootx, &rooty, &wx, &wy, &statemask)) | 
| 541 |  |  | break;          /* on another screen */ | 
| 542 |  |  |  | 
| 543 |  |  | if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { | 
| 544 |  |  | sleep(1); | 
| 545 |  |  | continue; | 
| 546 |  |  | } | 
| 547 | gregl | 3.4 | glClear(GL_COLOR_BUFFER_BIT); | 
| 548 |  |  | qtUpdate(); | 
| 549 | gregl | 3.1 | draw_grids(); | 
| 550 | gregl | 3.4 | glFlush(); | 
| 551 | gregl | 3.1 | } | 
| 552 |  |  | if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ | 
| 553 |  |  | movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); | 
| 554 |  |  | wx = levptr(XButtonReleasedEvent)->x; | 
| 555 |  |  | wy = levptr(XButtonReleasedEvent)->y; | 
| 556 |  |  | moveview(wx, odev.vres-1-wy, movdir, movorb); | 
| 557 |  |  | } | 
| 558 |  |  | dev_flush(); | 
| 559 | gregl | 3.4 |  | 
| 560 |  |  | qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */ | 
| 561 | gregl | 3.1 | } | 
| 562 |  |  |  | 
| 563 |  |  |  | 
| 564 |  |  | static | 
| 565 |  |  | getkey(ekey)                            /* get input key */ | 
| 566 |  |  | register XKeyPressedEvent  *ekey; | 
| 567 |  |  | { | 
| 568 |  |  | int  n; | 
| 569 |  |  | char    buf[8]; | 
| 570 |  |  |  | 
| 571 |  |  | n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); | 
| 572 |  |  | if (n != 1) | 
| 573 |  |  | return; | 
| 574 |  |  | switch (buf[0]) { | 
| 575 |  |  | case 'h':                       /* turn on height motion lock */ | 
| 576 |  |  | headlocked = 1; | 
| 577 |  |  | return; | 
| 578 |  |  | case 'H':                       /* turn off height motion lock */ | 
| 579 |  |  | headlocked = 0; | 
| 580 |  |  | return; | 
| 581 |  |  | case 'l':                       /* retrieve last view */ | 
| 582 |  |  | inpresflags |= DFL(DC_LASTVIEW); | 
| 583 |  |  | return; | 
| 584 |  |  | case 'p':                       /* pause computation */ | 
| 585 |  |  | inpresflags |= DFL(DC_PAUSE); | 
| 586 |  |  | return; | 
| 587 |  |  | case 'v':                       /* spit out view */ | 
| 588 |  |  | inpresflags |= DFL(DC_GETVIEW); | 
| 589 |  |  | return; | 
| 590 |  |  | case '\n': | 
| 591 |  |  | case '\r':                      /* resume computation */ | 
| 592 |  |  | inpresflags |= DFL(DC_RESUME); | 
| 593 |  |  | return; | 
| 594 |  |  | case CTRL('R'):                 /* redraw screen */ | 
| 595 | gregl | 3.8 | if (nxtzmax > FTINY) { | 
| 596 |  |  | curzmax = nxtzmax; | 
| 597 |  |  | nxtzmax = 0.; | 
| 598 |  |  | } | 
| 599 | gregl | 3.2 | glClear(GL_DEPTH_BUFFER_BIT); | 
| 600 | gregl | 3.4 | qtRedraw(0, 0, odev.hres, odev.vres); | 
| 601 | gregl | 3.1 | return; | 
| 602 |  |  | case CTRL('L'):                 /* refresh from server */ | 
| 603 |  |  | if (inpresflags & DFL(DC_REDRAW)) | 
| 604 |  |  | return; | 
| 605 | gregl | 3.8 | if (nxtzmax > FTINY) { | 
| 606 |  |  | curzmax = nxtzmax; | 
| 607 |  |  | nxtzmax = 0.; | 
| 608 |  |  | } | 
| 609 | gregl | 3.1 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
| 610 |  |  | draw_grids(); | 
| 611 |  |  | glFlush(); | 
| 612 | gregl | 3.4 | qtCompost(100);                 /* get rid of old values */ | 
| 613 | gregl | 3.1 | inpresflags |= DFL(DC_REDRAW);  /* resend values from server */ | 
| 614 | gregl | 3.13 | rayqleft = 0;                   /* hold off update */ | 
| 615 | gregl | 3.1 | return; | 
| 616 |  |  | case 'K':                       /* kill rtrace process(es) */ | 
| 617 |  |  | inpresflags |= DFL(DC_KILL); | 
| 618 |  |  | break; | 
| 619 |  |  | case 'R':                       /* restart rtrace */ | 
| 620 |  |  | inpresflags |= DFL(DC_RESTART); | 
| 621 |  |  | break; | 
| 622 |  |  | case 'C':                       /* clobber holodeck */ | 
| 623 |  |  | inpresflags |= DFL(DC_CLOBBER); | 
| 624 |  |  | break; | 
| 625 |  |  | case 'q':                       /* quit the program */ | 
| 626 |  |  | inpresflags |= DFL(DC_QUIT); | 
| 627 |  |  | return; | 
| 628 |  |  | default: | 
| 629 |  |  | XBell(ourdisplay, 0); | 
| 630 |  |  | return; | 
| 631 |  |  | } | 
| 632 |  |  | } | 
| 633 |  |  |  | 
| 634 |  |  |  | 
| 635 |  |  | static | 
| 636 |  |  | fixwindow(eexp)                         /* repair damage to window */ | 
| 637 |  |  | register XExposeEvent  *eexp; | 
| 638 |  |  | { | 
| 639 | gregl | 3.4 | int     xmin, xmax, ymin, ymax; | 
| 640 |  |  |  | 
| 641 | gregl | 3.2 | if (odev.hres == 0 || odev.vres == 0)   /* first exposure */ | 
| 642 |  |  | resizewindow((XConfigureEvent *)eexp); | 
| 643 | gregl | 3.4 | xmin = eexp->x; xmax = eexp->x + eexp->width; | 
| 644 |  |  | ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; | 
| 645 |  |  | /* clear portion of depth */ | 
| 646 |  |  | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | 
| 647 |  |  | glDepthFunc(GL_ALWAYS); | 
| 648 |  |  | glBegin(GL_POLYGON); | 
| 649 |  |  | glVertex3d((double)xmin/odev.hres, (double)ymin/odev.vres, 0.); | 
| 650 |  |  | glVertex3d((double)xmax/odev.hres, (double)ymin/odev.vres, 0.); | 
| 651 |  |  | glVertex3d((double)xmax/odev.hres, (double)ymax/odev.vres, 0.); | 
| 652 |  |  | glVertex3d((double)xmin/odev.hres, (double)ymax/odev.vres, 0.); | 
| 653 |  |  | glEnd(); | 
| 654 |  |  | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | 
| 655 |  |  | glDepthFunc(GL_LEQUAL); | 
| 656 |  |  | qtRedraw(xmin, ymin, xmax, ymax); | 
| 657 | gregl | 3.1 | } | 
| 658 |  |  |  | 
| 659 |  |  |  | 
| 660 |  |  | static | 
| 661 |  |  | resizewindow(ersz)                      /* resize window */ | 
| 662 |  |  | register XConfigureEvent  *ersz; | 
| 663 |  |  | { | 
| 664 | gregl | 3.6 | glViewport(0, 0, ersz->width, ersz->height); | 
| 665 |  |  |  | 
| 666 | gregl | 3.1 | if (ersz->width == odev.hres && ersz->height == odev.vres) | 
| 667 |  |  | return; | 
| 668 |  |  |  | 
| 669 |  |  | odev.hres = ersz->width; | 
| 670 |  |  | odev.vres = ersz->height; | 
| 671 |  |  |  | 
| 672 |  |  | odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); | 
| 673 |  |  | odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); | 
| 674 |  |  |  | 
| 675 |  |  | inpresflags |= DFL(DC_SETVIEW); | 
| 676 |  |  | } |