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