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 |
fputformat(formstr(outform), stdout); |
135 |
putchar('\n'); |
136 |
if (hresolu > 0 && vresolu > 0) |
137 |
fprtresolu(hresolu, vresolu, stdout); |
138 |
/* process file */ |
139 |
while (getvec(orig, inform, fp) == 0 && |
140 |
getvec(direc, inform, fp) == 0) { |
141 |
|
142 |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
143 |
fflush(stdout); |
144 |
continue; |
145 |
} |
146 |
samplendx++; |
147 |
/* compute and print */ |
148 |
if (imm_irrad) |
149 |
irrad(orig, direc); |
150 |
else |
151 |
traceray(orig, direc); |
152 |
/* flush if time */ |
153 |
if (--nextflush == 0) { |
154 |
fflush(stdout); |
155 |
nextflush = hresolu; |
156 |
} |
157 |
if (ferror(stdout)) |
158 |
error(SYSTEM, "write error"); |
159 |
if (--vcount == 0) /* check for end */ |
160 |
break; |
161 |
} |
162 |
if (vcount > 0) |
163 |
error(USER, "read error"); |
164 |
fclose(fp); |
165 |
} |
166 |
|
167 |
|
168 |
setoutput(vs) /* set up output tables */ |
169 |
register char *vs; |
170 |
{ |
171 |
extern int ourtrace(), (*trace)(); |
172 |
register int (**table)() = ray_out; |
173 |
|
174 |
castonly = 1; |
175 |
while (*vs) |
176 |
switch (*vs++) { |
177 |
case 't': /* trace */ |
178 |
*table = NULL; |
179 |
table = every_out; |
180 |
trace = ourtrace; |
181 |
castonly = 0; |
182 |
break; |
183 |
case 'o': /* origin */ |
184 |
*table++ = oputo; |
185 |
break; |
186 |
case 'd': /* direction */ |
187 |
*table++ = oputd; |
188 |
break; |
189 |
case 'v': /* value */ |
190 |
*table++ = oputv; |
191 |
castonly = 0; |
192 |
break; |
193 |
case 'l': /* effective distance */ |
194 |
*table++ = oputl; |
195 |
castonly = 0; |
196 |
break; |
197 |
case 'L': /* single ray length */ |
198 |
*table++ = oputL; |
199 |
break; |
200 |
case 'p': /* point */ |
201 |
*table++ = oputp; |
202 |
break; |
203 |
case 'n': /* normal */ |
204 |
*table++ = oputn; |
205 |
break; |
206 |
case 's': /* surface */ |
207 |
*table++ = oputs; |
208 |
break; |
209 |
case 'w': /* weight */ |
210 |
*table++ = oputw; |
211 |
break; |
212 |
case 'm': /* modifier */ |
213 |
*table++ = oputm; |
214 |
break; |
215 |
} |
216 |
*table = NULL; |
217 |
} |
218 |
|
219 |
|
220 |
traceray(org, dir) /* compute and print ray value(s) */ |
221 |
FVECT org, dir; |
222 |
{ |
223 |
register int (**tp)(); |
224 |
|
225 |
VCOPY(thisray.rorg, org); |
226 |
VCOPY(thisray.rdir, dir); |
227 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
228 |
if (castonly) |
229 |
localhit(&thisray, &thescene) || sourcehit(&thisray); |
230 |
else |
231 |
rayvalue(&thisray); |
232 |
|
233 |
if (ray_out[0] == NULL) |
234 |
return; |
235 |
for (tp = ray_out; *tp != NULL; tp++) |
236 |
(**tp)(&thisray); |
237 |
if (outform == 'a') |
238 |
putchar('\n'); |
239 |
} |
240 |
|
241 |
|
242 |
irrad(org, dir) /* compute immediate irradiance value */ |
243 |
FVECT org, dir; |
244 |
{ |
245 |
register int i; |
246 |
|
247 |
for (i = 0; i < 3; i++) { |
248 |
thisray.rorg[i] = org[i] + dir[i]; |
249 |
thisray.rdir[i] = -dir[i]; |
250 |
} |
251 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
252 |
/* pretend we hit surface */ |
253 |
thisray.rot = 1.0; |
254 |
thisray.rod = 1.0; |
255 |
VCOPY(thisray.ron, dir); |
256 |
for (i = 0; i < 3; i++) /* fudge factor */ |
257 |
thisray.rop[i] = org[i] + 1e-4*dir[i]; |
258 |
/* compute and print */ |
259 |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
260 |
oputv(&thisray); |
261 |
if (outform == 'a') |
262 |
putchar('\n'); |
263 |
} |
264 |
|
265 |
|
266 |
getvec(vec, fmt, fp) /* get a vector from fp */ |
267 |
register FVECT vec; |
268 |
int fmt; |
269 |
FILE *fp; |
270 |
{ |
271 |
extern char *fgetword(); |
272 |
static float vf[3]; |
273 |
static double vd[3]; |
274 |
char buf[32]; |
275 |
register int i; |
276 |
|
277 |
switch (fmt) { |
278 |
case 'a': /* ascii */ |
279 |
for (i = 0; i < 3; i++) { |
280 |
if (fgetword(buf, sizeof(buf), fp) == NULL || |
281 |
!isflt(buf)) |
282 |
return(-1); |
283 |
vec[i] = atof(buf); |
284 |
} |
285 |
break; |
286 |
case 'f': /* binary float */ |
287 |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
288 |
return(-1); |
289 |
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
290 |
break; |
291 |
case 'd': /* binary double */ |
292 |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
293 |
return(-1); |
294 |
vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2]; |
295 |
break; |
296 |
default: |
297 |
error(CONSISTENCY, "botched input format"); |
298 |
} |
299 |
return(0); |
300 |
} |
301 |
|
302 |
|
303 |
static |
304 |
ourtrace(r) /* print ray values */ |
305 |
RAY *r; |
306 |
{ |
307 |
register int (**tp)(); |
308 |
|
309 |
if (every_out[0] == NULL) |
310 |
return; |
311 |
tabin(r); |
312 |
for (tp = every_out; *tp != NULL; tp++) |
313 |
(**tp)(r); |
314 |
putchar('\n'); |
315 |
} |
316 |
|
317 |
|
318 |
static |
319 |
tabin(r) /* tab in appropriate amount */ |
320 |
RAY *r; |
321 |
{ |
322 |
register RAY *rp; |
323 |
|
324 |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
325 |
putchar('\t'); |
326 |
} |
327 |
|
328 |
|
329 |
static |
330 |
oputo(r) /* print origin */ |
331 |
register RAY *r; |
332 |
{ |
333 |
(*putreal)(r->rorg[0]); |
334 |
(*putreal)(r->rorg[1]); |
335 |
(*putreal)(r->rorg[2]); |
336 |
} |
337 |
|
338 |
|
339 |
static |
340 |
oputd(r) /* print direction */ |
341 |
register RAY *r; |
342 |
{ |
343 |
(*putreal)(r->rdir[0]); |
344 |
(*putreal)(r->rdir[1]); |
345 |
(*putreal)(r->rdir[2]); |
346 |
} |
347 |
|
348 |
|
349 |
static |
350 |
oputv(r) /* print value */ |
351 |
register RAY *r; |
352 |
{ |
353 |
COLR cout; |
354 |
|
355 |
if (outform == 'c') { |
356 |
setcolr(cout, colval(r->rcol,RED), |
357 |
colval(r->rcol,GRN), |
358 |
colval(r->rcol,BLU)); |
359 |
fwrite((char *)cout, sizeof(cout), 1, stdout); |
360 |
return; |
361 |
} |
362 |
(*putreal)(colval(r->rcol,RED)); |
363 |
(*putreal)(colval(r->rcol,GRN)); |
364 |
(*putreal)(colval(r->rcol,BLU)); |
365 |
} |
366 |
|
367 |
|
368 |
static |
369 |
oputl(r) /* print effective distance */ |
370 |
register RAY *r; |
371 |
{ |
372 |
(*putreal)(r->rt); |
373 |
} |
374 |
|
375 |
|
376 |
static |
377 |
oputL(r) /* print single ray length */ |
378 |
register RAY *r; |
379 |
{ |
380 |
(*putreal)(r->rot); |
381 |
} |
382 |
|
383 |
|
384 |
static |
385 |
oputp(r) /* print point */ |
386 |
register RAY *r; |
387 |
{ |
388 |
if (r->rot < FHUGE) { |
389 |
(*putreal)(r->rop[0]); |
390 |
(*putreal)(r->rop[1]); |
391 |
(*putreal)(r->rop[2]); |
392 |
} else { |
393 |
(*putreal)(0.0); |
394 |
(*putreal)(0.0); |
395 |
(*putreal)(0.0); |
396 |
} |
397 |
} |
398 |
|
399 |
|
400 |
static |
401 |
oputn(r) /* print normal */ |
402 |
register RAY *r; |
403 |
{ |
404 |
if (r->rot < FHUGE) { |
405 |
(*putreal)(r->ron[0]); |
406 |
(*putreal)(r->ron[1]); |
407 |
(*putreal)(r->ron[2]); |
408 |
} else { |
409 |
(*putreal)(0.0); |
410 |
(*putreal)(0.0); |
411 |
(*putreal)(0.0); |
412 |
} |
413 |
} |
414 |
|
415 |
|
416 |
static |
417 |
oputs(r) /* print name */ |
418 |
register RAY *r; |
419 |
{ |
420 |
if (r->ro != NULL) |
421 |
fputs(r->ro->oname, stdout); |
422 |
else |
423 |
putchar('*'); |
424 |
putchar('\t'); |
425 |
} |
426 |
|
427 |
|
428 |
static |
429 |
oputw(r) /* print weight */ |
430 |
register RAY *r; |
431 |
{ |
432 |
(*putreal)(r->rweight); |
433 |
} |
434 |
|
435 |
|
436 |
static |
437 |
oputm(r) /* print modifier */ |
438 |
register RAY *r; |
439 |
{ |
440 |
if (r->ro != NULL) |
441 |
fputs(objptr(r->ro->omod)->oname, stdout); |
442 |
else |
443 |
putchar('*'); |
444 |
putchar('\t'); |
445 |
} |
446 |
|
447 |
|
448 |
static |
449 |
puta(v) /* print ascii value */ |
450 |
double v; |
451 |
{ |
452 |
printf("%e\t", v); |
453 |
} |
454 |
|
455 |
|
456 |
static |
457 |
putd(v) /* print binary double */ |
458 |
double v; |
459 |
{ |
460 |
fwrite((char *)&v, sizeof(v), 1, stdout); |
461 |
} |
462 |
|
463 |
|
464 |
static |
465 |
putf(v) /* print binary float */ |
466 |
double v; |
467 |
{ |
468 |
float f = v; |
469 |
|
470 |
fwrite((char *)&f, sizeof(f), 1, stdout); |
471 |
} |