ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.31
Committed: Thu Dec 13 10:44:01 1990 UTC (34 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.30: +2 -0 lines
Log Message:
changed initialization of ofun[]

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     * rmain.c - main for ray tracing programs
9     *
10     * 3/24/87
11     */
12    
13     /* for flaky pre-processors */
14     #ifndef RPICT
15     #define RPICT 0
16     #endif
17     #ifndef RTRACE
18     #define RTRACE 0
19     #endif
20     #ifndef RVIEW
21     #define RVIEW 0
22     #endif
23    
24     #include "ray.h"
25    
26     #include "octree.h"
27    
28     #include <signal.h>
29    
30     #include "view.h"
31    
32     #ifndef DEFPATH
33     #define DEFPATH ":/usr/local/lib/ray"
34     #endif
35 greg 1.17 #ifndef ULIBVAR
36 greg 1.1 #define ULIBVAR "RAYPATH"
37 greg 1.17 #endif
38 greg 1.1
39     char *progname; /* argv[0] */
40    
41 greg 1.19 char *octname; /* octree name */
42    
43 greg 1.1 char *libpath; /* library directory list */
44    
45     char *sigerr[NSIG]; /* signal error messages */
46    
47     extern int stderr_v(); /* standard error output */
48     int (*errvec)() = stderr_v; /* error output vector */
49     int (*wrnvec)() = stderr_v; /* warning output vector */
50     int (*cmdvec)() = NULL; /* command error vector */
51    
52     int (*trace)() = NULL; /* trace call */
53    
54 greg 1.24 extern int ambnotify(); /* new object notify functions */
55     int (*addobjnotify[])() = {ambnotify, NULL};
56    
57 greg 1.1 CUBE thescene; /* our scene */
58    
59     extern int ralrm; /* seconds between reports */
60     extern float pctdone; /* percentage done */
61    
62     extern int greyscale; /* map colors to brightness? */
63     extern char *devname; /* output device name */
64    
65     extern int inform; /* input format */
66     extern int outform; /* output format */
67     extern char *outvals; /* output values */
68    
69     extern VIEW ourview; /* viewing parameters */
70    
71     extern int hresolu; /* horizontal resolution */
72     extern int vresolu; /* vertical resolution */
73 greg 1.22 extern double pixaspect; /* pixel aspect ratio */
74 greg 1.1
75     extern int psample; /* pixel sample size */
76     extern double maxdiff; /* max. sample difference */
77 greg 1.11 extern double dstrpix; /* square pixel distribution */
78 greg 1.1
79     extern double dstrsrc; /* square source distribution */
80 greg 1.11 extern double shadthresh; /* shadow threshold */
81 greg 1.15 extern double shadcert; /* shadow testing certainty */
82 greg 1.1
83     extern int maxdepth; /* maximum recursion depth */
84     extern double minweight; /* minimum ray weight */
85     extern long nrays; /* number of rays traced */
86    
87     extern COLOR ambval; /* ambient value */
88     extern double ambacc; /* ambient accuracy */
89     extern int ambres; /* ambient resolution */
90     extern int ambdiv; /* ambient divisions */
91     extern int ambssamp; /* ambient super-samples */
92     extern int ambounce; /* ambient bounces */
93     extern char *amblist[]; /* ambient include/exclude list */
94     extern int ambincl; /* include == 1, exclude == 0 */
95    
96    
97     main(argc, argv)
98     int argc;
99     char *argv[];
100     {
101 greg 1.16 #define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt
102 greg 1.1 double atof();
103     char *getenv();
104     int report();
105     char *err;
106     char *recover = NULL;
107 greg 1.20 char *zfile = NULL;
108 greg 1.1 char *errfile = NULL;
109     char *ambfile = NULL;
110     char **amblp = amblist;
111     int loadflags = ~IO_FILES;
112 greg 1.4 int rval, gotvfile = 0;
113 greg 1.1 int i;
114     /* global program name */
115     progname = argv[0];
116     /* get library path */
117     if ((libpath = getenv(ULIBVAR)) == NULL)
118     libpath = DEFPATH;
119 greg 1.31 /* initialize object types */
120     initotypes();
121 greg 1.10 /* option city */
122 greg 1.1 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
123 greg 1.18 if (!strcmp(argv[i], "-defaults") ||
124     !strcmp(argv[i], "-help")) {
125 greg 1.1 printdefaults();
126     quit(0);
127     }
128     #if RVIEW
129     if (!strcmp(argv[i], "-devices")) {
130     printdevices();
131     quit(0);
132     }
133     #endif
134 greg 1.21 #if RPICT|RVIEW
135     rval = getviewopt(&ourview, argc-i, argv+i);
136     if (rval >= 0) {
137     i += rval;
138     continue;
139     }
140     #endif
141 greg 1.1 switch (argv[i][1]) {
142     #if RPICT|RVIEW
143 greg 1.21 case 'v': /* view file */
144     if (argv[i][2] != 'f')
145 greg 1.10 goto badopt;
146 greg 1.21 check(3,1);
147 greg 1.25 rval = viewfile(argv[++i], &ourview, 0, 0);
148 greg 1.21 if (rval < 0) {
149     sprintf(errmsg,
150     "cannot open view file \"%s\"",
151     argv[i]);
152     error(SYSTEM, errmsg);
153     } else if (rval == 0) {
154     sprintf(errmsg,
155     "bad view file \"%s\"",
156     argv[i]);
157     error(USER, errmsg);
158     } else
159     gotvfile += rval;
160 greg 1.1 break;
161     #endif
162 greg 1.11 case 'd': /* direct */
163 greg 1.1 switch (argv[i][2]) {
164 greg 1.13 case 't': /* threshold */
165 greg 1.10 check(3,1);
166 greg 1.11 shadthresh = atof(argv[++i]);
167 greg 1.1 break;
168 greg 1.15 case 'c': /* certainty */
169     check(3,1);
170     shadcert = atof(argv[++i]);
171     break;
172 greg 1.11 case 'j': /* jitter */
173 greg 1.10 check(3,1);
174 greg 1.1 dstrsrc = atof(argv[++i]);
175     break;
176     default:
177 greg 1.10 goto badopt;
178 greg 1.1 }
179     break;
180     #if RPICT|RVIEW
181     case 's': /* sample */
182     switch (argv[i][2]) {
183     case 'p': /* pixel */
184 greg 1.10 check(3,1);
185 greg 1.1 psample = atoi(argv[++i]);
186     break;
187 greg 1.13 case 't': /* threshold */
188 greg 1.10 check(3,1);
189 greg 1.1 maxdiff = atof(argv[++i]);
190     break;
191 greg 1.11 #if RPICT
192     case 'j': /* jitter */
193     check(3,1);
194     dstrpix = atof(argv[++i]);
195     break;
196     #endif
197 greg 1.1 default:
198 greg 1.10 goto badopt;
199 greg 1.1 }
200     break;
201     #endif
202 greg 1.22 #if RPICT|RTRACE
203 greg 1.1 case 'x': /* x resolution */
204 greg 1.10 check(2,1);
205 greg 1.1 hresolu = atoi(argv[++i]);
206     break;
207     case 'y': /* y resolution */
208 greg 1.10 check(2,1);
209 greg 1.1 vresolu = atoi(argv[++i]);
210     break;
211 greg 1.22 #endif
212     #if RPICT
213     case 'p': /* pixel aspect */
214 greg 1.23 check(2,1);
215 greg 1.22 pixaspect = atof(argv[++i]);
216     break;
217     #endif
218 greg 1.1 case 'w': /* warnings */
219 greg 1.10 check(2,0);
220 greg 1.1 wrnvec = wrnvec==NULL ? stderr_v : NULL;
221     break;
222     case 'e': /* error file */
223 greg 1.10 check(2,1);
224 greg 1.1 errfile = argv[++i];
225     break;
226     case 'l': /* limit */
227     switch (argv[i][2]) {
228     case 'r': /* recursion */
229 greg 1.10 check(3,1);
230 greg 1.1 maxdepth = atoi(argv[++i]);
231     break;
232     case 'w': /* weight */
233 greg 1.10 check(3,1);
234 greg 1.1 minweight = atof(argv[++i]);
235     break;
236     default:
237 greg 1.10 goto badopt;
238 greg 1.1 }
239     break;
240     #if RPICT
241 greg 1.20 case 'z': /* z file */
242     check(2,1);
243     zfile = argv[++i];
244     break;
245 greg 1.1 case 'r': /* recover file */
246 greg 1.10 check(2,1);
247 greg 1.1 recover = argv[++i];
248 greg 1.25 rval = viewfile(recover, &ourview, &hresolu, &vresolu);
249 greg 1.5 if (rval <= 0) {
250     sprintf(errmsg,
251     "cannot recover view parameters from \"%s\"", recover);
252     error(WARNING, errmsg);
253 greg 1.25 } else {
254 greg 1.4 gotvfile += rval;
255 greg 1.25 pixaspect = 0.0;
256     }
257 greg 1.1 break;
258     case 't': /* timer */
259 greg 1.10 check(2,1);
260 greg 1.1 ralrm = atoi(argv[++i]);
261     break;
262     #endif
263     case 'a': /* ambient */
264     switch (argv[i][2]) {
265     case 'v': /* value */
266 greg 1.10 check(3,3);
267 greg 1.1 setcolor(ambval, atof(argv[i+1]),
268     atof(argv[i+2]),
269     atof(argv[i+3]));
270     i += 3;
271     break;
272     case 'a': /* accuracy */
273 greg 1.10 check(3,1);
274 greg 1.1 ambacc = atof(argv[++i]);
275     break;
276     case 'r': /* resolution */
277 greg 1.10 check(3,1);
278 greg 1.1 ambres = atoi(argv[++i]);
279     break;
280     case 'd': /* divisions */
281 greg 1.10 check(3,1);
282 greg 1.1 ambdiv = atoi(argv[++i]);
283     break;
284     case 's': /* super-samp */
285 greg 1.10 check(3,1);
286 greg 1.1 ambssamp = atoi(argv[++i]);
287     break;
288     case 'b': /* bounces */
289 greg 1.10 check(3,1);
290 greg 1.1 ambounce = atoi(argv[++i]);
291     break;
292     case 'i': /* include */
293 greg 1.10 check(3,1);
294 greg 1.1 if (ambincl != 1) {
295     ambincl = 1;
296     amblp = amblist;
297     }
298     *amblp++ = argv[++i];
299     break;
300     case 'e': /* exclude */
301 greg 1.10 check(3,1);
302 greg 1.1 if (ambincl != 0) {
303     ambincl = 0;
304     amblp = amblist;
305     }
306     *amblp++ = argv[++i];
307     break;
308     case 'f': /* file */
309 greg 1.10 check(3,1);
310 greg 1.1 ambfile= argv[++i];
311     break;
312     default:
313 greg 1.10 goto badopt;
314 greg 1.1 }
315     break;
316     #if RTRACE
317     case 'f': /* format i/o */
318     switch (argv[i][2]) {
319     case 'a': /* ascii */
320     case 'f': /* float */
321     case 'd': /* double */
322     inform = argv[i][2];
323     break;
324     default:
325 greg 1.10 goto badopt;
326 greg 1.1 }
327     switch (argv[i][3]) {
328     case '\0':
329     outform = inform;
330     break;
331     case 'a': /* ascii */
332     case 'f': /* float */
333     case 'd': /* double */
334 greg 1.12 check(4,0);
335 greg 1.1 outform = argv[i][3];
336     break;
337     default:
338 greg 1.10 goto badopt;
339 greg 1.1 }
340     break;
341     case 'o': /* output */
342     outvals = argv[i]+2;
343     break;
344 greg 1.6 case 'h': /* toggle header */
345 greg 1.10 check(2,0);
346 greg 1.6 loadflags ^= IO_INFO;
347 greg 1.1 break;
348     #endif
349     #if RVIEW
350     case 'b': /* black and white */
351 greg 1.10 check(2,0);
352 greg 1.1 greyscale = !greyscale;
353     break;
354     case 'o': /* output device */
355 greg 1.10 check(2,1);
356 greg 1.1 devname = argv[++i];
357     break;
358     #endif
359     default:
360 greg 1.19 goto badopt;
361 greg 1.1 }
362     }
363 greg 1.24 *amblp = NULL;
364 greg 1.1 #if RPICT|RVIEW
365     err = setview(&ourview); /* set viewing parameters */
366     if (err != NULL)
367     error(USER, err);
368     #endif
369 greg 1.22 #if RPICT
370 greg 1.23 normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
371 greg 1.22 #endif
372 greg 1.1 /* set up signal handling */
373 greg 1.30 #ifdef _AUX_SOURCE
374     #include <compat.h>
375     if ((rval = getcompat()) >= 0) {
376     rval |= COMPAT_BSDSIGNALS | COMPAT_SYSCALLS;
377     setcompat(rval);
378     }
379     #endif
380 greg 1.1 sigdie(SIGINT, "Interrupt");
381     sigdie(SIGHUP, "Hangup");
382     sigdie(SIGTERM, "Terminate");
383     sigdie(SIGPIPE, "Broken pipe");
384     #ifdef SIGXCPU
385     sigdie(SIGXCPU, "CPU limit exceeded");
386     sigdie(SIGXFSZ, "File size exceeded");
387     #endif
388     #if RPICT
389     signal(SIGALRM, report);
390     #else
391     sigdie(SIGALRM, "Alarm clock");
392     #endif
393     /* open error file */
394     if (errfile != NULL) {
395     if (freopen(errfile, "a", stderr) == NULL)
396 greg 1.14 quit(2);
397 greg 1.1 fprintf(stderr, "**************\n*** PID %5d: ",
398     getpid());
399     printargs(argc, argv, stderr);
400     fputs("\n", stderr);
401     fflush(stderr);
402     }
403     #ifdef NICE
404     nice(NICE); /* lower priority */
405     #endif
406 greg 1.19 /* get octree */
407 greg 1.1 #if RVIEW
408     loadflags &= ~IO_INFO;
409     #endif
410     if (i == argc)
411 greg 1.19 octname = NULL;
412     else if (i == argc-1)
413     octname = argv[i];
414 greg 1.1 else
415 greg 1.19 goto badopt;
416     #if RVIEW|RTRACE
417     if (octname == NULL)
418     error(USER, "missing octree argument");
419 greg 1.1 #endif
420 greg 1.19 readoct(octname, loadflags, &thescene, NULL);
421 greg 1.1
422     if (loadflags & IO_INFO) { /* print header */
423     printargs(i, argv, stdout);
424     #if RPICT
425     if (gotvfile) {
426     printf(VIEWSTR);
427     fprintview(&ourview, stdout);
428     printf("\n");
429     }
430 greg 1.23 if (pixaspect < .99 || pixaspect > 1.01)
431 greg 1.22 fputaspect(pixaspect, stdout);
432 greg 1.1 #endif
433     printf("\n");
434     }
435    
436     marksources(); /* find and mark sources */
437    
438 greg 1.24 setambient(ambfile); /* initialize ambient calculation */
439 greg 1.1
440     #if RPICT
441     if (ralrm > 0) /* report init time */
442     report();
443 greg 1.20 render(zfile, recover); /* render the scene */
444 greg 1.1 #endif
445     #if RTRACE
446     rtrace(NULL); /* trace rays from stdin */
447     #endif
448     #if RVIEW
449     rview(); /* go */
450     #endif
451     quit(0);
452 greg 1.19
453     badopt:
454     sprintf(errmsg, "command line error at '%s'", argv[i]);
455     error(USER, errmsg);
456    
457 greg 1.10 #undef check
458 greg 1.1 }
459    
460    
461     eputs(s) /* error output */
462     char *s;
463     {
464     if (errvec != NULL)
465     (*errvec)(s);
466     }
467    
468    
469     wputs(s) /* warning output */
470     char *s;
471     {
472     if (wrnvec != NULL)
473     (*wrnvec)(s);
474     }
475    
476    
477     cputs(s) /* command error output */
478     char *s;
479     {
480     if (cmdvec != NULL)
481     (*cmdvec)(s);
482     }
483    
484    
485     stderr_v(s) /* put string to stderr */
486     register char *s;
487     {
488 greg 1.26 static int midline = 0;
489 greg 1.1
490 greg 1.27 if (!*s)
491     return;
492 greg 1.26 if (!midline++) {
493 greg 1.1 fputs(progname, stderr);
494     fputs(": ", stderr);
495     }
496     fputs(s, stderr);
497 greg 1.27 if (s[strlen(s)-1] == '\n') {
498 greg 1.1 fflush(stderr);
499 greg 1.26 midline = 0;
500 greg 1.1 }
501     }
502    
503    
504     onsig(signo) /* fatal signal */
505     int signo;
506     {
507 greg 1.8 static int gotsig = 0;
508    
509     if (gotsig++) /* two signals and we're gone! */
510 greg 1.9 _exit(signo);
511 greg 1.8
512 greg 1.1 eputs("signal - ");
513     eputs(sigerr[signo]);
514     eputs("\n");
515 greg 1.14 quit(3);
516 greg 1.1 }
517    
518    
519     sigdie(signo, msg) /* set fatal signal */
520     int signo;
521     char *msg;
522     {
523     if (signal(signo, onsig) == SIG_IGN)
524     signal(signo, SIG_IGN);
525     sigerr[signo] = msg;
526     }
527    
528    
529     printdefaults() /* print default values to stdout */
530     {
531     register char *cp;
532    
533     #if RPICT|RVIEW
534 greg 1.7 printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
535 greg 1.1 ourview.type==VT_PER ? "perspective" :
536     ourview.type==VT_PAR ? "parallel" :
537 greg 1.28 ourview.type==VT_HEM ? "hemispherical" :
538     ourview.type==VT_ANG ? "angular" :
539 greg 1.1 "unknown");
540     printf("-vp %f %f %f\t# view point\n",
541     ourview.vp[0], ourview.vp[1], ourview.vp[2]);
542     printf("-vd %f %f %f\t# view direction\n",
543     ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
544     printf("-vu %f %f %f\t# view up\n",
545     ourview.vup[0], ourview.vup[1], ourview.vup[2]);
546     printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
547     printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
548 greg 1.22 printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
549     printf("-vl %f\t\t\t# view lift\n", ourview.voff);
550 greg 1.1 #endif
551 greg 1.22 #if RPICT|RTRACE
552 greg 1.1 printf("-x %-9d\t\t\t# x resolution\n", hresolu);
553     printf("-y %-9d\t\t\t# y resolution\n", vresolu);
554 greg 1.22 #endif
555     #if RPICT
556 greg 1.23 printf("-p %f\t\t\t# pixel aspect ratio\n", pixaspect);
557 greg 1.22 #endif
558 greg 1.1 #if RPICT|RVIEW
559     printf("-sp %-9d\t\t\t# sample pixel\n", psample);
560 greg 1.13 printf("-st %f\t\t\t# sample threshold\n", maxdiff);
561 greg 1.22 #endif
562 greg 1.11 #if RPICT
563     printf("-sj %f\t\t\t# sample jitter\n", dstrpix);
564 greg 1.1 #endif
565 greg 1.29 printf("-dt %f\t\t\t# direct threshold\n", shadthresh);
566     printf("-dc %f\t\t\t# direct certainty\n", shadcert);
567     printf("-dj %f\t\t\t# direct jitter\n", dstrsrc);
568 greg 1.1 printf("-av %f %f %f\t# ambient value\n", colval(ambval,RED),
569     colval(ambval,GRN), colval(ambval, BLU));
570     printf("-ab %-9d\t\t\t# ambient bounces\n", ambounce);
571     printf("-aa %f\t\t\t# ambient accuracy\n", ambacc);
572     printf("-ar %-9d\t\t\t# ambient resolution\n", ambres);
573     printf("-ad %-9d\t\t\t# ambient divisions\n", ambdiv);
574     printf("-as %-9d\t\t\t# ambient super-samples\n", ambssamp);
575     printf("-lr %-9d\t\t\t# limit reflection\n", maxdepth);
576     printf("-lw %f\t\t\t# limit weight\n", minweight);
577     #if RPICT
578     printf("-t %-9d\t\t\t# time between reports\n", ralrm);
579     #endif
580     #if RVIEW
581     printf("-o %s\t\t\t\t# output device\n", devname);
582     #endif
583     #if RTRACE
584     printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
585     inform, outform,
586     inform=='a' ? "ascii" :
587     inform=='f' ? "float" : "double",
588     outform=='a' ? "ascii" :
589     outform=='f' ? "float" : "double");
590     printf("-o%s\t\t\t\t# output", outvals);
591     for (cp = outvals; *cp; cp++)
592     switch (*cp) {
593     case 'i': printf(" irradiance"); break;
594     case 't': printf(" trace"); break;
595     case 'o': printf(" origin"); break;
596     case 'd': printf(" direction"); break;
597     case 'v': printf(" value"); break;
598     case 'l': printf(" length"); break;
599     case 'p': printf(" point"); break;
600     case 'n': printf(" normal"); break;
601     case 's': printf(" surface"); break;
602     case 'w': printf(" weight"); break;
603     case 'm': printf(" modifier"); break;
604     }
605     printf("\n");
606     #endif
607     }