--- ray/src/util/glrad.c 1998/06/10 17:52:18 3.4 +++ ray/src/util/glrad.c 2005/07/24 19:53:08 3.20 @@ -1,23 +1,24 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: glrad.c,v 3.20 2005/07/24 19:53:08 greg Exp $"; #endif - /* * Program to display Radiance scene using OpenGL. */ -#include "radogl.h" -#include "view.h" -#include "paths.h" #include #include #ifndef NOSTEREO #include #endif #include +#include +#include + +#include "radogl.h" +#include "view.h" +#include "paths.h" #include "glradicon.h" +#include "rtprocess.h" #ifndef MAXVIEW #define MAXVIEW 63 /* maximum number of standard views */ @@ -33,9 +34,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) @@ -77,14 +75,14 @@ char *scene[MAXSCENE+1]; /* material and scene file l int nscenef = 0; /* number of scene files */ char *octree; /* octree name (NULL if unnec.) */ -int rtpd[3]; /* rtrace process descriptors */ +SUBPROC rtpd; /* 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 +#define setstereobuf(bid) #else #define setstereobuf(bid) (glXWaitGL(), \ XSGISetStereoBuffer(ourdisplay, gwind, bid), \ @@ -93,20 +91,39 @@ int stereo = 0; /* do stereo? */ int displist; /* our scene display list */ -int in_dev_view = 0; /* currently in dev_view() */ +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(); +static void startrtrace(char *octname); +static void runrad(int ac, char **av); +static int findvw(register char *nm); +static int varmatch(register char *s, register char *vn); +static char * scan4var(char *buf, int buflen, char *vname, FILE *fp); +static void dev_open(char *id); +static void dev_close(void); +static int dev_view(register VIEW *nv); +static int dev_input(int nsecs); +static void render(void); +static int moveview(int dx, int dy, int mov, int orb); +static void waitabit(void); +static void getmove(XButtonPressedEvent *ebut); +static int getintersect(FVECT wp, FVECT org, FVECT dir, double md); +static void setglpersp(register VIEW *vp); +static int getkey(register XKeyPressedEvent *ekey); +static void zoomview(int pct, int dx, int dy); +static void gotoview(int vwnum); +static void appendview(char *nm, VIEW *vp); +static void copylastv(char *cause); +static void fixwindow(register XExposeEvent *eexp); +static void resizewindow(register XConfigureEvent *ersz); -main(argc, argv) -int argc; -char *argv[]; + +int +main( + int argc, + char *argv[] +) { char *viewsel = NULL; long vwintvl = 0; @@ -145,7 +162,7 @@ char *argv[]; /* run rad and get views */ runrad(argc-i, argv+i); /* check view */ - if (viewsel != NULL) + if (viewsel != NULL) { if (viewsel[0] == '-') { char *ve = viewsel; if (!sscanview(&thisview, viewsel) || @@ -160,14 +177,15 @@ char *argv[]; progname, viewsel); quit(1); } + } /* open GL */ dev_open(radfile = argv[i]); /* load octree or scene files */ if (octree != NULL) { - displist = rgl_octlist(octree, NULL, NULL); + displist = rgl_octlist(octree, NULL, NULL, NULL); startrtrace(octree); } else - displist = rgl_filelist(nscenef, scene); + displist = rgl_filelist(nscenef, scene, NULL); /* set initial view */ dev_view(currentview < 0 ? &thisview : vwl[currentview].v); /* input/render loop */ @@ -176,28 +194,35 @@ char *argv[]; /* all done */ quit(0); userr: - fprintf(stderr, "Usage: %s [-w][-b][-v view] rfile [VAR=value]..\n", + fprintf(stderr, + "Usage: %s [-w][-s][-b][-S][-v view] rfile [VAR=value]..\n", argv[0]); quit(1); + return 1; /* pro forma return */ } -quit(code) /* exit gracefully */ -int code; +void +quit( /* exit gracefully */ + int code +) { if (ourdisplay != NULL) dev_close(); - if (rtpd[2] > 0) { - if (close_process(rtpd) > 0) + /* if (rtpd.pid > 0) { */ + if (rtpd.running) { + if (close_process(&rtpd) > 0) wputs("bad exit status from rtrace\n"); - rtpd[2] = 0; + /* rtpd.pid = 0; */ } exit(code); } -startrtrace(octname) /* start rtrace on octname */ -char *octname; +static void +startrtrace( /* start rtrace on octname */ + char *octname +) { static char *av[12] = {"rtrace", "-h", "-fff", "-ld+", "-opL", "-x", "1"}; @@ -206,19 +231,20 @@ char *octname; if (nowarn) av[ac++] = "-w-"; av[ac++] = octname; av[ac] = NULL; - if (open_process(rtpd, av) <= 0) + if (open_process(&rtpd, av) <= 0) error(SYSTEM, "cannot start rtrace process"); } -runrad(ac, av) /* run rad and load variables */ -int ac; -char **av; +static void +runrad( /* run rad and load variables */ + int ac, + char **av +) { static char optfile[] = TEMPLATE; int nvn = 0, nvv = 0; FILE *fp; - int cval; register char *cp; char radcomm[256], buf[128], nam[32]; /* set rad commmand */ @@ -246,7 +272,7 @@ char **av; /* get exposure */ if ((cp = scan4var(buf, sizeof(buf), "EXPOSURE", fp)) != NULL) { expval = atof(cp); - if (*cp == '-' | *cp == '+') + if ((*cp == '-') | (*cp == '+')) expval = pow(2., expval); expval *= 0.5; /* compensate for local shading */ } @@ -278,7 +304,7 @@ char **av; do if (isview(buf)) { vwl[nvv].v = (VIEW *)bmalloc(sizeof(VIEW)); - copystruct(vwl[nvv].v, &stdview); + *(vwl[nvv].v) = stdview; sscanview(vwl[nvv].v, buf); if ((cp = setview(vwl[nvv++].v)) != NULL) { fprintf(stderr, "%s: bad view %d - %s\n", @@ -300,20 +326,21 @@ char **av; atof(sskip2(buf+4,1)), atof(sskip2(buf+4,2))); else if (backvis && !strncmp(buf, "-bv", 3) && - (!buf[3] || strchr(" 0-FfNn", buf[3]) != NULL)) + (!buf[3] || strchr("0-FfNn \n",buf[3])!=NULL)) backvis = 0; fclose(fp); unlink(optfile); /* delete options file */ } -int -findvw(nm) /* find named view */ -register char *nm; +static int +findvw( /* find named view */ + register char *nm +) { register int n; - if (*nm >= '1' & *nm <= '9' && + if ((*nm >= '1') & (*nm <= '9') && (n = atoi(nm)-1) <= MAXVIEW && vwl[n].v != NULL) return(n); for (n = 0; vwl[n].v != NULL; n++) @@ -323,9 +350,11 @@ register char *nm; } -int -varmatch(s, vn) /* match line to variable */ -register char *s, *vn; +static int +varmatch( /* match line to variable */ + register char *s, + register char *vn +) { register int c; @@ -341,12 +370,13 @@ register char *s, *vn; } -char * -scan4var(buf, buflen, vname, fp) /* scan for variable from fp */ -char *buf; -int buflen; -char *vname; -FILE *fp; +static char * +scan4var( /* scan for variable from fp */ + char *buf, + int buflen, + char *vname, + FILE *fp +) { int cval; register char *cp; @@ -366,15 +396,16 @@ FILE *fp; } -dev_open(id) /* initialize GLX driver */ -char *id; +static void +dev_open( /* initialize GLX driver */ + char *id +) { static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,4, GLX_GREEN_SIZE,4, GLX_BLUE_SIZE,4, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,15, None}; XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; - XSizeHints oursizhints; /* open display server */ ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) @@ -418,28 +449,23 @@ char *id; /* set window manager hints */ ourxwmhints.flags = InputHint|IconPixmapHint; ourxwmhints.input = True; - ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, - gwind, glradicon_bits, glradicon_width, glradicon_height); + ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind, + (char *)glradicon_bits, glradicon_width, glradicon_height); XSetWMHints(ourdisplay, gwind, &ourxwmhints); - oursizhints.min_width = MINWIDTH; - oursizhints.min_height = stereo ? MINHEIGHT/2 : 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); + glFrontFace(GL_CCW); + glCullFace(GL_BACK); if (backvis) glDisable(GL_CULL_FACE); - else { - glFrontFace(GL_CCW); - glCullFace(GL_BACK); + else glEnable(GL_CULL_FACE); - } glDrawBuffer(GL_BACK); /* figure out sensible view */ pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / @@ -452,14 +478,17 @@ char *id; } /* map the window */ XMapWindow(ourdisplay, gwind); + no_render++; do dev_input(0); /* get resize event */ - while (hres == 0 & vres == 0); + while ((hres == 0) & (vres == 0)); + no_render--; rgl_checkerr("initializing GLX"); } -dev_close() /* close our display and free resources */ +static void +dev_close(void) /* close our display and free resources */ { glXMakeCurrent(ourdisplay, None, NULL); glXDestroyContext(ourdisplay, gctx); @@ -470,9 +499,10 @@ dev_close() /* close our display and free resources } -int -dev_view(nv) /* assign new driver view */ -register VIEW *nv; +static int +dev_view( /* assign new driver view */ + register VIEW *nv +) { int newhres = hres, newvres = vres; double wa, va; @@ -481,14 +511,14 @@ register VIEW *nv; error(COMMAND, "illegal view type"); nv->type = VT_PER; } - if (nv->horiz > 160. | nv->vert > 160.) { + if ((nv->horiz > 160.) | (nv->vert > 160.)) { error(COMMAND, "illegal view angle"); if (nv->horiz > 160.) nv->horiz = 160.; if (nv->vert > 160.) nv->vert = 160.; } - if (hres != 0 & vres != 0) { + if ((hres != 0) & (vres != 0)) { wa = (vres*pheight)/(hres*pwidth); va = viewaspect(nv); if (va > wa+.05) { @@ -504,25 +534,26 @@ register VIEW *nv; newvres = (pwidth/pheight)*va*newhres + .5; } } - if (newhres != hres | newvres != vres) { - in_dev_view++; + if ((newhres != hres) | (newvres != vres)) { + no_render++; XResizeWindow(ourdisplay, gwind, newhres, newvres); do dev_input(0); /* get resize event */ - while (newhres != hres | newvres != vres); - in_dev_view--; + while ((newhres != hres) | (newvres != vres)); + no_render--; } } - copystruct(&thisview, nv); + thisview = *nv; setglpersp(&thisview); render(); return(1); } -int -dev_input(nsecs) /* get next input event */ -int nsecs; +static int +dev_input( /* get next input event */ + int nsecs +) { #if 0 static time_t lasttime = 0; @@ -560,11 +591,12 @@ int nsecs; } -render() /* render our display list and swap buffers */ +static void +render(void) /* render our display list and swap buffers */ { double d; - if (!mapped) + if (!mapped | no_render) return; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glCallList(displist); @@ -586,15 +618,19 @@ render() /* render our display list and swap buffers } -moveview(dx, dy, mov, orb) /* move our view */ -int dx, dy, mov, orb; +static int +moveview( /* move our view */ + int dx, + int dy, + int mov, + int orb +) { VIEW nv; FVECT odir, v1, wp; double d; - register int li; /* start with old view */ - copystruct(&nv, &thisview); + nv = thisview; /* change view direction */ if ((d = viewray(v1, odir, &thisview, (dx+.5)/hres, (dy+.5)/vres)) < -FTINY) @@ -632,9 +668,21 @@ int dx, dy, mov, orb; } -getmove(ebut) /* get view change */ -XButtonPressedEvent *ebut; +static void +waitabit(void) /* pause a moment */ { + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 5000000; + nanosleep(&ts, NULL); +} + + +static void +getmove( /* get view change */ + XButtonPressedEvent *ebut +) +{ int movdir = MOVDIR(ebut->button); int movorb = MOVORB(ebut->state); int moved = 0; @@ -648,6 +696,8 @@ XButtonPressedEvent *ebut; while (!XCheckMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent))) { + /* pause so as not to move too fast */ + waitabit(); if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, &rootx, &rooty, &wx, &wy, &statemask)) @@ -668,14 +718,18 @@ XButtonPressedEvent *ebut; } -getintersect(wp, org, dir, md) /* intersect ray with scene geometry */ -FVECT wp; /* returned world intersection point */ -FVECT org, dir; -double md; +static int +getintersect( /* intersect ray with scene geometry */ + FVECT wp, /* returned world intersection point */ + FVECT org, + FVECT dir, + double md +) { float fbuf[6]; /* check to see if rtrace is running */ - if (rtpd[2] <= 0) + /* if (rtpd.pid <= 0) */ + if (!rtpd.running) return(0); /* assign origin */ fbuf[0] = org[0]; fbuf[1] = org[1]; fbuf[2] = org[2]; @@ -683,8 +737,8 @@ double md; if (md <= FTINY) md = FHUGE; fbuf[3] = dir[0]*md; fbuf[4] = dir[1]*md; fbuf[5] = dir[2]*md; /* trace that ray */ - if (process(rtpd, fbuf, fbuf, 4*sizeof(float), 6*sizeof(float)) != - 4*sizeof(float)) + if (process(&rtpd, (char *)fbuf, (char *)fbuf, + 4*sizeof(float), 6*sizeof(float)) != 4*sizeof(float)) error(INTERNAL, "error getting data back from rtrace process"); if (fbuf[3] >= .99*FHUGE) return(0); /* missed local objects */ @@ -693,8 +747,10 @@ double md; } -setglpersp(vp) /* set perspective view in GL */ -register VIEW *vp; +static void +setglpersp( /* set perspective view in GL */ + register VIEW *vp +) { double d, xmin, xmax, ymin, ymax, zmin, zmax; @@ -725,9 +781,10 @@ register VIEW *vp; } -int -getkey(ekey) /* get input key */ -register XKeyPressedEvent *ekey; +static int +getkey( /* get input key */ + register XKeyPressedEvent *ekey +) { int n; char buf[8]; @@ -745,9 +802,9 @@ register XKeyPressedEvent *ekey; case 'l': /* retrieve last (premouse) view */ if (lastview.type) { VIEW vtmp; - copystruct(&vtmp, &thisview); + vtmp = thisview; dev_view(&lastview); - copystruct(&lastview, &vtmp); + lastview = vtmp; } else XBell(ourdisplay, 0); break; @@ -781,14 +838,16 @@ register XKeyPressedEvent *ekey; } -zoomview(pct, dx, dy) /* zoom in or out around (dx,dy) */ -int pct; -int dx, dy; +static void +zoomview( /* zoom in or out around (dx,dy) */ + int pct, + int dx, + int dy +) { double h, v; - FVECT direc; - if (pct == 100 | pct <= 0) + if ((pct == 100) | (pct <= 0)) return; copylastv("zooming"); h = (dx+.5)/hres - 0.5; @@ -807,28 +866,30 @@ int dx, dy; } -gotoview(vwnum) /* go to specified view number */ -int vwnum; +static void +gotoview( /* go to specified view number */ + int vwnum +) { if (vwnum < 0) for (vwnum = currentview; vwl[vwnum+1].v != NULL; vwnum++) ; else if (vwnum >= MAXVIEW || vwl[vwnum].v == NULL) vwnum = 0; - if (vwnum == currentview) - return; copylastv("standard view"); dev_view(vwl[currentview=vwnum].v); } -appendview(nm, vp) /* append standard view */ -char *nm; -VIEW *vp; +static void +appendview( /* append standard view */ + char *nm, + VIEW *vp +) { FILE *fp; /* check if already in there */ - if (!bcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) { + if (!memcmp(&thisview, vwl[currentview].v, sizeof(VIEW))) { error(COMMAND, "view already in standard list"); return; } @@ -849,28 +910,32 @@ VIEW *vp; if (currentview >= MAXVIEW) error(INTERNAL, "too many views in appendview"); vwl[currentview].v = (VIEW *)bmalloc(sizeof(VIEW)); - copystruct(vwl[currentview].v, &thisview); + *(vwl[currentview].v) = thisview; if (nm != NULL) vwl[currentview].nam = savqstr(nm); } -copylastv(cause) /* copy last view position */ -char *cause; +static void +copylastv( /* copy last view position */ + char *cause +) { static char *lastvc; if (cause == lastvc) return; /* only record one view per cause */ lastvc = cause; - copystruct(&lastview, &thisview); + lastview = thisview; } -fixwindow(eexp) /* repair damage to window */ -register XExposeEvent *eexp; +static void +fixwindow( /* repair damage to window */ + register XExposeEvent *eexp +) { - if (hres == 0 | vres == 0) { /* first exposure */ + if ((hres == 0) | (vres == 0)) { /* first exposure */ resizewindow((XConfigureEvent *)eexp); return; } @@ -881,8 +946,10 @@ register XExposeEvent *eexp; } -resizewindow(ersz) /* resize window */ -register XConfigureEvent *ersz; +static void +resizewindow( /* resize window */ + register XConfigureEvent *ersz +) { static char resizing[] = "resizing window"; double wa, va; @@ -890,7 +957,7 @@ register XConfigureEvent *ersz; glViewport(0, 0, hres=ersz->width, vres=ersz->height); if (hres > maxhres) maxhres = hres; if (vres > maxvres) maxvres = vres; - if (in_dev_view) + if (no_render) return; wa = (vres*pheight)/(hres*pwidth); va = viewaspect(&thisview);