ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.30
Committed: Mon Nov 26 11:57:40 1990 UTC (34 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.29: +7 -0 lines
Log Message:
added setcompat() call for proper signal handling on Mac

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