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