1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Make illum sources for optimizing rendering process |
9 |
*/ |
10 |
|
11 |
#include "mkillum.h" |
12 |
|
13 |
#include <signal.h> |
14 |
|
15 |
#include <ctype.h> |
16 |
|
17 |
/* default parameters */ |
18 |
#define SAMPDENS 128 /* points per projected steradian */ |
19 |
#define NSAMPS 32 /* samples per point */ |
20 |
#define DFLMAT "illum_mat" /* material name */ |
21 |
/* selection options */ |
22 |
#define S_NONE 0 /* select none */ |
23 |
#define S_ELEM 1 /* select specified element */ |
24 |
#define S_COMPL 2 /* select all but element */ |
25 |
#define S_ALL 3 /* select all */ |
26 |
|
27 |
/* rtrace command and defaults */ |
28 |
char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-di+", |
29 |
"-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", }; |
30 |
int rtargc = 14; |
31 |
/* overriding rtrace options */ |
32 |
char *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-fff", NULL }; |
33 |
|
34 |
struct rtproc rt; /* our rtrace process */ |
35 |
|
36 |
struct illum_args thisillum = { /* our illum and default values */ |
37 |
0, |
38 |
DFLMAT, |
39 |
DFLMAT, |
40 |
0, |
41 |
VOIDID, |
42 |
SAMPDENS, |
43 |
NSAMPS, |
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 |
extern char *fgetline(), *fgetword(), *sskip(), |
61 |
*atos(), *iskip(), *fskip(), *strcpy(); |
62 |
extern FILE *popen(); |
63 |
|
64 |
|
65 |
main(argc, argv) /* compute illum distributions using rtrace */ |
66 |
int argc; |
67 |
char *argv[]; |
68 |
{ |
69 |
extern char *getenv(), *getpath(); |
70 |
char *rtpath; |
71 |
FILE *fp; |
72 |
register int i; |
73 |
/* set global arguments */ |
74 |
gargv = argv; |
75 |
/* set up rtrace command */ |
76 |
for (i = 1; i < argc; i++) { |
77 |
if (argv[i][0] == '<' && !argv[i][1]) |
78 |
break; |
79 |
rtargv[rtargc++] = argv[i]; |
80 |
if (argv[i][0] == '-' && argv[i][1] == 'w') |
81 |
warnings = !warnings; |
82 |
} |
83 |
if ((gargc = i) < 2) |
84 |
error(USER, "too few arguments"); |
85 |
rtargc--; |
86 |
for (i = 0; myrtopts[i] != NULL; i++) |
87 |
rtargv[rtargc++] = myrtopts[i]; |
88 |
rtargv[rtargc++] = argv[gargc-1]; |
89 |
rtargv[rtargc] = NULL; |
90 |
/* just asking for defaults? */ |
91 |
if (!strcmp(argv[gargc-1], "-defaults")) { |
92 |
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
93 |
if (rtpath == NULL) { |
94 |
eputs(rtargv[0]); |
95 |
eputs(": command not found\n"); |
96 |
exit(1); |
97 |
} |
98 |
execv(rtpath, rtargv); |
99 |
perror(rtpath); |
100 |
exit(1); |
101 |
} |
102 |
/* else initialize and run our calculation */ |
103 |
init(); |
104 |
if (gargc+1 < argc) |
105 |
for (i = gargc+1; i < argc; i++) { |
106 |
if ((fp = fopen(argv[i], "r")) == NULL) { |
107 |
sprintf(errmsg, |
108 |
"cannot open scene file \"%s\"", argv[i]); |
109 |
error(SYSTEM, errmsg); |
110 |
} |
111 |
filter(fp, argv[i]); |
112 |
fclose(fp); |
113 |
} |
114 |
else |
115 |
filter(stdin, "standard input"); |
116 |
quit(0); |
117 |
} |
118 |
|
119 |
|
120 |
quit(status) /* exit with status */ |
121 |
int status; |
122 |
{ |
123 |
int rtstat; |
124 |
|
125 |
rtstat = close_process(rt.pd); |
126 |
if (status == 0) |
127 |
if (rtstat < 0) |
128 |
error(WARNING, |
129 |
"unknown return status from rtrace process"); |
130 |
else |
131 |
status = rtstat; |
132 |
exit(status); |
133 |
} |
134 |
|
135 |
|
136 |
init() /* start rtrace and set up buffers */ |
137 |
{ |
138 |
extern int o_face(), o_sphere(), o_ring(); |
139 |
int maxbytes; |
140 |
/* set up object functions */ |
141 |
ofun[OBJ_FACE].funp = o_face; |
142 |
ofun[OBJ_SPHERE].funp = o_sphere; |
143 |
ofun[OBJ_RING].funp = o_ring; |
144 |
/* set up signal handling */ |
145 |
signal(SIGPIPE, quit); |
146 |
/* start rtrace process */ |
147 |
maxbytes = open_process(rt.pd, rtargv); |
148 |
if (maxbytes == 0) { |
149 |
eputs(rtargv[0]); |
150 |
eputs(": command not found\n"); |
151 |
exit(1); |
152 |
} |
153 |
if (maxbytes < 0) |
154 |
error(SYSTEM, "cannot start rtrace process"); |
155 |
rt.bsiz = maxbytes/(6*sizeof(float)); |
156 |
rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--); |
157 |
rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz); |
158 |
if (rt.buf == NULL || rt.dest == NULL) |
159 |
error(SYSTEM, "out of memory in init"); |
160 |
rt.nrays = 0; |
161 |
/* set up urand */ |
162 |
initurand(2048); |
163 |
} |
164 |
|
165 |
|
166 |
eputs(s) /* put string to stderr */ |
167 |
register char *s; |
168 |
{ |
169 |
static int midline = 0; |
170 |
|
171 |
if (!*s) return; |
172 |
if (!midline) { |
173 |
fputs(progname, stderr); |
174 |
fputs(": ", stderr); |
175 |
} |
176 |
fputs(s, stderr); |
177 |
midline = s[strlen(s)-1] != '\n'; |
178 |
} |
179 |
|
180 |
|
181 |
wputs(s) /* print warning if enabled */ |
182 |
char *s; |
183 |
{ |
184 |
if (warnings) |
185 |
eputs(s); |
186 |
} |
187 |
|
188 |
|
189 |
filter(infp, name) /* process stream */ |
190 |
register FILE *infp; |
191 |
char *name; |
192 |
{ |
193 |
char buf[512]; |
194 |
FILE *pfp; |
195 |
register 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 |
xoptions(s, nm) /* process options in string s */ |
222 |
char *s; |
223 |
char *nm; |
224 |
{ |
225 |
extern FILE *freopen(); |
226 |
char buf[64]; |
227 |
int nerrs = 0; |
228 |
register char *cp; |
229 |
|
230 |
if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) { |
231 |
fputs(s, stdout); /* not for us */ |
232 |
return; |
233 |
} |
234 |
cp = s+10; |
235 |
while (*cp) { |
236 |
switch (*cp) { |
237 |
case ' ': |
238 |
case '\t': |
239 |
case '\n': |
240 |
cp++; |
241 |
continue; |
242 |
case 'm': /* material name */ |
243 |
if (*++cp != '=') |
244 |
break; |
245 |
if (!*++cp) |
246 |
break; |
247 |
atos(thisillum.matname, MAXSTR, cp); |
248 |
cp = sskip(cp); |
249 |
if (!(thisillum.flags & IL_DATCLB)) { |
250 |
strcpy(thisillum.datafile, thisillum.matname); |
251 |
thisillum.dfnum = 0; |
252 |
} |
253 |
continue; |
254 |
case 'f': /* data file name */ |
255 |
if (*++cp != '=') |
256 |
break; |
257 |
if (!*++cp) { |
258 |
strcpy(thisillum.datafile,thisillum.matname); |
259 |
thisillum.flags &= ~IL_DATCLB; |
260 |
continue; |
261 |
} |
262 |
atos(thisillum.datafile, MAXSTR, cp); |
263 |
cp = sskip(cp); |
264 |
thisillum.dfnum = 0; |
265 |
thisillum.flags |= IL_DATCLB; |
266 |
continue; |
267 |
case 'i': /* include material */ |
268 |
case 'e': /* exclude material */ |
269 |
if (cp[1] != '=') |
270 |
break; |
271 |
matselect = (*++cp == 'i') ? S_ELEM : S_COMPL; |
272 |
atos(matcheck, MAXSTR, ++cp); |
273 |
cp = sskip(cp); |
274 |
continue; |
275 |
case 'a': /* use everything */ |
276 |
cp = sskip(cp); |
277 |
matselect = S_ALL; |
278 |
continue; |
279 |
case 'n': /* use nothing (passive) */ |
280 |
cp = sskip(cp); |
281 |
matselect = S_NONE; |
282 |
continue; |
283 |
case 'c': /* color calculation */ |
284 |
if (*++cp != '=') |
285 |
break; |
286 |
switch (*++cp) { |
287 |
case 'a': /* average */ |
288 |
thisillum.flags = (thisillum.flags|IL_COLAVG) |
289 |
& ~IL_COLDST; |
290 |
break; |
291 |
case 'd': /* distribution */ |
292 |
thisillum.flags |= (IL_COLDST|IL_COLAVG); |
293 |
break; |
294 |
case 'n': /* none */ |
295 |
thisillum.flags &= ~(IL_COLAVG|IL_COLDST); |
296 |
break; |
297 |
default: |
298 |
goto opterr; |
299 |
} |
300 |
cp = sskip(cp); |
301 |
continue; |
302 |
case 'd': /* point sample density */ |
303 |
if (*++cp != '=') |
304 |
break; |
305 |
if (!isintd(++cp, " \t\n")) |
306 |
break; |
307 |
thisillum.sampdens = atoi(cp); |
308 |
cp = sskip(cp); |
309 |
continue; |
310 |
case 's': /* point super-samples */ |
311 |
if (*++cp != '=') |
312 |
break; |
313 |
if (!isintd(++cp, " \t\n")) |
314 |
break; |
315 |
thisillum.nsamps = atoi(cp); |
316 |
cp = sskip(cp); |
317 |
continue; |
318 |
case 'l': /* light sources */ |
319 |
cp++; |
320 |
if (*cp == '+') |
321 |
thisillum.flags |= IL_LIGHT; |
322 |
else if (*cp == '-') |
323 |
thisillum.flags &= ~IL_LIGHT; |
324 |
else |
325 |
break; |
326 |
cp++; |
327 |
continue; |
328 |
case 'o': /* output file */ |
329 |
if (*++cp != '=') |
330 |
break; |
331 |
if (!*++cp) |
332 |
break; |
333 |
atos(buf, sizeof(buf), cp); |
334 |
cp = sskip(cp); |
335 |
if (freopen(buf, "w", stdout) == NULL) { |
336 |
sprintf(errmsg, |
337 |
"cannot open output file \"%s\"", buf); |
338 |
error(SYSTEM, errmsg); |
339 |
} |
340 |
doneheader = 0; |
341 |
continue; |
342 |
case '!': /* processed file! */ |
343 |
sprintf(errmsg, "(%s): processed by mkillum already!", |
344 |
nm); |
345 |
error(WARNING, errmsg); |
346 |
return; |
347 |
} |
348 |
opterr: /* skip faulty option */ |
349 |
cp = sskip(cp); |
350 |
nerrs++; |
351 |
} |
352 |
/* print header? */ |
353 |
checkhead(); |
354 |
/* issue warnings? */ |
355 |
if (nerrs) { |
356 |
sprintf(errmsg, "(%s): %d error(s) in option line:", |
357 |
nm, nerrs); |
358 |
error(WARNING, errmsg); |
359 |
wputs(s); |
360 |
printf("# %s: the following option line has %d error(s):\n", |
361 |
progname, nerrs); |
362 |
} |
363 |
/* print pure comment */ |
364 |
printf("# %s", s+2); |
365 |
} |
366 |
|
367 |
|
368 |
printhead(ac, av) /* print out header */ |
369 |
register int ac; |
370 |
register char **av; |
371 |
{ |
372 |
putchar('#'); |
373 |
while (ac-- > 0) { |
374 |
putchar(' '); |
375 |
fputs(*av++, stdout); |
376 |
} |
377 |
fputs("\n#@mkillum !\n", stdout); |
378 |
} |
379 |
|
380 |
|
381 |
xobject(fp, nm) /* translate an object from fp */ |
382 |
FILE *fp; |
383 |
char *nm; |
384 |
{ |
385 |
OBJREC thisobj; |
386 |
char str[MAXSTR]; |
387 |
int doit; |
388 |
/* read the object */ |
389 |
if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL) |
390 |
goto readerr; |
391 |
if (fgetword(str, MAXSTR, fp) == NULL) |
392 |
goto readerr; |
393 |
/* is it an alias? */ |
394 |
if (!strcmp(str, ALIASID)) { |
395 |
if (fgetword(str, MAXSTR, fp) == NULL) |
396 |
goto readerr; |
397 |
printf("\n%s %s %s", thisillum.altmat, ALIASID, str); |
398 |
if (fgetword(str, MAXSTR, fp) == NULL) |
399 |
goto readerr; |
400 |
printf("\t%s\n", str); |
401 |
return; |
402 |
} |
403 |
thisobj.omod = OVOID; /* unused field */ |
404 |
if ((thisobj.otype = otype(str)) < 0) { |
405 |
sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str); |
406 |
error(USER, errmsg); |
407 |
} |
408 |
if (fgetword(str, MAXSTR, fp) == NULL) |
409 |
goto readerr; |
410 |
thisobj.oname = str; |
411 |
if (readfargs(&thisobj.oargs, fp) != 1) |
412 |
goto readerr; |
413 |
thisobj.os = NULL; |
414 |
/* check for translation */ |
415 |
switch (matselect) { |
416 |
case S_NONE: |
417 |
doit = 0; |
418 |
break; |
419 |
case S_ALL: |
420 |
doit = 1; |
421 |
break; |
422 |
case S_ELEM: |
423 |
doit = !strcmp(thisillum.altmat, matcheck); |
424 |
break; |
425 |
case S_COMPL: |
426 |
doit = strcmp(thisillum.altmat, matcheck); |
427 |
break; |
428 |
} |
429 |
doit = doit && issurface(thisobj.otype); |
430 |
/* print header? */ |
431 |
checkhead(); |
432 |
/* process object */ |
433 |
if (doit) |
434 |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm); |
435 |
else |
436 |
printobj(thisillum.altmat, &thisobj); |
437 |
/* free arguments */ |
438 |
freefargs(&thisobj.oargs); |
439 |
return; |
440 |
readerr: |
441 |
sprintf(errmsg, "(%s): error reading scene", nm); |
442 |
error(USER, errmsg); |
443 |
} |