--- ray/src/hd/rhd_glx.c 1997/12/23 18:31:00 3.1 +++ ray/src/hd/rhd_glx.c 2003/02/22 02:07:24 3.29 @@ -1,51 +1,58 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhd_glx.c,v 3.29 2003/02/22 02:07:24 greg Exp $"; #endif - /* * OpenGL GLX driver for holodeck display. - * Based on rview driver. + * Based on old GLX driver using cones. + * + * Define symbol STEREO for stereo viewing. + * Define symbol DOBJ for display object viewing. */ +#ifdef NOSTEREO +#ifdef STEREO +#undef STEREO +#else +#undef NOSTEREO +#endif +#endif + #include "standard.h" -#include "tonemap.h" -#include "rhdriver.h" -#include - -#include "x11icon.h" - -#ifndef FEQ -#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) +#include +#include +#ifdef STEREO +#include #endif -#ifndef int4 -#define int4 int +#include "rhd_sample.h" +#ifdef DOBJ +#include "rhdobj.h" #endif -#ifndef FREEPCT -#define FREEPCT 25 /* percentage of values to free */ +#include "x11icon.h" + +#ifndef RAYQLEN +#define RAYQLEN 1024 /* max. rays to queue before flush */ #endif -#ifndef NCONEV -#define NCONEV 7 /* number of cone base vertices */ +#ifndef FEQ +#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) #endif -#ifndef CONEH -#define CONEH 0.25 /* cone height (fraction of depth) */ -#endif -#ifndef CONEW -#define CONEW 0.035 /* cone width (fraction of screen) */ -#endif -#define GAMMA 2.2 /* default gamma correction */ +#define GAMMA 1.4 /* default gamma correction */ +#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) + #define MOVPCT 7 /* percent distance to move /frame */ #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) #define MOVDEG (-5) /* degrees to orbit CW/down /frame */ #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) +#ifndef TARGETFPS +#define TARGETFPS 2.0 /* target frames/sec during motion */ +#endif + #define MINWIDTH 480 /* minimum graphics window width */ #define MINHEIGHT 400 /* minimum graphics window height */ @@ -53,6 +60,10 @@ static char SCCSid[] = "$SunId$ SGI"; #define BORWIDTH 5 /* border width */ +#define setstereobuf(bid) (glXWaitGL(), \ + XSGISetStereoBuffer(ourdisplay, gwind, bid), \ + glXWaitX()) + #define ourscreen DefaultScreen(ourdisplay) #define ourroot RootWindow(ourdisplay,ourscreen) #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\ @@ -62,10 +73,17 @@ static char SCCSid[] = "$SunId$ SGI"; struct driver odev; /* global device driver structure */ +char odev_args[64]; /* command arguments */ + +#ifdef STEREO +static VIEW vwright; /* right eye view */ +#endif + +static int rayqleft = 0; /* rays left to queue before flush */ + static XEvent currentevent; /* current event */ static int mapped = 0; /* window is mapped? */ -static unsigned long *pixval = NULL; /* allocated pixels */ static unsigned long ourblack=0, ourwhite=~0; static Display *ourdisplay = NULL; /* our display */ @@ -73,74 +91,83 @@ static XVisualInfo *ourvinf; /* our visual informati static Window gwind = 0; /* our graphics window */ static GLXContext gctx; /* our GLX context */ -static double mindepth = FHUGE; /* minimum depth value so far */ -static double maxdepth = 0.; /* maximum depth value */ - static double pwidth, pheight; /* pixel dimensions (mm) */ -static FVECT conev[NCONEV]; /* drawing cone */ -static double coneh; /* cone height */ +static double mindpth, maxdpth; /* min. and max. depth */ +double dev_zmin, dev_zmax; /* fore and aft clipping plane dist. */ + static int inpresflags; /* input result flags */ static int headlocked = 0; /* lock vertical motion */ -static struct { - float (*wp)[3]; /* world intersection point array */ - int4 *wd; /* world direction array */ - TMbright *brt; /* encoded brightness array */ - BYTE (*chr)[3]; /* encoded chrominance array */ - BYTE (*rgb)[3]; /* tone-mapped color array */ - BYTE *alpha; /* alpha values */ - int nl; /* count of values */ - int bl, tl; /* bottom and top (next) value index */ - int tml; /* next value needing tone-mapping */ - int drl; /* next value in need of drawing */ - char *base; /* base of allocated memory */ -} rV; /* our collection of values */ +static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), + setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); -#define redraw() (rV.drl = rV.bl) +#ifdef STEREO +static int pushright(), popright(); +#endif +static double getdistance(); +#define mapdepth(d) ((d)> 0.9995 ? FHUGE: dev_zmin/ \ + (1.-(d)*(1.-dev_zmin/dev_zmax))) -static int resizewindow(), getevent(), getkey(), moveview(), - setGLview(), getmove(), fixwindow(), mytmflags(), - newvalue(), drawvalue(), Compost(), - FindValue(), AllocValues(), TMapValues(), FreeValues(); +extern time_t time(); -dev_open(id) /* initialize X11 driver */ +dev_open(id) /* initialize GLX driver */ char *id; { extern char *getenv(); + static RGBPRIMS myprims = STDPRIMS; static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, - GLX_ALPHA_SIZE,8, GLX_DEPTH_SIZE,23, - /*GLX_DOUBLEBUFFER,*/ None}; - static int atlOK[] = {GLX_RGBA, GLX_RED_SIZE,4, - GLX_GREEN_SIZE,4, GLX_BLUE_SIZE,4, - GLX_ALPHA_SIZE,4, GLX_DEPTH_SIZE,15, - /*GLX_DOUBLEBUFFER,*/ None}; - char *gv; + GLX_DEPTH_SIZE,15, None}; + char *ev; double gamval = GAMMA; + RGBPRIMP dpri = stdprims; XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; XSizeHints oursizhints; + /* check for unsupported stereo */ +#ifdef NOSTEREO + error(INTERNAL, "stereo display driver unavailable"); +#endif /* open display server */ ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) error(USER, "cannot open X-windows; DISPLAY variable set?\n"); +#ifdef STEREO + switch (XSGIQueryStereoMode(ourdisplay, ourroot)) { + case STEREO_TOP: + case STEREO_BOTTOM: + break; + case STEREO_OFF: + error(USER, + "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); + case X_STEREO_UNSUPPORTED: + error(USER, "stereo mode not supported on this screen"); + default: + error(INTERNAL, "unknown stereo mode"); + } +#endif /* find a usable visual */ ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); if (ourvinf == NULL) - ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); - if (ourvinf == NULL) error(USER, "no suitable visuals available"); /* get a context */ gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); /* set gamma and tone mapping */ - if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL - || (gv = getenv("DISPLAY_GAMMA")) != NULL) - gamval = atof(gv); - if (tmInit(mytmflags(), stdprims, gamval) == NULL) + if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL + || (ev = getenv("DISPLAY_GAMMA")) != NULL) + gamval = atof(ev); + if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL && + sscanf(ev, "%f %f %f %f %f %f %f %f", + &myprims[RED][CIEX],&myprims[RED][CIEY], + &myprims[GRN][CIEX],&myprims[GRN][CIEY], + &myprims[BLU][CIEX],&myprims[BLU][CIEY], + &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) + dpri = myprims; + if (tmInit(mytmflags(), dpri, gamval) == NULL) error(SYSTEM, "not enough memory in dev_open"); /* open window */ ourwinattr.background_pixel = ourblack; @@ -151,7 +178,11 @@ char *id; ourvinf->visual, AllocNone); gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, +#ifdef STEREO + (DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2, +#else DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, +#endif BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual, CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr); if (gwind == 0) @@ -164,42 +195,55 @@ char *id; gwind, x11icon_bits, x11icon_width, x11icon_height); XSetWMHints(ourdisplay, gwind, &ourxwmhints); oursizhints.min_width = MINWIDTH; +#ifdef STEREO + oursizhints.min_height = MINHEIGHT/2; + oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH; + oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) - + 2*BORWIDTH)/2; + oursizhints.flags = PMinSize|PMaxSize; +#else oursizhints.min_height = MINHEIGHT; oursizhints.flags = PMinSize; +#endif XSetNormalHints(ourdisplay, gwind, &oursizhints); - /* map the window and get its size */ - XMapWindow(ourdisplay, gwind); - dev_input(); - /* allocate our value list */ - if (!AllocValues(DisplayWidth(ourdisplay,ourscreen) * - DisplayHeight(ourdisplay,ourscreen) / 4)) - error(SYSTEM, "insufficient memory for value storage"); + /* set GLX context */ + glXMakeCurrent(ourdisplay, gwind, gctx); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glShadeModel(GL_SMOOTH); + glDisable(GL_DITHER); + glDisable(GL_CULL_FACE); /* figure out sensible view */ pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / DisplayWidth(ourdisplay, ourscreen); pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / DisplayHeight(ourdisplay, ourscreen); +#ifdef STEREO + pheight *= 2.; + setstereobuf(STEREO_BUFFER_LEFT); +#endif + checkglerr("setting rendering parameters"); copystruct(&odev.v, &stdview); odev.v.type = VT_PER; - odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); - odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); + /* map the window */ + XMapWindow(ourdisplay, gwind); + dev_input(); /* sets size and view angles */ + /* allocate our samples */ + if (!smInit(DisplayWidth(ourdisplay,ourscreen) * + DisplayHeight(ourdisplay,ourscreen) / 10)) + error(SYSTEM, "insufficient memory for value storage"); + mindpth = FHUGE; maxdpth = FTINY; odev.name = id; odev.ifd = ConnectionNumber(ourdisplay); - /* set GLX context and clear buffers */ - glXMakeCurrent(ourdisplay, gwind, gctx); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - glShadeModel(GL_FLAT); - glDisable(GL_DITHER); - glDrawBuffer(GL_FRONT_AND_BACK); - glReadBuffer(GL_BACK); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - setGLview(); /* initialize view */ } dev_close() /* close our display and free resources */ { + smInit(0); +#ifdef DOBJ + dobj_cleanup(); +#endif glXMakeCurrent(ourdisplay, None, NULL); glXDestroyContext(ourdisplay, gctx); XDestroyWindow(ourdisplay, gwind); @@ -207,33 +251,46 @@ dev_close() /* close our display and free resources XCloseDisplay(ourdisplay); ourdisplay = NULL; tmDone(NULL); - FreeValues(); odev.v.type = 0; odev.hres = odev.vres = 0; odev.ifd = -1; } +dev_clear() /* clear our representation */ +{ + smInit(rsL.max_samp); + wipeclean(1); + rayqleft = 0; /* hold off update */ +} + + int dev_view(nv) /* assign new driver view */ register VIEW *nv; { + double d; + if (nv->type != VT_PER || /* check view legality */ - nv->horiz > 120. || nv->vert > 120.) { + nv->horiz > 160. || nv->vert > 160.) { error(COMMAND, "illegal view type/angle"); - nv->type = VT_PER; + nv->type = odev.v.type; nv->horiz = odev.v.horiz; nv->vert = odev.v.vert; return(0); } if (nv != &odev.v) { - if (!FEQ(nv->horiz,odev.v.horiz) || /* resize window? */ + /* resize window? */ + if (!FEQ(nv->horiz,odev.v.horiz) || !FEQ(nv->vert,odev.v.vert)) { int dw = DisplayWidth(ourdisplay,ourscreen); int dh = DisplayHeight(ourdisplay,ourscreen); dw -= 25; /* for window frame */ dh -= 50; +#ifdef STEREO + dh /= 2; +#endif odev.hres = 2.*VIEWDIST/pwidth * tan(PI/180./2.*nv->horiz); odev.vres = 2.*VIEWDIST/pheight * @@ -247,16 +304,56 @@ register VIEW *nv; odev.vres = dh; } XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); + dev_input(); /* get resize event */ } - copystruct(&odev.v, nv); - setGLview(); + copystruct(&odev.v, nv); /* setview() already called */ } - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - redraw(); +#ifdef STEREO + copystruct(&vwright, nv); + d = eyesepdist / sqrt(nv->hn2); + VSUM(vwright.vp, nv->vp, nv->hvec, d); + /* setview(&vwright); -- Unnecessary */ +#endif + wipeclean(0); return(1); } +dev_section(gfn, pfn) /* add octree for geometry rendering */ +char *gfn, *pfn; +{ + /* unimplemented */ +} + + +dev_auxcom(cmd, args) /* process an auxiliary command */ +char *cmd, *args; +{ +#ifdef DOBJ + if (dobj_command(cmd, args) >= 0) + return; +#endif + sprintf(errmsg, "%s: unknown command", cmd); + error(COMMAND, errmsg); +} + + +VIEW * +dev_auxview(n, hvres) /* return nth auxiliary view */ +int n; +int hvres[2]; +{ + hvres[0] = odev.hres; hvres[1] = odev.vres; + if (n == 0) + return(&odev.v); +#ifdef STEREO + if (n == 1) + return(&vwright); +#endif + return(NULL); +} + + int dev_input() /* get X11 input */ { @@ -265,22 +362,39 @@ dev_input() /* get X11 input */ do getevent(); - while (XQLength(ourdisplay) > 0); + while (XPending(ourdisplay) > 0); + odev.inpready = 0; + return(inpresflags); } -dev_value(c, p, v) /* add a pixel value to our list */ +dev_value(c, d, p) /* add a pixel value to our mesh */ COLR c; -FVECT p, v; +FVECT d, p; { - register int li; - - li = newvalue(); - VCOPY(rV.wp[li], p); - rV.wd[li] = encodedir(v); - tmCvColrs(&rV.brt[li], rV.chr[li], c, 1); + double depth; +#ifdef DOBJ + if (dobj_lightsamp != NULL) { /* in light source sampling */ + (*dobj_lightsamp)(c, d, p); + return; + } +#endif + if (p != NULL) { /* add depth to our range */ + depth = (p[0] - odev.v.vp[0])*d[0] + + (p[1] - odev.v.vp[1])*d[1] + + (p[2] - odev.v.vp[2])*d[2]; + if (depth > FTINY) { + if (depth < mindpth) + mindpth = depth; + if (depth > maxdpth) + maxdpth = depth; + } + } + smNewSamp(c, d, p); /* add to display representation */ + if (!--rayqleft) + dev_flush(); /* flush output */ } @@ -288,279 +402,80 @@ int dev_flush() /* flush output */ { if (mapped) { - TMapValues(0); - while (rV.drl != rV.tl) { - drawvalue(rV.drl); - if (++rV.drl >= rV.nl) - rV.drl = 0; - } - glFlush(); +#ifdef STEREO + pushright(); /* update right eye */ + smUpdate(&vwright, MAXQUALITY); +#ifdef DOBJ + dobj_render(); /* usually in foreground */ +#endif + popright(); /* update left eye */ +#endif + smUpdate(&odev.v, MAXQUALITY); + checkglerr("rendering mesh"); +#ifdef DOBJ + dobj_render(); +#endif + glFlush(); /* flush OGL */ } - return(XPending(ourdisplay)); + rayqleft = RAYQLEN; + /* flush X11 and return # pending */ + return(odev.inpready = XPending(ourdisplay)); } -static int -mytmflags() /* figure out tone mapping flags */ +checkglerr(where) /* check for GL or GLU error */ +char *where; { - extern char *progname; - register char *cp, *tail; - /* find basic name */ - for (cp = tail = progname; *cp; cp++) - if (*cp == '/') - tail = cp+1; - for (cp = tail; *cp && *cp != '.'; cp++) - ; - if (cp-tail == 3 && !strncmp(tail, "glx", 3)) - return(TM_F_CAMERA); - if (cp-tail == 4 && !strncmp(tail, "glxh", 4)) - return(TM_F_HUMAN); - error(USER, "illegal driver name"); -} + register GLenum errcode; - -static -setGLview() /* set our GL view */ -{ - double xmin, xmax, ymin, ymax, zmin, zmax; - double d, cx, sx, crad; - FVECT vx, vy; - register int i, j; - /* compute view frustum */ - if (normalize(odev.v.vdir) == 0.0) - return; - if (mindepth < maxdepth) { - zmin = 0.25*mindepth; - zmax = 8.0*maxdepth; - } else { - zmin = 0.01; - zmax = 1000.; + while ((errcode = glGetError()) != GL_NO_ERROR) { + sprintf(errmsg, "OpenGL error %s: %s", + where, gluErrorString(errcode)); + error(WARNING, errmsg); } - if (odev.v.vfore > FTINY) - zmin = odev.v.vfore; - if (odev.v.vaft > FTINY) - zmax = odev.v.vaft; - xmax = zmin * tan(PI/180./2. * odev.v.horiz); - xmin = -xmax; - d = odev.v.hoff * (xmax - xmin); - xmin += d; xmax += d; - ymax = zmin * tan(PI/180./2. * odev.v.vert); - ymin = -ymax; - d = odev.v.voff * (ymax - ymin); - ymin += d; ymax += d; - /* set view matrix */ - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glFrustum(xmin, xmax, ymin, ymax, zmin, zmax); - gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], - odev.v.vp[0] + odev.v.vdir[0], - odev.v.vp[1] + odev.v.vdir[1], - odev.v.vp[2] + odev.v.vdir[2], - odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); - /* set viewport */ - glViewport(0, 0, odev.hres, odev.vres); - /* initialize cone for Vornoi polys */ - coneh = CONEH*(zmax - zmin); - crad = 0.5 * CONEW * 0.5*(xmax-xmin + ymax-ymin) * (zmin+coneh)/zmin; - vy[0] = vy[1] = vy[2] = 0.; - for (i = 0; i < 3; i++) - if (odev.v.vdir[i] < 0.6 && odev.v.vdir[i] > -0.6) - break; - vy[i] = 1.; - fcross(vx, vy, odev.v.vdir); - normalize(vx); - fcross(vy, odev.v.vdir, vx); - for (j = 0, d = 0.; j < NCONEV; j++, d += 2.*PI/NCONEV) { - cx = crad*cos(d); sx = crad*sin(d); - for (i = 0; i < 3; i++) - conev[j][i] = coneh*odev.v.vdir[i] + - cx*vx[i] + sx*vy[i]; - } } -static int -newvalue() /* allocate a leaf from our pile */ -{ - int li; - - li = rV.tl++; - if (rV.tl >= rV.nl) /* get next leaf in ring */ - rV.tl = 0; - if (rV.tl == rV.bl) /* need to shake some free */ - Compost(FREEPCT); - return(li); -} - - +#ifdef STEREO static -drawvalue(li) /* draw a pixel value as a cone */ -register int li; +pushright() /* push on right view */ { - static FVECT apex, disp; - double d, h, v; - register int i; - /* compute cone's base displacement */ - VCOPY(apex, rV.wp[li]); - disp[0] = apex[0] - odev.v.vp[0]; - disp[1] = apex[1] - odev.v.vp[1]; - disp[2] = apex[2] - odev.v.vp[2]; - if ((d = DOT(disp,odev.v.vdir)) <= odev.v.vfore || d <= FTINY) - return; - if (d > 1e5) { /* background region */ - if (odev.v.vaft > FTINY) - return; - h = maxdepth/d; - disp[0] *= h; disp[1] *= h; disp[2] *= h; - apex[0] = odev.v.vp[0] + disp[0]; - apex[1] = odev.v.vp[1] + disp[1]; - apex[2] = odev.v.vp[2] + disp[2]; - d = maxdepth; - } else if (d > maxdepth) - maxdepth = d; - h = DOT(disp,odev.v.hvec)/(d*odev.v.hn2); - v = DOT(disp,odev.v.vvec)/(d*odev.v.vn2); - if (fabs(h - odev.v.hoff) < 0.5 && fabs(v - odev.v.voff) < 0.5 && - d < mindepth) -{ -fprintf(stderr, "min depth now %lf (%lf,%lf,%lf)\n", d, apex[0], apex[1], apex[2]); - mindepth = d; -} - for (i = 0; i < 3; i++) - disp[i] = apex[i] + coneh*(h*odev.v.hvec[i] + v*odev.v.vvec[i]); - /* draw pyramid (cone approx.) */ - glColor4ub(rV.rgb[li][0], rV.rgb[li][1], rV.rgb[li][2], rV.alpha[li]); - glBegin(GL_TRIANGLE_FAN); - glVertex3f(apex[0], apex[1], apex[2]); - for (i = 0; i < NCONEV; i++) - glVertex3d(conev[i][0] + disp[0], conev[i][1] + disp[1], - conev[i][2] + disp[2]); - /* connect last side to first */ - glVertex3d(conev[0][0] + disp[0], conev[0][1] + disp[1], - conev[0][2] + disp[2]); - glEnd(); /* done */ -} + double d; - -#define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\ - sizeof(TMbright)+7*sizeof(BYTE)) - -static -AllocValues(n) /* allocate space for n values */ -register int n; -{ - unsigned nbytes; - register unsigned i; - - if (n <= 0) - return(0); - if (rV.nl >= n) - return(rV.nl); - else if (rV.nl > 0) - free(rV.base); - /* round space up to nearest power of 2 */ - nbytes = n*LEAFSIZ + 8; - for (i = 1024; nbytes > i; i <<= 1) - ; - n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */ - rV.base = (char *)malloc(n*LEAFSIZ); - if (rV.base == NULL) - return(0); - /* assign larger alignment types earlier */ - rV.wp = (float (*)[3])rV.base; - rV.wd = (int4 *)(rV.wp + n); - rV.brt = (TMbright *)(rV.wd + n); - rV.chr = (BYTE (*)[3])(rV.brt + n); - rV.rgb = (BYTE (*)[3])(rV.chr + n); - rV.alpha = (BYTE *)(rV.rgb + n); - rV.nl = n; - rV.drl = rV.tml = rV.bl = rV.tl = 0; - return(n); + setstereobuf(STEREO_BUFFER_RIGHT); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + d = -eyesepdist / sqrt(odev.v.hn2); + glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], d*odev.v.hvec[2]); + checkglerr("setting right view"); } -#undef LEAFSIZ - static -FreeValues() /* free our allocated values */ +popright() /* pop off right view */ { - if (rV.nl <= 0) - return; - free(rV.base); - rV.base = NULL; - rV.nl = 0; + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + setstereobuf(STEREO_BUFFER_LEFT); } +#endif -static -TMapValues(redo) /* map our values to RGB */ -int redo; -{ - int aorg, alen, borg, blen; - /* recompute mapping? */ - if (redo) - rV.tml = rV.bl; - /* already done? */ - if (rV.tml == rV.tl) - return(1); - /* compute segments */ - aorg = rV.tml; - if (rV.tl >= aorg) { - alen = rV.tl - aorg; - blen = 0; - } else { - alen = rV.nl - aorg; - borg = 0; - blen = rV.tl; - } - /* (re)compute tone mapping? */ - if (rV.tml == rV.bl) { - tmClearHisto(); - tmAddHisto(rV.brt+aorg, alen, 1); - if (blen > 0) - tmAddHisto(rV.brt+borg, blen, 1); - if (tmComputeMapping(0., 0., 0.) != TM_E_OK) - return(0); - rV.drl = rV.bl; - } - if (tmMapPixels(rV.rgb+aorg, rV.brt+aorg, - rV.chr+aorg, alen) != TM_E_OK) - return(0); - if (blen > 0) - tmMapPixels(rV.rgb+borg, rV.brt+borg, - rV.chr+borg, blen); - /* compute unique alpha values */ - rV.tml = rV.tl; - return(1); -} - - static int -Compost(pct) /* free up some values */ -int pct; +mytmflags() /* figure out tone mapping flags */ { - int nused, nclear, nmapped, ndrawn; - /* figure out how many values to clear */ - nclear = rV.nl * pct / 100; - nused = rV.tl - rV.bl; - if (nused <= 0) nused += rV.nl; - nclear -= rV.nl - nused; - if (nclear <= 0) - return(0); - if (nclear >= nused) { /* clear them all */ - rV.drl = rV.tml = rV.bl = rV.tl = 0; - return(nused); - } - /* else clear values from bottom */ - ndrawn = rV.drl - rV.bl; - if (ndrawn < 0) ndrawn += rV.nl; - nmapped = rV.tml - rV.bl; - if (nmapped < 0) nmapped += rV.nl; - rV.bl += nclear; - if (rV.bl >= rV.nl) rV.bl -= rV.nl; - if (ndrawn < nclear) rV.drl = rV.bl; - if (nmapped < nclear) rV.tml = rV.bl; - return(nclear); + extern char *progname; + register char *cp, *tail; + /* find basic name */ + for (cp = tail = progname; *cp; cp++) + if (*cp == '/') + tail = cp+1; + for (cp = tail; *cp && *cp != '.'; cp++) + ; + if (cp > tail && cp[-1] == 'h') + return(TM_F_HUMAN|TM_F_NOSTDERR); + else + return(TM_F_CAMERA|TM_F_NOSTDERR); } @@ -585,7 +500,10 @@ getevent() /* get next event */ getkey(levptr(XKeyPressedEvent)); break; case ButtonPress: - getmove(levptr(XButtonPressedEvent)); + if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) + getframe(levptr(XButtonPressedEvent)); + else + getmove(levptr(XButtonPressedEvent)); break; } } @@ -601,25 +519,34 @@ register FVECT wp[2]; static -draw_grids() /* draw holodeck section grids */ +draw_grids(fore) /* draw holodeck section grids */ +int fore; { - static GLubyte gridrgba[4] = {0x0, 0xff, 0xff, 0x00}; - - if (!mapped) - return; - glColor4ub(gridrgba[0], gridrgba[1], gridrgba[2], gridrgba[3]); - /* draw each grid line */ - glBegin(GL_LINES); + if (fore) + glColor4ub(0, 255, 255, 0); + else + glColor4ub(0, 0, 0, 0); + glBegin(GL_LINES); /* draw each grid line */ gridlines(draw3dline); glEnd(); + checkglerr("drawing grid lines"); } - -static int -FindValue(dx, dy) /* find a value on the display */ +static double +getdistance(dx, dy, direc) /* distance from fore plane along view ray */ int dx, dy; +FVECT direc; { - return(-1); /* not found */ + GLfloat gldepth; + double dist; + + if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) + return(FHUGE); + glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT,GL_FLOAT, &gldepth); + dist = mapdepth(gldepth); + if (dist >= .99*FHUGE) + return(FHUGE); + return((dist-odev.v.vfore)/DOT(direc,odev.v.vdir)); } @@ -628,31 +555,47 @@ moveview(dx, dy, mov, orb) /* move our view */ int dx, dy, mov, orb; { VIEW nv; - FVECT odir, v1; - double d; + FVECT odir, v1, wip; + double d,d1; register int li; /* start with old view */ copystruct(&nv, &odev.v); - /* change view direction */ - if (mov | orb) { - if ((li = FindValue(dx, dy)) < 0) - return(0); /* not on window */ - VSUM(odir, rV.wp[li], nv.vp, -1.); - } else { - if (viewray(nv.vp, nv.vdir, &odev.v, - (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) - return(0); /* outside view */ - } + /* orient our motion */ + if (viewray(v1, odir, &odev.v, + (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) + return(0); /* outside view */ + if (mov | orb) { /* moving relative to geometry */ + d = getdistance(dx, dy, odir);/*distance from front plane */ +#ifdef DOBJ + d1 = dobj_trace(NULL, v1, odir); /* check objects */ + /* check holodeck */ + if (d1 < d) + d = d1; +#endif + if (d >= .99*FHUGE) + d = 0.5*(dev_zmax+dev_zmin); /* just guess */ + VSUM(wip, v1, odir, d); + VSUB(odir, wip, odev.v.vp); +#if 0 + fprintf(stderr, "moveview: hit %s at (%f,%f,%f) (t=%f)\n", + li < 0 ? "object" : "mesh", + wip[0], wip[1], wip[2], + (wip[0]-odev.v.vp[0])*odir[0] + + (wip[1]-odev.v.vp[1])*odir[1] + + (wip[2]-odev.v.vp[2])*odir[2]); +#endif + } else /* panning with constant viewpoint */ + VCOPY(nv.vdir, odir); if (orb && mov) { /* orbit left/right */ spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); - VSUM(nv.vp, rV.wp[li], odir, -1.); + VSUM(nv.vp, wip, odir, -1.); spinvector(nv.vdir, nv.vdir, nv.vup, d); } else if (orb) { /* orbit up/down */ fcross(v1, odir, nv.vup); if (normalize(v1) == 0.) return(0); spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); - VSUM(nv.vp, rV.wp[li], odir, -1.); + VSUM(nv.vp, wip, odir, -1.); spinvector(nv.vdir, nv.vdir, v1, d); } else if (mov) { /* move forward/backward */ d = MOVPCT/100. * mov; @@ -660,7 +603,7 @@ int dx, dy, mov, orb; } if (!mov ^ !orb && headlocked) { /* restore head height */ VSUM(v1, odev.v.vp, nv.vp, -1.); - d = DOT(v1, odev.v.vup); + d = DOT(v1, nv.vup); VSUM(nv.vp, nv.vp, odev.v.vup, d); } if (setview(&nv) != NULL) @@ -672,30 +615,98 @@ int dx, dy, mov, orb; static +getframe(ebut) /* get focus frame */ +XButtonPressedEvent *ebut; +{ + int startx = ebut->x, starty = ebut->y; + int endx, endy; + + XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); + endx = levptr(XButtonReleasedEvent)->x; + endy = levptr(XButtonReleasedEvent)->y; + if (endx == startx | endy == starty) { + XBell(ourdisplay, 0); + return; + } + if (endx < startx) {register int c = endx; endx = startx; startx = c;} + if (endy < starty) {register int c = endy; endy = starty; starty = c;} + sprintf(odev_args, "%.3f %.3f %.3f %.3f", + (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, + (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); + inpresflags |= DFL(DC_FOCUS); +} + + +static getmove(ebut) /* get view change */ XButtonPressedEvent *ebut; { int movdir = MOVDIR(ebut->button); int movorb = MOVORB(ebut->state); + int qlevel = MAXQUALITY-1; + time_t lasttime, thistime; + int nframes; Window rootw, childw; int rootx, rooty, wx, wy; unsigned int statemask; - XNoOp(ourdisplay); + XNoOp(ourdisplay); /* makes sure we're not idle */ + nframes = 0; while (!XCheckMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent))) { - + /* get cursor position */ if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, &rootx, &rooty, &wx, &wy, &statemask)) break; /* on another screen */ + draw_grids(0); /* clear old grid lines */ +#ifdef STEREO + pushright(); draw_grids(0); popright(); +#endif + /* compute view motion */ if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { sleep(1); + lasttime++; + continue; /* cursor in bad place */ + } + draw_grids(1); /* redraw grid */ +#ifdef STEREO + pushright(); + draw_grids(1); + smUpdate(&vwright, qlevel); +#ifdef DOBJ + dobj_render(); +#endif + popright(); +#endif + /* redraw mesh */ + smUpdate(&odev.v, qlevel); +#ifdef DOBJ + dobj_render(); /* redraw object */ +#endif + glFlush(); + /* figure out good quality level */ + if (!nframes++) { /* ignore first frame */ + lasttime = time(0); continue; } - draw_grids(); - dev_flush(); + thistime = time(0); + if (thistime - lasttime >= 6 || + nframes > (int)(3*3*TARGETFPS)) { + qlevel = thistime<=lasttime ? 3*MAXQUALITY : + (int)((double)nframes/(thistime-lasttime) + / TARGETFPS * qlevel + 0.5); + nframes = 0; + if (qlevel >= MAXQUALITY) { + if (qlevel >= 3*MAXQUALITY) { /* brakes!! */ + sleep(1); + lasttime++; + } + qlevel = MAXQUALITY-1; + } else if (qlevel < 1) + qlevel = 1; + } } if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); @@ -703,14 +714,72 @@ XButtonPressedEvent *ebut; wy = levptr(XButtonReleasedEvent)->y; moveview(wx, odev.vres-1-wy, movdir, movorb); } - dev_flush(); } static +setglpersp(vp) /* set perspective view in GL */ +register VIEW *vp; +{ + double d, xmin, xmax, ymin, ymax; + + if (mindpth >= maxdpth) { + dev_zmin = 1.; + dev_zmax = 100.; + } else { + dev_zmin = 0.1*mindpth; + dev_zmax = 5.0*maxdpth; + } + if (odev.v.vfore > FTINY) + dev_zmin = odev.v.vfore; + if (odev.v.vaft > FTINY) + dev_zmax = odev.v.vaft; + if (dev_zmin*500. < dev_zmax) + dev_zmax = dev_zmin*500.; + xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz); + xmin = -xmax; + d = odev.v.hoff * (xmax - xmin); + xmin += d; xmax += d; + ymax = dev_zmin * tan(PI/180./2. * odev.v.vert); + ymin = -ymax; + d = odev.v.voff * (ymax - ymin); + ymin += d; ymax += d; + /* set view matrix */ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax); + gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], + odev.v.vp[0] + odev.v.vdir[0], + odev.v.vp[1] + odev.v.vdir[1], + odev.v.vp[2] + odev.v.vdir[2], + odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); + checkglerr("setting perspective view"); +} + + +static +wipeclean(tmflag) /* prepare for redraw */ +int tmflag; +{ + /* clear depth buffer */ +#ifdef STEREO + setstereobuf(STEREO_BUFFER_RIGHT); + glClear(GL_DEPTH_BUFFER_BIT); + setstereobuf(STEREO_BUFFER_LEFT); +#endif + glClear(GL_DEPTH_BUFFER_BIT); + smClean(tmflag); /* reset drawing routines */ + setglpersp(&odev.v); /* reset view & clipping planes */ +} + + +static getkey(ekey) /* get input key */ register XKeyPressedEvent *ekey; { + Window rootw, childw; + int rootx, rooty, wx, wy; + unsigned int statemask; int n; char buf[8]; @@ -733,26 +802,43 @@ register XKeyPressedEvent *ekey; case 'v': /* spit out view */ inpresflags |= DFL(DC_GETVIEW); return; + case 'f': /* frame view position */ + if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, + &rootx, &rooty, &wx, &wy, &statemask)) + return; /* on another screen */ + sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, + 1.-(wy+.5)/odev.vres); + inpresflags |= DFL(DC_FOCUS); + return; + case 'F': /* unfocus */ + odev_args[0] = '\0'; + inpresflags |= DFL(DC_FOCUS); + return; case '\n': case '\r': /* resume computation */ inpresflags |= DFL(DC_RESUME); return; case CTRL('R'): /* redraw screen */ - TMapValues(1); - redraw(); + wipeclean(1); return; case CTRL('L'): /* refresh from server */ if (inpresflags & DFL(DC_REDRAW)) return; + setglpersp(&odev.v); /* reset clipping planes */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDrawBuffer(GL_FRONT); - glDisable(GL_DEPTH_TEST); - draw_grids(); + glDisable(GL_DEPTH_TEST); /* so grids will clear */ + draw_grids(1); +#ifdef STEREO + pushright(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + draw_grids(1); + popright(); +#endif glEnable(GL_DEPTH_TEST); - glDrawBuffer(GL_FRONT_AND_BACK); glFlush(); - Compost(100); /* get rid of old values */ + smInit(rsL.max_samp); /* get rid of old values */ inpresflags |= DFL(DC_REDRAW); /* resend values from server */ + rayqleft = 0; /* hold off update */ return; case 'K': /* kill rtrace process(es) */ inpresflags |= DFL(DC_KILL); @@ -777,14 +863,13 @@ static fixwindow(eexp) /* repair damage to window */ register XExposeEvent *eexp; { - if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ - odev.hres = eexp->width; - odev.vres = eexp->height; + if (odev.hres == 0 | odev.vres == 0) { /* first exposure */ + resizewindow((XConfigureEvent *)eexp); + return; } - if (eexp->width == odev.hres && eexp->height == odev.vres) - TMapValues(1); - if (!eexp->count) - redraw(); + if (eexp->count) /* wait for final exposure */ + return; + wipeclean(0); /* clear depth */ } @@ -792,6 +877,8 @@ static resizewindow(ersz) /* resize window */ register XConfigureEvent *ersz; { + glViewport(0, 0, ersz->width, ersz->height); + if (ersz->width == odev.hres && ersz->height == odev.vres) return; @@ -800,8 +887,6 @@ register XConfigureEvent *ersz; odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); - - setGLview(); inpresflags |= DFL(DC_SETVIEW); }