--- ray/src/hd/rhd_ogl.c 1998/12/21 15:31:49 3.5 +++ ray/src/hd/rhd_ogl.c 1999/01/05 18:16:17 3.15 @@ -1,4 +1,4 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ +/* Copyright (c) 1999 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -37,24 +37,30 @@ static char SCCSid[] = "$SunId$ SGI"; #include "x11icon.h" #ifndef RAYQLEN -#define RAYQLEN 10240 /* max. rays to queue before flush */ +#define RAYQLEN 0 /* max. rays to queue before flush */ #endif - -#ifndef PORTALP -#define PORTRED 2 /* portal red color */ -#define PORTGRN -1 /* portal green left alone */ -#define PORTBLU 128 /* portal blue color */ -#define PORTALP -1 /* don't use alpha channel */ +#ifndef MINWIDTH +#define MINWIDTH 480 /* minimum graphics window width */ +#define MINHEIGHT 400 /* minimum graphics window height */ #endif -#define isportal(c) ((PORTRED<0 || (c)[0]==PORTRED) && \ - (PORTGRN<0 || (c)[1]==PORTGRN) && \ - (PORTBLU<0 || (c)[2]==PORTBLU) && \ - (PORTALP<0 || (c)[3]==PORTALP)) +#ifndef VIEWDIST +#define VIEWDIST 356 /* assumed viewing distance (mm) */ +#endif +#ifndef BORWIDTH +#define BORWIDTH 5 /* border width */ +#endif #ifndef FEQ #define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) #endif +#define VWHEADLOCK 01 /* head position is locked flag */ +#define VWPERSP 02 /* perspective view is set */ +#define VWORTHO 04 /* orthographic view is set */ +#define VWCHANGE 010 /* view has changed */ +#define VWSTEADY 020 /* view is now steady */ +#define VWMAPPED 040 /* window is mapped */ + #define GAMMA 1.4 /* default gamma correction */ #define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) @@ -64,13 +70,6 @@ static char SCCSid[] = "$SunId$ SGI"; #define MOVDEG (-5) /* degrees to orbit CW/down /frame */ #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) -#define MINWIDTH 480 /* minimum graphics window width */ -#define MINHEIGHT 400 /* minimum graphics window height */ - -#define VIEWDIST 356 /* assumed viewing distance (mm) */ - -#define BORWIDTH 5 /* border width */ - #define setstereobuf(bid) (glXWaitGL(), \ XSGISetStereoBuffer(ourdisplay, gwind, bid), \ glXWaitX()) @@ -97,7 +96,6 @@ static int rayqleft = 0; /* rays left to queue before static XEvent currentevent; /* current event */ -static int mapped = 0; /* window is mapped? */ static unsigned long ourblack=0, ourwhite=~0; static Display *ourdisplay = NULL; /* our display */ @@ -115,12 +113,8 @@ static double dev_zrat; /* (1. - dev_zmin/dev_zmax) * static int inpresflags; /* input result flags */ -static int headlocked; /* lock vertical motion */ +static int viewflags; /* what's happening with view */ -static int isperspective; /* perspective/ortho view */ - -static int viewsteady; /* is view steady? */ - static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), xferdepth(), freedepth(), setglortho(), setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); @@ -141,15 +135,12 @@ char *id; { extern char *getenv(); static RGBPRIMS myprims = STDPRIMS; -#if (PORTALP<0) - static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, - GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, - GLX_DEPTH_SIZE,15, None}; -#else - static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, - GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, - GLX_ALPHA_SIZE,2, GLX_DEPTH_SIZE,15, None}; -#endif + static int atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER, + GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, + GLX_BLUE_SIZE,8, GLX_DEPTH_SIZE,15, None}; + static int atlOK[] = {GLX_RGBA, GLX_DOUBLEBUFFER, + GLX_RED_SIZE,4, GLX_GREEN_SIZE,4, + GLX_BLUE_SIZE,4, GLX_DEPTH_SIZE,15, None}; char *ev; double gamval = GAMMA; RGBPRIMP dpri = stdprims; @@ -180,6 +171,8 @@ char *id; #endif /* find a usable visual */ ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); + if (ourvinf == NULL) + ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); CHECK(ourvinf==NULL, USER, "no suitable visuals available"); /* get a context */ gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); @@ -236,11 +229,13 @@ char *id; glXMakeCurrent(ourdisplay, gwind, gctx); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); - glDisable(GL_DITHER); + glClearColor(0, 0, 0, 0); glFrontFace(GL_CCW); glDisable(GL_CULL_FACE); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* figure out sensible view */ pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / DisplayWidth(ourdisplay, ourscreen); @@ -253,14 +248,12 @@ char *id; checkglerr("setting rendering parameters"); copystruct(&odev.v, &stdview); odev.v.type = VT_PER; - headlocked = 0; /* free up head movement */ - viewsteady = 1; /* view starts static */ - isperspective = -1; /* but no view set, yet */ + viewflags = VWSTEADY; /* view starts static */ /* map the window */ XMapWindow(ourdisplay, gwind); dev_input(); /* sets size and view angles */ if (!odInit(DisplayWidth(ourdisplay,ourscreen) * - DisplayHeight(ourdisplay,ourscreen) / 4)) + DisplayHeight(ourdisplay,ourscreen) / 3)) error(SYSTEM, "insufficient memory for value storage"); odev.name = id; odev.firstuse = 1; /* can't recycle samples */ @@ -292,7 +285,9 @@ dev_close() /* close our display and free resources dev_clear() /* clear our representation */ { - wipeclean(); + viewflags |= VWCHANGE; /* pretend our view has changed */ + wipeclean(); /* clean off display and samples */ + dev_flush(); /* redraw geometry & get depth */ rayqleft = 0; /* hold off update */ } @@ -339,14 +334,14 @@ register VIEW *nv; dev_input(); /* get resize event */ } copystruct(&odev.v, nv); /* setview() already called */ + viewflags |= VWCHANGE; + } #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 - } else - viewsteady = 1; wipeclean(); return(1); } @@ -381,8 +376,15 @@ dev_auxcom(cmd, args) /* process an auxiliary command char *cmd, *args; { #ifdef DOBJ - if (dobj_command(cmd, args) >= 0) + int vischange; + + if ((vischange = dobj_command(cmd, args)) >= 0) { + if (vischange) { + imm_mode = beam_sync(1) > 0; + dev_clear(); + } return; + } #endif sprintf(errmsg, "%s: unknown command", cmd); error(COMMAND, errmsg); @@ -442,15 +444,13 @@ dev_flush() /* flush output as appropriate */ { int ndrawn; - if (mapped && isperspective > 0) { + if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) { #ifdef STEREO pushright(); /* draw right eye */ ndrawn = gmDrawGeom(); #ifdef DOBJ ndrawn += dobj_render(); #endif - if (ndrawn) - gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); checkglerr("rendering right eye"); popright(); /* draw left eye */ #endif @@ -458,24 +458,28 @@ dev_flush() /* flush output as appropriate */ #ifdef DOBJ ndrawn += dobj_render(); #endif - if (ndrawn) - gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); + glXSwapBuffers(ourdisplay, gwind); checkglerr("rendering base view"); } - if (mapped && viewsteady) - if (isperspective > 0) { /* first time after steady */ - if (ndrawn) - xferdepth(); /* transfer and clear depth */ - setglortho(); /* set orthographic view */ - } else if (!isperspective) { + if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == + (VWMAPPED|VWSTEADY|VWPERSP)) { + /* first time after steady */ + if (ndrawn) + xferdepth(); /* transfer and clear depth */ + setglortho(); /* set orthographic view */ + + } + if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == + (VWMAPPED|VWSTEADY|VWORTHO)) { + /* else update cones */ #ifdef STEREO - pushright(); - odUpdate(1); /* draw right eye */ - popright(); + pushright(); + odUpdate(1); /* draw right eye */ + popright(); #endif - odUpdate(0); /* draw left eye */ - } - glFlush(); /* flush OpenGL */ + odUpdate(0); /* draw left eye */ + glFlush(); /* flush OpenGL */ + } rayqleft = RAYQLEN; /* flush X11 and return # pending */ return(odev.inpready = XPending(ourdisplay)); @@ -499,7 +503,7 @@ static xferdepth() /* load and clear depth buffer */ { register GLfloat *dbp; - register GLubyte *cbuf; + register GLubyte *pbuf; if (depthbuffer == NULL) { /* allocate private depth buffer */ #ifdef STEREO @@ -512,41 +516,49 @@ xferdepth() /* load and clear depth buffer */ } /* allocate alpha buffer for portals */ if (gmPortals) - cbuf = (GLubyte *)malloc(odev.hres*odev.vres* - (4*sizeof(GLubyte))); + pbuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte)); else - cbuf = NULL; + pbuf = NULL; #ifdef STEREO - setstereobuf(STEREO_BUFFER_RIGHT); + pushright(); glReadPixels(0, 0, odev.hres, odev.vres, GL_DEPTH_COMPONENT, GL_FLOAT, depthright); - if (cbuf != NULL) + if (pbuf != NULL) { + glClear(GL_COLOR_BUFFER_BIT); + gmDrawPortals(0xff, -1, -1, -1); glReadPixels(0, 0, odev.hres, odev.vres, - GL_RGBA, GL_UNSIGNED_BYTE, cbuf); + GL_RED, GL_UNSIGNED_BYTE, pbuf); + } for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; ) - if (cbuf != NULL && isportal(cbuf+4*(dbp-depthright))) + if (pbuf != NULL && pbuf[dbp-depthright]&0x40) *dbp = FHUGE; else *dbp = mapdepth(*dbp); glClear(GL_DEPTH_BUFFER_BIT); - setstereobuf(STEREO_BUFFER_LEFT); odDepthMap(1, depthright); + popright(); #endif /* read back depth buffer */ glReadPixels(0, 0, odev.hres, odev.vres, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); - if (cbuf != NULL) + if (pbuf != NULL) { + glClear(GL_COLOR_BUFFER_BIT); /* find portals */ + gmDrawPortals(0xff, -1, -1, -1); glReadPixels(0, 0, odev.hres, odev.vres, - GL_RGBA, GL_UNSIGNED_BYTE, cbuf); + GL_RED, GL_UNSIGNED_BYTE, pbuf); +#ifdef DEBUG + glXSwapBuffers(ourdisplay, gwind); +#endif + } for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) - if (cbuf != NULL && isportal(cbuf+4*(dbp-depthbuffer))) + if (pbuf != NULL && pbuf[dbp-depthbuffer]&0x40) *dbp = FHUGE; else *dbp = mapdepth(*dbp); glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */ - if (cbuf != NULL) - free((char *)cbuf); /* free our color buffer */ odDepthMap(0, depthbuffer); /* transfer depth data */ + if (pbuf != NULL) + free((char *)pbuf); /* free our portal buffer */ } @@ -572,7 +584,6 @@ int dx, dy; FVECT direc; { GLfloat gldepth; - GLubyte glcolor[4]; double dist; if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) @@ -582,12 +593,6 @@ FVECT direc; else { glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT, GL_FLOAT, &gldepth); - if (gmPortals) { - glReadPixels(dx,dy, 1,1, GL_RGBA, - GL_UNSIGNED_BYTE, glcolor); - if (isportal(glcolor)) - return(FHUGE); - } dist = mapdepth(gldepth); } if (dist >= .99*FHUGE) @@ -603,7 +608,7 @@ pushright() /* push on right view & buffer */ double d; setstereobuf(STEREO_BUFFER_RIGHT); - if (isperspective > 0) { + if (viewflags & VWPERSP) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); d = -eyesepdist / sqrt(odev.v.hn2); @@ -617,7 +622,7 @@ pushright() /* push on right view & buffer */ static popright() /* pop off right view & buffer */ { - if (isperspective > 0) { + if (viewflags & VWPERSP) { glMatrixMode(GL_MODELVIEW); glPopMatrix(); } @@ -660,11 +665,11 @@ getevent() /* get next event */ resizewindow(levptr(XConfigureEvent)); break; case UnmapNotify: - mapped = 0; + viewflags &= ~VWMAPPED; break; case MapNotify: odRemap(0); - mapped = 1; + viewflags |= VWMAPPED; break; case Expose: fixwindow(levptr(XExposeEvent)); @@ -698,7 +703,7 @@ int fore; glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT); glDisable(GL_LIGHTING); if (fore) - glColor3ub(0, 255, 255); + glColor3ub(4, 250, 250); else glColor3ub(0, 0, 0); glBegin(GL_LINES); /* draw each grid line */ @@ -751,7 +756,7 @@ int dx, dy, mov, orb; d = MOVPCT/100. * mov; VSUM(nv.vp, nv.vp, odir, d); } - if (!mov ^ !orb && headlocked) { /* restore head height */ + if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */ VSUM(v1, odev.v.vp, nv.vp, -1.); d = DOT(v1, nv.vup); VSUM(nv.vp, nv.vp, odev.v.vup, d); @@ -800,8 +805,8 @@ XButtonPressedEvent *ebut; XNoOp(ourdisplay); /* makes sure we're not idle */ - viewsteady = 0; /* flag moving view */ - setglpersp(&odev.v); /* start us off in perspective */ + viewflags &= ~VWSTEADY; /* flag moving view */ + setglpersp(); /* start us off in perspective */ while (!XCheckMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent))) { /* get cursor position */ @@ -821,8 +826,6 @@ XButtonPressedEvent *ebut; #ifdef DOBJ ndrawn += dobj_render(); #endif - if (ndrawn) - gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); popright(); #endif /* redraw octrees */ @@ -830,18 +833,9 @@ XButtonPressedEvent *ebut; #ifdef DOBJ ndrawn += dobj_render(); /* redraw objects */ #endif - if (ndrawn) - gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); - glFlush(); - if (!ndrawn) { + glXSwapBuffers(ourdisplay, gwind); + if (!ndrawn) sleep(1); /* for reasonable interaction */ -#ifdef STEREO - pushright(); - draw_grids(0); - popright(); -#endif - draw_grids(0); - } } if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); @@ -849,12 +843,12 @@ XButtonPressedEvent *ebut; wy = levptr(XButtonReleasedEvent)->y; moveview(wx, odev.vres-1-wy, movdir, movorb); } + viewflags |= VWSTEADY; /* done goofing around */ } static -setglpersp(vp) /* set perspective view in GL */ -register VIEW *vp; +setglpersp() /* set perspective view in GL */ { double d, xmin, xmax, ymin, ymax; GLfloat vec[4]; @@ -866,7 +860,7 @@ register VIEW *vp; dev_zmax = 100.; } else { dev_zmin = 0.5*depthlim[0]; - dev_zmax = 1.75*depthlim[1]; + dev_zmax = 1.25*depthlim[1]; if (dev_zmin > dev_zmax/5.) dev_zmin = dev_zmax/5.; } @@ -895,7 +889,6 @@ register VIEW *vp; odev.v.vp[2] + odev.v.vdir[2], odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); checkglerr("setting perspective view"); - isperspective = 1; vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec); vec[0] = -odev.v.vdir[0]; @@ -911,37 +904,47 @@ register VIEW *vp; glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); /* light our GL objects */ glShadeModel(GL_SMOOTH); + viewflags &= ~VWORTHO; + viewflags |= VWPERSP; } static setglortho() /* set up orthographic view for cone drawing */ { + glDrawBuffer(GL_FRONT); /* use single-buffer mode */ /* set view matrix */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0., (double)odev.hres, 0., (double)odev.vres, 0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH)); checkglerr("setting orthographic view"); - isperspective = 0; glDisable(GL_LIGHTING); /* cones are constant color */ glShadeModel(GL_FLAT); + viewflags &= ~VWPERSP; + viewflags |= VWORTHO; } static wipeclean() /* prepare for redraw */ { - /* clear depth buffer */ + glDrawBuffer(GL_BACK); /* use double-buffer mode */ + glReadBuffer(GL_BACK); + /* clear buffers */ #ifdef STEREO setstereobuf(STEREO_BUFFER_RIGHT); - glClear(GL_DEPTH_BUFFER_BIT); + glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); setstereobuf(STEREO_BUFFER_LEFT); #endif - glClear(GL_DEPTH_BUFFER_BIT); - if (viewsteady) /* clear samples if steady */ - odClean(); + glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); freedepth(); + if ((viewflags&(VWCHANGE|VWSTEADY)) == + (VWCHANGE|VWSTEADY)) { /* clear samples if new */ + odClean(); + viewflags &= ~VWCHANGE; /* change noted */ + } else if (viewflags & VWSTEADY) + odRedrawAll(); setglpersp(&odev.v); /* reset view & clipping planes */ } @@ -961,10 +964,10 @@ register XKeyPressedEvent *ekey; return; switch (buf[0]) { case 'h': /* turn on height motion lock */ - headlocked = 1; + viewflags |= VWHEADLOCK; return; case 'H': /* turn off height motion lock */ - headlocked = 0; + viewflags &= ~VWHEADLOCK; return; case 'l': /* retrieve last view */ inpresflags |= DFL(DC_LASTVIEW); @@ -992,25 +995,23 @@ register XKeyPressedEvent *ekey; inpresflags |= DFL(DC_RESUME); return; case CTRL('R'): /* redraw screen */ - odRemap(0); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + odRemap(0); /* new tone mapping */ + glClear(GL_DEPTH_BUFFER_BIT); #ifdef STEREO setstereobuf(STEREO_BUFFER_RIGHT); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glClear(GL_DEPTH_BUFFER_BIT); setstereobuf(STEREO_BUFFER_LEFT); #endif return; case CTRL('L'): /* refresh from server */ if (inpresflags & DFL(DC_REDRAW)) - return; + return; /* already called */ XRaiseWindow(ourdisplay, gwind); - XFlush(ourdisplay); - sleep(1); - wipeclean(); /* fresh display */ - odRemap(1); /* fresh tone mapping */ - dev_flush(); /* draw octrees */ + XFlush(ourdisplay); /* raise up window */ + sleep(1); /* wait for restacking */ + dev_clear(); /* clear buffer and samples */ + odRemap(1); /* start fresh histogram */ inpresflags |= DFL(DC_REDRAW); /* resend values from server */ - rayqleft = 0; /* hold off update */ return; case 'K': /* kill rtrace process(es) */ inpresflags |= DFL(DC_KILL); @@ -1043,6 +1044,13 @@ register XExposeEvent *eexp; } xmin = eexp->x; xmax = eexp->x + eexp->width; ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; + + if (xmin <= 0 && xmax >= odev.hres-1 && + ymin <= 0 && ymax >= odev.vres) { + DCHECK(eexp->count, WARNING, "multiple clear in fixwindow"); + wipeclean(); /* make sure we're go */ + return; + } /* clear portion of depth */ glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); @@ -1085,4 +1093,5 @@ register XConfigureEvent *ersz; odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); inpresflags |= DFL(DC_SETVIEW); + viewflags |= VWCHANGE; }