ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.23
Committed: Thu Jan 11 10:09:22 1990 UTC (35 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.22: +4 -6 lines
Log Message:
changed interface to normaspect()

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