--- ray/src/hd/rhd_glx.c 1998/08/25 18:11:17 3.22 +++ ray/src/hd/rhd_glx.c 1999/06/10 15:22:21 3.28 @@ -1,4 +1,4 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ +/* Copyright (c) 1999 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -21,14 +21,14 @@ static char SCCSid[] = "$SunId$ SGI"; #endif #include "standard.h" -#include "rhd_sample.h" -#include #include #include #ifdef STEREO #include #endif + +#include "rhd_sample.h" #ifdef DOBJ #include "rhdobj.h" #endif @@ -36,7 +36,7 @@ static char SCCSid[] = "$SunId$ SGI"; #include "x11icon.h" #ifndef RAYQLEN -#define RAYQLEN 250 /* max. rays to queue before flush */ +#define RAYQLEN 1024 /* max. rays to queue before flush */ #endif #ifndef FEQ @@ -45,13 +45,15 @@ static char SCCSid[] = "$SunId$ SGI"; #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 4.0 /* target frames/sec during motion */ +#define TARGETFPS 2.0 /* target frames/sec during motion */ #endif #define MINWIDTH 480 /* minimum graphics window width */ @@ -74,7 +76,11 @@ 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 */ @@ -99,11 +105,14 @@ static int inpresflags; /* input result flags */ static int headlocked = 0; /* lock vertical motion */ static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), - setglpersp(), getmove(), fixwindow(), mytmflags(); + setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); #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))) extern time_t time(); @@ -216,6 +225,7 @@ char *id; pheight *= 2.; setstereobuf(STEREO_BUFFER_LEFT); #endif + checkglerr("setting rendering parameters"); copystruct(&odev.v, &stdview); odev.v.type = VT_PER; /* map the window */ @@ -253,7 +263,7 @@ dev_close() /* close our display and free resources dev_clear() /* clear our representation */ { smInit(rsL.max_samp); - wipeclean(); + wipeclean(1); rayqleft = 0; /* hold off update */ } @@ -300,18 +310,25 @@ register VIEW *nv; dev_input(); /* get resize event */ } copystruct(&odev.v, nv); /* setview() already called */ + } #ifdef STEREO - copystruct(&vwright, nv); - d = eyesepdist / sqrt(nv->hn2); - VSUM(vwright.vp, nv->vp, nv->hvec, d); - /* setview(&vwright); -- Unnecessary */ + copystruct(&vwright, nv); + d = eyesepdist / sqrt(nv->hn2); + VSUM(vwright.vp, nv->vp, nv->hvec, d); + /* setview(&vwright); -- Unnecessary */ #endif - } - wipeclean(); + 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; { @@ -390,15 +407,13 @@ dev_flush() /* flush output */ if (mapped) { #ifdef STEREO pushright(); /* update right eye */ - glClear(GL_DEPTH_BUFFER_BIT); - smUpdate(&vwright, 100); + smUpdate(&vwright, MAXQUALITY); #ifdef DOBJ dobj_render(); /* usually in foreground */ #endif popright(); /* update left eye */ - glClear(GL_DEPTH_BUFFER_BIT); #endif - smUpdate(&odev.v, 100); + smUpdate(&odev.v, MAXQUALITY); checkglerr("rendering mesh"); #ifdef DOBJ dobj_render(); @@ -435,6 +450,7 @@ pushright() /* push on right view */ 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"); } @@ -487,7 +503,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; } } @@ -506,8 +525,6 @@ static draw_grids(fore) /* draw holodeck section grids */ int fore; { - if (!mapped) - return; if (fore) glColor4ub(0, 255, 255, 0); else @@ -515,16 +532,34 @@ int fore; glBegin(GL_LINES); /* draw each grid line */ gridlines(draw3dline); glEnd(); + checkglerr("drawing grid lines"); } +static double +getdistance(dx, dy, direc) /* distance from fore plane along view ray */ +int dx, dy; +FVECT direc; +{ + 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)); +} + + static moveview(dx, dy, mov, orb) /* move our view */ int dx, dy, mov, orb; { VIEW nv; FVECT odir, v1, wip; - double d; + double d,d1; register int li; /* start with old view */ copystruct(&nv, &odev.v); @@ -533,23 +568,25 @@ int dx, dy, mov, orb; (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 - d = dobj_trace(NULL, v1, odir); /* check objects */ + d1 = dobj_trace(NULL, v1, odir); /* check objects */ /* check holodeck */ - if ((li = smFindSamp(v1, odir)) >= 0) { - VCOPY(wip, rsL.wp[li]); - if (d < .99*FHUGE && d*d <= dist2(v1, wip)) - li = -1; /* object is closer */ - } else if (d >= .99*FHUGE) - return(0); /* nothing visible */ - if (li < 0) - VSUM(wip, v1, odir, d); /* else get object point */ -#else - if ((li = smFindSamp(v1, odir)) < 0) - return(0); /* not on window */ - VCOPY(wip, rsL.wp[li]); + if (d1 < d) + d = d1; #endif - VSUM(odir, wip, odev.v.vp, -1.); + 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 */ @@ -581,34 +618,62 @@ 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 = 99; + 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 */ - lasttime = time(0); nframes = 0; + 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; + continue; /* cursor in bad place */ } + draw_grids(1); /* redraw grid */ #ifdef STEREO pushright(); draw_grids(1); @@ -618,27 +683,30 @@ XButtonPressedEvent *ebut; #endif popright(); #endif - draw_grids(1); + /* redraw mesh */ smUpdate(&odev.v, qlevel); #ifdef DOBJ - dobj_render(); + dobj_render(); /* redraw object */ #endif glFlush(); - checkglerr("moving view"); - nframes++; + /* figure out good quality level */ + if (!nframes++) { /* ignore first frame */ + lasttime = time(0); + continue; + } thistime = time(0); - if (thistime - lasttime >= 3 || + if (thistime - lasttime >= 6 || nframes > (int)(3*3*TARGETFPS)) { - qlevel = thistime<=lasttime ? 1000 : + qlevel = thistime<=lasttime ? 3*MAXQUALITY : (int)((double)nframes/(thistime-lasttime) / TARGETFPS * qlevel + 0.5); - lasttime = thistime; nframes = 0; - if (qlevel > 99) { - if (qlevel > 300) { /* put on the brakes */ + nframes = 0; + if (qlevel >= MAXQUALITY) { + if (qlevel >= 3*MAXQUALITY) { /* brakes!! */ sleep(1); lasttime++; } - qlevel = 99; + qlevel = MAXQUALITY-1; } else if (qlevel < 1) qlevel = 1; } @@ -659,20 +727,18 @@ register VIEW *vp; double d, xmin, xmax, ymin, ymax; if (mindpth >= maxdpth) { - dev_zmin = 0.1; + dev_zmin = 1.; dev_zmax = 100.; } else { - dev_zmin = 0.5*mindpth; - dev_zmax = 1.5*maxdpth; - if (dev_zmin > dev_zmax/100.) - dev_zmin = dev_zmax/100.; + 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 < dev_zmax/5000.) - dev_zmin = dev_zmax/5000.; + if (dev_zmin < dev_zmax/500.) + dev_zmin = dev_zmax/500.; xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz); xmin = -xmax; d = odev.v.hoff * (xmax - xmin); @@ -690,11 +756,13 @@ register VIEW *vp; 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() /* prepare for redraw */ +wipeclean(tmflag) /* prepare for redraw */ +int tmflag; { /* clear depth buffer */ #ifdef STEREO @@ -703,7 +771,7 @@ wipeclean() /* prepare for redraw */ setstereobuf(STEREO_BUFFER_LEFT); #endif glClear(GL_DEPTH_BUFFER_BIT); - smClean(); /* reset drawing routines */ + smClean(tmflag); /* reset drawing routines */ setglpersp(&odev.v); /* reset view & clipping planes */ } @@ -712,6 +780,9 @@ 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]; @@ -734,18 +805,31 @@ 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 */ - wipeclean(); + 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); - glDisable(GL_DEPTH_TEST); + glDisable(GL_DEPTH_TEST); /* so grids will clear */ draw_grids(1); #ifdef STEREO pushright(); @@ -757,7 +841,6 @@ register XKeyPressedEvent *ekey; glFlush(); smInit(rsL.max_samp); /* get rid of old values */ inpresflags |= DFL(DC_REDRAW); /* resend values from server */ - setglpersp(&odev.v); /* reset clipping planes */ rayqleft = 0; /* hold off update */ return; case 'K': /* kill rtrace process(es) */ @@ -789,7 +872,7 @@ register XExposeEvent *eexp; } if (eexp->count) /* wait for final exposure */ return; - wipeclean(); /* clear depth */ + wipeclean(0); /* clear depth */ }