1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: mkillum.c,v 2.42 2016/09/15 22:34:41 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(FILE *infp, char *name); |
49 |
void xoptions(char *s, char *nm); |
50 |
void printopts(void); |
51 |
void printhead(int ac, 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 |
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 |
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 |
else if (ray_pnprocs < 0) |
182 |
_exit(ec); /* avoid flush() in child */ |
183 |
exit(ec); |
184 |
} |
185 |
|
186 |
|
187 |
void |
188 |
filter( /* process stream */ |
189 |
FILE *infp, |
190 |
char *name |
191 |
) |
192 |
{ |
193 |
char buf[512]; |
194 |
FILE *pfp; |
195 |
int c; |
196 |
|
197 |
while ((c = getc(infp)) != EOF) { |
198 |
if (isspace(c)) |
199 |
continue; |
200 |
if (c == '#') { /* comment/options */ |
201 |
buf[0] = c; |
202 |
fgets(buf+1, sizeof(buf)-1, infp); |
203 |
xoptions(buf, name); |
204 |
} else if (c == '!') { /* command */ |
205 |
buf[0] = c; |
206 |
fgetline(buf+1, sizeof(buf)-1, infp); |
207 |
if ((pfp = popen(buf+1, "r")) == NULL) { |
208 |
sprintf(errmsg, "cannot execute \"%s\"", buf); |
209 |
error(SYSTEM, errmsg); |
210 |
} |
211 |
filter(pfp, buf); |
212 |
pclose(pfp); |
213 |
} else { /* object */ |
214 |
ungetc(c, infp); |
215 |
xobject(infp, name); |
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
|
221 |
void |
222 |
xoptions( /* process options in string s */ |
223 |
char *s, |
224 |
char *nm |
225 |
) |
226 |
{ |
227 |
char buf[64]; |
228 |
int negax; |
229 |
int nerrs = 0; |
230 |
char *cp; |
231 |
|
232 |
if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) { |
233 |
fputs(s, stdout); /* not for us */ |
234 |
return; |
235 |
} |
236 |
cp = s+10; |
237 |
while (*cp) { |
238 |
switch (*cp) { |
239 |
case ' ': |
240 |
case '\t': |
241 |
case '\n': |
242 |
case '\r': |
243 |
case '\f': |
244 |
cp++; |
245 |
continue; |
246 |
case 'm': /* material name */ |
247 |
if (*++cp != '=') |
248 |
break; |
249 |
if (!*++cp || isspace(*cp)) |
250 |
break; |
251 |
atos(thisillum.matname, MAXSTR, cp); |
252 |
cp = sskip(cp); |
253 |
if (!(thisillum.flags & IL_DATCLB)) { |
254 |
strcpy(thisillum.datafile, thisillum.matname); |
255 |
thisillum.dfnum = 0; |
256 |
} |
257 |
continue; |
258 |
case 'f': /* data file name */ |
259 |
if (*++cp != '=') |
260 |
break; |
261 |
if (!*++cp || isspace(*cp)) { |
262 |
strcpy(thisillum.datafile,thisillum.matname); |
263 |
thisillum.dfnum = 0; |
264 |
thisillum.flags &= ~IL_DATCLB; |
265 |
continue; |
266 |
} |
267 |
atos(thisillum.datafile, MAXSTR, cp); |
268 |
cp = sskip(cp); |
269 |
thisillum.dfnum = 0; |
270 |
thisillum.flags |= IL_DATCLB; |
271 |
continue; |
272 |
case 'i': /* include material */ |
273 |
case 'e': /* exclude material */ |
274 |
if (cp[1] != '=') |
275 |
break; |
276 |
matselect = (*cp == 'i') ? S_ELEM : S_COMPL; |
277 |
cp += 2; |
278 |
atos(matcheck, MAXSTR, cp); |
279 |
cp = sskip(cp); |
280 |
continue; |
281 |
case 'a': /* use everything */ |
282 |
cp = sskip(cp); |
283 |
matselect = S_ALL; |
284 |
continue; |
285 |
case 'n': /* use nothing (passive) */ |
286 |
cp = sskip(cp); |
287 |
matselect = S_NONE; |
288 |
continue; |
289 |
case 'c': /* color calculation */ |
290 |
if (*++cp != '=') |
291 |
break; |
292 |
switch (*++cp) { |
293 |
case 'a': /* average */ |
294 |
thisillum.flags = (thisillum.flags|IL_COLAVG) |
295 |
& ~IL_COLDST; |
296 |
break; |
297 |
case 'd': /* distribution */ |
298 |
thisillum.flags |= (IL_COLDST|IL_COLAVG); |
299 |
break; |
300 |
case 'n': /* none */ |
301 |
thisillum.flags &= ~(IL_COLAVG|IL_COLDST); |
302 |
break; |
303 |
default: |
304 |
goto opterr; |
305 |
} |
306 |
cp = sskip(cp); |
307 |
continue; |
308 |
case 'd': /* sample density */ |
309 |
if (*++cp != '=') |
310 |
break; |
311 |
if (!*++cp || isspace(*cp)) |
312 |
continue; |
313 |
if (isintd(cp, " \t\n\r")) { |
314 |
thisillum.sampdens = atoi(cp); |
315 |
} else { |
316 |
error(WARNING, "direct BSDF input unsupported"); |
317 |
goto opterr; |
318 |
} |
319 |
cp = sskip(cp); |
320 |
continue; |
321 |
case 's': /* surface super-samples */ |
322 |
if (*++cp != '=') |
323 |
break; |
324 |
if (!isintd(++cp, " \t\n\r")) |
325 |
break; |
326 |
thisillum.nsamps = atoi(cp); |
327 |
cp = sskip(cp); |
328 |
continue; |
329 |
case 'l': /* light sources */ |
330 |
cp++; |
331 |
if (*cp == '+') |
332 |
thisillum.flags |= IL_LIGHT; |
333 |
else if (*cp == '-') |
334 |
thisillum.flags &= ~IL_LIGHT; |
335 |
else |
336 |
break; |
337 |
cp++; |
338 |
continue; |
339 |
case 'b': /* brightness */ |
340 |
if (*++cp != '=') |
341 |
break; |
342 |
if (!isfltd(++cp, " \t\n\r")) |
343 |
break; |
344 |
thisillum.minbrt = atof(cp); |
345 |
if (thisillum.minbrt < 0.) |
346 |
thisillum.minbrt = 0.; |
347 |
cp = sskip(cp); |
348 |
continue; |
349 |
case 'o': /* output file */ |
350 |
if (*++cp != '=') |
351 |
break; |
352 |
if (!*++cp || isspace(*cp)) |
353 |
break; |
354 |
atos(buf, sizeof(buf), cp); |
355 |
cp = sskip(cp); |
356 |
if (freopen(buf, "w", stdout) == NULL) { |
357 |
sprintf(errmsg, |
358 |
"cannot open output file \"%s\"", buf); |
359 |
error(SYSTEM, errmsg); |
360 |
} |
361 |
doneheader = 0; |
362 |
continue; |
363 |
case '!': /* processed file! */ |
364 |
sprintf(errmsg, "(%s): already processed!", nm); |
365 |
error(WARNING, errmsg); |
366 |
matselect = S_NONE; |
367 |
return; |
368 |
} |
369 |
opterr: /* skip faulty option */ |
370 |
while (*cp && !isspace(*cp)) |
371 |
cp++; |
372 |
nerrs++; |
373 |
} |
374 |
/* print header? */ |
375 |
checkhead(); |
376 |
/* issue warnings? */ |
377 |
if (nerrs) { |
378 |
sprintf(errmsg, "(%s): %d error(s) in option line:", |
379 |
nm, nerrs); |
380 |
error(WARNING, errmsg); |
381 |
wputs(s); |
382 |
printf("# %s: the following option line has %d error(s):\n", |
383 |
progname, nerrs); |
384 |
} |
385 |
/* print pure comment */ |
386 |
printf("# %s", s+2); |
387 |
} |
388 |
|
389 |
void |
390 |
printopts(void) /* print out option default values */ |
391 |
{ |
392 |
printf("m=%-15s\t\t# material name\n", thisillum.matname); |
393 |
printf("f=%-15s\t\t# data file name\n", thisillum.datafile); |
394 |
if (thisillum.flags & IL_COLAVG) |
395 |
if (thisillum.flags & IL_COLDST) |
396 |
printf("c=d\t\t\t\t# color distribution\n"); |
397 |
else |
398 |
printf("c=a\t\t\t\t# color average\n"); |
399 |
else |
400 |
printf("c=n\t\t\t\t# color none\n"); |
401 |
if (thisillum.flags & IL_LIGHT) |
402 |
printf("l+\t\t\t\t# light type on\n"); |
403 |
else |
404 |
printf("l-\t\t\t\t# light type off\n"); |
405 |
printf("d=%d\t\t\t\t# density of directions\n", thisillum.sampdens); |
406 |
printf("s=%d\t\t\t\t# samples per direction\n", thisillum.nsamps); |
407 |
printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt); |
408 |
} |
409 |
|
410 |
|
411 |
void |
412 |
printhead( /* print out header */ |
413 |
int ac, |
414 |
char **av |
415 |
) |
416 |
{ |
417 |
putchar('#'); |
418 |
while (ac-- > 0) { |
419 |
putchar(' '); |
420 |
fputs(*av++, stdout); |
421 |
} |
422 |
fputs("\n#@mkillum !\n", stdout); |
423 |
} |
424 |
|
425 |
|
426 |
void |
427 |
xobject( /* translate an object from fp */ |
428 |
FILE *fp, |
429 |
char *nm |
430 |
) |
431 |
{ |
432 |
OBJREC thisobj; |
433 |
char str[MAXSTR]; |
434 |
int doit; |
435 |
/* read the object */ |
436 |
if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL) |
437 |
goto readerr; |
438 |
if (fgetword(str, MAXSTR, fp) == NULL) |
439 |
goto readerr; |
440 |
/* is it an alias? */ |
441 |
if (!strcmp(str, ALIASKEY)) { |
442 |
if (fgetword(str, MAXSTR, fp) == NULL) |
443 |
goto readerr; |
444 |
printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str); |
445 |
if (fgetword(str, MAXSTR, fp) == NULL) |
446 |
goto readerr; |
447 |
printf("\t%s\n", str); |
448 |
return; |
449 |
} |
450 |
thisobj.omod = OVOID; /* unused field */ |
451 |
if ((thisobj.otype = otype(str)) < 0) { |
452 |
sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str); |
453 |
error(USER, errmsg); |
454 |
} |
455 |
if (fgetword(str, MAXSTR, fp) == NULL) |
456 |
goto readerr; |
457 |
thisobj.oname = str; |
458 |
if (readfargs(&thisobj.oargs, fp) != 1) |
459 |
goto readerr; |
460 |
thisobj.os = NULL; |
461 |
/* check for translation */ |
462 |
switch (matselect) { |
463 |
case S_NONE: |
464 |
doit = 0; |
465 |
break; |
466 |
case S_ALL: |
467 |
doit = 1; |
468 |
break; |
469 |
case S_ELEM: |
470 |
doit = !strcmp(thisillum.altmat, matcheck); |
471 |
break; |
472 |
case S_COMPL: |
473 |
doit = strcmp(thisillum.altmat, matcheck); |
474 |
break; |
475 |
} |
476 |
doit = doit && issurface(thisobj.otype); |
477 |
/* print header? */ |
478 |
checkhead(); |
479 |
/* process object */ |
480 |
if (doit) |
481 |
switch (thisobj.otype) { |
482 |
case OBJ_FACE: |
483 |
my_face(&thisobj, &thisillum, nm); |
484 |
break; |
485 |
case OBJ_SPHERE: |
486 |
my_sphere(&thisobj, &thisillum, nm); |
487 |
break; |
488 |
case OBJ_RING: |
489 |
my_ring(&thisobj, &thisillum, nm); |
490 |
break; |
491 |
default: |
492 |
my_default(&thisobj, &thisillum, nm); |
493 |
break; |
494 |
} |
495 |
else |
496 |
printobj(thisillum.altmat, &thisobj); |
497 |
/* free arguments */ |
498 |
freefargs(&thisobj.oargs); |
499 |
return; |
500 |
readerr: |
501 |
sprintf(errmsg, "(%s): error reading input", nm); |
502 |
error(USER, errmsg); |
503 |
} |