ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 1.9
Committed: Tue Mar 27 11:40:10 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +1 -4 lines
Log Message:
Added rt field to RAY structure for more accurate z-buffering

File Contents

# User Rev Content
1 greg 1.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 greg 1.7 * If the direction vector is (0,0,0), then the output is flushed.
20 greg 1.1 * 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 greg 1.4 double shadthresh = .05; /* shadow threshold */
40 greg 1.5 double shadcert = .5; /* shadow certainty */
41 greg 1.1
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 greg 1.6 int ambres = 32; /* ambient resolution */
48 greg 1.1 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 greg 1.2 FVECT orig, direc;
80 greg 1.1 /* 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 greg 1.7 if (normalize(direc) == 0.0) { /* zero ==> flush */
99     fflush(stdout);
100     continue;
101     }
102 greg 1.1 /* compute and print */
103     if (outvals[0] == 'i')
104     irrad(orig, direc);
105     else
106     radiance(orig, direc);
107 greg 1.7 /* flush if time */
108 greg 1.1 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 greg 1.8 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
230 greg 1.1 return(-1);
231     vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
232     break;
233     case 'd': /* binary double */
234 greg 1.8 if (fread((char *)vec, sizeof(double), 3, fp) != 3)
235 greg 1.1 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 greg 1.9 (*putreal)(r->rt);
303 greg 1.1 }
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 greg 1.8 fwrite((char *)&v, sizeof(v), 1, stdout);
383 greg 1.1 }
384    
385    
386     static
387     putf(v) /* print binary float */
388     double v;
389     {
390     float f = v;
391    
392 greg 1.8 fwrite((char *)&f, sizeof(f), 1, stdout);
393 greg 1.1 }