54 |
|
|
55 |
|
static RAY thisray; /* for our convenience */ |
56 |
|
|
57 |
+ |
static FILE *inpfp = NULL; /* input stream pointer */ |
58 |
+ |
|
59 |
+ |
static FVECT *inp_queue = NULL; /* ray input queue if flushing */ |
60 |
+ |
static int inp_qpos = 0; /* next ray to return */ |
61 |
+ |
static int inp_qend = 0; /* number of rays in this work group */ |
62 |
+ |
|
63 |
|
typedef void putf_t(RREAL *v, int n); |
64 |
< |
static putf_t puta, putd, putf; |
64 |
> |
static putf_t puta, putd, putf, putrgbe; |
65 |
|
|
66 |
|
typedef void oputf_t(RAY *r); |
67 |
|
static oputf_t oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp, |
76 |
|
static void rtcompute(FVECT org, FVECT dir, double dmax); |
77 |
|
static int printvals(RAY *r); |
78 |
|
static int getvec(FVECT vec, int fmt, FILE *fp); |
79 |
+ |
static int iszerovec(const FVECT vec); |
80 |
+ |
static double nextray(FVECT org, FVECT dir); |
81 |
|
static void tabin(RAY *r); |
82 |
|
static void ourtrace(RAY *r); |
83 |
|
|
125 |
|
{ |
126 |
|
unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu |
127 |
|
: (unsigned long)vresolu; |
128 |
< |
long nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : hresolu; |
128 |
> |
long nextflush = (!vresolu | (hresolu <= 1)) * hresolu; |
129 |
|
int something2flush = 0; |
130 |
|
FILE *fp; |
131 |
|
double d; |
132 |
|
FVECT orig, direc; |
133 |
|
/* set up input */ |
134 |
|
if (fname == NULL) |
135 |
< |
fp = stdin; |
136 |
< |
else if ((fp = fopen(fname, "r")) == NULL) { |
135 |
> |
inpfp = stdin; |
136 |
> |
else if ((inpfp = fopen(fname, "r")) == NULL) { |
137 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
138 |
|
error(SYSTEM, errmsg); |
139 |
|
} |
140 |
+ |
#ifdef getc_unlocked |
141 |
+ |
flockfile(inpfp); /* avoid lock/unlock overhead */ |
142 |
+ |
flockfile(stdout); |
143 |
+ |
#endif |
144 |
|
if (inform != 'a') |
145 |
< |
SET_FILE_BINARY(fp); |
145 |
> |
SET_FILE_BINARY(inpfp); |
146 |
|
/* set up output */ |
147 |
|
setoutput(outvals); |
148 |
|
if (imm_irrad) |
149 |
|
castonly = 0; |
150 |
< |
else if (castonly) |
150 |
> |
else if (castonly || every_out[0] != NULL) |
151 |
|
nproc = 1; /* don't bother multiprocessing */ |
152 |
|
if ((nextflush > 0) & (nproc > nextflush)) { |
153 |
|
error(WARNING, "reducing number of processes to match flush interval"); |
158 |
|
case 'f': putreal = putf; break; |
159 |
|
case 'd': putreal = putd; break; |
160 |
|
case 'c': |
161 |
< |
if (strcmp(outvals, "v")) |
162 |
< |
error(USER, "color format with value output only"); |
163 |
< |
break; |
161 |
> |
if (outvals[0] && (outvals[1] || !strchr("vrx", outvals[0]))) |
162 |
> |
error(USER, "color format only with -ov, -or, -ox"); |
163 |
> |
putreal = putrgbe; break; |
164 |
|
default: |
165 |
|
error(CONSISTENCY, "botched output format"); |
166 |
|
} |
174 |
|
else |
175 |
|
fflush(stdout); |
176 |
|
} |
177 |
< |
/* process file */ |
178 |
< |
while (getvec(orig, inform, fp) == 0 && |
179 |
< |
getvec(direc, inform, fp) == 0) { |
180 |
< |
|
169 |
< |
d = normalize(direc); |
170 |
< |
if (d == 0.0) { /* zero ==> flush */ |
171 |
< |
if ((--nextflush <= 0) | !vcount && something2flush) { |
177 |
> |
/* process input rays */ |
178 |
> |
while ((d = nextray(orig, direc)) >= 0.0) { |
179 |
> |
if (d == 0.0) { /* flush request? */ |
180 |
> |
if (something2flush) { |
181 |
|
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
182 |
|
error(USER, "child(ren) died"); |
183 |
|
bogusray(); |
184 |
|
fflush(stdout); |
185 |
+ |
nextflush = (!vresolu | (hresolu <= 1)) * hresolu; |
186 |
|
something2flush = 0; |
177 |
– |
nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : |
178 |
– |
hresolu; |
187 |
|
} else |
188 |
|
bogusray(); |
189 |
|
} else { /* compute and print */ |
190 |
|
rtcompute(orig, direc, lim_dist ? d : 0.0); |
191 |
< |
/* flush if time */ |
184 |
< |
if (!--nextflush) { |
191 |
> |
if (!--nextflush) { /* flush if time */ |
192 |
|
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
193 |
|
error(USER, "child(ren) died"); |
194 |
|
fflush(stdout); |
206 |
|
error(USER, "unable to complete processing"); |
207 |
|
ray_pclose(0); |
208 |
|
} |
209 |
+ |
if (vcount) |
210 |
+ |
error(WARNING, "unexpected EOF on input"); |
211 |
|
if (fflush(stdout) < 0) |
212 |
|
error(SYSTEM, "write error"); |
213 |
< |
if (vcount) |
214 |
< |
error(USER, "unexpected EOF on input"); |
215 |
< |
if (fname != NULL) |
216 |
< |
fclose(fp); |
213 |
> |
if (fname != NULL) { |
214 |
> |
fclose(inpfp); |
215 |
> |
inpfp = NULL; |
216 |
> |
} |
217 |
> |
nextray(NULL, NULL); |
218 |
|
} |
219 |
|
|
220 |
|
|
277 |
|
break; |
278 |
|
case 'V': /* contribution */ |
279 |
|
*table++ = oputV; |
270 |
– |
castonly = 0; |
280 |
|
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
281 |
|
error(WARNING, |
282 |
|
"-otV accuracy depends on -aa 0 -as 0"); |
310 |
|
case 'W': /* coefficient */ |
311 |
|
*table++ = oputW; |
312 |
|
castonly = 0; |
313 |
< |
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
313 |
> |
if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0)) |
314 |
|
error(WARNING, |
315 |
|
"-otW accuracy depends on -aa 0 -as 0"); |
316 |
|
break; |
325 |
|
break; |
326 |
|
} |
327 |
|
*table = NULL; |
328 |
+ |
/* compatibility */ |
329 |
+ |
for (table = ray_out; *table != NULL; table++) { |
330 |
+ |
if ((*table == oputV) | (*table == oputW)) |
331 |
+ |
error(WARNING, "-oVW options require trace mode"); |
332 |
+ |
if ((do_irrad | imm_irrad) && |
333 |
+ |
(*table == oputr) | (*table == oputR) | |
334 |
+ |
(*table == oputx) | (*table == oputX)) |
335 |
+ |
error(WARNING, "-orRxX options incompatible with -I+ and -i+"); |
336 |
+ |
} |
337 |
|
} |
338 |
|
|
339 |
|
|
432 |
|
|
433 |
|
|
434 |
|
static int |
435 |
< |
getvec( /* get a vector from fp */ |
435 |
> |
getvec( /* get a vector from fp */ |
436 |
|
FVECT vec, |
437 |
|
int fmt, |
438 |
|
FILE *fp |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
+ |
static int |
473 |
+ |
iszerovec(const FVECT vec) |
474 |
+ |
{ |
475 |
+ |
return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0); |
476 |
+ |
} |
477 |
+ |
|
478 |
+ |
|
479 |
+ |
static double |
480 |
+ |
nextray( /* return next ray in work group (-1.0 if EOF) */ |
481 |
+ |
FVECT org, |
482 |
+ |
FVECT dir |
483 |
+ |
) |
484 |
+ |
{ |
485 |
+ |
const size_t qlength = !vresolu * hresolu; |
486 |
+ |
|
487 |
+ |
if ((org == NULL) | (dir == NULL)) { |
488 |
+ |
if (inp_queue != NULL) /* asking to free queue */ |
489 |
+ |
free(inp_queue); |
490 |
+ |
inp_queue = NULL; |
491 |
+ |
inp_qpos = inp_qend = 0; |
492 |
+ |
return(-1.); |
493 |
+ |
} |
494 |
+ |
if (!inp_qend) { /* initialize FIFO queue */ |
495 |
+ |
int rsiz = 6*20; /* conservative ascii ray size */ |
496 |
+ |
if (inform == 'f') rsiz = 6*sizeof(float); |
497 |
+ |
else if (inform == 'd') rsiz = 6*sizeof(double); |
498 |
+ |
if ((inpfp == stdin) & (qlength*rsiz > 512)) /* pipe limit */ |
499 |
+ |
inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength); |
500 |
+ |
inp_qend = -(inp_queue == NULL); /* flag for no queue */ |
501 |
+ |
} |
502 |
+ |
if (inp_qend < 0) { /* not queuing? */ |
503 |
+ |
if (getvec(org, inform, inpfp) < 0 || |
504 |
+ |
getvec(dir, inform, inpfp) < 0) |
505 |
+ |
return(-1.); |
506 |
+ |
return normalize(dir); |
507 |
+ |
} |
508 |
+ |
if (inp_qpos >= inp_qend) { /* need to refill input queue? */ |
509 |
+ |
for (inp_qend = 0; inp_qend < qlength; inp_qend++) { |
510 |
+ |
if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0 |
511 |
+ |
|| getvec(inp_queue[2*inp_qend+1], |
512 |
+ |
inform, inpfp) < 0) |
513 |
+ |
break; /* hit EOF */ |
514 |
+ |
if (iszerovec(inp_queue[2*inp_qend+1])) { |
515 |
+ |
++inp_qend; /* flush request */ |
516 |
+ |
break; |
517 |
+ |
} |
518 |
+ |
} |
519 |
+ |
inp_qpos = 0; |
520 |
+ |
} |
521 |
+ |
if (inp_qpos >= inp_qend) /* unexpected EOF? */ |
522 |
+ |
return(-1.); |
523 |
+ |
VCOPY(org, inp_queue[2*inp_qpos]); |
524 |
+ |
VCOPY(dir, inp_queue[2*inp_qpos+1]); |
525 |
+ |
++inp_qpos; |
526 |
+ |
return normalize(dir); |
527 |
+ |
} |
528 |
+ |
|
529 |
+ |
|
530 |
|
void |
531 |
|
tranotify( /* record new modifier */ |
532 |
|
OBJECT obj |
662 |
|
{ |
663 |
|
RREAL cval[3]; |
664 |
|
|
589 |
– |
if (outform == 'c') { |
590 |
– |
COLR cout; |
591 |
– |
setcolr(cout, colval(r->rcol,RED), |
592 |
– |
colval(r->rcol,GRN), |
593 |
– |
colval(r->rcol,BLU)); |
594 |
– |
putbinary(cout, sizeof(cout), 1, stdout); |
595 |
– |
return; |
596 |
– |
} |
665 |
|
cval[0] = colval(r->rcol,RED); |
666 |
|
cval[1] = colval(r->rcol,GRN); |
667 |
|
cval[2] = colval(r->rcol,BLU); |
719 |
|
RAY *r |
720 |
|
) |
721 |
|
{ |
722 |
< |
if (r->rot < FHUGE) |
722 |
> |
if (r->rot < FHUGE*.99) |
723 |
|
(*putreal)(r->rop, 3); |
724 |
|
else |
725 |
|
(*putreal)(vdummy, 3); |
731 |
|
RAY *r |
732 |
|
) |
733 |
|
{ |
734 |
< |
if (r->rot < FHUGE) |
735 |
< |
(*putreal)(r->ron, 3); |
736 |
< |
else |
734 |
> |
if (r->rot < FHUGE*.99) { |
735 |
> |
if (r->rflips & 1) { /* undo any flippin' flips */ |
736 |
> |
FVECT unrm; |
737 |
> |
unrm[0] = -r->ron[0]; |
738 |
> |
unrm[1] = -r->ron[1]; |
739 |
> |
unrm[2] = -r->ron[2]; |
740 |
> |
(*putreal)(unrm, 3); |
741 |
> |
} else |
742 |
> |
(*putreal)(r->ron, 3); |
743 |
> |
} else |
744 |
|
(*putreal)(vdummy, 3); |
745 |
|
} |
746 |
|
|
752 |
|
{ |
753 |
|
FVECT pnorm; |
754 |
|
|
755 |
< |
if (r->rot >= FHUGE) { |
755 |
> |
if (r->rot >= FHUGE*.99) { |
756 |
|
(*putreal)(vdummy, 3); |
757 |
|
return; |
758 |
|
} |
859 |
|
|
860 |
|
|
861 |
|
static void |
862 |
< |
putd(RREAL *v, int n) /* print binary double(s) */ |
862 |
> |
putd(RREAL *v, int n) /* output binary double(s) */ |
863 |
|
{ |
864 |
|
#ifdef SMLFLT |
865 |
|
double da[3]; |
877 |
|
|
878 |
|
|
879 |
|
static void |
880 |
< |
putf(RREAL *v, int n) /* print binary float(s) */ |
880 |
> |
putf(RREAL *v, int n) /* output binary float(s) */ |
881 |
|
{ |
882 |
|
#ifndef SMLFLT |
883 |
|
float fa[3]; |
891 |
|
#else |
892 |
|
putbinary(v, sizeof(RREAL), n, stdout); |
893 |
|
#endif |
894 |
+ |
} |
895 |
+ |
|
896 |
+ |
|
897 |
+ |
static void |
898 |
+ |
putrgbe(RREAL *v, int n) /* output RGBE color */ |
899 |
+ |
{ |
900 |
+ |
COLR cout; |
901 |
+ |
|
902 |
+ |
if (n != 3) |
903 |
+ |
error(INTERNAL, "putrgbe() not called with 3 components"); |
904 |
+ |
setcolr(cout, v[0], v[1], v[2]); |
905 |
+ |
putbinary(cout, sizeof(cout), 1, stdout); |
906 |
|
} |