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