1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* rpmain.c - main for rpict batch rendering program |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "ray.h" |
11 |
|
12 |
#include "source.h" |
13 |
|
14 |
#include "ambient.h" |
15 |
|
16 |
#include "random.h" |
17 |
|
18 |
#include "paths.h" |
19 |
|
20 |
#include <sys/types.h> |
21 |
|
22 |
#include <signal.h> |
23 |
|
24 |
#include "view.h" |
25 |
|
26 |
#include "paths.h" |
27 |
/* persistent processes define */ |
28 |
#ifdef F_SETLKW |
29 |
#define PERSIST 1 /* normal persist */ |
30 |
#define PARALLEL 2 /* parallel persist */ |
31 |
#define PCHILD 3 /* child of normal persist */ |
32 |
#endif |
33 |
|
34 |
char *progname; /* argv[0] */ |
35 |
|
36 |
char *octname; /* octree name */ |
37 |
|
38 |
char *sigerr[NSIG]; /* signal error messages */ |
39 |
|
40 |
char *shm_boundary = NULL; /* boundary of shared memory */ |
41 |
|
42 |
char *errfile = NULL; /* error output file */ |
43 |
|
44 |
extern time_t time(); |
45 |
extern time_t tstart; /* start time */ |
46 |
|
47 |
extern int ralrm; /* seconds between reports */ |
48 |
|
49 |
extern VIEW ourview; /* viewing parameters */ |
50 |
|
51 |
extern int hresolu; /* horizontal resolution */ |
52 |
extern int vresolu; /* vertical resolution */ |
53 |
extern double pixaspect; /* pixel aspect ratio */ |
54 |
|
55 |
extern int psample; /* pixel sample size */ |
56 |
extern double maxdiff; /* max. sample difference */ |
57 |
extern double dstrpix; /* square pixel distribution */ |
58 |
|
59 |
extern double mblur; /* motion blur parameter */ |
60 |
|
61 |
void onsig(); |
62 |
void sigdie(); |
63 |
void printdefaults(); |
64 |
|
65 |
|
66 |
int |
67 |
main(argc, argv) |
68 |
int argc; |
69 |
char *argv[]; |
70 |
{ |
71 |
#define check(ol,al) if (argv[i][ol] || \ |
72 |
badarg(argc-i-1,argv+i+1,al)) \ |
73 |
goto badopt |
74 |
#define bool(olen,var) switch (argv[i][olen]) { \ |
75 |
case '\0': var = !var; break; \ |
76 |
case 'y': case 'Y': case 't': case 'T': \ |
77 |
case '+': case '1': var = 1; break; \ |
78 |
case 'n': case 'N': case 'f': case 'F': \ |
79 |
case '-': case '0': var = 0; break; \ |
80 |
default: goto badopt; } |
81 |
char *err; |
82 |
char *recover = NULL; |
83 |
char *outfile = NULL; |
84 |
char *zfile = NULL; |
85 |
int loadflags = ~IO_FILES; |
86 |
int seqstart = 0; |
87 |
int persist = 0; |
88 |
int duped1; |
89 |
int rval; |
90 |
int i; |
91 |
/* record start time */ |
92 |
tstart = time((time_t *)NULL); |
93 |
/* global program name */ |
94 |
progname = argv[0] = fixargv0(argv[0]); |
95 |
/* option city */ |
96 |
for (i = 1; i < argc; i++) { |
97 |
/* expand arguments */ |
98 |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
99 |
; |
100 |
if (rval < 0) { |
101 |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
102 |
error(SYSTEM, errmsg); |
103 |
} |
104 |
if (argv[i] == NULL || argv[i][0] != '-') |
105 |
break; /* break from options */ |
106 |
if (!strcmp(argv[i], "-version")) { |
107 |
puts(VersionID); |
108 |
quit(0); |
109 |
} |
110 |
if (!strcmp(argv[i], "-defaults") || |
111 |
!strcmp(argv[i], "-help")) { |
112 |
printdefaults(); |
113 |
quit(0); |
114 |
} |
115 |
rval = getrenderopt(argc-i, argv+i); |
116 |
if (rval >= 0) { |
117 |
i += rval; |
118 |
continue; |
119 |
} |
120 |
rval = getviewopt(&ourview, argc-i, argv+i); |
121 |
if (rval >= 0) { |
122 |
i += rval; |
123 |
continue; |
124 |
} |
125 |
/* rpict options */ |
126 |
switch (argv[i][1]) { |
127 |
case 'v': /* view file */ |
128 |
if (argv[i][2] != 'f') |
129 |
goto badopt; |
130 |
check(3,"s"); |
131 |
rval = viewfile(argv[++i], &ourview, NULL); |
132 |
if (rval < 0) { |
133 |
sprintf(errmsg, |
134 |
"cannot open view file \"%s\"", |
135 |
argv[i]); |
136 |
error(SYSTEM, errmsg); |
137 |
} else if (rval == 0) { |
138 |
sprintf(errmsg, |
139 |
"bad view file \"%s\"", |
140 |
argv[i]); |
141 |
error(USER, errmsg); |
142 |
} |
143 |
break; |
144 |
case 'p': /* pixel */ |
145 |
switch (argv[i][2]) { |
146 |
case 's': /* sample */ |
147 |
check(3,"i"); |
148 |
psample = atoi(argv[++i]); |
149 |
break; |
150 |
case 't': /* threshold */ |
151 |
check(3,"f"); |
152 |
maxdiff = atof(argv[++i]); |
153 |
break; |
154 |
case 'j': /* jitter */ |
155 |
check(3,"f"); |
156 |
dstrpix = atof(argv[++i]); |
157 |
break; |
158 |
case 'a': /* aspect */ |
159 |
check(3,"f"); |
160 |
pixaspect = atof(argv[++i]); |
161 |
break; |
162 |
case 'm': /* motion */ |
163 |
check(3,"f"); |
164 |
mblur = atof(argv[++i]); |
165 |
break; |
166 |
default: |
167 |
goto badopt; |
168 |
} |
169 |
break; |
170 |
case 'x': /* x resolution */ |
171 |
check(2,"i"); |
172 |
hresolu = atoi(argv[++i]); |
173 |
break; |
174 |
case 'y': /* y resolution */ |
175 |
check(2,"i"); |
176 |
vresolu = atoi(argv[++i]); |
177 |
break; |
178 |
case 'S': /* slave index */ |
179 |
check(2,"i"); |
180 |
seqstart = atoi(argv[++i]); |
181 |
break; |
182 |
case 'o': /* output file */ |
183 |
check(2,"s"); |
184 |
outfile = argv[++i]; |
185 |
break; |
186 |
case 'z': /* z file */ |
187 |
check(2,"s"); |
188 |
zfile = argv[++i]; |
189 |
break; |
190 |
case 'r': /* recover file */ |
191 |
if (argv[i][2] == 'o') { /* +output */ |
192 |
check(3,"s"); |
193 |
outfile = argv[i+1]; |
194 |
} else |
195 |
check(2,"s"); |
196 |
recover = argv[++i]; |
197 |
break; |
198 |
case 't': /* timer */ |
199 |
check(2,"i"); |
200 |
ralrm = atoi(argv[++i]); |
201 |
break; |
202 |
#ifdef PERSIST |
203 |
case 'P': /* persist file */ |
204 |
if (argv[i][2] == 'P') { |
205 |
check(3,"s"); |
206 |
persist = PARALLEL; |
207 |
} else { |
208 |
check(2,"s"); |
209 |
persist = PERSIST; |
210 |
} |
211 |
persistfile(argv[++i]); |
212 |
break; |
213 |
#endif |
214 |
case 'w': /* warnings */ |
215 |
rval = erract[WARNING].pf != NULL; |
216 |
bool(2,rval); |
217 |
if (rval) erract[WARNING].pf = wputs; |
218 |
else erract[WARNING].pf = NULL; |
219 |
break; |
220 |
case 'e': /* error file */ |
221 |
check(2,"s"); |
222 |
errfile = argv[++i]; |
223 |
break; |
224 |
default: |
225 |
goto badopt; |
226 |
} |
227 |
} |
228 |
err = setview(&ourview); /* set viewing parameters */ |
229 |
if (err != NULL) |
230 |
error(USER, err); |
231 |
/* initialize object types */ |
232 |
initotypes(); |
233 |
/* initialize urand */ |
234 |
initurand(2048); |
235 |
/* set up signal handling */ |
236 |
sigdie(SIGINT, "Interrupt"); |
237 |
sigdie(SIGHUP, "Hangup"); |
238 |
sigdie(SIGTERM, "Terminate"); |
239 |
sigdie(SIGPIPE, "Broken pipe"); |
240 |
sigdie(SIGALRM, "Alarm clock"); |
241 |
#ifdef SIGXCPU |
242 |
sigdie(SIGXCPU, "CPU limit exceeded"); |
243 |
sigdie(SIGXFSZ, "File size exceeded"); |
244 |
#endif |
245 |
/* open error file */ |
246 |
if (errfile != NULL) { |
247 |
if (freopen(errfile, "a", stderr) == NULL) |
248 |
quit(2); |
249 |
fprintf(stderr, "**************\n*** PID %5d: ", |
250 |
getpid()); |
251 |
printargs(argc, argv, stderr); |
252 |
putc('\n', stderr); |
253 |
fflush(stderr); |
254 |
} |
255 |
#ifdef NICE |
256 |
nice(NICE); /* lower priority */ |
257 |
#endif |
258 |
/* get octree */ |
259 |
if (i == argc) |
260 |
octname = NULL; |
261 |
else if (i == argc-1) |
262 |
octname = argv[i]; |
263 |
else |
264 |
goto badopt; |
265 |
if (seqstart > 0 && octname == NULL) |
266 |
error(USER, "missing octree argument"); |
267 |
/* set up output */ |
268 |
#ifdef PERSIST |
269 |
if (persist) { |
270 |
if (recover != NULL) |
271 |
error(USER, "persist option used with recover file"); |
272 |
if (seqstart <= 0) |
273 |
error(USER, "persist option only for sequences"); |
274 |
if (outfile == NULL) |
275 |
duped1 = dup(fileno(stdout)); /* don't lose our output */ |
276 |
openheader(); |
277 |
} else |
278 |
#endif |
279 |
if (outfile != NULL) |
280 |
openheader(); |
281 |
#ifdef MSDOS |
282 |
setmode(fileno(stdout), O_BINARY); |
283 |
if (octname == NULL) |
284 |
setmode(fileno(stdin), O_BINARY); |
285 |
#endif |
286 |
readoct(octname, loadflags, &thescene, NULL); |
287 |
nsceneobjs = nobjects; |
288 |
|
289 |
if (loadflags & IO_INFO) { /* print header */ |
290 |
printargs(i, argv, stdout); |
291 |
printf("SOFTWARE= %s\n", VersionID); |
292 |
fputnow(stdout); |
293 |
} |
294 |
|
295 |
marksources(); /* find and mark sources */ |
296 |
|
297 |
setambient(); /* initialize ambient calculation */ |
298 |
|
299 |
#ifdef PERSIST |
300 |
if (persist) { |
301 |
fflush(stdout); |
302 |
if (outfile == NULL) { /* reconnect stdout */ |
303 |
dup2(duped1, fileno(stdout)); |
304 |
close(duped1); |
305 |
} |
306 |
if (persist == PARALLEL) { /* multiprocessing */ |
307 |
preload_objs(); /* preload scene */ |
308 |
shm_boundary = (char *)malloc(16); |
309 |
strcpy(shm_boundary, "SHM_BOUNDARY"); |
310 |
while ((rval=fork()) == 0) { /* keep on forkin' */ |
311 |
pflock(1); |
312 |
pfhold(); |
313 |
tstart = time((time_t *)NULL); |
314 |
} |
315 |
if (rval < 0) |
316 |
error(SYSTEM, "cannot fork child for persist function"); |
317 |
pfdetach(); /* parent exits */ |
318 |
} |
319 |
} |
320 |
runagain: |
321 |
if (persist) |
322 |
if (outfile == NULL) /* if out to stdout */ |
323 |
dupheader(); /* send header */ |
324 |
else /* if out to file */ |
325 |
duped1 = dup(fileno(stdout)); /* hang onto pipe */ |
326 |
#endif |
327 |
/* batch render picture(s) */ |
328 |
rpict(seqstart, outfile, zfile, recover); |
329 |
/* flush ambient file */ |
330 |
ambsync(); |
331 |
#ifdef PERSIST |
332 |
if (persist == PERSIST) { /* first run-through */ |
333 |
if ((rval=fork()) == 0) { /* child loops until killed */ |
334 |
pflock(1); |
335 |
persist = PCHILD; |
336 |
} else { /* original process exits */ |
337 |
if (rval < 0) |
338 |
error(SYSTEM, "cannot fork child for persist function"); |
339 |
pfdetach(); /* parent exits */ |
340 |
} |
341 |
} |
342 |
if (persist == PCHILD) { /* wait for a signal then go again */ |
343 |
if (outfile != NULL) |
344 |
close(duped1); /* release output handle */ |
345 |
pfhold(); |
346 |
tstart = time((time_t *)NULL); /* reinitialize */ |
347 |
raynum = nrays = 0; |
348 |
goto runagain; |
349 |
} |
350 |
#endif |
351 |
quit(0); |
352 |
|
353 |
badopt: |
354 |
sprintf(errmsg, "command line error at '%s'", argv[i]); |
355 |
error(USER, errmsg); |
356 |
|
357 |
#undef check |
358 |
#undef bool |
359 |
} |
360 |
|
361 |
|
362 |
void |
363 |
wputs(s) /* warning output function */ |
364 |
char *s; |
365 |
{ |
366 |
int lasterrno = errno; |
367 |
eputs(s); |
368 |
errno = lasterrno; |
369 |
} |
370 |
|
371 |
|
372 |
void |
373 |
eputs(s) /* put string to stderr */ |
374 |
register char *s; |
375 |
{ |
376 |
static int midline = 0; |
377 |
|
378 |
if (!*s) |
379 |
return; |
380 |
if (!midline++) { |
381 |
fputs(progname, stderr); |
382 |
fputs(": ", stderr); |
383 |
} |
384 |
fputs(s, stderr); |
385 |
if (s[strlen(s)-1] == '\n') { |
386 |
fflush(stderr); |
387 |
midline = 0; |
388 |
} |
389 |
} |
390 |
|
391 |
|
392 |
void |
393 |
onsig(signo) /* fatal signal */ |
394 |
int signo; |
395 |
{ |
396 |
static int gotsig = 0; |
397 |
|
398 |
if (gotsig++) /* two signals and we're gone! */ |
399 |
_exit(signo); |
400 |
|
401 |
alarm(15); /* allow 15 seconds to clean up */ |
402 |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
403 |
eputs("signal - "); |
404 |
eputs(sigerr[signo]); |
405 |
eputs("\n"); |
406 |
quit(3); |
407 |
} |
408 |
|
409 |
|
410 |
void |
411 |
sigdie(signo, msg) /* set fatal signal */ |
412 |
int signo; |
413 |
char *msg; |
414 |
{ |
415 |
if (signal(signo, onsig) == SIG_IGN) |
416 |
signal(signo, SIG_IGN); |
417 |
sigerr[signo] = msg; |
418 |
} |
419 |
|
420 |
|
421 |
void |
422 |
printdefaults() /* print default values to stdout */ |
423 |
{ |
424 |
register char *cp; |
425 |
|
426 |
printf("-vt%c\t\t\t\t# view type %s\n", ourview.type, |
427 |
ourview.type==VT_PER ? "perspective" : |
428 |
ourview.type==VT_PAR ? "parallel" : |
429 |
ourview.type==VT_HEM ? "hemispherical" : |
430 |
ourview.type==VT_ANG ? "angular" : |
431 |
ourview.type==VT_CYL ? "cylindrical" : |
432 |
"unknown"); |
433 |
printf("-vp %f %f %f\t# view point\n", |
434 |
ourview.vp[0], ourview.vp[1], ourview.vp[2]); |
435 |
printf("-vd %f %f %f\t# view direction\n", |
436 |
ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); |
437 |
printf("-vu %f %f %f\t# view up\n", |
438 |
ourview.vup[0], ourview.vup[1], ourview.vup[2]); |
439 |
printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz); |
440 |
printf("-vv %f\t\t\t# view vertical size\n", ourview.vert); |
441 |
printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore); |
442 |
printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft); |
443 |
printf("-vs %f\t\t\t# view shift\n", ourview.hoff); |
444 |
printf("-vl %f\t\t\t# view lift\n", ourview.voff); |
445 |
printf("-x %-9d\t\t\t# x resolution\n", hresolu); |
446 |
printf("-y %-9d\t\t\t# y resolution\n", vresolu); |
447 |
printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect); |
448 |
printf("-pj %f\t\t\t# pixel jitter\n", dstrpix); |
449 |
printf("-pm %f\t\t\t# pixel motion\n", mblur); |
450 |
printf("-ps %-9d\t\t\t# pixel sample\n", psample); |
451 |
printf("-pt %f\t\t\t# pixel threshold\n", maxdiff); |
452 |
printf("-t %-9d\t\t\t# time between reports\n", ralrm); |
453 |
printf(erract[WARNING].pf != NULL ? |
454 |
"-w+\t\t\t\t# warning messages on\n" : |
455 |
"-w-\t\t\t\t# warning messages off\n"); |
456 |
print_rdefaults(); |
457 |
} |