ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 1.1
Committed: Thu Feb 2 10:41:37 1989 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

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