ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.13
Committed: Mon Mar 8 12:37:33 1993 UTC (31 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +2 -1 lines
Log Message:
portability fixes (removed gcc warnings)

File Contents

# User Rev Content
1 greg 2.6 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
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 greg 1.13 * radiance is computed. The '-i' or '-I' options indicate that
24 greg 1.1 * irradiance values are desired.
25     */
26    
27     #include "ray.h"
28    
29 greg 1.11 #include "octree.h"
30    
31 greg 1.1 #include "otypes.h"
32    
33 greg 2.6 #include "resolu.h"
34    
35 greg 1.14 int dimlist[MAXDIM]; /* sampling dimensions */
36     int ndims = 0; /* number of sampling dimensions */
37     int samplendx = 0; /* index for this sample */
38    
39 greg 1.13 int imm_irrad = 0; /* compute immediate irradiance? */
40    
41 greg 1.1 int inform = 'a'; /* input format */
42     int outform = 'a'; /* output format */
43     char *outvals = "v"; /* output specification */
44    
45     int hresolu = 0; /* horizontal (scan) size */
46     int vresolu = 0; /* vertical resolution */
47    
48     double dstrsrc = 0.0; /* square source distribution */
49 greg 1.4 double shadthresh = .05; /* shadow threshold */
50 greg 1.5 double shadcert = .5; /* shadow certainty */
51 greg 1.20 int directrelay = 1; /* number of source relays */
52 greg 1.17 int vspretest = 512; /* virtual source pretest density */
53 greg 2.10 int directvis = 1; /* sources visible? */
54 greg 1.20 double srcsizerat = .25; /* maximum ratio source size/dist. */
55 greg 1.1
56 greg 2.4 double specthresh = .15; /* specular sampling threshold */
57 greg 2.3 double specjitter = 1.; /* specular sampling jitter */
58    
59 greg 1.1 int maxdepth = 6; /* maximum recursion depth */
60     double minweight = 4e-3; /* minimum ray weight */
61    
62     COLOR ambval = BLKCOLOR; /* ambient value */
63     double ambacc = 0.2; /* ambient accuracy */
64 greg 1.6 int ambres = 32; /* ambient resolution */
65 greg 1.1 int ambdiv = 128; /* ambient divisions */
66     int ambssamp = 0; /* ambient super-samples */
67     int ambounce = 0; /* ambient bounces */
68     char *amblist[128]; /* ambient include/exclude list */
69     int ambincl = -1; /* include == 1, exclude == 0 */
70    
71 greg 1.13 extern OBJREC Lamb; /* a Lambertian surface */
72    
73 greg 1.1 static RAY thisray; /* for our convenience */
74    
75 greg 2.5 static int oputo(), oputd(), oputv(), oputl(), oputL(),
76 greg 2.9 oputp(), oputn(), oputN(), oputs(), oputw(), oputm();
77 greg 1.1
78 greg 2.13 static int ourtrace(), tabin();
79 greg 1.1 static int (*ray_out[10])(), (*every_out[10])();
80 greg 1.11 static int castonly;
81 greg 1.1
82 greg 1.10 static int puta(), putf(), putd();
83 greg 1.1
84     static int (*putreal)();
85    
86    
87     quit(code) /* quit program */
88     int code;
89     {
90 greg 2.11 #ifndef NIX
91     headclean(); /* delete header file */
92     pfclean(); /* clean up persist files */
93     #endif
94 greg 1.1 exit(code);
95     }
96    
97    
98 greg 2.6 char *
99     formstr(f) /* return format identifier */
100     int f;
101     {
102     switch (f) {
103     case 'a': return("ascii");
104     case 'f': return("float");
105     case 'd': return("double");
106     case 'c': return(COLRFMT);
107     }
108     return("unknown");
109     }
110    
111    
112 greg 1.1 rtrace(fname) /* trace rays from file */
113     char *fname;
114     {
115     long vcount = hresolu>1 ? hresolu*vresolu : vresolu;
116     long nextflush = hresolu;
117     FILE *fp;
118 greg 1.2 FVECT orig, direc;
119 greg 1.1 /* set up input */
120     if (fname == NULL)
121     fp = stdin;
122     else if ((fp = fopen(fname, "r")) == NULL) {
123     sprintf(errmsg, "cannot open input file \"%s\"", fname);
124     error(SYSTEM, errmsg);
125     }
126 greg 2.8 #ifdef MSDOS
127     if (inform != 'a')
128     setmode(fileno(fp), O_BINARY);
129     #endif
130 greg 1.1 /* set up output */
131     setoutput(outvals);
132     switch (outform) {
133     case 'a': putreal = puta; break;
134     case 'f': putreal = putf; break;
135     case 'd': putreal = putd; break;
136 greg 2.6 case 'c':
137     if (strcmp(outvals, "v"))
138     error(USER, "color format with value output only");
139     break;
140     default:
141     error(CONSISTENCY, "botched output format");
142 greg 1.1 }
143 greg 2.12 if (hresolu > 0) {
144     if (vresolu > 0)
145     fprtresolu(hresolu, vresolu, stdout);
146     fflush(stdout);
147     }
148 greg 1.1 /* process file */
149     while (getvec(orig, inform, fp) == 0 &&
150     getvec(direc, inform, fp) == 0) {
151    
152 greg 1.7 if (normalize(direc) == 0.0) { /* zero ==> flush */
153     fflush(stdout);
154     continue;
155     }
156 greg 1.14 samplendx++;
157 greg 1.1 /* compute and print */
158 greg 1.13 if (imm_irrad)
159 greg 1.1 irrad(orig, direc);
160     else
161 greg 1.13 traceray(orig, direc);
162 greg 1.7 /* flush if time */
163 greg 1.1 if (--nextflush == 0) {
164     fflush(stdout);
165     nextflush = hresolu;
166     }
167     if (ferror(stdout))
168     error(SYSTEM, "write error");
169     if (--vcount == 0) /* check for end */
170     break;
171     }
172 greg 2.12 fflush(stdout);
173 greg 1.1 if (vcount > 0)
174     error(USER, "read error");
175 greg 2.12 if (fname != NULL)
176     fclose(fp);
177 greg 1.1 }
178    
179    
180     setoutput(vs) /* set up output tables */
181     register char *vs;
182     {
183 greg 2.13 extern int (*trace)();
184 greg 1.1 register int (**table)() = ray_out;
185    
186 greg 1.11 castonly = 1;
187 greg 1.1 while (*vs)
188     switch (*vs++) {
189     case 't': /* trace */
190     *table = NULL;
191     table = every_out;
192     trace = ourtrace;
193 greg 1.11 castonly = 0;
194 greg 1.1 break;
195     case 'o': /* origin */
196     *table++ = oputo;
197     break;
198     case 'd': /* direction */
199     *table++ = oputd;
200     break;
201     case 'v': /* value */
202     *table++ = oputv;
203 greg 1.11 castonly = 0;
204 greg 1.1 break;
205 greg 2.5 case 'l': /* effective distance */
206 greg 1.1 *table++ = oputl;
207 greg 1.11 castonly = 0;
208 greg 1.1 break;
209 greg 2.5 case 'L': /* single ray length */
210     *table++ = oputL;
211     break;
212 greg 1.1 case 'p': /* point */
213     *table++ = oputp;
214     break;
215 greg 2.9 case 'n': /* perturbed normal */
216 greg 1.1 *table++ = oputn;
217 greg 2.9 castonly = 0;
218 greg 1.1 break;
219 greg 2.9 case 'N': /* unperturbed normal */
220     *table++ = oputN;
221     break;
222 greg 1.1 case 's': /* surface */
223     *table++ = oputs;
224     break;
225     case 'w': /* weight */
226     *table++ = oputw;
227     break;
228     case 'm': /* modifier */
229     *table++ = oputm;
230     break;
231     }
232     *table = NULL;
233     }
234    
235    
236 greg 1.13 traceray(org, dir) /* compute and print ray value(s) */
237 greg 1.1 FVECT org, dir;
238     {
239     register int (**tp)();
240    
241     VCOPY(thisray.rorg, org);
242     VCOPY(thisray.rdir, dir);
243     rayorigin(&thisray, NULL, PRIMARY, 1.0);
244 greg 1.11 if (castonly)
245     localhit(&thisray, &thescene) || sourcehit(&thisray);
246     else
247     rayvalue(&thisray);
248 greg 1.1
249     if (ray_out[0] == NULL)
250     return;
251     for (tp = ray_out; *tp != NULL; tp++)
252     (**tp)(&thisray);
253     if (outform == 'a')
254     putchar('\n');
255     }
256    
257    
258 greg 1.13 irrad(org, dir) /* compute immediate irradiance value */
259 greg 1.1 FVECT org, dir;
260     {
261     register int i;
262    
263     for (i = 0; i < 3; i++) {
264     thisray.rorg[i] = org[i] + dir[i];
265     thisray.rdir[i] = -dir[i];
266     }
267     rayorigin(&thisray, NULL, PRIMARY, 1.0);
268     /* pretend we hit surface */
269     thisray.rot = 1.0;
270     thisray.rod = 1.0;
271     VCOPY(thisray.ron, dir);
272     for (i = 0; i < 3; i++) /* fudge factor */
273     thisray.rop[i] = org[i] + 1e-4*dir[i];
274     /* compute and print */
275     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
276     oputv(&thisray);
277     if (outform == 'a')
278     putchar('\n');
279     }
280    
281    
282     getvec(vec, fmt, fp) /* get a vector from fp */
283     register FVECT vec;
284     int fmt;
285     FILE *fp;
286     {
287 greg 1.19 extern char *fgetword();
288 greg 1.1 static float vf[3];
289 greg 2.5 static double vd[3];
290 greg 1.19 char buf[32];
291     register int i;
292 greg 1.1
293     switch (fmt) {
294     case 'a': /* ascii */
295 greg 1.19 for (i = 0; i < 3; i++) {
296     if (fgetword(buf, sizeof(buf), fp) == NULL ||
297     !isflt(buf))
298     return(-1);
299     vec[i] = atof(buf);
300     }
301 greg 1.1 break;
302     case 'f': /* binary float */
303 greg 1.8 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
304 greg 1.1 return(-1);
305     vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
306     break;
307     case 'd': /* binary double */
308 greg 2.5 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
309 greg 1.1 return(-1);
310 greg 2.5 vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2];
311 greg 1.1 break;
312 greg 2.6 default:
313     error(CONSISTENCY, "botched input format");
314 greg 1.1 }
315     return(0);
316     }
317    
318    
319     static
320     ourtrace(r) /* print ray values */
321     RAY *r;
322     {
323     register int (**tp)();
324    
325     if (every_out[0] == NULL)
326     return;
327     tabin(r);
328     for (tp = every_out; *tp != NULL; tp++)
329     (**tp)(r);
330     putchar('\n');
331     }
332    
333    
334     static
335     tabin(r) /* tab in appropriate amount */
336     RAY *r;
337     {
338     register RAY *rp;
339    
340     for (rp = r->parent; rp != NULL; rp = rp->parent)
341     putchar('\t');
342     }
343    
344    
345     static
346     oputo(r) /* print origin */
347     register RAY *r;
348     {
349     (*putreal)(r->rorg[0]);
350     (*putreal)(r->rorg[1]);
351     (*putreal)(r->rorg[2]);
352     }
353    
354    
355     static
356     oputd(r) /* print direction */
357     register RAY *r;
358     {
359     (*putreal)(r->rdir[0]);
360     (*putreal)(r->rdir[1]);
361     (*putreal)(r->rdir[2]);
362     }
363    
364    
365     static
366     oputv(r) /* print value */
367     register RAY *r;
368     {
369 greg 2.6 COLR cout;
370    
371     if (outform == 'c') {
372     setcolr(cout, colval(r->rcol,RED),
373     colval(r->rcol,GRN),
374     colval(r->rcol,BLU));
375     fwrite((char *)cout, sizeof(cout), 1, stdout);
376     return;
377     }
378 greg 1.1 (*putreal)(colval(r->rcol,RED));
379     (*putreal)(colval(r->rcol,GRN));
380     (*putreal)(colval(r->rcol,BLU));
381     }
382    
383    
384     static
385 greg 2.5 oputl(r) /* print effective distance */
386 greg 1.1 register RAY *r;
387     {
388 greg 1.9 (*putreal)(r->rt);
389 greg 2.5 }
390    
391    
392     static
393     oputL(r) /* print single ray length */
394     register RAY *r;
395     {
396     (*putreal)(r->rot);
397 greg 1.1 }
398    
399    
400     static
401     oputp(r) /* print point */
402     register RAY *r;
403     {
404     if (r->rot < FHUGE) {
405     (*putreal)(r->rop[0]);
406     (*putreal)(r->rop[1]);
407     (*putreal)(r->rop[2]);
408     } else {
409     (*putreal)(0.0);
410     (*putreal)(0.0);
411     (*putreal)(0.0);
412     }
413     }
414    
415    
416     static
417 greg 2.9 oputN(r) /* print unperturbed normal */
418 greg 1.1 register RAY *r;
419     {
420     if (r->rot < FHUGE) {
421     (*putreal)(r->ron[0]);
422     (*putreal)(r->ron[1]);
423     (*putreal)(r->ron[2]);
424     } else {
425     (*putreal)(0.0);
426     (*putreal)(0.0);
427     (*putreal)(0.0);
428     }
429 greg 2.9 }
430    
431    
432     static
433     oputn(r) /* print perturbed normal */
434     RAY *r;
435     {
436     FVECT pnorm;
437    
438     if (r->rot >= FHUGE) {
439     (*putreal)(0.0);
440     (*putreal)(0.0);
441     (*putreal)(0.0);
442     return;
443     }
444     raynormal(pnorm, r);
445     (*putreal)(pnorm[0]);
446     (*putreal)(pnorm[1]);
447     (*putreal)(pnorm[2]);
448 greg 1.1 }
449    
450    
451     static
452     oputs(r) /* print name */
453     register RAY *r;
454     {
455     if (r->ro != NULL)
456     fputs(r->ro->oname, stdout);
457     else
458     putchar('*');
459     putchar('\t');
460     }
461    
462    
463     static
464     oputw(r) /* print weight */
465     register RAY *r;
466     {
467     (*putreal)(r->rweight);
468     }
469    
470    
471     static
472     oputm(r) /* print modifier */
473     register RAY *r;
474     {
475     if (r->ro != NULL)
476     fputs(objptr(r->ro->omod)->oname, stdout);
477     else
478     putchar('*');
479     putchar('\t');
480     }
481    
482    
483     static
484     puta(v) /* print ascii value */
485     double v;
486     {
487     printf("%e\t", v);
488     }
489    
490    
491     static
492     putd(v) /* print binary double */
493     double v;
494     {
495 greg 1.8 fwrite((char *)&v, sizeof(v), 1, stdout);
496 greg 1.1 }
497    
498    
499     static
500     putf(v) /* print binary float */
501     double v;
502     {
503     float f = v;
504    
505 greg 1.8 fwrite((char *)&f, sizeof(f), 1, stdout);
506 greg 1.1 }