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