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