1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
16 |
|
* xorg yorg zorg xdir ydir zdir |
17 |
|
* |
18 |
|
* The direction need not be normalized. Output is flexible. |
19 |
+ |
* If the direction vector is (0,0,0), then the output is flushed. |
20 |
|
* All values default to ascii representation of real |
21 |
|
* numbers. Binary representations can be selected |
22 |
|
* with '-ff' for float or '-fd' for double. By default, |
23 |
< |
* radiance is computed. The '-i' option indicates that |
23 |
> |
* radiance is computed. The '-i' or '-I' options indicate that |
24 |
|
* irradiance values are desired. |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "ray.h" |
28 |
|
|
29 |
+ |
#include "octree.h" |
30 |
+ |
|
31 |
|
#include "otypes.h" |
32 |
|
|
33 |
+ |
#include "resolu.h" |
34 |
+ |
|
35 |
+ |
int dimlist[MAXDIM]; /* sampling dimensions */ |
36 |
+ |
int ndims = 0; /* number of sampling dimensions */ |
37 |
+ |
int samplendx = 0; /* index for this sample */ |
38 |
+ |
|
39 |
+ |
int imm_irrad = 0; /* compute immediate irradiance? */ |
40 |
+ |
|
41 |
|
int inform = 'a'; /* input format */ |
42 |
|
int outform = 'a'; /* output format */ |
43 |
|
char *outvals = "v"; /* output specification */ |
44 |
|
|
45 |
+ |
char *tralist[128]; /* list of modifers to trace (or no) */ |
46 |
+ |
int traincl = -1; /* include == 1, exclude == 0 */ |
47 |
+ |
#define MAXTSET 511 /* maximum number in trace set */ |
48 |
+ |
OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */ |
49 |
+ |
|
50 |
|
int hresolu = 0; /* horizontal (scan) size */ |
51 |
|
int vresolu = 0; /* vertical resolution */ |
52 |
|
|
53 |
|
double dstrsrc = 0.0; /* square source distribution */ |
54 |
|
double shadthresh = .05; /* shadow threshold */ |
55 |
|
double shadcert = .5; /* shadow certainty */ |
56 |
+ |
int directrelay = 1; /* number of source relays */ |
57 |
+ |
int vspretest = 512; /* virtual source pretest density */ |
58 |
+ |
int directvis = 1; /* sources visible? */ |
59 |
+ |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
60 |
|
|
61 |
+ |
double specthresh = .15; /* specular sampling threshold */ |
62 |
+ |
double specjitter = 1.; /* specular sampling jitter */ |
63 |
+ |
|
64 |
|
int maxdepth = 6; /* maximum recursion depth */ |
65 |
|
double minweight = 4e-3; /* minimum ray weight */ |
66 |
|
|
67 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
68 |
|
double ambacc = 0.2; /* ambient accuracy */ |
69 |
< |
int ambres = 128; /* ambient resolution */ |
69 |
> |
int ambres = 32; /* ambient resolution */ |
70 |
|
int ambdiv = 128; /* ambient divisions */ |
71 |
|
int ambssamp = 0; /* ambient super-samples */ |
72 |
|
int ambounce = 0; /* ambient bounces */ |
73 |
|
char *amblist[128]; /* ambient include/exclude list */ |
74 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
75 |
|
|
76 |
+ |
extern OBJREC Lamb; /* a Lambertian surface */ |
77 |
+ |
|
78 |
|
static RAY thisray; /* for our convenience */ |
79 |
|
|
80 |
< |
extern int oputo(), oputd(), oputv(), oputl(), |
81 |
< |
oputp(), oputn(), oputs(), oputw(), oputm(); |
80 |
> |
static int oputo(), oputd(), oputv(), oputl(), oputL(), |
81 |
> |
oputp(), oputn(), oputN(), oputs(), oputw(), oputm(); |
82 |
|
|
83 |
< |
static int (*ray_out[10])(), (*every_out[10])(); |
83 |
> |
static int ourtrace(), tabin(); |
84 |
> |
static int (*ray_out[16])(), (*every_out[16])(); |
85 |
> |
static int castonly = 0; |
86 |
|
|
87 |
< |
extern int puta(), putf(), putd(); |
87 |
> |
static int puta(), putf(), putd(); |
88 |
|
|
89 |
|
static int (*putreal)(); |
90 |
|
|
92 |
|
quit(code) /* quit program */ |
93 |
|
int code; |
94 |
|
{ |
95 |
+ |
#ifndef NIX |
96 |
+ |
headclean(); /* delete header file */ |
97 |
+ |
pfclean(); /* clean up persist files */ |
98 |
+ |
#endif |
99 |
|
exit(code); |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
+ |
char * |
104 |
+ |
formstr(f) /* return format identifier */ |
105 |
+ |
int f; |
106 |
+ |
{ |
107 |
+ |
switch (f) { |
108 |
+ |
case 'a': return("ascii"); |
109 |
+ |
case 'f': return("float"); |
110 |
+ |
case 'd': return("double"); |
111 |
+ |
case 'c': return(COLRFMT); |
112 |
+ |
} |
113 |
+ |
return("unknown"); |
114 |
+ |
} |
115 |
+ |
|
116 |
+ |
|
117 |
|
rtrace(fname) /* trace rays from file */ |
118 |
|
char *fname; |
119 |
|
{ |
128 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
129 |
|
error(SYSTEM, errmsg); |
130 |
|
} |
131 |
+ |
#ifdef MSDOS |
132 |
+ |
if (inform != 'a') |
133 |
+ |
setmode(fileno(fp), O_BINARY); |
134 |
+ |
#endif |
135 |
|
/* set up output */ |
136 |
< |
setoutput(outvals); |
136 |
> |
if (imm_irrad) |
137 |
> |
outvals = "v"; |
138 |
> |
else |
139 |
> |
setoutput(outvals); |
140 |
|
switch (outform) { |
141 |
|
case 'a': putreal = puta; break; |
142 |
|
case 'f': putreal = putf; break; |
143 |
|
case 'd': putreal = putd; break; |
144 |
+ |
case 'c': |
145 |
+ |
if (strcmp(outvals, "v")) |
146 |
+ |
error(USER, "color format with value output only"); |
147 |
+ |
break; |
148 |
+ |
default: |
149 |
+ |
error(CONSISTENCY, "botched output format"); |
150 |
|
} |
151 |
+ |
if (hresolu > 0) { |
152 |
+ |
if (vresolu > 0) |
153 |
+ |
fprtresolu(hresolu, vresolu, stdout); |
154 |
+ |
fflush(stdout); |
155 |
+ |
} |
156 |
|
/* process file */ |
157 |
|
while (getvec(orig, inform, fp) == 0 && |
158 |
|
getvec(direc, inform, fp) == 0) { |
159 |
|
|
160 |
< |
if (normalize(direc) == 0.0) |
161 |
< |
error(USER, "zero direction vector"); |
160 |
> |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
161 |
> |
fflush(stdout); |
162 |
> |
continue; |
163 |
> |
} |
164 |
> |
samplendx++; |
165 |
|
/* compute and print */ |
166 |
< |
if (outvals[0] == 'i') |
166 |
> |
if (imm_irrad) |
167 |
|
irrad(orig, direc); |
168 |
|
else |
169 |
< |
radiance(orig, direc); |
170 |
< |
/* flush if requested */ |
169 |
> |
traceray(orig, direc); |
170 |
> |
/* flush if time */ |
171 |
|
if (--nextflush == 0) { |
172 |
|
fflush(stdout); |
173 |
|
nextflush = hresolu; |
177 |
|
if (--vcount == 0) /* check for end */ |
178 |
|
break; |
179 |
|
} |
180 |
+ |
fflush(stdout); |
181 |
|
if (vcount > 0) |
182 |
|
error(USER, "read error"); |
183 |
< |
fclose(fp); |
183 |
> |
if (fname != NULL) |
184 |
> |
fclose(fp); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
setoutput(vs) /* set up output tables */ |
189 |
|
register char *vs; |
190 |
|
{ |
191 |
< |
extern int ourtrace(), (*trace)(); |
191 |
> |
extern int (*trace)(); |
192 |
|
register int (**table)() = ray_out; |
193 |
|
|
194 |
+ |
castonly = 1; |
195 |
|
while (*vs) |
196 |
|
switch (*vs++) { |
197 |
|
case 't': /* trace */ |
198 |
|
*table = NULL; |
199 |
|
table = every_out; |
200 |
|
trace = ourtrace; |
201 |
+ |
castonly = 0; |
202 |
|
break; |
203 |
|
case 'o': /* origin */ |
204 |
|
*table++ = oputo; |
208 |
|
break; |
209 |
|
case 'v': /* value */ |
210 |
|
*table++ = oputv; |
211 |
+ |
castonly = 0; |
212 |
|
break; |
213 |
< |
case 'l': /* length */ |
213 |
> |
case 'l': /* effective distance */ |
214 |
|
*table++ = oputl; |
215 |
+ |
castonly = 0; |
216 |
|
break; |
217 |
+ |
case 'L': /* single ray length */ |
218 |
+ |
*table++ = oputL; |
219 |
+ |
break; |
220 |
|
case 'p': /* point */ |
221 |
|
*table++ = oputp; |
222 |
|
break; |
223 |
< |
case 'n': /* normal */ |
223 |
> |
case 'n': /* perturbed normal */ |
224 |
|
*table++ = oputn; |
225 |
+ |
castonly = 0; |
226 |
|
break; |
227 |
+ |
case 'N': /* unperturbed normal */ |
228 |
+ |
*table++ = oputN; |
229 |
+ |
break; |
230 |
|
case 's': /* surface */ |
231 |
|
*table++ = oputs; |
232 |
|
break; |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
< |
radiance(org, dir) /* compute radiance value */ |
244 |
> |
traceray(org, dir) /* compute and print ray value(s) */ |
245 |
|
FVECT org, dir; |
246 |
|
{ |
247 |
|
register int (**tp)(); |
249 |
|
VCOPY(thisray.rorg, org); |
250 |
|
VCOPY(thisray.rdir, dir); |
251 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
252 |
< |
rayvalue(&thisray); |
252 |
> |
if (castonly) |
253 |
> |
localhit(&thisray, &thescene) || sourcehit(&thisray); |
254 |
> |
else |
255 |
> |
rayvalue(&thisray); |
256 |
|
|
257 |
|
if (ray_out[0] == NULL) |
258 |
|
return; |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
< |
irrad(org, dir) /* compute irradiance value */ |
266 |
> |
irrad(org, dir) /* compute immediate irradiance value */ |
267 |
|
FVECT org, dir; |
268 |
|
{ |
187 |
– |
static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
188 |
– |
static OBJREC Lamb = { |
189 |
– |
OVOID, MAT_PLASTIC, "Lambertian", |
190 |
– |
{0, 5, NULL, Lambfa}, NULL, -1, |
191 |
– |
}; |
269 |
|
register int i; |
270 |
|
|
271 |
|
for (i = 0; i < 3; i++) { |
292 |
|
int fmt; |
293 |
|
FILE *fp; |
294 |
|
{ |
295 |
+ |
extern char *fgetword(); |
296 |
|
static float vf[3]; |
297 |
+ |
static double vd[3]; |
298 |
+ |
char buf[32]; |
299 |
+ |
register int i; |
300 |
|
|
301 |
|
switch (fmt) { |
302 |
|
case 'a': /* ascii */ |
303 |
< |
if (fscanf(fp, "%lf %lf %lf", vec, vec+1, vec+2) != 3) |
304 |
< |
return(-1); |
303 |
> |
for (i = 0; i < 3; i++) { |
304 |
> |
if (fgetword(buf, sizeof(buf), fp) == NULL || |
305 |
> |
!isflt(buf)) |
306 |
> |
return(-1); |
307 |
> |
vec[i] = atof(buf); |
308 |
> |
} |
309 |
|
break; |
310 |
|
case 'f': /* binary float */ |
311 |
< |
if (fread(vf, sizeof(float), 3, fp) != 3) |
311 |
> |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
312 |
|
return(-1); |
313 |
|
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
314 |
|
break; |
315 |
|
case 'd': /* binary double */ |
316 |
< |
if (fread(vec, sizeof(double), 3, fp) != 3) |
316 |
> |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
317 |
|
return(-1); |
318 |
+ |
vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2]; |
319 |
|
break; |
320 |
+ |
default: |
321 |
+ |
error(CONSISTENCY, "botched input format"); |
322 |
|
} |
323 |
|
return(0); |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
+ |
tranotify(obj) /* record new modifier */ |
328 |
+ |
OBJECT obj; |
329 |
+ |
{ |
330 |
+ |
static int hitlimit = 0; |
331 |
+ |
register OBJREC *o = objptr(obj); |
332 |
+ |
register char **tralp; |
333 |
+ |
|
334 |
+ |
if (hitlimit || !ismodifier(o->otype)) |
335 |
+ |
return; |
336 |
+ |
for (tralp = tralist; *tralp != NULL; tralp++) |
337 |
+ |
if (!strcmp(o->oname, *tralp)) { |
338 |
+ |
if (traset[0] >= MAXTSET) { |
339 |
+ |
error(WARNING, "too many modifiers in trace list"); |
340 |
+ |
hitlimit++; |
341 |
+ |
return; /* should this be fatal? */ |
342 |
+ |
} |
343 |
+ |
insertelem(traset, obj); |
344 |
+ |
return; |
345 |
+ |
} |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
|
349 |
|
static |
350 |
|
ourtrace(r) /* print ray values */ |
351 |
|
RAY *r; |
354 |
|
|
355 |
|
if (every_out[0] == NULL) |
356 |
|
return; |
357 |
+ |
if (traincl == 1 && r->ro == NULL) |
358 |
+ |
return; |
359 |
+ |
if (traincl != -1 && traincl != inset(traset, r->ro->omod)) |
360 |
+ |
return; |
361 |
|
tabin(r); |
362 |
|
for (tp = every_out; *tp != NULL; tp++) |
363 |
|
(**tp)(r); |
400 |
|
oputv(r) /* print value */ |
401 |
|
register RAY *r; |
402 |
|
{ |
403 |
+ |
COLR cout; |
404 |
+ |
|
405 |
+ |
if (outform == 'c') { |
406 |
+ |
setcolr(cout, colval(r->rcol,RED), |
407 |
+ |
colval(r->rcol,GRN), |
408 |
+ |
colval(r->rcol,BLU)); |
409 |
+ |
fwrite((char *)cout, sizeof(cout), 1, stdout); |
410 |
+ |
return; |
411 |
+ |
} |
412 |
|
(*putreal)(colval(r->rcol,RED)); |
413 |
|
(*putreal)(colval(r->rcol,GRN)); |
414 |
|
(*putreal)(colval(r->rcol,BLU)); |
416 |
|
|
417 |
|
|
418 |
|
static |
419 |
< |
oputl(r) /* print length */ |
419 |
> |
oputl(r) /* print effective distance */ |
420 |
|
register RAY *r; |
421 |
|
{ |
422 |
< |
if (r->rot < FHUGE) |
300 |
< |
(*putreal)(r->rot); |
301 |
< |
else |
302 |
< |
(*putreal)(0.0); |
422 |
> |
(*putreal)(r->rt); |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
|
static |
427 |
+ |
oputL(r) /* print single ray length */ |
428 |
+ |
register RAY *r; |
429 |
+ |
{ |
430 |
+ |
(*putreal)(r->rot); |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
|
434 |
+ |
static |
435 |
|
oputp(r) /* print point */ |
436 |
|
register RAY *r; |
437 |
|
{ |
448 |
|
|
449 |
|
|
450 |
|
static |
451 |
< |
oputn(r) /* print normal */ |
451 |
> |
oputN(r) /* print unperturbed normal */ |
452 |
|
register RAY *r; |
453 |
|
{ |
454 |
|
if (r->rot < FHUGE) { |
464 |
|
|
465 |
|
|
466 |
|
static |
467 |
+ |
oputn(r) /* print perturbed normal */ |
468 |
+ |
RAY *r; |
469 |
+ |
{ |
470 |
+ |
FVECT pnorm; |
471 |
+ |
|
472 |
+ |
if (r->rot >= FHUGE) { |
473 |
+ |
(*putreal)(0.0); |
474 |
+ |
(*putreal)(0.0); |
475 |
+ |
(*putreal)(0.0); |
476 |
+ |
return; |
477 |
+ |
} |
478 |
+ |
raynormal(pnorm, r); |
479 |
+ |
(*putreal)(pnorm[0]); |
480 |
+ |
(*putreal)(pnorm[1]); |
481 |
+ |
(*putreal)(pnorm[2]); |
482 |
+ |
} |
483 |
+ |
|
484 |
+ |
|
485 |
+ |
static |
486 |
|
oputs(r) /* print name */ |
487 |
|
register RAY *r; |
488 |
|
{ |
526 |
|
putd(v) /* print binary double */ |
527 |
|
double v; |
528 |
|
{ |
529 |
< |
fwrite(&v, sizeof(v), 1, stdout); |
529 |
> |
fwrite((char *)&v, sizeof(v), 1, stdout); |
530 |
|
} |
531 |
|
|
532 |
|
|
536 |
|
{ |
537 |
|
float f = v; |
538 |
|
|
539 |
< |
fwrite(&f, sizeof(f), 1, stdout); |
539 |
> |
fwrite((char *)&f, sizeof(f), 1, stdout); |
540 |
|
} |