ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.36
Committed: Wed May 1 11:16:54 1991 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.35: +9 -1 lines
Log Message:
added -i option for irradiance calculation

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