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