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