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