1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
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 |
int hresolu = 0; /* horizontal (scan) size */ |
46 |
int vresolu = 0; /* vertical resolution */ |
47 |
|
48 |
double dstrsrc = 0.0; /* square source distribution */ |
49 |
double shadthresh = .05; /* shadow threshold */ |
50 |
double shadcert = .5; /* shadow certainty */ |
51 |
int directrelay = 1; /* number of source relays */ |
52 |
int vspretest = 512; /* virtual source pretest density */ |
53 |
int directinvis = 0; /* sources invisible? */ |
54 |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
55 |
|
56 |
double specthresh = .15; /* specular sampling threshold */ |
57 |
double specjitter = 1.; /* specular sampling jitter */ |
58 |
|
59 |
int maxdepth = 6; /* maximum recursion depth */ |
60 |
double minweight = 4e-3; /* minimum ray weight */ |
61 |
|
62 |
COLOR ambval = BLKCOLOR; /* ambient value */ |
63 |
double ambacc = 0.2; /* ambient accuracy */ |
64 |
int ambres = 32; /* ambient resolution */ |
65 |
int ambdiv = 128; /* ambient divisions */ |
66 |
int ambssamp = 0; /* ambient super-samples */ |
67 |
int ambounce = 0; /* ambient bounces */ |
68 |
char *amblist[128]; /* ambient include/exclude list */ |
69 |
int ambincl = -1; /* include == 1, exclude == 0 */ |
70 |
|
71 |
extern OBJREC Lamb; /* a Lambertian surface */ |
72 |
|
73 |
static RAY thisray; /* for our convenience */ |
74 |
|
75 |
static int oputo(), oputd(), oputv(), oputl(), oputL(), |
76 |
oputp(), oputn(), oputs(), oputw(), oputm(); |
77 |
|
78 |
static int (*ray_out[10])(), (*every_out[10])(); |
79 |
static int castonly; |
80 |
|
81 |
static int puta(), putf(), putd(); |
82 |
|
83 |
static int (*putreal)(); |
84 |
|
85 |
|
86 |
quit(code) /* quit program */ |
87 |
int code; |
88 |
{ |
89 |
exit(code); |
90 |
} |
91 |
|
92 |
|
93 |
char * |
94 |
formstr(f) /* return format identifier */ |
95 |
int f; |
96 |
{ |
97 |
switch (f) { |
98 |
case 'a': return("ascii"); |
99 |
case 'f': return("float"); |
100 |
case 'd': return("double"); |
101 |
case 'c': return(COLRFMT); |
102 |
} |
103 |
return("unknown"); |
104 |
} |
105 |
|
106 |
|
107 |
rtrace(fname) /* trace rays from file */ |
108 |
char *fname; |
109 |
{ |
110 |
long vcount = hresolu>1 ? hresolu*vresolu : vresolu; |
111 |
long nextflush = hresolu; |
112 |
FILE *fp; |
113 |
FVECT orig, direc; |
114 |
/* set up input */ |
115 |
if (fname == NULL) |
116 |
fp = stdin; |
117 |
else if ((fp = fopen(fname, "r")) == NULL) { |
118 |
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
119 |
error(SYSTEM, errmsg); |
120 |
} |
121 |
/* set up output */ |
122 |
setoutput(outvals); |
123 |
switch (outform) { |
124 |
case 'a': putreal = puta; break; |
125 |
case 'f': putreal = putf; break; |
126 |
case 'd': putreal = putd; break; |
127 |
case 'c': |
128 |
if (strcmp(outvals, "v")) |
129 |
error(USER, "color format with value output only"); |
130 |
break; |
131 |
default: |
132 |
error(CONSISTENCY, "botched output format"); |
133 |
} |
134 |
if (hresolu > 0 && vresolu > 0) |
135 |
fprtresolu(hresolu, vresolu, stdout); |
136 |
/* process file */ |
137 |
while (getvec(orig, inform, fp) == 0 && |
138 |
getvec(direc, inform, fp) == 0) { |
139 |
|
140 |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
141 |
fflush(stdout); |
142 |
continue; |
143 |
} |
144 |
samplendx++; |
145 |
/* compute and print */ |
146 |
if (imm_irrad) |
147 |
irrad(orig, direc); |
148 |
else |
149 |
traceray(orig, direc); |
150 |
/* flush if time */ |
151 |
if (--nextflush == 0) { |
152 |
fflush(stdout); |
153 |
nextflush = hresolu; |
154 |
} |
155 |
if (ferror(stdout)) |
156 |
error(SYSTEM, "write error"); |
157 |
if (--vcount == 0) /* check for end */ |
158 |
break; |
159 |
} |
160 |
if (vcount > 0) |
161 |
error(USER, "read error"); |
162 |
fclose(fp); |
163 |
} |
164 |
|
165 |
|
166 |
setoutput(vs) /* set up output tables */ |
167 |
register char *vs; |
168 |
{ |
169 |
extern int ourtrace(), (*trace)(); |
170 |
register int (**table)() = ray_out; |
171 |
|
172 |
castonly = 1; |
173 |
while (*vs) |
174 |
switch (*vs++) { |
175 |
case 't': /* trace */ |
176 |
*table = NULL; |
177 |
table = every_out; |
178 |
trace = ourtrace; |
179 |
castonly = 0; |
180 |
break; |
181 |
case 'o': /* origin */ |
182 |
*table++ = oputo; |
183 |
break; |
184 |
case 'd': /* direction */ |
185 |
*table++ = oputd; |
186 |
break; |
187 |
case 'v': /* value */ |
188 |
*table++ = oputv; |
189 |
castonly = 0; |
190 |
break; |
191 |
case 'l': /* effective distance */ |
192 |
*table++ = oputl; |
193 |
castonly = 0; |
194 |
break; |
195 |
case 'L': /* single ray length */ |
196 |
*table++ = oputL; |
197 |
break; |
198 |
case 'p': /* point */ |
199 |
*table++ = oputp; |
200 |
break; |
201 |
case 'n': /* normal */ |
202 |
*table++ = oputn; |
203 |
break; |
204 |
case 's': /* surface */ |
205 |
*table++ = oputs; |
206 |
break; |
207 |
case 'w': /* weight */ |
208 |
*table++ = oputw; |
209 |
break; |
210 |
case 'm': /* modifier */ |
211 |
*table++ = oputm; |
212 |
break; |
213 |
} |
214 |
*table = NULL; |
215 |
} |
216 |
|
217 |
|
218 |
traceray(org, dir) /* compute and print ray value(s) */ |
219 |
FVECT org, dir; |
220 |
{ |
221 |
register int (**tp)(); |
222 |
|
223 |
VCOPY(thisray.rorg, org); |
224 |
VCOPY(thisray.rdir, dir); |
225 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
226 |
if (castonly) |
227 |
localhit(&thisray, &thescene) || sourcehit(&thisray); |
228 |
else |
229 |
rayvalue(&thisray); |
230 |
|
231 |
if (ray_out[0] == NULL) |
232 |
return; |
233 |
for (tp = ray_out; *tp != NULL; tp++) |
234 |
(**tp)(&thisray); |
235 |
if (outform == 'a') |
236 |
putchar('\n'); |
237 |
} |
238 |
|
239 |
|
240 |
irrad(org, dir) /* compute immediate irradiance value */ |
241 |
FVECT org, dir; |
242 |
{ |
243 |
register int i; |
244 |
|
245 |
for (i = 0; i < 3; i++) { |
246 |
thisray.rorg[i] = org[i] + dir[i]; |
247 |
thisray.rdir[i] = -dir[i]; |
248 |
} |
249 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
250 |
/* pretend we hit surface */ |
251 |
thisray.rot = 1.0; |
252 |
thisray.rod = 1.0; |
253 |
VCOPY(thisray.ron, dir); |
254 |
for (i = 0; i < 3; i++) /* fudge factor */ |
255 |
thisray.rop[i] = org[i] + 1e-4*dir[i]; |
256 |
/* compute and print */ |
257 |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
258 |
oputv(&thisray); |
259 |
if (outform == 'a') |
260 |
putchar('\n'); |
261 |
} |
262 |
|
263 |
|
264 |
getvec(vec, fmt, fp) /* get a vector from fp */ |
265 |
register FVECT vec; |
266 |
int fmt; |
267 |
FILE *fp; |
268 |
{ |
269 |
extern char *fgetword(); |
270 |
static float vf[3]; |
271 |
static double vd[3]; |
272 |
char buf[32]; |
273 |
register int i; |
274 |
|
275 |
switch (fmt) { |
276 |
case 'a': /* ascii */ |
277 |
for (i = 0; i < 3; i++) { |
278 |
if (fgetword(buf, sizeof(buf), fp) == NULL || |
279 |
!isflt(buf)) |
280 |
return(-1); |
281 |
vec[i] = atof(buf); |
282 |
} |
283 |
break; |
284 |
case 'f': /* binary float */ |
285 |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
286 |
return(-1); |
287 |
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
288 |
break; |
289 |
case 'd': /* binary double */ |
290 |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
291 |
return(-1); |
292 |
vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2]; |
293 |
break; |
294 |
default: |
295 |
error(CONSISTENCY, "botched input format"); |
296 |
} |
297 |
return(0); |
298 |
} |
299 |
|
300 |
|
301 |
static |
302 |
ourtrace(r) /* print ray values */ |
303 |
RAY *r; |
304 |
{ |
305 |
register int (**tp)(); |
306 |
|
307 |
if (every_out[0] == NULL) |
308 |
return; |
309 |
tabin(r); |
310 |
for (tp = every_out; *tp != NULL; tp++) |
311 |
(**tp)(r); |
312 |
putchar('\n'); |
313 |
} |
314 |
|
315 |
|
316 |
static |
317 |
tabin(r) /* tab in appropriate amount */ |
318 |
RAY *r; |
319 |
{ |
320 |
register RAY *rp; |
321 |
|
322 |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
323 |
putchar('\t'); |
324 |
} |
325 |
|
326 |
|
327 |
static |
328 |
oputo(r) /* print origin */ |
329 |
register RAY *r; |
330 |
{ |
331 |
(*putreal)(r->rorg[0]); |
332 |
(*putreal)(r->rorg[1]); |
333 |
(*putreal)(r->rorg[2]); |
334 |
} |
335 |
|
336 |
|
337 |
static |
338 |
oputd(r) /* print direction */ |
339 |
register RAY *r; |
340 |
{ |
341 |
(*putreal)(r->rdir[0]); |
342 |
(*putreal)(r->rdir[1]); |
343 |
(*putreal)(r->rdir[2]); |
344 |
} |
345 |
|
346 |
|
347 |
static |
348 |
oputv(r) /* print value */ |
349 |
register RAY *r; |
350 |
{ |
351 |
COLR cout; |
352 |
|
353 |
if (outform == 'c') { |
354 |
setcolr(cout, colval(r->rcol,RED), |
355 |
colval(r->rcol,GRN), |
356 |
colval(r->rcol,BLU)); |
357 |
fwrite((char *)cout, sizeof(cout), 1, stdout); |
358 |
return; |
359 |
} |
360 |
(*putreal)(colval(r->rcol,RED)); |
361 |
(*putreal)(colval(r->rcol,GRN)); |
362 |
(*putreal)(colval(r->rcol,BLU)); |
363 |
} |
364 |
|
365 |
|
366 |
static |
367 |
oputl(r) /* print effective distance */ |
368 |
register RAY *r; |
369 |
{ |
370 |
(*putreal)(r->rt); |
371 |
} |
372 |
|
373 |
|
374 |
static |
375 |
oputL(r) /* print single ray length */ |
376 |
register RAY *r; |
377 |
{ |
378 |
(*putreal)(r->rot); |
379 |
} |
380 |
|
381 |
|
382 |
static |
383 |
oputp(r) /* print point */ |
384 |
register RAY *r; |
385 |
{ |
386 |
if (r->rot < FHUGE) { |
387 |
(*putreal)(r->rop[0]); |
388 |
(*putreal)(r->rop[1]); |
389 |
(*putreal)(r->rop[2]); |
390 |
} else { |
391 |
(*putreal)(0.0); |
392 |
(*putreal)(0.0); |
393 |
(*putreal)(0.0); |
394 |
} |
395 |
} |
396 |
|
397 |
|
398 |
static |
399 |
oputn(r) /* print normal */ |
400 |
register RAY *r; |
401 |
{ |
402 |
if (r->rot < FHUGE) { |
403 |
(*putreal)(r->ron[0]); |
404 |
(*putreal)(r->ron[1]); |
405 |
(*putreal)(r->ron[2]); |
406 |
} else { |
407 |
(*putreal)(0.0); |
408 |
(*putreal)(0.0); |
409 |
(*putreal)(0.0); |
410 |
} |
411 |
} |
412 |
|
413 |
|
414 |
static |
415 |
oputs(r) /* print name */ |
416 |
register RAY *r; |
417 |
{ |
418 |
if (r->ro != NULL) |
419 |
fputs(r->ro->oname, stdout); |
420 |
else |
421 |
putchar('*'); |
422 |
putchar('\t'); |
423 |
} |
424 |
|
425 |
|
426 |
static |
427 |
oputw(r) /* print weight */ |
428 |
register RAY *r; |
429 |
{ |
430 |
(*putreal)(r->rweight); |
431 |
} |
432 |
|
433 |
|
434 |
static |
435 |
oputm(r) /* print modifier */ |
436 |
register RAY *r; |
437 |
{ |
438 |
if (r->ro != NULL) |
439 |
fputs(objptr(r->ro->omod)->oname, stdout); |
440 |
else |
441 |
putchar('*'); |
442 |
putchar('\t'); |
443 |
} |
444 |
|
445 |
|
446 |
static |
447 |
puta(v) /* print ascii value */ |
448 |
double v; |
449 |
{ |
450 |
printf("%e\t", v); |
451 |
} |
452 |
|
453 |
|
454 |
static |
455 |
putd(v) /* print binary double */ |
456 |
double v; |
457 |
{ |
458 |
fwrite((char *)&v, sizeof(v), 1, stdout); |
459 |
} |
460 |
|
461 |
|
462 |
static |
463 |
putf(v) /* print binary float */ |
464 |
double v; |
465 |
{ |
466 |
float f = v; |
467 |
|
468 |
fwrite((char *)&f, sizeof(f), 1, stdout); |
469 |
} |