| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: rvmain.c,v 2.3 2003/06/05 19:29:34 schorsch Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | *  rvmain.c - main for rview interactive viewer | 
| 6 | */ | 
| 7 |  | 
| 8 | #include "copyright.h" | 
| 9 |  | 
| 10 | #include  <signal.h> | 
| 11 |  | 
| 12 | #include  "platform.h" | 
| 13 | #include  "ray.h" | 
| 14 | #include  "source.h" | 
| 15 | #include  "ambient.h" | 
| 16 | #include  "rpaint.h" | 
| 17 | #include  "random.h" | 
| 18 | #include  "paths.h" | 
| 19 | #include  "view.h" | 
| 20 |  | 
| 21 | char  *progname;                        /* argv[0] */ | 
| 22 |  | 
| 23 | char  *octname;                         /* octree name */ | 
| 24 |  | 
| 25 | char  *sigerr[NSIG];                    /* signal error messages */ | 
| 26 |  | 
| 27 | char  *shm_boundary = NULL;             /* boundary of shared memory */ | 
| 28 |  | 
| 29 | char  *errfile = NULL;                  /* error output file */ | 
| 30 |  | 
| 31 | extern int  greyscale;                  /* map colors to brightness? */ | 
| 32 | extern char  *dvcname;                  /* output device name */ | 
| 33 | extern double  exposure;                /* exposure compensation */ | 
| 34 |  | 
| 35 | extern VIEW  ourview;                   /* viewing parameters */ | 
| 36 |  | 
| 37 | extern char  rifname[];                 /* rad input file name */ | 
| 38 |  | 
| 39 | extern int  hresolu;                    /* horizontal resolution */ | 
| 40 | extern int  vresolu;                    /* vertical resolution */ | 
| 41 |  | 
| 42 | extern int  psample;                    /* pixel sample size */ | 
| 43 | extern double  maxdiff;                 /* max. sample difference */ | 
| 44 |  | 
| 45 | static void onsig(int  signo); | 
| 46 | static void sigdie(int  signo, char  *msg); | 
| 47 | static void printdefaults(void); | 
| 48 |  | 
| 49 |  | 
| 50 | int | 
| 51 | main(int  argc, char  *argv[]) | 
| 52 | { | 
| 53 | #define  check(ol,al)           if (argv[i][ol] || \ | 
| 54 | badarg(argc-i-1,argv+i+1,al)) \ | 
| 55 | goto badopt | 
| 56 | #define  bool(olen,var)         switch (argv[i][olen]) { \ | 
| 57 | case '\0': var = !var; break; \ | 
| 58 | case 'y': case 'Y': case 't': case 'T': \ | 
| 59 | case '+': case '1': var = 1; break; \ | 
| 60 | case 'n': case 'N': case 'f': case 'F': \ | 
| 61 | case '-': case '0': var = 0; break; \ | 
| 62 | default: goto badopt; } | 
| 63 | char  *err; | 
| 64 | int  rval; | 
| 65 | int  i; | 
| 66 | /* global program name */ | 
| 67 | progname = argv[0] = fixargv0(argv[0]); | 
| 68 | /* option city */ | 
| 69 | for (i = 1; i < argc; i++) { | 
| 70 | /* expand arguments */ | 
| 71 | while ((rval = expandarg(&argc, &argv, i)) > 0) | 
| 72 | ; | 
| 73 | if (rval < 0) { | 
| 74 | sprintf(errmsg, "cannot expand '%s'", argv[i]); | 
| 75 | error(SYSTEM, errmsg); | 
| 76 | } | 
| 77 | if (argv[i] == NULL || argv[i][0] != '-') | 
| 78 | break;                  /* break from options */ | 
| 79 | if (!strcmp(argv[i], "-version")) { | 
| 80 | puts(VersionID); | 
| 81 | quit(0); | 
| 82 | } | 
| 83 | if (!strcmp(argv[i], "-defaults") || | 
| 84 | !strcmp(argv[i], "-help")) { | 
| 85 | printdefaults(); | 
| 86 | quit(0); | 
| 87 | } | 
| 88 | if (!strcmp(argv[i], "-devices")) { | 
| 89 | printdevices(); | 
| 90 | quit(0); | 
| 91 | } | 
| 92 | rval = getrenderopt(argc-i, argv+i); | 
| 93 | if (rval >= 0) { | 
| 94 | i += rval; | 
| 95 | continue; | 
| 96 | } | 
| 97 | rval = getviewopt(&ourview, argc-i, argv+i); | 
| 98 | if (rval >= 0) { | 
| 99 | i += rval; | 
| 100 | continue; | 
| 101 | } | 
| 102 | switch (argv[i][1]) { | 
| 103 | case 'v':                               /* view file */ | 
| 104 | if (argv[i][2] != 'f') | 
| 105 | goto badopt; | 
| 106 | check(3,"s"); | 
| 107 | rval = viewfile(argv[++i], &ourview, NULL); | 
| 108 | if (rval < 0) { | 
| 109 | sprintf(errmsg, | 
| 110 | "cannot open view file \"%s\"", | 
| 111 | argv[i]); | 
| 112 | error(SYSTEM, errmsg); | 
| 113 | } else if (rval == 0) { | 
| 114 | sprintf(errmsg, | 
| 115 | "bad view file \"%s\"", | 
| 116 | argv[i]); | 
| 117 | error(USER, errmsg); | 
| 118 | } | 
| 119 | break; | 
| 120 | case 'b':                               /* grayscale */ | 
| 121 | bool(2,greyscale); | 
| 122 | break; | 
| 123 | case 'p':                               /* pixel */ | 
| 124 | switch (argv[i][2]) { | 
| 125 | case 's':                               /* sample */ | 
| 126 | check(3,"i"); | 
| 127 | psample = atoi(argv[++i]); | 
| 128 | break; | 
| 129 | case 't':                               /* threshold */ | 
| 130 | check(3,"f"); | 
| 131 | maxdiff = atof(argv[++i]); | 
| 132 | break; | 
| 133 | case 'e':                               /* exposure */ | 
| 134 | check(3,"f"); | 
| 135 | exposure = atof(argv[++i]); | 
| 136 | if (argv[i][0] == '+' || argv[i][0] == '-') | 
| 137 | exposure = pow(2.0, exposure); | 
| 138 | break; | 
| 139 | default: | 
| 140 | goto badopt; | 
| 141 | } | 
| 142 | break; | 
| 143 | case 'w':                               /* warnings */ | 
| 144 | rval = erract[WARNING].pf != NULL; | 
| 145 | bool(2,rval); | 
| 146 | if (rval) erract[WARNING].pf = wputs; | 
| 147 | else erract[WARNING].pf = NULL; | 
| 148 | break; | 
| 149 | case 'e':                               /* error file */ | 
| 150 | check(2,"s"); | 
| 151 | errfile = argv[++i]; | 
| 152 | break; | 
| 153 | case 'o':                               /* output device */ | 
| 154 | check(2,"s"); | 
| 155 | dvcname = argv[++i]; | 
| 156 | break; | 
| 157 | case 'R':                               /* render input file */ | 
| 158 | check(2,"s"); | 
| 159 | strcpy(rifname, argv[++i]); | 
| 160 | break; | 
| 161 | default: | 
| 162 | goto badopt; | 
| 163 | } | 
| 164 | } | 
| 165 | err = setview(&ourview);        /* set viewing parameters */ | 
| 166 | if (err != NULL) | 
| 167 | error(USER, err); | 
| 168 | /* initialize object types */ | 
| 169 | initotypes(); | 
| 170 | /* initialize urand */ | 
| 171 | initurand(2048); | 
| 172 | /* set up signal handling */ | 
| 173 | sigdie(SIGINT, "Interrupt"); | 
| 174 | sigdie(SIGHUP, "Hangup"); | 
| 175 | sigdie(SIGTERM, "Terminate"); | 
| 176 | sigdie(SIGPIPE, "Broken pipe"); | 
| 177 | sigdie(SIGALRM, "Alarm clock"); | 
| 178 | #ifdef  SIGXCPU | 
| 179 | sigdie(SIGXCPU, "CPU limit exceeded"); | 
| 180 | sigdie(SIGXFSZ, "File size exceeded"); | 
| 181 | #endif | 
| 182 | /* open error file */ | 
| 183 | if (errfile != NULL) { | 
| 184 | if (freopen(errfile, "a", stderr) == NULL) | 
| 185 | quit(2); | 
| 186 | fprintf(stderr, "**************\n*** PID %5d: ", | 
| 187 | getpid()); | 
| 188 | printargs(argc, argv, stderr); | 
| 189 | putc('\n', stderr); | 
| 190 | fflush(stderr); | 
| 191 | } | 
| 192 | #ifdef  NICE | 
| 193 | nice(NICE);                     /* lower priority */ | 
| 194 | #endif | 
| 195 | /* get octree */ | 
| 196 | if (i == argc) | 
| 197 | octname = NULL; | 
| 198 | else if (i == argc-1) | 
| 199 | octname = argv[i]; | 
| 200 | else | 
| 201 | goto badopt; | 
| 202 | if (octname == NULL) | 
| 203 | error(USER, "missing octree argument"); | 
| 204 | /* set up output */ | 
| 205 | SET_FILE_BINARY(stdout); | 
| 206 | readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL); | 
| 207 | nsceneobjs = nobjects; | 
| 208 |  | 
| 209 | marksources();                  /* find and mark sources */ | 
| 210 |  | 
| 211 | setambient();                   /* initialize ambient calculation */ | 
| 212 |  | 
| 213 | rview();                        /* run interactive viewer */ | 
| 214 |  | 
| 215 | ambsync();                      /* flush ambient file */ | 
| 216 |  | 
| 217 | quit(0); | 
| 218 |  | 
| 219 | badopt: | 
| 220 | sprintf(errmsg, "command line error at '%s'", argv[i]); | 
| 221 | error(USER, errmsg); | 
| 222 | return 1; /* pro forma return */ | 
| 223 |  | 
| 224 | #undef  check | 
| 225 | #undef  bool | 
| 226 | } | 
| 227 |  | 
| 228 |  | 
| 229 | void | 
| 230 | wputs(                          /* warning output function */ | 
| 231 | char    *s | 
| 232 | ) | 
| 233 | { | 
| 234 | int  lasterrno = errno; | 
| 235 | eputs(s); | 
| 236 | errno = lasterrno; | 
| 237 | } | 
| 238 |  | 
| 239 |  | 
| 240 | void | 
| 241 | eputs(                          /* put string to stderr */ | 
| 242 | register char  *s | 
| 243 | ) | 
| 244 | { | 
| 245 | static int  midline = 0; | 
| 246 |  | 
| 247 | if (!*s) | 
| 248 | return; | 
| 249 | if (!midline++) { | 
| 250 | fputs(progname, stderr); | 
| 251 | fputs(": ", stderr); | 
| 252 | } | 
| 253 | fputs(s, stderr); | 
| 254 | if (s[strlen(s)-1] == '\n') { | 
| 255 | fflush(stderr); | 
| 256 | midline = 0; | 
| 257 | } | 
| 258 | } | 
| 259 |  | 
| 260 |  | 
| 261 | static void | 
| 262 | onsig(                          /* fatal signal */ | 
| 263 | int  signo | 
| 264 | ) | 
| 265 | { | 
| 266 | static int  gotsig = 0; | 
| 267 |  | 
| 268 | if (gotsig++)                   /* two signals and we're gone! */ | 
| 269 | _exit(signo); | 
| 270 |  | 
| 271 | alarm(15);                      /* allow 15 seconds to clean up */ | 
| 272 | signal(SIGALRM, SIG_DFL);       /* make certain we do die */ | 
| 273 | eputs("signal - "); | 
| 274 | eputs(sigerr[signo]); | 
| 275 | eputs("\n"); | 
| 276 | quit(3); | 
| 277 | } | 
| 278 |  | 
| 279 |  | 
| 280 | static void | 
| 281 | sigdie(                 /* set fatal signal */ | 
| 282 | int  signo, | 
| 283 | char  *msg | 
| 284 | ) | 
| 285 | { | 
| 286 | if (signal(signo, onsig) == SIG_IGN) | 
| 287 | signal(signo, SIG_IGN); | 
| 288 | sigerr[signo] = msg; | 
| 289 | } | 
| 290 |  | 
| 291 |  | 
| 292 | static void | 
| 293 | printdefaults(void)                     /* print default values to stdout */ | 
| 294 | { | 
| 295 | printf(greyscale ? "-b+\t\t\t\t# greyscale on\n" : | 
| 296 | "-b-\t\t\t\t# greyscale off\n"); | 
| 297 | printf("-vt%c\t\t\t\t# view type %s\n", ourview.type, | 
| 298 | ourview.type==VT_PER ? "perspective" : | 
| 299 | ourview.type==VT_PAR ? "parallel" : | 
| 300 | ourview.type==VT_HEM ? "hemispherical" : | 
| 301 | ourview.type==VT_ANG ? "angular" : | 
| 302 | ourview.type==VT_CYL ? "cylindrical" : | 
| 303 | "unknown"); | 
| 304 | printf("-vp %f %f %f\t# view point\n", | 
| 305 | ourview.vp[0], ourview.vp[1], ourview.vp[2]); | 
| 306 | printf("-vd %f %f %f\t# view direction\n", | 
| 307 | ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); | 
| 308 | printf("-vu %f %f %f\t# view up\n", | 
| 309 | ourview.vup[0], ourview.vup[1], ourview.vup[2]); | 
| 310 | printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz); | 
| 311 | printf("-vv %f\t\t\t# view vertical size\n", ourview.vert); | 
| 312 | printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore); | 
| 313 | printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft); | 
| 314 | printf("-vs %f\t\t\t# view shift\n", ourview.hoff); | 
| 315 | printf("-vl %f\t\t\t# view lift\n", ourview.voff); | 
| 316 | printf("-pe %f\t\t\t# pixel exposure\n", exposure); | 
| 317 | printf("-ps %-9d\t\t\t# pixel sample\n", psample); | 
| 318 | printf("-pt %f\t\t\t# pixel threshold\n", maxdiff); | 
| 319 | printf("-o %s\t\t\t\t# output device\n", dvcname); | 
| 320 | printf(erract[WARNING].pf != NULL ? | 
| 321 | "-w+\t\t\t\t# warning messages on\n" : | 
| 322 | "-w-\t\t\t\t# warning messages off\n"); | 
| 323 | print_rdefaults(); | 
| 324 | } |