ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.35
Committed: Fri Apr 19 10:39:09 1991 UTC (34 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.34: +6 -0 lines
Log Message:
added format info to headers

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