--- ray/src/util/glrad.c 1998/07/15 17:51:13 3.9 +++ ray/src/util/glrad.c 2016/04/25 20:06:22 3.25 @@ -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.25 2016/04/25 20:06:22 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 */ @@ -28,9 +29,9 @@ static char SCCSid[] = "$SunId$ SGI"; #define ZOOMPCT 9 /* percent to zoom at a time */ -#define MOVPCT 7 /* percent distance to move /frame */ +#define MOVPCT 4 /* percent distance to move /frame */ #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) -#define MOVDEG (-5) /* degrees to orbit CW/down /frame */ +#define MOVDEG (-1.5) /* degrees to orbit CW/down /frame */ #define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) #define BORWIDTH 5 /* border width */ @@ -74,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), \ @@ -92,18 +93,37 @@ int displist; /* our scene display list */ 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(char *nm); +static int varmatch(char *s, 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(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(VIEW *vp); +static int getkey(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(XExposeEvent *eexp); +static void resizewindow(XConfigureEvent *ersz); -main(argc, argv) -int argc; -char *argv[]; + +int +main( + int argc, + char *argv[] +) { char *viewsel = NULL; long vwintvl = 0; @@ -142,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) || @@ -157,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 */ @@ -177,25 +198,31 @@ userr: "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"}; @@ -204,20 +231,21 @@ 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 *cp; char radcomm[256], buf[128], nam[32]; /* set rad commmand */ strcpy(radcomm, "rad -w -v 0 "); /* look out below! */ @@ -244,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 */ } @@ -253,7 +281,7 @@ char **av; eyedist = atof(cp); /* look for materials */ while ((cp = scan4var(buf, sizeof(buf), "materials", fp)) != NULL) { - nscenef += wordstring(scene+nscenef, cp); + nscenef += wordstring(scene+nscenef, MAXSCENE-nscenef, cp); buf[0] = '\0'; } /* look for octree */ @@ -261,7 +289,7 @@ char **av; octree = savqstr(cp); /* look for scene files */ while ((cp = scan4var(buf, sizeof(buf), "scene", fp)) != NULL) { - nscenef += wordstring(scene+nscenef, cp); + nscenef += wordstring(scene+nscenef, MAXSCENE-nscenef, cp); buf[0] = '\0'; } /* load view names */ @@ -276,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", @@ -298,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 */ + char *nm +) { - register int n; + 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++) @@ -321,11 +350,13 @@ register char *nm; } -int -varmatch(s, vn) /* match line to variable */ -register char *s, *vn; +static int +varmatch( /* match line to variable */ + char *s, + char *vn +) { - register int c; + int c; for ( ; *vn && *s == *vn; s++, vn++) ; @@ -339,15 +370,16 @@ 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; + char *cp; /* search out matching line */ while ((cval = varmatch(buf, vname))) { if (cval > 0) /* gone too far? */ @@ -364,8 +396,10 @@ 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, @@ -415,8 +449,8 @@ 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); /* set GLX context */ glXMakeCurrent(ourdisplay, gwind, gctx); @@ -447,13 +481,14 @@ char *id; 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); @@ -464,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 */ + VIEW *nv +) { int newhres = hres, newvres = vres; double wa, va; @@ -475,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) { @@ -498,25 +534,26 @@ register VIEW *nv; newvres = (pwidth/pheight)*va*newhres + .5; } } - if (newhres != hres | newvres != vres) { + if ((newhres != hres) | (newvres != vres)) { no_render++; XResizeWindow(ourdisplay, gwind, newhres, newvres); do dev_input(0); /* get resize event */ - while (newhres != hres | newvres != vres); + 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; @@ -554,7 +591,8 @@ int nsecs; } -render() /* render our display list and swap buffers */ +static void +render(void) /* render our display list and swap buffers */ { double d; @@ -580,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) @@ -604,12 +646,11 @@ int dx, dy, mov, orb; VSUM(nv.vp, wp, odir, -1.); spinvector(nv.vdir, nv.vdir, nv.vup, d); } else if (orb) { /* orbit up/down */ - fcross(v1, odir, nv.vup); - if (normalize(v1) == 0.) + if (geodesic(odir, odir, nv.vup, + d=MOVDEG*PI/180.*orb, GEOD_RAD) == 0.0) return(0); - spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); VSUM(nv.vp, wp, odir, -1.); - spinvector(nv.vdir, nv.vdir, v1, d); + geodesic(nv.vdir, nv.vdir, nv.vup, d, GEOD_RAD); } else if (mov) { /* move forward/backward */ d = MOVPCT/100. * mov; VSUM(nv.vp, nv.vp, odir, d); @@ -626,9 +667,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 = 50000000; + nanosleep(&ts, NULL); +} + + +static void +getmove( /* get view change */ + XButtonPressedEvent *ebut +) +{ int movdir = MOVDIR(ebut->button); int movorb = MOVORB(ebut->state); int moved = 0; @@ -642,6 +695,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)) @@ -662,14 +717,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]; @@ -677,8 +736,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 */ @@ -687,8 +746,10 @@ double md; } -setglpersp(vp) /* set perspective view in GL */ -register VIEW *vp; +static void +setglpersp( /* set perspective view in GL */ + VIEW *vp +) { double d, xmin, xmax, ymin, ymax, zmin, zmax; @@ -719,9 +780,10 @@ register VIEW *vp; } -int -getkey(ekey) /* get input key */ -register XKeyPressedEvent *ekey; +static int +getkey( /* get input key */ + XKeyPressedEvent *ekey +) { int n; char buf[8]; @@ -739,9 +801,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; @@ -775,14 +837,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; @@ -801,8 +865,10 @@ 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++) @@ -814,13 +880,15 @@ int vwnum; } -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; } @@ -841,28 +909,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 */ + XExposeEvent *eexp +) { - if (hres == 0 | vres == 0) { /* first exposure */ + if ((hres == 0) | (vres == 0)) { /* first exposure */ resizewindow((XConfigureEvent *)eexp); return; } @@ -873,8 +945,10 @@ register XExposeEvent *eexp; } -resizewindow(ersz) /* resize window */ -register XConfigureEvent *ersz; +static void +resizewindow( /* resize window */ + XConfigureEvent *ersz +) { static char resizing[] = "resizing window"; double wa, va;