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