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