1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1994 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 = 2; /* number of source relays */ |
57 |
+ |
int vspretest = 512; /* virtual source pretest density */ |
58 |
+ |
int directvis = 1; /* sources visible? */ |
59 |
+ |
double srcsizerat = .2; /* maximum ratio source size/dist. */ |
60 |
|
|
61 |
+ |
double specthresh = .15; /* specular sampling threshold */ |
62 |
+ |
double specjitter = 1.; /* specular sampling jitter */ |
63 |
+ |
|
64 |
+ |
int backvis = 1; /* back face visibility */ |
65 |
+ |
|
66 |
|
int maxdepth = 6; /* maximum recursion depth */ |
67 |
|
double minweight = 4e-3; /* minimum ray weight */ |
68 |
|
|
69 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
70 |
|
double ambacc = 0.2; /* ambient accuracy */ |
71 |
|
int ambres = 128; /* ambient resolution */ |
72 |
< |
int ambdiv = 128; /* ambient divisions */ |
72 |
> |
int ambdiv = 512; /* ambient divisions */ |
73 |
|
int ambssamp = 0; /* ambient super-samples */ |
74 |
|
int ambounce = 0; /* ambient bounces */ |
75 |
|
char *amblist[128]; /* ambient include/exclude list */ |
76 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
77 |
|
|
78 |
+ |
extern OBJREC Lamb; /* a Lambertian surface */ |
79 |
+ |
|
80 |
|
static RAY thisray; /* for our convenience */ |
81 |
|
|
82 |
< |
extern int oputo(), oputd(), oputv(), oputl(), |
83 |
< |
oputp(), oputn(), oputs(), oputw(), oputm(); |
82 |
> |
static int oputo(), oputd(), oputv(), oputl(), oputL(), |
83 |
> |
oputp(), oputn(), oputN(), oputs(), oputw(), oputm(); |
84 |
|
|
85 |
< |
static int (*ray_out[10])(), (*every_out[10])(); |
85 |
> |
static int ourtrace(), tabin(); |
86 |
> |
static int (*ray_out[16])(), (*every_out[16])(); |
87 |
> |
static int castonly = 0; |
88 |
|
|
89 |
< |
extern int puta(), putf(), putd(); |
89 |
> |
static int puta(), putf(), putd(); |
90 |
|
|
91 |
|
static int (*putreal)(); |
92 |
|
|
94 |
|
quit(code) /* quit program */ |
95 |
|
int code; |
96 |
|
{ |
97 |
+ |
#ifndef NIX |
98 |
+ |
headclean(); /* delete header file */ |
99 |
+ |
pfclean(); /* clean up persist files */ |
100 |
+ |
#endif |
101 |
|
exit(code); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
+ |
char * |
106 |
+ |
formstr(f) /* return format identifier */ |
107 |
+ |
int f; |
108 |
+ |
{ |
109 |
+ |
switch (f) { |
110 |
+ |
case 'a': return("ascii"); |
111 |
+ |
case 'f': return("float"); |
112 |
+ |
case 'd': return("double"); |
113 |
+ |
case 'c': return(COLRFMT); |
114 |
+ |
} |
115 |
+ |
return("unknown"); |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
|
119 |
|
rtrace(fname) /* trace rays from file */ |
120 |
|
char *fname; |
121 |
|
{ |
122 |
|
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
123 |
|
long nextflush = hresolu; |
124 |
|
FILE *fp; |
125 |
< |
FVECT orig, direc, vcol; |
125 |
> |
FVECT orig, direc; |
126 |
|
/* set up input */ |
127 |
|
if (fname == NULL) |
128 |
|
fp = stdin; |
130 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
131 |
|
error(SYSTEM, errmsg); |
132 |
|
} |
133 |
+ |
#ifdef MSDOS |
134 |
+ |
if (inform != 'a') |
135 |
+ |
setmode(fileno(fp), O_BINARY); |
136 |
+ |
#endif |
137 |
|
/* set up output */ |
138 |
|
setoutput(outvals); |
139 |
|
switch (outform) { |
140 |
|
case 'a': putreal = puta; break; |
141 |
|
case 'f': putreal = putf; break; |
142 |
|
case 'd': putreal = putd; break; |
143 |
+ |
case 'c': |
144 |
+ |
if (strcmp(outvals, "v")) |
145 |
+ |
error(USER, "color format with value output only"); |
146 |
+ |
break; |
147 |
+ |
default: |
148 |
+ |
error(CONSISTENCY, "botched output format"); |
149 |
|
} |
150 |
+ |
if (hresolu > 0) { |
151 |
+ |
if (vresolu > 0) |
152 |
+ |
fprtresolu(hresolu, vresolu, stdout); |
153 |
+ |
fflush(stdout); |
154 |
+ |
} |
155 |
|
/* process file */ |
156 |
|
while (getvec(orig, inform, fp) == 0 && |
157 |
|
getvec(direc, inform, fp) == 0) { |
158 |
|
|
159 |
< |
if (normalize(direc) == 0.0) |
160 |
< |
error(USER, "zero direction vector"); |
159 |
> |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
160 |
> |
fflush(stdout); |
161 |
> |
continue; |
162 |
> |
} |
163 |
> |
samplendx++; |
164 |
|
/* compute and print */ |
165 |
< |
if (outvals[0] == 'i') |
165 |
> |
if (imm_irrad) |
166 |
|
irrad(orig, direc); |
167 |
|
else |
168 |
< |
radiance(orig, direc); |
169 |
< |
/* flush if requested */ |
168 |
> |
rad(orig, direc); |
169 |
> |
/* flush if time */ |
170 |
|
if (--nextflush == 0) { |
171 |
|
fflush(stdout); |
172 |
|
nextflush = hresolu; |
176 |
|
if (--vcount == 0) /* check for end */ |
177 |
|
break; |
178 |
|
} |
179 |
+ |
fflush(stdout); |
180 |
|
if (vcount > 0) |
181 |
|
error(USER, "read error"); |
182 |
< |
fclose(fp); |
182 |
> |
if (fname != NULL) |
183 |
> |
fclose(fp); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
setoutput(vs) /* set up output tables */ |
188 |
|
register char *vs; |
189 |
|
{ |
190 |
< |
extern int ourtrace(), (*trace)(); |
190 |
> |
extern int (*trace)(); |
191 |
|
register int (**table)() = ray_out; |
192 |
|
|
193 |
+ |
castonly = 1; |
194 |
|
while (*vs) |
195 |
|
switch (*vs++) { |
196 |
|
case 't': /* trace */ |
197 |
|
*table = NULL; |
198 |
|
table = every_out; |
199 |
|
trace = ourtrace; |
200 |
+ |
castonly = 0; |
201 |
|
break; |
202 |
|
case 'o': /* origin */ |
203 |
|
*table++ = oputo; |
207 |
|
break; |
208 |
|
case 'v': /* value */ |
209 |
|
*table++ = oputv; |
210 |
+ |
castonly = 0; |
211 |
|
break; |
212 |
< |
case 'l': /* length */ |
212 |
> |
case 'l': /* effective distance */ |
213 |
|
*table++ = oputl; |
214 |
+ |
castonly = 0; |
215 |
|
break; |
216 |
+ |
case 'L': /* single ray length */ |
217 |
+ |
*table++ = oputL; |
218 |
+ |
break; |
219 |
|
case 'p': /* point */ |
220 |
|
*table++ = oputp; |
221 |
|
break; |
222 |
< |
case 'n': /* normal */ |
222 |
> |
case 'n': /* perturbed normal */ |
223 |
|
*table++ = oputn; |
224 |
+ |
castonly = 0; |
225 |
|
break; |
226 |
+ |
case 'N': /* unperturbed normal */ |
227 |
+ |
*table++ = oputN; |
228 |
+ |
break; |
229 |
|
case 's': /* surface */ |
230 |
|
*table++ = oputs; |
231 |
|
break; |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
< |
radiance(org, dir) /* compute radiance value */ |
243 |
> |
rad(org, dir) /* compute and print ray value(s) */ |
244 |
|
FVECT org, dir; |
245 |
|
{ |
166 |
– |
register int (**tp)(); |
167 |
– |
|
246 |
|
VCOPY(thisray.rorg, org); |
247 |
|
VCOPY(thisray.rdir, dir); |
248 |
+ |
thisray.rmax = 0.0; |
249 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
250 |
< |
rayvalue(&thisray); |
251 |
< |
|
252 |
< |
if (ray_out[0] == NULL) |
253 |
< |
return; |
254 |
< |
for (tp = ray_out; *tp != NULL; tp++) |
176 |
< |
(**tp)(&thisray); |
177 |
< |
if (outform == 'a') |
178 |
< |
putchar('\n'); |
250 |
> |
if (castonly) |
251 |
> |
localhit(&thisray, &thescene) || sourcehit(&thisray); |
252 |
> |
else |
253 |
> |
rayvalue(&thisray); |
254 |
> |
printvals(&thisray); |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
< |
irrad(org, dir) /* compute irradiance value */ |
258 |
> |
irrad(org, dir) /* compute immediate irradiance value */ |
259 |
|
FVECT org, dir; |
260 |
|
{ |
185 |
– |
static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
186 |
– |
static OBJREC Lamb = { |
187 |
– |
OVOID, MAT_PLASTIC, "Lambertian", |
188 |
– |
{0, 5, NULL, Lambfa}, NULL, -1, |
189 |
– |
}; |
261 |
|
register int i; |
262 |
|
|
263 |
|
for (i = 0; i < 3; i++) { |
266 |
|
} |
267 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
268 |
|
/* pretend we hit surface */ |
269 |
< |
thisray.rot = 1.0; |
269 |
> |
thisray.rot = 1.0-1e-4; |
270 |
|
thisray.rod = 1.0; |
271 |
|
VCOPY(thisray.ron, dir); |
272 |
|
for (i = 0; i < 3; i++) /* fudge factor */ |
273 |
|
thisray.rop[i] = org[i] + 1e-4*dir[i]; |
274 |
|
/* compute and print */ |
275 |
|
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
276 |
< |
oputv(&thisray); |
276 |
> |
printvals(&thisray); |
277 |
> |
} |
278 |
> |
|
279 |
> |
|
280 |
> |
printvals(r) /* print requested ray values */ |
281 |
> |
RAY *r; |
282 |
> |
{ |
283 |
> |
register int (**tp)(); |
284 |
> |
|
285 |
> |
if (ray_out[0] == NULL) |
286 |
> |
return; |
287 |
> |
for (tp = ray_out; *tp != NULL; tp++) |
288 |
> |
(**tp)(r); |
289 |
|
if (outform == 'a') |
290 |
|
putchar('\n'); |
291 |
|
} |
296 |
|
int fmt; |
297 |
|
FILE *fp; |
298 |
|
{ |
299 |
+ |
extern char *fgetword(); |
300 |
|
static float vf[3]; |
301 |
+ |
static double vd[3]; |
302 |
+ |
char buf[32]; |
303 |
+ |
register int i; |
304 |
|
|
305 |
|
switch (fmt) { |
306 |
|
case 'a': /* ascii */ |
307 |
< |
if (fscanf(fp, "%lf %lf %lf", vec, vec+1, vec+2) != 3) |
308 |
< |
return(-1); |
307 |
> |
for (i = 0; i < 3; i++) { |
308 |
> |
if (fgetword(buf, sizeof(buf), fp) == NULL || |
309 |
> |
!isflt(buf)) |
310 |
> |
return(-1); |
311 |
> |
vec[i] = atof(buf); |
312 |
> |
} |
313 |
|
break; |
314 |
|
case 'f': /* binary float */ |
315 |
< |
if (fread(vf, sizeof(float), 3, fp) != 3) |
315 |
> |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
316 |
|
return(-1); |
317 |
|
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
318 |
|
break; |
319 |
|
case 'd': /* binary double */ |
320 |
< |
if (fread(vec, sizeof(double), 3, fp) != 3) |
320 |
> |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
321 |
|
return(-1); |
322 |
+ |
vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2]; |
323 |
|
break; |
324 |
+ |
default: |
325 |
+ |
error(CONSISTENCY, "botched input format"); |
326 |
|
} |
327 |
|
return(0); |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
+ |
tranotify(obj) /* record new modifier */ |
332 |
+ |
OBJECT obj; |
333 |
+ |
{ |
334 |
+ |
static int hitlimit = 0; |
335 |
+ |
register OBJREC *o = objptr(obj); |
336 |
+ |
register char **tralp; |
337 |
+ |
|
338 |
+ |
if (hitlimit || !ismodifier(o->otype)) |
339 |
+ |
return; |
340 |
+ |
for (tralp = tralist; *tralp != NULL; tralp++) |
341 |
+ |
if (!strcmp(o->oname, *tralp)) { |
342 |
+ |
if (traset[0] >= MAXTSET) { |
343 |
+ |
error(WARNING, "too many modifiers in trace list"); |
344 |
+ |
hitlimit++; |
345 |
+ |
return; /* should this be fatal? */ |
346 |
+ |
} |
347 |
+ |
insertelem(traset, obj); |
348 |
+ |
return; |
349 |
+ |
} |
350 |
+ |
} |
351 |
+ |
|
352 |
+ |
|
353 |
|
static |
354 |
|
ourtrace(r) /* print ray values */ |
355 |
|
RAY *r; |
358 |
|
|
359 |
|
if (every_out[0] == NULL) |
360 |
|
return; |
361 |
+ |
if (r->ro == NULL) { |
362 |
+ |
if (traincl == 1) |
363 |
+ |
return; |
364 |
+ |
} else if (traincl != -1 && traincl != inset(traset, r->ro->omod)) |
365 |
+ |
return; |
366 |
|
tabin(r); |
367 |
|
for (tp = every_out; *tp != NULL; tp++) |
368 |
|
(**tp)(r); |
405 |
|
oputv(r) /* print value */ |
406 |
|
register RAY *r; |
407 |
|
{ |
408 |
+ |
COLR cout; |
409 |
+ |
|
410 |
+ |
if (outform == 'c') { |
411 |
+ |
setcolr(cout, colval(r->rcol,RED), |
412 |
+ |
colval(r->rcol,GRN), |
413 |
+ |
colval(r->rcol,BLU)); |
414 |
+ |
fwrite((char *)cout, sizeof(cout), 1, stdout); |
415 |
+ |
return; |
416 |
+ |
} |
417 |
|
(*putreal)(colval(r->rcol,RED)); |
418 |
|
(*putreal)(colval(r->rcol,GRN)); |
419 |
|
(*putreal)(colval(r->rcol,BLU)); |
421 |
|
|
422 |
|
|
423 |
|
static |
424 |
< |
oputl(r) /* print length */ |
424 |
> |
oputl(r) /* print effective distance */ |
425 |
|
register RAY *r; |
426 |
|
{ |
427 |
< |
if (r->rot < FHUGE) |
298 |
< |
(*putreal)(r->rot); |
299 |
< |
else |
300 |
< |
(*putreal)(0.0); |
427 |
> |
(*putreal)(r->rt); |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
static |
432 |
+ |
oputL(r) /* print single ray length */ |
433 |
+ |
register RAY *r; |
434 |
+ |
{ |
435 |
+ |
(*putreal)(r->rot); |
436 |
+ |
} |
437 |
+ |
|
438 |
+ |
|
439 |
+ |
static |
440 |
|
oputp(r) /* print point */ |
441 |
|
register RAY *r; |
442 |
|
{ |
453 |
|
|
454 |
|
|
455 |
|
static |
456 |
< |
oputn(r) /* print normal */ |
456 |
> |
oputN(r) /* print unperturbed normal */ |
457 |
|
register RAY *r; |
458 |
|
{ |
459 |
|
if (r->rot < FHUGE) { |
469 |
|
|
470 |
|
|
471 |
|
static |
472 |
+ |
oputn(r) /* print perturbed normal */ |
473 |
+ |
RAY *r; |
474 |
+ |
{ |
475 |
+ |
FVECT pnorm; |
476 |
+ |
|
477 |
+ |
if (r->rot >= FHUGE) { |
478 |
+ |
(*putreal)(0.0); |
479 |
+ |
(*putreal)(0.0); |
480 |
+ |
(*putreal)(0.0); |
481 |
+ |
return; |
482 |
+ |
} |
483 |
+ |
raynormal(pnorm, r); |
484 |
+ |
(*putreal)(pnorm[0]); |
485 |
+ |
(*putreal)(pnorm[1]); |
486 |
+ |
(*putreal)(pnorm[2]); |
487 |
+ |
} |
488 |
+ |
|
489 |
+ |
|
490 |
+ |
static |
491 |
|
oputs(r) /* print name */ |
492 |
|
register RAY *r; |
493 |
|
{ |
531 |
|
putd(v) /* print binary double */ |
532 |
|
double v; |
533 |
|
{ |
534 |
< |
fwrite(&v, sizeof(v), 1, stdout); |
534 |
> |
fwrite((char *)&v, sizeof(v), 1, stdout); |
535 |
|
} |
536 |
|
|
537 |
|
|
541 |
|
{ |
542 |
|
float f = v; |
543 |
|
|
544 |
< |
fwrite(&f, sizeof(f), 1, stdout); |
544 |
> |
fwrite((char *)&f, sizeof(f), 1, stdout); |
545 |
|
} |