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