1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: mkillum.c,v 2.40 2013/08/11 02:57:49 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Make illum sources for optimizing rendering process |
6 |
*/ |
7 |
|
8 |
#include <signal.h> |
9 |
#include <ctype.h> |
10 |
|
11 |
#include "paths.h" /* win_popen() */ |
12 |
#include "mkillum.h" |
13 |
|
14 |
/* default parameters */ |
15 |
#define SAMPDENS 48 /* points per projected steradian */ |
16 |
#define NSAMPS 32 /* samples per point */ |
17 |
#define DFLMAT "illum_mat" /* material name */ |
18 |
#define DFLDAT "illum" /* data file name */ |
19 |
/* selection options */ |
20 |
#define S_NONE 0 /* select none */ |
21 |
#define S_ELEM 1 /* select specified element */ |
22 |
#define S_COMPL 2 /* select all but element */ |
23 |
#define S_ALL 3 /* select all */ |
24 |
|
25 |
struct illum_args thisillum = { /* our illum and default values */ |
26 |
0, |
27 |
DFLMAT, |
28 |
DFLDAT, |
29 |
0, |
30 |
VOIDID, |
31 |
SAMPDENS, |
32 |
NSAMPS, |
33 |
0., |
34 |
}; |
35 |
|
36 |
char matcheck[MAXSTR]; /* current material to include or exclude */ |
37 |
int matselect = S_ALL; /* selection criterion */ |
38 |
|
39 |
int gargc; /* global argc */ |
40 |
char **gargv; /* global argv */ |
41 |
|
42 |
int doneheader = 0; /* printed header yet? */ |
43 |
#define checkhead() if (!doneheader++) printhead(gargc,gargv) |
44 |
|
45 |
int warnings = 1; /* print warnings? */ |
46 |
|
47 |
void init(char *octnm, int np); |
48 |
void filter(register FILE *infp, char *name); |
49 |
void xoptions(char *s, char *nm); |
50 |
void printopts(void); |
51 |
void printhead(register int ac, register char **av); |
52 |
void xobject(FILE *fp, char *nm); |
53 |
|
54 |
|
55 |
int |
56 |
main( /* compute illum distributions using rtrace */ |
57 |
int argc, |
58 |
char *argv[] |
59 |
) |
60 |
{ |
61 |
int nprocs = 1; |
62 |
FILE *fp; |
63 |
int rval; |
64 |
register int i; |
65 |
/* set global arguments */ |
66 |
gargv = argv; |
67 |
progname = gargv[0]; |
68 |
/* set up rendering defaults */ |
69 |
dstrsrc = 0.5; |
70 |
directrelay = 3; |
71 |
ambounce = 2; |
72 |
/* get options from command line */ |
73 |
for (i = 1; i < argc; i++) { |
74 |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
75 |
; |
76 |
if (rval < 0) { |
77 |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
78 |
error(SYSTEM, errmsg); |
79 |
} |
80 |
if (argv[i][0] != '-') |
81 |
break; |
82 |
if (!strcmp(argv[i], "-w")) { |
83 |
warnings = 0; |
84 |
continue; |
85 |
} |
86 |
if (!strcmp(argv[i], "-n")) { |
87 |
nprocs = atoi(argv[++i]); |
88 |
if (nprocs <= 0) |
89 |
error(USER, "illegal number of processes"); |
90 |
continue; |
91 |
} |
92 |
if (!strcmp(argv[i], "-defaults")) { |
93 |
printopts(); |
94 |
print_rdefaults(); |
95 |
quit(0); |
96 |
} |
97 |
rval = getrenderopt(argc-i, argv+i); |
98 |
if (rval < 0) { |
99 |
sprintf(errmsg, "bad render option at '%s'", argv[i]); |
100 |
error(USER, errmsg); |
101 |
} |
102 |
i += rval; |
103 |
} |
104 |
gargc = ++i; |
105 |
/* add "mandatory" render options */ |
106 |
do_irrad = 0; |
107 |
if (gargc > argc || argv[gargc-1][0] == '-') |
108 |
error(USER, "missing octree argument"); |
109 |
/* else initialize and run our calculation */ |
110 |
init(argv[gargc-1], nprocs); |
111 |
if (gargc < argc) { |
112 |
if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1]) |
113 |
error(USER, "use '< file1 file2 ..' for multiple inputs"); |
114 |
for (i = gargc+1; i < argc; i++) { |
115 |
if ((fp = fopen(argv[i], "r")) == NULL) { |
116 |
sprintf(errmsg, |
117 |
"cannot open scene file \"%s\"", argv[i]); |
118 |
error(SYSTEM, errmsg); |
119 |
} |
120 |
filter(fp, argv[i]); |
121 |
fclose(fp); |
122 |
} |
123 |
} else |
124 |
filter(stdin, "standard input"); |
125 |
quit(0); |
126 |
return 0; /* pro forma return */ |
127 |
} |
128 |
|
129 |
|
130 |
void |
131 |
init(char *octnm, int np) /* start rendering process(es) */ |
132 |
{ |
133 |
/* set up signal handling */ |
134 |
signal(SIGINT, quit); |
135 |
#ifdef SIGHUP |
136 |
signal(SIGHUP, quit); |
137 |
#endif |
138 |
#ifdef SIGTERM |
139 |
signal(SIGTERM, quit); |
140 |
#endif |
141 |
#ifdef SIGPIPE |
142 |
signal(SIGPIPE, quit); |
143 |
#endif |
144 |
/* start rendering process(es) */ |
145 |
ray_pinit(octnm, np); |
146 |
} |
147 |
|
148 |
|
149 |
void |
150 |
eputs( /* put string to stderr */ |
151 |
register char *s |
152 |
) |
153 |
{ |
154 |
static int midline = 0; |
155 |
|
156 |
if (!*s) return; |
157 |
if (!midline) { |
158 |
fputs(progname, stderr); |
159 |
fputs(": ", stderr); |
160 |
} |
161 |
fputs(s, stderr); |
162 |
midline = s[strlen(s)-1] != '\n'; |
163 |
} |
164 |
|
165 |
|
166 |
void |
167 |
wputs(s) /* print warning if enabled */ |
168 |
char *s; |
169 |
{ |
170 |
if (warnings) |
171 |
eputs(s); |
172 |
} |
173 |
|
174 |
|
175 |
void |
176 |
quit(ec) /* make sure exit is called */ |
177 |
int ec; |
178 |
{ |
179 |
if (ray_pnprocs > 0) /* close children if any */ |
180 |
ray_pclose(0); |
181 |
exit(ec); |
182 |
} |
183 |
|
184 |
|
185 |
void |
186 |
filter( /* process stream */ |
187 |
register FILE *infp, |
188 |
char *name |
189 |
) |
190 |
{ |
191 |
char buf[512]; |
192 |
FILE *pfp; |
193 |
register int c; |
194 |
|
195 |
while ((c = getc(infp)) != EOF) { |
196 |
if (isspace(c)) |
197 |
continue; |
198 |
if (c == '#') { /* comment/options */ |
199 |
buf[0] = c; |
200 |
fgets(buf+1, sizeof(buf)-1, infp); |
201 |
xoptions(buf, name); |
202 |
} else if (c == '!') { /* command */ |
203 |
buf[0] = c; |
204 |
fgetline(buf+1, sizeof(buf)-1, infp); |
205 |
if ((pfp = popen(buf+1, "r")) == NULL) { |
206 |
sprintf(errmsg, "cannot execute \"%s\"", buf); |
207 |
error(SYSTEM, errmsg); |
208 |
} |
209 |
filter(pfp, buf); |
210 |
pclose(pfp); |
211 |
} else { /* object */ |
212 |
ungetc(c, infp); |
213 |
xobject(infp, name); |
214 |
} |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
void |
220 |
xoptions( /* process options in string s */ |
221 |
char *s, |
222 |
char *nm |
223 |
) |
224 |
{ |
225 |
extern FILE *freopen(); |
226 |
char buf[64]; |
227 |
int negax; |
228 |
int nerrs = 0; |
229 |
register char *cp; |
230 |
|
231 |
if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) { |
232 |
fputs(s, stdout); /* not for us */ |
233 |
return; |
234 |
} |
235 |
cp = s+10; |
236 |
while (*cp) { |
237 |
switch (*cp) { |
238 |
case ' ': |
239 |
case '\t': |
240 |
case '\n': |
241 |
case '\r': |
242 |
case '\f': |
243 |
cp++; |
244 |
continue; |
245 |
case 'm': /* material name */ |
246 |
if (*++cp != '=') |
247 |
break; |
248 |
if (!*++cp || isspace(*cp)) |
249 |
break; |
250 |
atos(thisillum.matname, MAXSTR, cp); |
251 |
cp = sskip(cp); |
252 |
if (!(thisillum.flags & IL_DATCLB)) { |
253 |
strcpy(thisillum.datafile, thisillum.matname); |
254 |
thisillum.dfnum = 0; |
255 |
} |
256 |
continue; |
257 |
case 'f': /* data file name */ |
258 |
if (*++cp != '=') |
259 |
break; |
260 |
if (!*++cp || isspace(*cp)) { |
261 |
strcpy(thisillum.datafile,thisillum.matname); |
262 |
thisillum.dfnum = 0; |
263 |
thisillum.flags &= ~IL_DATCLB; |
264 |
continue; |
265 |
} |
266 |
atos(thisillum.datafile, MAXSTR, cp); |
267 |
cp = sskip(cp); |
268 |
thisillum.dfnum = 0; |
269 |
thisillum.flags |= IL_DATCLB; |
270 |
continue; |
271 |
case 'i': /* include material */ |
272 |
case 'e': /* exclude material */ |
273 |
if (cp[1] != '=') |
274 |
break; |
275 |
matselect = (*cp == 'i') ? S_ELEM : S_COMPL; |
276 |
cp += 2; |
277 |
atos(matcheck, MAXSTR, cp); |
278 |
cp = sskip(cp); |
279 |
continue; |
280 |
case 'a': /* use everything */ |
281 |
cp = sskip(cp); |
282 |
matselect = S_ALL; |
283 |
continue; |
284 |
case 'n': /* use nothing (passive) */ |
285 |
cp = sskip(cp); |
286 |
matselect = S_NONE; |
287 |
continue; |
288 |
case 'c': /* color calculation */ |
289 |
if (*++cp != '=') |
290 |
break; |
291 |
switch (*++cp) { |
292 |
case 'a': /* average */ |
293 |
thisillum.flags = (thisillum.flags|IL_COLAVG) |
294 |
& ~IL_COLDST; |
295 |
break; |
296 |
case 'd': /* distribution */ |
297 |
thisillum.flags |= (IL_COLDST|IL_COLAVG); |
298 |
break; |
299 |
case 'n': /* none */ |
300 |
thisillum.flags &= ~(IL_COLAVG|IL_COLDST); |
301 |
break; |
302 |
default: |
303 |
goto opterr; |
304 |
} |
305 |
cp = sskip(cp); |
306 |
continue; |
307 |
case 'd': /* sample density */ |
308 |
if (*++cp != '=') |
309 |
break; |
310 |
if (!*++cp || isspace(*cp)) |
311 |
continue; |
312 |
if (isintd(cp, " \t\n\r")) { |
313 |
thisillum.sampdens = atoi(cp); |
314 |
} else { |
315 |
error(WARNING, "direct BSDF input unsupported"); |
316 |
goto opterr; |
317 |
} |
318 |
cp = sskip(cp); |
319 |
continue; |
320 |
case 's': /* surface super-samples */ |
321 |
if (*++cp != '=') |
322 |
break; |
323 |
if (!isintd(++cp, " \t\n\r")) |
324 |
break; |
325 |
thisillum.nsamps = atoi(cp); |
326 |
cp = sskip(cp); |
327 |
continue; |
328 |
case 'l': /* light sources */ |
329 |
cp++; |
330 |
if (*cp == '+') |
331 |
thisillum.flags |= IL_LIGHT; |
332 |
else if (*cp == '-') |
333 |
thisillum.flags &= ~IL_LIGHT; |
334 |
else |
335 |
break; |
336 |
cp++; |
337 |
continue; |
338 |
case 'b': /* brightness */ |
339 |
if (*++cp != '=') |
340 |
break; |
341 |
if (!isfltd(++cp, " \t\n\r")) |
342 |
break; |
343 |
thisillum.minbrt = atof(cp); |
344 |
if (thisillum.minbrt < 0.) |
345 |
thisillum.minbrt = 0.; |
346 |
cp = sskip(cp); |
347 |
continue; |
348 |
case 'o': /* output file */ |
349 |
if (*++cp != '=') |
350 |
break; |
351 |
if (!*++cp || isspace(*cp)) |
352 |
break; |
353 |
atos(buf, sizeof(buf), cp); |
354 |
cp = sskip(cp); |
355 |
if (freopen(buf, "w", stdout) == NULL) { |
356 |
sprintf(errmsg, |
357 |
"cannot open output file \"%s\"", buf); |
358 |
error(SYSTEM, errmsg); |
359 |
} |
360 |
doneheader = 0; |
361 |
continue; |
362 |
case '!': /* processed file! */ |
363 |
sprintf(errmsg, "(%s): already processed!", nm); |
364 |
error(WARNING, errmsg); |
365 |
matselect = S_NONE; |
366 |
return; |
367 |
} |
368 |
opterr: /* skip faulty option */ |
369 |
while (*cp && !isspace(*cp)) |
370 |
cp++; |
371 |
nerrs++; |
372 |
} |
373 |
/* print header? */ |
374 |
checkhead(); |
375 |
/* issue warnings? */ |
376 |
if (nerrs) { |
377 |
sprintf(errmsg, "(%s): %d error(s) in option line:", |
378 |
nm, nerrs); |
379 |
error(WARNING, errmsg); |
380 |
wputs(s); |
381 |
printf("# %s: the following option line has %d error(s):\n", |
382 |
progname, nerrs); |
383 |
} |
384 |
/* print pure comment */ |
385 |
printf("# %s", s+2); |
386 |
} |
387 |
|
388 |
void |
389 |
printopts(void) /* print out option default values */ |
390 |
{ |
391 |
printf("m=%-15s\t\t# material name\n", thisillum.matname); |
392 |
printf("f=%-15s\t\t# data file name\n", thisillum.datafile); |
393 |
if (thisillum.flags & IL_COLAVG) |
394 |
if (thisillum.flags & IL_COLDST) |
395 |
printf("c=d\t\t\t\t# color distribution\n"); |
396 |
else |
397 |
printf("c=a\t\t\t\t# color average\n"); |
398 |
else |
399 |
printf("c=n\t\t\t\t# color none\n"); |
400 |
if (thisillum.flags & IL_LIGHT) |
401 |
printf("l+\t\t\t\t# light type on\n"); |
402 |
else |
403 |
printf("l-\t\t\t\t# light type off\n"); |
404 |
printf("d=%d\t\t\t\t# density of directions\n", thisillum.sampdens); |
405 |
printf("s=%d\t\t\t\t# samples per direction\n", thisillum.nsamps); |
406 |
printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt); |
407 |
} |
408 |
|
409 |
|
410 |
void |
411 |
printhead( /* print out header */ |
412 |
register int ac, |
413 |
register char **av |
414 |
) |
415 |
{ |
416 |
putchar('#'); |
417 |
while (ac-- > 0) { |
418 |
putchar(' '); |
419 |
fputs(*av++, stdout); |
420 |
} |
421 |
fputs("\n#@mkillum !\n", stdout); |
422 |
} |
423 |
|
424 |
|
425 |
void |
426 |
xobject( /* translate an object from fp */ |
427 |
FILE *fp, |
428 |
char *nm |
429 |
) |
430 |
{ |
431 |
OBJREC thisobj; |
432 |
char str[MAXSTR]; |
433 |
int doit; |
434 |
/* read the object */ |
435 |
if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL) |
436 |
goto readerr; |
437 |
if (fgetword(str, MAXSTR, fp) == NULL) |
438 |
goto readerr; |
439 |
/* is it an alias? */ |
440 |
if (!strcmp(str, ALIASKEY)) { |
441 |
if (fgetword(str, MAXSTR, fp) == NULL) |
442 |
goto readerr; |
443 |
printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str); |
444 |
if (fgetword(str, MAXSTR, fp) == NULL) |
445 |
goto readerr; |
446 |
printf("\t%s\n", str); |
447 |
return; |
448 |
} |
449 |
thisobj.omod = OVOID; /* unused field */ |
450 |
if ((thisobj.otype = otype(str)) < 0) { |
451 |
sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str); |
452 |
error(USER, errmsg); |
453 |
} |
454 |
if (fgetword(str, MAXSTR, fp) == NULL) |
455 |
goto readerr; |
456 |
thisobj.oname = str; |
457 |
if (readfargs(&thisobj.oargs, fp) != 1) |
458 |
goto readerr; |
459 |
thisobj.os = NULL; |
460 |
/* check for translation */ |
461 |
switch (matselect) { |
462 |
case S_NONE: |
463 |
doit = 0; |
464 |
break; |
465 |
case S_ALL: |
466 |
doit = 1; |
467 |
break; |
468 |
case S_ELEM: |
469 |
doit = !strcmp(thisillum.altmat, matcheck); |
470 |
break; |
471 |
case S_COMPL: |
472 |
doit = strcmp(thisillum.altmat, matcheck); |
473 |
break; |
474 |
} |
475 |
doit = doit && issurface(thisobj.otype); |
476 |
/* print header? */ |
477 |
checkhead(); |
478 |
/* process object */ |
479 |
if (doit) |
480 |
switch (thisobj.otype) { |
481 |
case OBJ_FACE: |
482 |
my_face(&thisobj, &thisillum, nm); |
483 |
break; |
484 |
case OBJ_SPHERE: |
485 |
my_sphere(&thisobj, &thisillum, nm); |
486 |
break; |
487 |
case OBJ_RING: |
488 |
my_ring(&thisobj, &thisillum, nm); |
489 |
break; |
490 |
default: |
491 |
my_default(&thisobj, &thisillum, nm); |
492 |
break; |
493 |
} |
494 |
else |
495 |
printobj(thisillum.altmat, &thisobj); |
496 |
/* free arguments */ |
497 |
freefargs(&thisobj.oargs); |
498 |
return; |
499 |
readerr: |
500 |
sprintf(errmsg, "(%s): error reading input", nm); |
501 |
error(USER, errmsg); |
502 |
} |