ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.17
Committed: Wed Jul 26 12:24:32 1989 UTC (35 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +2 -0 lines
Log Message:
made RAYPATH redefinable from Makefile

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