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