1 |
greg |
1.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 |
greg |
1.7 |
* If the direction vector is (0,0,0), then the output is flushed. |
20 |
greg |
1.1 |
* 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 |
greg |
1.13 |
* radiance is computed. The '-i' or '-I' options indicate that |
24 |
greg |
1.1 |
* irradiance values are desired. |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include "ray.h" |
28 |
|
|
|
29 |
greg |
1.11 |
#include "octree.h" |
30 |
|
|
|
31 |
greg |
1.1 |
#include "otypes.h" |
32 |
|
|
|
33 |
greg |
1.14 |
int dimlist[MAXDIM]; /* sampling dimensions */ |
34 |
|
|
int ndims = 0; /* number of sampling dimensions */ |
35 |
|
|
int samplendx = 0; /* index for this sample */ |
36 |
|
|
|
37 |
greg |
1.13 |
int imm_irrad = 0; /* compute immediate irradiance? */ |
38 |
|
|
|
39 |
greg |
1.1 |
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 |
greg |
1.4 |
double shadthresh = .05; /* shadow threshold */ |
48 |
greg |
1.5 |
double shadcert = .5; /* shadow certainty */ |
49 |
greg |
1.15 |
int directrelay = 0; /* number of source relays */ |
50 |
greg |
1.17 |
int vspretest = 512; /* virtual source pretest density */ |
51 |
greg |
1.18 |
int directinvis = 0; /* sources invisible? */ |
52 |
greg |
1.1 |
|
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 |
greg |
1.6 |
int ambres = 32; /* ambient resolution */ |
59 |
greg |
1.1 |
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 |
greg |
1.13 |
extern OBJREC Lamb; /* a Lambertian surface */ |
66 |
|
|
|
67 |
greg |
1.1 |
static RAY thisray; /* for our convenience */ |
68 |
|
|
|
69 |
greg |
1.10 |
static int oputo(), oputd(), oputv(), oputl(), |
70 |
greg |
1.1 |
oputp(), oputn(), oputs(), oputw(), oputm(); |
71 |
|
|
|
72 |
|
|
static int (*ray_out[10])(), (*every_out[10])(); |
73 |
greg |
1.11 |
static int castonly; |
74 |
greg |
1.1 |
|
75 |
greg |
1.10 |
static int puta(), putf(), putd(); |
76 |
greg |
1.1 |
|
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 |
greg |
1.2 |
FVECT orig, direc; |
94 |
greg |
1.1 |
/* 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 |
greg |
1.7 |
if (normalize(direc) == 0.0) { /* zero ==> flush */ |
113 |
|
|
fflush(stdout); |
114 |
|
|
continue; |
115 |
|
|
} |
116 |
greg |
1.14 |
samplendx++; |
117 |
greg |
1.1 |
/* compute and print */ |
118 |
greg |
1.13 |
if (imm_irrad) |
119 |
greg |
1.1 |
irrad(orig, direc); |
120 |
|
|
else |
121 |
greg |
1.13 |
traceray(orig, direc); |
122 |
greg |
1.7 |
/* flush if time */ |
123 |
greg |
1.1 |
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 |
greg |
1.11 |
castonly = 1; |
145 |
greg |
1.1 |
while (*vs) |
146 |
|
|
switch (*vs++) { |
147 |
|
|
case 't': /* trace */ |
148 |
|
|
*table = NULL; |
149 |
|
|
table = every_out; |
150 |
|
|
trace = ourtrace; |
151 |
greg |
1.11 |
castonly = 0; |
152 |
greg |
1.1 |
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 |
greg |
1.11 |
castonly = 0; |
162 |
greg |
1.1 |
break; |
163 |
|
|
case 'l': /* length */ |
164 |
|
|
*table++ = oputl; |
165 |
greg |
1.11 |
castonly = 0; |
166 |
greg |
1.1 |
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 |
greg |
1.13 |
traceray(org, dir) /* compute and print ray value(s) */ |
188 |
greg |
1.1 |
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 |
greg |
1.11 |
if (castonly) |
196 |
|
|
localhit(&thisray, &thescene) || sourcehit(&thisray); |
197 |
|
|
else |
198 |
|
|
rayvalue(&thisray); |
199 |
greg |
1.1 |
|
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 |
greg |
1.13 |
irrad(org, dir) /* compute immediate irradiance value */ |
210 |
greg |
1.1 |
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 |
greg |
1.19 |
extern char *fgetword(); |
239 |
|
|
extern double atof(); |
240 |
greg |
1.1 |
static float vf[3]; |
241 |
greg |
1.19 |
char buf[32]; |
242 |
|
|
register int i; |
243 |
greg |
1.1 |
|
244 |
|
|
switch (fmt) { |
245 |
|
|
case 'a': /* ascii */ |
246 |
greg |
1.19 |
for (i = 0; i < 3; i++) { |
247 |
|
|
if (fgetword(buf, sizeof(buf), fp) == NULL || |
248 |
|
|
!isflt(buf)) |
249 |
|
|
return(-1); |
250 |
|
|
vec[i] = atof(buf); |
251 |
|
|
} |
252 |
greg |
1.1 |
break; |
253 |
|
|
case 'f': /* binary float */ |
254 |
greg |
1.8 |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
255 |
greg |
1.1 |
return(-1); |
256 |
|
|
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
257 |
|
|
break; |
258 |
|
|
case 'd': /* binary double */ |
259 |
greg |
1.8 |
if (fread((char *)vec, sizeof(double), 3, fp) != 3) |
260 |
greg |
1.1 |
return(-1); |
261 |
|
|
break; |
262 |
|
|
} |
263 |
|
|
return(0); |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
|
267 |
|
|
static |
268 |
|
|
ourtrace(r) /* print ray values */ |
269 |
|
|
RAY *r; |
270 |
|
|
{ |
271 |
|
|
register int (**tp)(); |
272 |
|
|
|
273 |
|
|
if (every_out[0] == NULL) |
274 |
|
|
return; |
275 |
|
|
tabin(r); |
276 |
|
|
for (tp = every_out; *tp != NULL; tp++) |
277 |
|
|
(**tp)(r); |
278 |
|
|
putchar('\n'); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
static |
283 |
|
|
tabin(r) /* tab in appropriate amount */ |
284 |
|
|
RAY *r; |
285 |
|
|
{ |
286 |
|
|
register RAY *rp; |
287 |
|
|
|
288 |
|
|
for (rp = r->parent; rp != NULL; rp = rp->parent) |
289 |
|
|
putchar('\t'); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
|
293 |
|
|
static |
294 |
|
|
oputo(r) /* print origin */ |
295 |
|
|
register RAY *r; |
296 |
|
|
{ |
297 |
|
|
(*putreal)(r->rorg[0]); |
298 |
|
|
(*putreal)(r->rorg[1]); |
299 |
|
|
(*putreal)(r->rorg[2]); |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
|
303 |
|
|
static |
304 |
|
|
oputd(r) /* print direction */ |
305 |
|
|
register RAY *r; |
306 |
|
|
{ |
307 |
|
|
(*putreal)(r->rdir[0]); |
308 |
|
|
(*putreal)(r->rdir[1]); |
309 |
|
|
(*putreal)(r->rdir[2]); |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
|
313 |
|
|
static |
314 |
|
|
oputv(r) /* print value */ |
315 |
|
|
register RAY *r; |
316 |
|
|
{ |
317 |
|
|
(*putreal)(colval(r->rcol,RED)); |
318 |
|
|
(*putreal)(colval(r->rcol,GRN)); |
319 |
|
|
(*putreal)(colval(r->rcol,BLU)); |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
|
323 |
|
|
static |
324 |
|
|
oputl(r) /* print length */ |
325 |
|
|
register RAY *r; |
326 |
|
|
{ |
327 |
greg |
1.9 |
(*putreal)(r->rt); |
328 |
greg |
1.1 |
} |
329 |
|
|
|
330 |
|
|
|
331 |
|
|
static |
332 |
|
|
oputp(r) /* print point */ |
333 |
|
|
register RAY *r; |
334 |
|
|
{ |
335 |
|
|
if (r->rot < FHUGE) { |
336 |
|
|
(*putreal)(r->rop[0]); |
337 |
|
|
(*putreal)(r->rop[1]); |
338 |
|
|
(*putreal)(r->rop[2]); |
339 |
|
|
} else { |
340 |
|
|
(*putreal)(0.0); |
341 |
|
|
(*putreal)(0.0); |
342 |
|
|
(*putreal)(0.0); |
343 |
|
|
} |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
|
347 |
|
|
static |
348 |
|
|
oputn(r) /* print normal */ |
349 |
|
|
register RAY *r; |
350 |
|
|
{ |
351 |
|
|
if (r->rot < FHUGE) { |
352 |
|
|
(*putreal)(r->ron[0]); |
353 |
|
|
(*putreal)(r->ron[1]); |
354 |
|
|
(*putreal)(r->ron[2]); |
355 |
|
|
} else { |
356 |
|
|
(*putreal)(0.0); |
357 |
|
|
(*putreal)(0.0); |
358 |
|
|
(*putreal)(0.0); |
359 |
|
|
} |
360 |
|
|
} |
361 |
|
|
|
362 |
|
|
|
363 |
|
|
static |
364 |
|
|
oputs(r) /* print name */ |
365 |
|
|
register RAY *r; |
366 |
|
|
{ |
367 |
|
|
if (r->ro != NULL) |
368 |
|
|
fputs(r->ro->oname, stdout); |
369 |
|
|
else |
370 |
|
|
putchar('*'); |
371 |
|
|
putchar('\t'); |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
|
375 |
|
|
static |
376 |
|
|
oputw(r) /* print weight */ |
377 |
|
|
register RAY *r; |
378 |
|
|
{ |
379 |
|
|
(*putreal)(r->rweight); |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
|
383 |
|
|
static |
384 |
|
|
oputm(r) /* print modifier */ |
385 |
|
|
register RAY *r; |
386 |
|
|
{ |
387 |
|
|
if (r->ro != NULL) |
388 |
|
|
fputs(objptr(r->ro->omod)->oname, stdout); |
389 |
|
|
else |
390 |
|
|
putchar('*'); |
391 |
|
|
putchar('\t'); |
392 |
|
|
} |
393 |
|
|
|
394 |
|
|
|
395 |
|
|
static |
396 |
|
|
puta(v) /* print ascii value */ |
397 |
|
|
double v; |
398 |
|
|
{ |
399 |
|
|
printf("%e\t", v); |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
|
403 |
|
|
static |
404 |
|
|
putd(v) /* print binary double */ |
405 |
|
|
double v; |
406 |
|
|
{ |
407 |
greg |
1.8 |
fwrite((char *)&v, sizeof(v), 1, stdout); |
408 |
greg |
1.1 |
} |
409 |
|
|
|
410 |
|
|
|
411 |
|
|
static |
412 |
|
|
putf(v) /* print binary float */ |
413 |
|
|
double v; |
414 |
|
|
{ |
415 |
|
|
float f = v; |
416 |
|
|
|
417 |
greg |
1.8 |
fwrite((char *)&f, sizeof(f), 1, stdout); |
418 |
greg |
1.1 |
} |