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