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