--- ray/src/hd/rhd_ogl.c 1998/12/20 20:36:48 3.2 +++ 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 @@ -130,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; @@ -145,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: @@ -154,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: @@ -164,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 */ @@ -196,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; @@ -221,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); @@ -245,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 */ @@ -330,8 +355,7 @@ register VIEW *nv; VSUM(vwright.vp, nv->vp, nv->hvec, d); /* setview(&vwright); -- Unnecessary */ #endif - } else - viewsteady = 1; + } wipeclean(); return(1); } @@ -346,6 +370,7 @@ char *gfn, *pfn; if (gfn == NULL) { gmEndGeom(); gmEndPortal(); + wipeclean(); /* new geometry, so redraw it */ return; } if (access(gfn, R_OK) == 0) @@ -429,38 +454,38 @@ dev_flush() /* flush output as appropriate */ if (mapped && isperspective > 0) { #ifdef STEREO pushright(); /* draw right eye */ - gmDrawGeom(0); + 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 = gmDrawGeom(1); + 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)); @@ -484,48 +509,54 @@ static xferdepth() /* load and clear depth buffer */ { register GLfloat *dbp; - register GLubyte *abuf; + 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); - /* read alpha buffer for portals */ - if (gmPortals) - abuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte)); - else - abuf = NULL; - if (abuf != NULL) + if (cbuf != NULL) glReadPixels(0, 0, odev.hres, odev.vres, - GL_ALPHA, GL_UNSIGNED_BYTE, abuf); + GL_RGBA, GL_UNSIGNED_BYTE, cbuf); for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) - if (abuf != NULL && abuf[dbp-depthbuffer]) + 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 */ - if (abuf != NULL) - free((char *)abuf); /* free alpha buffer */ } @@ -551,7 +582,7 @@ int dx, dy; FVECT direc; { GLfloat gldepth; - GLubyte glalpha; + GLubyte glcolor[4]; double dist; if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) @@ -561,12 +592,13 @@ FVECT direc; else { glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT, GL_FLOAT, &gldepth); - if (gmPortals) - glReadPixels(dx,dy, 1,1, GL_ALPHA, - GL_UNSIGNED_BYTE, &glalpha); - else - glalpha = 0; - dist = glalpha ? FHUGE : mapdepth(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) return(FHUGE); @@ -795,27 +827,24 @@ XButtonPressedEvent *ebut; #ifdef STEREO pushright(); draw_grids(1); - gmDrawGeom(0); + ndrawn = gmDrawGeom(); #ifdef DOBJ - dobj_render(); + ndrawn += dobj_render(); #endif + if (ndrawn && gmPortals) + gmDrawPortals(PORTRED,PORTGRN,PORTBLU,PORTALP); popright(); #endif /* redraw octrees */ - ndrawn = gmDrawGeom(1); + ndrawn = gmDrawGeom(); #ifdef DOBJ ndrawn += dobj_render(); /* redraw objects */ #endif - glFlush(); - if (!ndrawn) { + if (ndrawn && gmPortals) + gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); + 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); @@ -823,6 +852,7 @@ XButtonPressedEvent *ebut; wy = levptr(XButtonReleasedEvent)->y; moveview(wx, odev.vres-1-wy, movdir, movorb); } + viewsteady = 1; /* done goofing around */ } @@ -891,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(); @@ -906,16 +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(); - freedepth(); setglpersp(&odev.v); /* reset view & clipping planes */ } @@ -967,10 +999,10 @@ register XKeyPressedEvent *ekey; return; case CTRL('R'): /* redraw screen */ odRemap(0); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + 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;