ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.46
Committed: Thu Oct 10 12:08:53 1991 UTC (33 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.45: +7 -0 lines
Log Message:
changed SOFT= to SOFTWARE=

File Contents

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