--- ray/src/hd/rhd_ogl.c 1998/12/18 11:55:19 3.1 +++ ray/src/hd/rhd_ogl.c 1998/12/21 19:17:28 3.6 @@ -37,9 +37,20 @@ static char SCCSid[] = "$SunId$ SGI"; #include "x11icon.h" #ifndef RAYQLEN -#define RAYQLEN 10240 /* max. rays to queue before flush */ +#define RAYQLEN 50000 /* 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 */ +#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 FEQ #define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) #endif @@ -111,7 +122,7 @@ static int isperspective; /* perspective/ortho view * static int viewsteady; /* is view steady? */ static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), - xferdepth(), setglortho(), + xferdepth(), freedepth(), setglortho(), setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); static double getdistance(); @@ -120,6 +131,8 @@ static double getdistance(); static int pushright(), popright(); #endif +extern int gmPortals; /* GL portal list id */ + extern time_t time(); @@ -128,9 +141,23 @@ 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, +#if (PORTALP<0) + 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}; +#else + static int atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER, + GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, + GLX_BLUE_SIZE,8, GLX_ALPHA_SIZE,2, 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_ALPHA_SIZE,2, + GLX_DEPTH_SIZE,15, None}; +#endif char *ev; double gamval = GAMMA; RGBPRIMP dpri = stdprims; @@ -143,8 +170,8 @@ char *id; #endif /* open display server */ ourdisplay = XOpenDisplay(NULL); - if (ourdisplay == NULL) - error(USER, "cannot open X-windows; DISPLAY variable set?\n"); + CHECK(ourdisplay==NULL, USER, + "cannot open X-windows; DISPLAY variable set?"); #ifdef STEREO switch (XSGIQueryStereoMode(ourdisplay, ourroot)) { case STEREO_TOP: @@ -152,7 +179,7 @@ char *id; break; case STEREO_OFF: error(USER, - "wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); + "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: @@ -162,7 +189,8 @@ char *id; /* find a usable visual */ ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); if (ourvinf == NULL) - error(USER, "no suitable visuals available"); + ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); + CHECK(ourvinf==NULL, USER, "no suitable visuals available"); /* get a context */ gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); /* set gamma and tone mapping */ @@ -194,8 +222,7 @@ char *id; #endif BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual, CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr); - if (gwind == 0) - error(SYSTEM, "cannot create window\n"); + CHECK(gwind==0, SYSTEM, "cannot create window"); XStoreName(ourdisplay, gwind, id); /* set window manager hints */ ourxwmhints.flags = InputHint|IconPixmapHint; @@ -219,7 +246,7 @@ char *id; glXMakeCurrent(ourdisplay, gwind, gctx); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); - glDisable(GL_DITHER); + glEnable(GL_DITHER); glFrontFace(GL_CCW); glDisable(GL_CULL_FACE); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); @@ -243,7 +270,7 @@ char *id; XMapWindow(ourdisplay, gwind); dev_input(); /* sets size and view angles */ if (!odInit(DisplayWidth(ourdisplay,ourscreen) * - DisplayHeight(ourdisplay,ourscreen) / 10)) + DisplayHeight(ourdisplay,ourscreen) / 4)) error(SYSTEM, "insufficient memory for value storage"); odev.name = id; odev.firstuse = 1; /* can't recycle samples */ @@ -256,6 +283,9 @@ dev_close() /* close our display and free resources #ifdef DOBJ dobj_cleanup(); #endif + freedepth(); + gmEndGeom(); + gmEndPortal(); odDone(); glXMakeCurrent(ourdisplay, None, NULL); glXDestroyContext(ourdisplay, gctx); @@ -325,29 +355,34 @@ register VIEW *nv; VSUM(vwright.vp, nv->vp, nv->hvec, d); /* setview(&vwright); -- Unnecessary */ #endif - } else - viewsteady = 1; + } wipeclean(); return(1); } -dev_section(ofn) /* add octree for geometry rendering */ -char *ofn; +dev_section(gfn, pfn) /* add octree for geometry rendering */ +char *gfn, *pfn; { extern char *index(); char *cp; - if (ofn == NULL) - otEndOctree(); - else if (access(ofn, R_OK) == 0) - otNewOctree(ofn); + if (gfn == NULL) { + gmEndGeom(); + gmEndPortal(); + wipeclean(); /* new geometry, so redraw it */ + return; + } + if (access(gfn, R_OK) == 0) + gmNewGeom(gfn); #ifdef DEBUG else { - sprintf(errmsg, "cannot load octree \"%s\"", ofn); + sprintf(errmsg, "cannot load octree \"%s\"", gfn); error(WARNING, errmsg); } #endif + if (pfn != NULL) + gmNewPortal(pfn); } @@ -419,38 +454,38 @@ dev_flush() /* flush output as appropriate */ if (mapped && isperspective > 0) { #ifdef STEREO pushright(); /* draw right eye */ - otDrawOctrees(); + ndrawn = gmDrawGeom(); #ifdef DOBJ - dobj_render(); + ndrawn += dobj_render(); #endif + if (ndrawn && gmPortals) + gmDrawPortals(PORTRED,PORTGRN,PORTBLU,PORTALP); checkglerr("rendering right eye"); popright(); /* draw left eye */ #endif - ndrawn = otDrawOctrees(); + ndrawn = gmDrawGeom(); #ifdef DOBJ ndrawn += dobj_render(); #endif + if (ndrawn && gmPortals) + gmDrawPortals(PORTRED,PORTGRN,PORTBLU,PORTALP); + glXSwapBuffers(ourdisplay, gwind); checkglerr("rendering base view"); } - if (mapped && viewsteady) { - if (isperspective) { /* first time after steady */ -#ifdef STEREO - pushright(); - popright(); -#endif + if (mapped && viewsteady) + if (isperspective > 0) { /* first time after steady */ if (ndrawn) xferdepth(); /* transfer and clear depth */ setglortho(); /* set orthographic view */ - } else { + } else if (!isperspective) { #ifdef STEREO pushright(); odUpdate(1); /* draw right eye */ popright(); #endif odUpdate(0); /* draw left eye */ + glFlush(); /* flush OpenGL */ } - } - glFlush(); /* flush OpenGL */ rayqleft = RAYQLEN; /* flush X11 and return # pending */ return(odev.inpready = XPending(ourdisplay)); @@ -474,42 +509,80 @@ static xferdepth() /* load and clear depth buffer */ { register GLfloat *dbp; + register GLubyte *cbuf; - if (depthbuffer == NULL) { + if (depthbuffer == NULL) { /* allocate private depth buffer */ #ifdef STEREO depthright = (GLfloat *)malloc( odev.hres*odev.vres*sizeof(GLfloat)); #endif depthbuffer = (GLfloat *)malloc( odev.hres*odev.vres*sizeof(GLfloat)); - if (depthbuffer == NULL) - error(SYSTEM, "out of memory in xferdepth"); + CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth"); } + /* allocate alpha buffer for portals */ + if (gmPortals) + cbuf = (GLubyte *)malloc(odev.hres*odev.vres * + (4*sizeof(GLubyte))); + else + cbuf = NULL; #ifdef STEREO setstereobuf(STEREO_BUFFER_RIGHT); glReadPixels(0, 0, odev.hres, odev.vres, GL_DEPTH_COMPONENT, GL_FLOAT, depthright); - setstereobuf(STEREO_BUFFER_LEFT); + if (cbuf != NULL) + glReadPixels(0, 0, odev.hres, odev.vres, + GL_RGBA, GL_UNSIGNED_BYTE, cbuf); for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; ) - *dbp = mapdepth(*dbp); - odDepthMap(1, depthright); + if (cbuf != NULL && isportal(cbuf+4*(dbp-depthright))) + *dbp = FHUGE; + else + *dbp = mapdepth(*dbp); glClear(GL_DEPTH_BUFFER_BIT); + setstereobuf(STEREO_BUFFER_LEFT); + odDepthMap(1, depthright); #endif + /* read back depth buffer */ glReadPixels(0, 0, odev.hres, odev.vres, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); + if (cbuf != NULL) + glReadPixels(0, 0, odev.hres, odev.vres, + GL_RGBA, GL_UNSIGNED_BYTE, cbuf); for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) - *dbp = mapdepth(*dbp); + if (cbuf != NULL && isportal(cbuf+4*(dbp-depthbuffer))) + *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 */ - glClear(GL_DEPTH_BUFFER_BIT); /* clear system buffer */ } +static +freedepth() /* free recorded depth buffer */ +{ + if (depthbuffer == NULL) + return; +#ifdef STEREO + odDepthMap(1, NULL); + free((char *)depthright); + depthright = NULL; +#endif + odDepthMap(0, NULL); + free((char *)depthbuffer); + depthbuffer = NULL; +} + + static double getdistance(dx, dy, direc) /* distance from fore plane along view ray */ int dx, dy; FVECT direc; { GLfloat gldepth; + GLubyte glcolor[4]; double dist; if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) @@ -519,6 +592,12 @@ 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) @@ -594,6 +673,7 @@ getevent() /* get next event */ mapped = 0; break; case MapNotify: + odRemap(0); mapped = 1; break; case Expose: @@ -689,7 +769,7 @@ int dx, dy, mov, orb; if (setview(&nv) != NULL) return(0); /* illegal view */ dev_view(&nv); - inpresflags |= DFL(DC_SETVIEW)|DFL(DC_REDRAW); + inpresflags |= DFL(DC_SETVIEW); return(1); } @@ -723,6 +803,7 @@ XButtonPressedEvent *ebut; { int movdir = MOVDIR(ebut->button); int movorb = MOVORB(ebut->state); + int ndrawn; Window rootw, childw; int rootx, rooty, wx, wy; unsigned int statemask; @@ -746,18 +827,24 @@ XButtonPressedEvent *ebut; #ifdef STEREO pushright(); draw_grids(1); - otDrawOctrees(); + ndrawn = gmDrawGeom(); #ifdef DOBJ - dobj_render(); + ndrawn += dobj_render(); #endif + if (ndrawn && gmPortals) + gmDrawPortals(PORTRED,PORTGRN,PORTBLU,PORTALP); popright(); #endif /* redraw octrees */ - otDrawOctrees(); + ndrawn = gmDrawGeom(); #ifdef DOBJ - dobj_render(); /* redraw objects */ + ndrawn += dobj_render(); /* redraw objects */ #endif - glFlush(); + if (ndrawn && gmPortals) + gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); + glXSwapBuffers(ourdisplay, gwind); + if (!ndrawn) + sleep(1); /* for reasonable interaction */ } if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); @@ -765,6 +852,7 @@ XButtonPressedEvent *ebut; wy = levptr(XButtonReleasedEvent)->y; moveview(wx, odev.vres-1-wy, movdir, movorb); } + viewsteady = 1; /* done goofing around */ } @@ -776,13 +864,13 @@ register VIEW *vp; GLfloat vec[4]; double depthlim[2]; /* set depth limits */ - otDepthLimit(depthlim, odev.v.vp, odev.v.vdir); + gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir); if (depthlim[0] >= depthlim[1]) { dev_zmin = 1.; dev_zmax = 100.; } else { dev_zmin = 0.5*depthlim[0]; - dev_zmax = 1.5*depthlim[1]; + dev_zmax = 1.75*depthlim[1]; if (dev_zmin > dev_zmax/5.) dev_zmin = dev_zmax/5.; } @@ -833,6 +921,7 @@ register VIEW *vp; static setglortho() /* set up orthographic view for cone drawing */ { + glDrawBuffer(GL_FRONT); /* use single-buffer mode */ /* set view matrix */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -848,25 +937,17 @@ setglortho() /* set up orthographic view for cone dr static wipeclean() /* prepare for redraw */ { - /* clear depth buffer */ + glDrawBuffer(GL_BACK); /* use double-buffer mode */ + /* 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); + glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); + freedepth(); if (viewsteady) /* clear samples if steady */ odClean(); - if (depthbuffer != NULL) { /* free recorded depth buffer */ -#ifdef STEREO - odDepthMap(1, NULL); - free((char *)depthright); - depthright = NULL; -#endif - odDepthMap(0, NULL); - free((char *)depthbuffer); - depthbuffer = NULL; - } setglpersp(&odev.v); /* reset view & clipping planes */ } @@ -917,11 +998,11 @@ register XKeyPressedEvent *ekey; inpresflags |= DFL(DC_RESUME); return; case CTRL('R'): /* redraw screen */ - odRemap(); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + odRemap(0); + 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; @@ -932,7 +1013,7 @@ register XKeyPressedEvent *ekey; XFlush(ourdisplay); sleep(1); wipeclean(); /* fresh display */ - odRemap(); /* new tone mapping */ + odRemap(1); /* fresh tone mapping */ dev_flush(); /* draw octrees */ inpresflags |= DFL(DC_REDRAW); /* resend values from server */ rayqleft = 0; /* hold off update */ @@ -969,6 +1050,7 @@ register XExposeEvent *eexp; xmin = eexp->x; xmax = eexp->x + eexp->width; ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; /* clear portion of depth */ + glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthFunc(GL_ALWAYS); glBegin(GL_POLYGON); @@ -988,8 +1070,7 @@ register XExposeEvent *eexp; odRedraw(1, xmin, ymin, xmax, ymax); setstereobuf(STEREO_BUFFER_LEFT); #endif - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - glDepthFunc(GL_LEQUAL); + glPopAttrib(); odRedraw(0, xmin, ymin, xmax, ymax); } @@ -1009,5 +1090,5 @@ 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); - inpresflags |= DFL(DC_SETVIEW)|DFL(DC_REDRAW); + inpresflags |= DFL(DC_SETVIEW); }