ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.39
Committed: Wed Jun 19 16:36:41 1991 UTC (33 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.38: +6 -0 lines
Log Message:
added virtual sources

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