1 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* rtrace.c - program and variables for individual ray tracing. |
9 |
* |
10 |
* 6/11/86 |
11 |
*/ |
12 |
|
13 |
/* |
14 |
* Input is in the form: |
15 |
* |
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' 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 |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
62 |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
63 |
double seccg = 0.; /* global scattering eccentricity */ |
64 |
double ssampdist = 0.; /* scatter sampling distance */ |
65 |
|
66 |
double specthresh = .15; /* specular sampling threshold */ |
67 |
double specjitter = 1.; /* specular sampling jitter */ |
68 |
|
69 |
int backvis = 1; /* back face visibility */ |
70 |
|
71 |
int maxdepth = 6; /* maximum recursion depth */ |
72 |
double minweight = 4e-3; /* minimum ray weight */ |
73 |
|
74 |
COLOR ambval = BLKCOLOR; /* ambient value */ |
75 |
int ambvwt = 0; /* initial weight for ambient value */ |
76 |
double ambacc = 0.2; /* ambient accuracy */ |
77 |
int ambres = 128; /* ambient resolution */ |
78 |
int ambdiv = 512; /* ambient divisions */ |
79 |
int ambssamp = 0; /* ambient super-samples */ |
80 |
int ambounce = 0; /* ambient bounces */ |
81 |
char *amblist[128]; /* ambient include/exclude list */ |
82 |
int ambincl = -1; /* include == 1, exclude == 0 */ |
83 |
|
84 |
extern OBJREC Lamb; /* a Lambertian surface */ |
85 |
|
86 |
static RAY thisray; /* for our convenience */ |
87 |
|
88 |
static int oputo(), oputd(), oputv(), oputl(), oputL(), |
89 |
oputp(), oputn(), oputN(), oputs(), oputw(), oputm(); |
90 |
|
91 |
static int ourtrace(), tabin(); |
92 |
static int (*ray_out[16])(), (*every_out[16])(); |
93 |
static int castonly = 0; |
94 |
|
95 |
static int puta(), putf(), putd(); |
96 |
|
97 |
static int (*putreal)(); |
98 |
|
99 |
|
100 |
quit(code) /* quit program */ |
101 |
int code; |
102 |
{ |
103 |
#ifndef NIX |
104 |
headclean(); /* delete header file */ |
105 |
pfclean(); /* clean up persist files */ |
106 |
#endif |
107 |
exit(code); |
108 |
} |
109 |
|
110 |
|
111 |
char * |
112 |
formstr(f) /* return format identifier */ |
113 |
int f; |
114 |
{ |
115 |
switch (f) { |
116 |
case 'a': return("ascii"); |
117 |
case 'f': return("float"); |
118 |
case 'd': return("double"); |
119 |
case 'c': return(COLRFMT); |
120 |
} |
121 |
return("unknown"); |
122 |
} |
123 |
|
124 |
|
125 |
rtrace(fname) /* trace rays from file */ |
126 |
char *fname; |
127 |
{ |
128 |
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
129 |
long nextflush = hresolu; |
130 |
FILE *fp; |
131 |
FVECT orig, direc; |
132 |
/* set up input */ |
133 |
if (fname == NULL) |
134 |
fp = stdin; |
135 |
else if ((fp = fopen(fname, "r")) == NULL) { |
136 |
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
137 |
error(SYSTEM, errmsg); |
138 |
} |
139 |
#ifdef MSDOS |
140 |
if (inform != 'a') |
141 |
setmode(fileno(fp), O_BINARY); |
142 |
#endif |
143 |
/* set up output */ |
144 |
setoutput(outvals); |
145 |
switch (outform) { |
146 |
case 'a': putreal = puta; break; |
147 |
case 'f': putreal = putf; break; |
148 |
case 'd': putreal = putd; break; |
149 |
case 'c': |
150 |
if (strcmp(outvals, "v")) |
151 |
error(USER, "color format with value output only"); |
152 |
break; |
153 |
default: |
154 |
error(CONSISTENCY, "botched output format"); |
155 |
} |
156 |
if (hresolu > 0) { |
157 |
if (vresolu > 0) |
158 |
fprtresolu(hresolu, vresolu, stdout); |
159 |
fflush(stdout); |
160 |
} |
161 |
/* process file */ |
162 |
while (getvec(orig, inform, fp) == 0 && |
163 |
getvec(direc, inform, fp) == 0) { |
164 |
|
165 |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
166 |
bogusray(); |
167 |
if (--nextflush <= 0) { |
168 |
fflush(stdout); |
169 |
nextflush = hresolu; |
170 |
} |
171 |
} else { |
172 |
samplendx++; |
173 |
/* compute and print */ |
174 |
if (imm_irrad) |
175 |
irrad(orig, direc); |
176 |
else |
177 |
rad(orig, direc); |
178 |
/* flush if time */ |
179 |
if (--nextflush == 0) { |
180 |
fflush(stdout); |
181 |
nextflush = hresolu; |
182 |
} |
183 |
} |
184 |
if (ferror(stdout)) |
185 |
error(SYSTEM, "write error"); |
186 |
if (--vcount == 0) /* check for end */ |
187 |
break; |
188 |
} |
189 |
fflush(stdout); |
190 |
if (vcount > 0) |
191 |
error(USER, "read error"); |
192 |
if (fname != NULL) |
193 |
fclose(fp); |
194 |
} |
195 |
|
196 |
|
197 |
setoutput(vs) /* set up output tables */ |
198 |
register char *vs; |
199 |
{ |
200 |
extern int (*trace)(); |
201 |
register int (**table)() = ray_out; |
202 |
|
203 |
castonly = 1; |
204 |
while (*vs) |
205 |
switch (*vs++) { |
206 |
case 't': /* trace */ |
207 |
*table = NULL; |
208 |
table = every_out; |
209 |
trace = ourtrace; |
210 |
castonly = 0; |
211 |
break; |
212 |
case 'o': /* origin */ |
213 |
*table++ = oputo; |
214 |
break; |
215 |
case 'd': /* direction */ |
216 |
*table++ = oputd; |
217 |
break; |
218 |
case 'v': /* value */ |
219 |
*table++ = oputv; |
220 |
castonly = 0; |
221 |
break; |
222 |
case 'l': /* effective distance */ |
223 |
*table++ = oputl; |
224 |
castonly = 0; |
225 |
break; |
226 |
case 'L': /* single ray length */ |
227 |
*table++ = oputL; |
228 |
break; |
229 |
case 'p': /* point */ |
230 |
*table++ = oputp; |
231 |
break; |
232 |
case 'n': /* perturbed normal */ |
233 |
*table++ = oputn; |
234 |
castonly = 0; |
235 |
break; |
236 |
case 'N': /* unperturbed normal */ |
237 |
*table++ = oputN; |
238 |
break; |
239 |
case 's': /* surface */ |
240 |
*table++ = oputs; |
241 |
break; |
242 |
case 'w': /* weight */ |
243 |
*table++ = oputw; |
244 |
break; |
245 |
case 'm': /* modifier */ |
246 |
*table++ = oputm; |
247 |
break; |
248 |
} |
249 |
*table = NULL; |
250 |
} |
251 |
|
252 |
|
253 |
bogusray() /* print out empty record */ |
254 |
{ |
255 |
thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] = |
256 |
thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0; |
257 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
258 |
printvals(&thisray); |
259 |
} |
260 |
|
261 |
|
262 |
rad(org, dir) /* compute and print ray value(s) */ |
263 |
FVECT org, dir; |
264 |
{ |
265 |
VCOPY(thisray.rorg, org); |
266 |
VCOPY(thisray.rdir, dir); |
267 |
thisray.rmax = 0.0; |
268 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
269 |
if (castonly) |
270 |
localhit(&thisray, &thescene) || sourcehit(&thisray); |
271 |
else |
272 |
rayvalue(&thisray); |
273 |
printvals(&thisray); |
274 |
} |
275 |
|
276 |
|
277 |
irrad(org, dir) /* compute immediate irradiance value */ |
278 |
FVECT org, dir; |
279 |
{ |
280 |
register int i; |
281 |
|
282 |
for (i = 0; i < 3; i++) { |
283 |
thisray.rorg[i] = org[i] + dir[i]; |
284 |
thisray.rdir[i] = -dir[i]; |
285 |
} |
286 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
287 |
/* pretend we hit surface */ |
288 |
thisray.rot = 1.0-1e-4; |
289 |
thisray.rod = 1.0; |
290 |
VCOPY(thisray.ron, dir); |
291 |
for (i = 0; i < 3; i++) /* fudge factor */ |
292 |
thisray.rop[i] = org[i] + 1e-4*dir[i]; |
293 |
/* compute and print */ |
294 |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
295 |
printvals(&thisray); |
296 |
} |
297 |
|
298 |
|
299 |
printvals(r) /* print requested ray values */ |
300 |
RAY *r; |
301 |
{ |
302 |
register int (**tp)(); |
303 |
|
304 |
if (ray_out[0] == NULL) |
305 |
return; |
306 |
for (tp = ray_out; *tp != NULL; tp++) |
307 |
(**tp)(r); |
308 |
if (outform == 'a') |
309 |
putchar('\n'); |
310 |
} |
311 |
|
312 |
|
313 |
getvec(vec, fmt, fp) /* get a vector from fp */ |
314 |
register FVECT vec; |
315 |
int fmt; |
316 |
FILE *fp; |
317 |
{ |
318 |
extern char *fgetword(); |
319 |
static float vf[3]; |
320 |
static double vd[3]; |
321 |
char buf[32]; |
322 |
register int i; |
323 |
|
324 |
switch (fmt) { |
325 |
case 'a': /* ascii */ |
326 |
for (i = 0; i < 3; i++) { |
327 |
if (fgetword(buf, sizeof(buf), fp) == NULL || |
328 |
!isflt(buf)) |
329 |
return(-1); |
330 |
vec[i] = atof(buf); |
331 |
} |
332 |
break; |
333 |
case 'f': /* binary float */ |
334 |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
335 |
return(-1); |
336 |
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
337 |
break; |
338 |
case 'd': /* binary double */ |
339 |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
340 |
return(-1); |
341 |
vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2]; |
342 |
break; |
343 |
default: |
344 |
error(CONSISTENCY, "botched input format"); |
345 |
} |
346 |
return(0); |
347 |
} |
348 |
|
349 |
|
350 |
tranotify(obj) /* record new modifier */ |
351 |
OBJECT obj; |
352 |
{ |
353 |
static int hitlimit = 0; |
354 |
register OBJREC *o = objptr(obj); |
355 |
register char **tralp; |
356 |
|
357 |
if (hitlimit || !ismodifier(o->otype)) |
358 |
return; |
359 |
for (tralp = tralist; *tralp != NULL; tralp++) |
360 |
if (!strcmp(o->oname, *tralp)) { |
361 |
if (traset[0] >= MAXTSET) { |
362 |
error(WARNING, "too many modifiers in trace list"); |
363 |
hitlimit++; |
364 |
return; /* should this be fatal? */ |
365 |
} |
366 |
insertelem(traset, obj); |
367 |
return; |
368 |
} |
369 |
} |
370 |
|
371 |
|
372 |
static |
373 |
ourtrace(r) /* print ray values */ |
374 |
RAY *r; |
375 |
{ |
376 |
register int (**tp)(); |
377 |
|
378 |
if (every_out[0] == NULL) |
379 |
return; |
380 |
if (r->ro == NULL) { |
381 |
if (traincl == 1) |
382 |
return; |
383 |
} else if (traincl != -1 && traincl != inset(traset, r->ro->omod)) |
384 |
return; |
385 |
tabin(r); |
386 |
for (tp = every_out; *tp != NULL; tp++) |
387 |
(**tp)(r); |
388 |
putchar('\n'); |
389 |
} |
390 |
|
391 |
|
392 |
static |
393 |
tabin(r) /* tab in appropriate amount */ |
394 |
RAY *r; |
395 |
{ |
396 |
register RAY *rp; |
397 |
|
398 |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
399 |
putchar('\t'); |
400 |
} |
401 |
|
402 |
|
403 |
static |
404 |
oputo(r) /* print origin */ |
405 |
register RAY *r; |
406 |
{ |
407 |
(*putreal)(r->rorg[0]); |
408 |
(*putreal)(r->rorg[1]); |
409 |
(*putreal)(r->rorg[2]); |
410 |
} |
411 |
|
412 |
|
413 |
static |
414 |
oputd(r) /* print direction */ |
415 |
register RAY *r; |
416 |
{ |
417 |
(*putreal)(r->rdir[0]); |
418 |
(*putreal)(r->rdir[1]); |
419 |
(*putreal)(r->rdir[2]); |
420 |
} |
421 |
|
422 |
|
423 |
static |
424 |
oputv(r) /* print value */ |
425 |
register RAY *r; |
426 |
{ |
427 |
COLR cout; |
428 |
|
429 |
if (outform == 'c') { |
430 |
setcolr(cout, colval(r->rcol,RED), |
431 |
colval(r->rcol,GRN), |
432 |
colval(r->rcol,BLU)); |
433 |
fwrite((char *)cout, sizeof(cout), 1, stdout); |
434 |
return; |
435 |
} |
436 |
(*putreal)(colval(r->rcol,RED)); |
437 |
(*putreal)(colval(r->rcol,GRN)); |
438 |
(*putreal)(colval(r->rcol,BLU)); |
439 |
} |
440 |
|
441 |
|
442 |
static |
443 |
oputl(r) /* print effective distance */ |
444 |
register RAY *r; |
445 |
{ |
446 |
(*putreal)(r->rt); |
447 |
} |
448 |
|
449 |
|
450 |
static |
451 |
oputL(r) /* print single ray length */ |
452 |
register RAY *r; |
453 |
{ |
454 |
(*putreal)(r->rot); |
455 |
} |
456 |
|
457 |
|
458 |
static |
459 |
oputp(r) /* print point */ |
460 |
register RAY *r; |
461 |
{ |
462 |
if (r->rot < FHUGE) { |
463 |
(*putreal)(r->rop[0]); |
464 |
(*putreal)(r->rop[1]); |
465 |
(*putreal)(r->rop[2]); |
466 |
} else { |
467 |
(*putreal)(0.0); |
468 |
(*putreal)(0.0); |
469 |
(*putreal)(0.0); |
470 |
} |
471 |
} |
472 |
|
473 |
|
474 |
static |
475 |
oputN(r) /* print unperturbed normal */ |
476 |
register RAY *r; |
477 |
{ |
478 |
if (r->rot < FHUGE) { |
479 |
(*putreal)(r->ron[0]); |
480 |
(*putreal)(r->ron[1]); |
481 |
(*putreal)(r->ron[2]); |
482 |
} else { |
483 |
(*putreal)(0.0); |
484 |
(*putreal)(0.0); |
485 |
(*putreal)(0.0); |
486 |
} |
487 |
} |
488 |
|
489 |
|
490 |
static |
491 |
oputn(r) /* print perturbed normal */ |
492 |
RAY *r; |
493 |
{ |
494 |
FVECT pnorm; |
495 |
|
496 |
if (r->rot >= FHUGE) { |
497 |
(*putreal)(0.0); |
498 |
(*putreal)(0.0); |
499 |
(*putreal)(0.0); |
500 |
return; |
501 |
} |
502 |
raynormal(pnorm, r); |
503 |
(*putreal)(pnorm[0]); |
504 |
(*putreal)(pnorm[1]); |
505 |
(*putreal)(pnorm[2]); |
506 |
} |
507 |
|
508 |
|
509 |
static |
510 |
oputs(r) /* print name */ |
511 |
register RAY *r; |
512 |
{ |
513 |
if (r->ro != NULL) |
514 |
fputs(r->ro->oname, stdout); |
515 |
else |
516 |
putchar('*'); |
517 |
putchar('\t'); |
518 |
} |
519 |
|
520 |
|
521 |
static |
522 |
oputw(r) /* print weight */ |
523 |
register RAY *r; |
524 |
{ |
525 |
(*putreal)(r->rweight); |
526 |
} |
527 |
|
528 |
|
529 |
static |
530 |
oputm(r) /* print modifier */ |
531 |
register RAY *r; |
532 |
{ |
533 |
if (r->ro != NULL) |
534 |
if (r->ro->omod != OVOID) |
535 |
fputs(objptr(r->ro->omod)->oname, stdout); |
536 |
else |
537 |
fputs(VOIDID, stdout); |
538 |
else |
539 |
putchar('*'); |
540 |
putchar('\t'); |
541 |
} |
542 |
|
543 |
|
544 |
static |
545 |
puta(v) /* print ascii value */ |
546 |
double v; |
547 |
{ |
548 |
printf("%e\t", v); |
549 |
} |
550 |
|
551 |
|
552 |
static |
553 |
putd(v) /* print binary double */ |
554 |
double v; |
555 |
{ |
556 |
fwrite((char *)&v, sizeof(v), 1, stdout); |
557 |
} |
558 |
|
559 |
|
560 |
static |
561 |
putf(v) /* print binary float */ |
562 |
double v; |
563 |
{ |
564 |
float f = v; |
565 |
|
566 |
fwrite((char *)&f, sizeof(f), 1, stdout); |
567 |
} |