--- ray/src/util/glrad.c 1998/06/09 11:43:18 3.1 +++ ray/src/util/glrad.c 1998/07/07 08:58:34 3.7 @@ -13,6 +13,9 @@ static char SCCSid[] = "$SunId$ SGI"; #include "paths.h" #include #include +#ifndef NOSTEREO +#include +#endif #include #include "glradicon.h" @@ -30,9 +33,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 BORWIDTH 5 /* border width */ #define ourscreen DefaultScreen(ourdisplay) @@ -65,8 +65,8 @@ struct { int currentview = 0; /* current view number */ VIEW thisview = STDVIEW; /* displayed view */ +double eyedist = 1; /* interocular distance */ VIEW lastview; /* last recorded view */ -char *lastvc = NULL; /* cause of last view change */ char *progname; /* global argv[0] */ char *radfile; /* rad input file */ @@ -76,11 +76,27 @@ char *octree; /* octree name (NULL if unnec.) */ int rtpd[3]; /* rtrace process descriptors */ +int silent = 0; /* run rad silently? */ int backvis = 1; /* back faces visible? */ +int stereo = 0; /* do stereo? */ +#ifdef NOSTEREO +#define setstereobuf(bid) 0 +#else +#define setstereobuf(bid) (glXWaitGL(), \ + XSGISetStereoBuffer(ourdisplay, gwind, bid), \ + glXWaitX()) +#endif + int displist; /* our scene display list */ -extern char *fgets(), *fgetline(), *atos(), *scan4var(); +int no_render = 0; /* don't rerender */ + +#ifdef BSD +#define strchr index +#endif + +extern char *strchr(), *fgets(), *fgetline(), *atos(), *scan4var(); extern int nowarn; /* turn warnings off? */ extern time_t time(); @@ -102,6 +118,12 @@ char *argv[]; case 'w': nowarn = !nowarn; break; + case 's': + silent = !silent; + break; + case 'S': + stereo = !stereo; + break; case 'c': vwintvl = atoi(argv[++i]); break; @@ -113,13 +135,28 @@ char *argv[]; } if (i >= argc) goto userr; +#ifdef NOSTEREO + if (stereo) + error(INTERNAL, "stereo not supported in this version"); +#endif /* run rad and get views */ runrad(argc-i, argv+i); /* check view */ - if (viewsel != NULL && (currentview = findvw(viewsel)) < 0) { - fprintf(stderr, "%s: no such view\n", viewsel); - quit(1); - } + if (viewsel != NULL) + if (viewsel[0] == '-') { + char *ve = viewsel; + if (!sscanview(&thisview, viewsel) || + (ve = setview(&thisview)) != NULL) { + fprintf(stderr, "%s: bad view: %s\n", + progname, ve); + quit(1); + } + currentview = -1; + } else if ((currentview = findvw(viewsel)) < 0) { + fprintf(stderr, "%s: no such view: %s\n", + progname, viewsel); + quit(1); + } /* open GL */ dev_open(radfile = argv[i]); /* load octree or scene files */ @@ -129,14 +166,14 @@ char *argv[]; } else displist = rgl_filelist(nscenef, scene); /* set initial view */ - dev_view(vwl[currentview].v); + dev_view(currentview < 0 ? &thisview : vwl[currentview].v); /* input/render loop */ while (dev_input(vwintvl)) ; /* all done */ quit(0); userr: - fprintf(stderr, "Usage: %s [-w][-c #secs][-v view] rfile [VAR=value]..\n", + fprintf(stderr, "Usage: %s [-w][-b][-v view] rfile [VAR=value]..\n", argv[0]); quit(1); } @@ -184,6 +221,10 @@ char **av; /* set rad commmand */ strcpy(radcomm, "rad -w -v 0 "); /* look out below! */ cp = radcomm + 19; + if (silent) { + strcpy(cp, "-s "); + cp += 3; + } while (ac--) { strcpy(cp, *av++); while (*cp) cp++; @@ -204,7 +245,11 @@ char **av; expval = atof(cp); if (*cp == '-' | *cp == '+') expval = pow(2., expval); + expval *= 0.5; /* compensate for local shading */ } + /* look for eye separation */ + if ((cp = scan4var(buf, sizeof(buf), "EYESEP", fp)) != NULL) + eyedist = atof(cp); /* look for materials */ while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) { nscenef += wordstring(scene+nscenef, cp); @@ -245,14 +290,15 @@ char **av; /* open options file */ if ((fp = fopen(optfile, "r")) == NULL) error(SYSTEM, "cannot open options file"); - /* get ambient value */ + /* get relevant options */ while (fgets(buf, sizeof(buf), fp) != NULL) - if (!strncmp(buf, "-av ", 4)) { + if (!strncmp(buf, "-av ", 4)) setcolor(ambval, atof(buf+4), atof(sskip2(buf+4,1)), atof(sskip2(buf+4,2))); - break; - } + else if (backvis && !strncmp(buf, "-bv", 3) && + (!buf[3] || strchr(" 0-FfNn", buf[3]) != NULL)) + backvis = 0; fclose(fp); unlink(optfile); /* delete options file */ } @@ -325,7 +371,6 @@ char *id; GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None}; XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; - XSizeHints oursizhints; /* open display server */ ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) @@ -351,23 +396,34 @@ char *id; if (gwind == 0) error(SYSTEM, "cannot create window\n"); XStoreName(ourdisplay, gwind, id); +#ifndef NOSTEREO + if (stereo) /* check if stereo working */ + switch (XSGIQueryStereoMode(ourdisplay, gwind)) { + 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 not supported on this screen"); + default: + error(INTERNAL, "unknown stereo mode"); + } +#endif /* set window manager hints */ ourxwmhints.flags = InputHint|IconPixmapHint; ourxwmhints.input = True; ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind, glradicon_bits, glradicon_width, glradicon_height); XSetWMHints(ourdisplay, gwind, &ourxwmhints); - oursizhints.min_width = MINWIDTH; - oursizhints.min_height = MINHEIGHT; - oursizhints.flags = PMinSize; - XSetNormalHints(ourdisplay, gwind, &oursizhints); /* set GLX context */ glXMakeCurrent(ourdisplay, gwind, gctx); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glShadeModel(GL_SMOOTH); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); + glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glEnable(GL_LIGHTING); if (backvis) glDisable(GL_CULL_FACE); @@ -382,8 +438,17 @@ char *id; DisplayWidth(ourdisplay, ourscreen); pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / DisplayHeight(ourdisplay, ourscreen); + if (stereo) { /* set stereo mode */ + setstereobuf(STEREO_BUFFER_LEFT); + pheight *= 2.; + } /* map the window */ XMapWindow(ourdisplay, gwind); + no_render++; + do + dev_input(0); /* get resize event */ + while (hres == 0 & vres == 0); + no_render--; rgl_checkerr("initializing GLX"); } @@ -419,7 +484,7 @@ register VIEW *nv; } if (hres != 0 & vres != 0) { wa = (vres*pheight)/(hres*pwidth); - va = viewaspect(&thisview); + va = viewaspect(nv); if (va > wa+.05) { newvres = (pwidth/pheight)*va*newhres + .5; if (newvres > maxvres) { @@ -434,10 +499,12 @@ register VIEW *nv; } } if (newhres != hres | newvres != vres) { + no_render++; XResizeWindow(ourdisplay, gwind, newhres, newvres); do dev_input(0); /* get resize event */ while (newhres != hres | newvres != vres); + no_render--; } } copystruct(&thisview, nv); @@ -489,10 +556,25 @@ int nsecs; render() /* render our display list and swap buffers */ { - if (!mapped) + double d; + + if (!mapped | no_render) return; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glCallList(displist); + if (stereo) { /* do right eye for stereo */ + setstereobuf(STEREO_BUFFER_RIGHT); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + d = -eyedist / sqrt(thisview.hn2); + glTranslated(d*thisview.hvec[0], d*thisview.hvec[1], + d*thisview.hvec[2]); + glCallList(displist); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + setstereobuf(STEREO_BUFFER_LEFT); + } glXSwapBuffers(ourdisplay, gwind); /* calls glFlush() */ rgl_checkerr("rendering display list"); } @@ -554,7 +636,8 @@ XButtonPressedEvent *ebut; int rootx, rooty, wx, wy; unsigned int statemask; - copylastv("moving"); + copylastv( movorb ? (movdir ? "left/right" : "up/down") : + (movdir ? "fore/back" : "rotate") ); XNoOp(ourdisplay); while (!XCheckMaskEvent(ourdisplay, @@ -609,8 +692,8 @@ register VIEW *vp; { double d, xmin, xmax, ymin, ymax, zmin, zmax; - zmin = 0.05; - zmax = 5000.; + zmin = 0.1; + zmax = 1000.; if (thisview.vfore > FTINY) zmin = thisview.vfore; if (thisview.vaft > FTINY) @@ -654,7 +737,7 @@ register XKeyPressedEvent *ekey; headlocked = 0; break; case 'l': /* retrieve last (premouse) view */ - if (lastvc != NULL) { + if (lastview.type) { VIEW vtmp; copystruct(&vtmp, &thisview); dev_view(&lastview); @@ -726,8 +809,7 @@ int vwnum; ; else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL) vwnum = 0; - if (vwnum == currentview) - return; + copylastv("standard view"); dev_view(vwl[currentview=vwnum].v); } @@ -768,6 +850,8 @@ VIEW *vp; copylastv(cause) /* copy last view position */ char *cause; { + static char *lastvc; + if (cause == lastvc) return; /* only record one view per cause */ lastvc = cause; @@ -798,6 +882,8 @@ register XConfigureEvent *ersz; glViewport(0, 0, hres=ersz->width, vres=ersz->height); if (hres > maxhres) maxhres = hres; if (vres > maxvres) maxvres = vres; + if (no_render) + return; wa = (vres*pheight)/(hres*pwidth); va = viewaspect(&thisview); if (va > wa+.05) {