--- ray/src/util/vwrays.c 1997/10/17 10:42:45 3.3 +++ ray/src/util/vwrays.c 2004/03/26 23:34:23 3.10 @@ -1,24 +1,23 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: vwrays.c,v 3.10 2004/03/26 23:34:23 schorsch Exp $"; #endif - /* * Compute rays corresponding to a given picture or view. */ - +#include "platform.h" #include "standard.h" - #include "view.h" -#include "resolu.h" +typedef void putfunc(FVECT ro, FVECT rd); +static putfunc puta; +static putfunc putf; +static putfunc putd; +static void pix2rays(FILE *fp); +static void putrays(void); -extern int putf(), putd(), puta(); +static putfunc *putr = puta; -int (*putr)() = puta; - VIEW vw = STDVIEW; RESOLU rs = {PIXSTANDARD, 512, 512}; @@ -27,12 +26,16 @@ double pa = 1.; int zfd = -1; +int fromstdin = 0; + char *progname; -main(argc, argv) -int argc; -char *argv[]; +int +main( + int argc, + char *argv[] +) { char *err; int rval, getdim = 0; @@ -96,10 +99,13 @@ char *argv[]; case 'p': /* pixel aspect ratio */ pa = atof(argv[++i]); break; + case 'i': /* get pixels from stdin */ + fromstdin = 1; + break; default: goto userr; } - if (i > argc | i+2 < argc) + if ((i > argc) | (i+2 < argc)) goto userr; if (i < argc) { rval = viewfile(argv[i], &vw, &rs); @@ -128,21 +134,78 @@ char *argv[]; vw.vaft > FTINY ? '+' : '-'); exit(0); } - putrays(); + if (fromstdin) + pix2rays(stdin); + else + putrays(); exit(0); userr: fprintf(stderr, - "Usage: %s [ -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", + "Usage: %s [ -i -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", progname); exit(1); } -putrays() +static void +pix2rays( + FILE *fp +) { - static FLOAT loc[2]; static FVECT rorg, rdir; - float *zbuf; + float zval; + double px, py; + int pp[2]; + double d; + register int i; + + while (fscanf(fp, "%lf %lf", &px, &py) == 2) { + if (px < 0 || px >= rs.xr || + py < 0 || py >= rs.yr) { + fprintf(stderr, + "%s: (x,y) pair (%.0f,%.0f) out of range\n", + progname, px, py); + exit(1); + } + if (zfd >= 0) { + loc2pix(pp, &rs, px/rs.xr, py/rs.yr); + if (lseek(zfd, + (pp[1]*scanlen(&rs)+pp[0])*sizeof(float), + SEEK_SET) < 0 || + read(zfd, &zval, sizeof(float)) + < sizeof(float)) { + fprintf(stderr, "%s: depth buffer read error\n", + progname); + exit(1); + } + } + d = viewray(rorg, rdir, &vw, px/rs.xr, py/rs.yr); + if (d < -FTINY) + rorg[0] = rorg[1] = rorg[2] = + rdir[0] = rdir[1] = rdir[2] = 0.; + else if (zfd >= 0) + for (i = 0; i < 3; i++) { + rorg[i] += rdir[i]*zval; + rdir[i] = -rdir[i]; + } + else if (d > FTINY) { + rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; + } + (*putr)(rorg, rdir); + } + if (!feof(fp)) { + fprintf(stderr, "%s: expected px py on input\n", progname); + exit(1); + } +} + + +static void +putrays(void) +{ + static RREAL loc[2]; + static FVECT rorg, rdir; + float *zbuf = NULL; int sc; double d; register int si, i; @@ -181,12 +244,15 @@ putrays() } } if (zfd >= 0) - free((char *)zbuf); + free((void *)zbuf); } -puta(ro, rd) /* put out ray in ASCII format */ -FVECT ro, rd; +static void +puta( /* put out ray in ASCII format */ + FVECT ro, + FVECT rd +) { printf("%.5e %.5e %.5e %.5e %.5e %.5e\n", ro[0], ro[1], ro[2], @@ -194,8 +260,11 @@ FVECT ro, rd; } -putf(ro, rd) /* put out ray in float format */ -FVECT ro, rd; +static void +putf( /* put out ray in float format */ + FVECT ro, + FVECT rd +) { float v[6]; @@ -205,8 +274,11 @@ FVECT ro, rd; } -putd(ro, rd) /* put out ray in double format */ -FVECT ro, rd; +static void +putd( /* put out ray in double format */ + FVECT ro, + FVECT rd +) { double v[6];