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