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 "standard.h" |
12 |
|
13 |
#include "object.h" |
14 |
|
15 |
#include "otypes.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", "-ab", "2", |
29 |
"-ad", "256", "-as", "128", "-aa", ".15" }; |
30 |
int rtargc = 13; |
31 |
/* overriding rtrace options */ |
32 |
char *myrtopts[] = { "-I-", "-i-", "-di+", "-ov", "-h-", "-fff", NULL }; |
33 |
|
34 |
/* illum flags */ |
35 |
#define IL_FLAT 0x1 /* flat surface */ |
36 |
#define IL_LIGHT 0x2 /* light rather than illum */ |
37 |
#define IL_COLDST 0x4 /* colored distribution */ |
38 |
#define IL_COLAVG 0x8 /* compute average color */ |
39 |
#define IL_DATCLB 0x10 /* OK to clobber data file */ |
40 |
|
41 |
struct illum_args { |
42 |
int flags; /* flags from list above */ |
43 |
char matname[MAXSTR]; /* illum material name */ |
44 |
char datafile[MAXSTR]; /* distribution data file name */ |
45 |
int dfnum; /* data file number */ |
46 |
char altmatname[MAXSTR]; /* alternate material name */ |
47 |
int nsamps; /* # of samples in each direction */ |
48 |
int nalt, nazi; /* # of altitude and azimuth angles */ |
49 |
FVECT orient; /* coordinate system orientation */ |
50 |
} thisillum = { /* default values */ |
51 |
0, |
52 |
DFLMAT, |
53 |
DFLMAT, |
54 |
0, |
55 |
VOIDID, |
56 |
NSAMPS, |
57 |
}; |
58 |
|
59 |
int sampdens = SAMPDENS; /* sample point density */ |
60 |
char matcheck[MAXSTR]; /* current material to include or exclude */ |
61 |
int matselect = S_ALL; /* selection criterion */ |
62 |
|
63 |
int rt_pd[3]; /* rtrace pipe descriptors */ |
64 |
float *rt_buf; /* rtrace i/o buffer */ |
65 |
int rt_bsiz; /* maximum rays for rtrace buffer */ |
66 |
int rt_nrays; /* current length of rtrace buffer */ |
67 |
|
68 |
int gargc; /* global argc */ |
69 |
char **gargv; /* global argv */ |
70 |
#define progname gargv[0] |
71 |
|
72 |
int doneheader = 0; /* printed header yet? */ |
73 |
|
74 |
extern char *fgetline(), *fgetword(), *sskip(), |
75 |
*atos(), *iskip(), *fskip(); |
76 |
extern FILE *popen(); |
77 |
|
78 |
|
79 |
main(argc, argv) /* compute illum distributions using rtrace */ |
80 |
int argc; |
81 |
char *argv[]; |
82 |
{ |
83 |
extern char *getenv(), *getpath(); |
84 |
char *rtpath; |
85 |
register int i; |
86 |
/* set global arguments */ |
87 |
gargc = argc; gargv = argv; |
88 |
/* set up rtrace command */ |
89 |
if (argc < 2) |
90 |
error(USER, "too few arguments"); |
91 |
for (i = 1; i < argc-1; i++) |
92 |
rtargv[rtargc++] = argv[i]; |
93 |
for (i = 0; myrtopts[i] != NULL; i++) |
94 |
rtargv[rtargc++] = myrtopts[i]; |
95 |
rtargv[rtargc++] = argv[argc-1]; |
96 |
rtargv[rtargc] = NULL; |
97 |
/* just asking for defaults? */ |
98 |
if (!strcmp(argv[argc-1], "-defaults")) { |
99 |
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
100 |
if (rtpath == NULL) { |
101 |
eputs(rtargv[0]); |
102 |
eputs(": command not found\n"); |
103 |
exit(1); |
104 |
} |
105 |
execv(rtpath, rtargv); |
106 |
perror(rtpath); |
107 |
exit(1); |
108 |
} |
109 |
/* else initialize and run our calculation */ |
110 |
init(); |
111 |
mkillum(stdin, "standard input"); |
112 |
exit(cleanup()); /* cleanup returns rtrace status */ |
113 |
} |
114 |
|
115 |
|
116 |
init() /* start rtrace and set up buffers */ |
117 |
{ |
118 |
int maxbytes; |
119 |
|
120 |
maxbytes = open_process(rt_pd, rtargv); |
121 |
if (maxbytes == 0) { |
122 |
eputs(rtargv[0]); |
123 |
eputs(": command not found\n"); |
124 |
exit(1); |
125 |
} |
126 |
if (maxbytes < 0) |
127 |
error(SYSTEM, "cannot start rtrace process"); |
128 |
rt_bsiz = maxbytes/(6*sizeof(float)); |
129 |
rt_buf = (float *)malloc(rt_bsiz*(6*sizeof(float))); |
130 |
if (rt_buf == NULL) |
131 |
error(SYSTEM, "out of memory in init"); |
132 |
rt_bsiz--; /* allow for flush ray */ |
133 |
rt_nrays = 0; |
134 |
} |
135 |
|
136 |
|
137 |
int |
138 |
cleanup() /* close rtrace process and return status */ |
139 |
{ |
140 |
int status; |
141 |
|
142 |
free((char *)rt_buf); |
143 |
rt_bsiz = 0; |
144 |
status = close_process(rt_pd); |
145 |
if (status < 0) |
146 |
return(0); /* unknown status */ |
147 |
return(status); |
148 |
} |
149 |
|
150 |
|
151 |
eputs(s) /* put string to stderr */ |
152 |
register char *s; |
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 |
mkillum(infp, name) /* process stream */ |
167 |
register FILE *infp; |
168 |
char *name; |
169 |
{ |
170 |
char buf[512]; |
171 |
FILE *pfp; |
172 |
register int c; |
173 |
|
174 |
while ((c = getc(infp)) != EOF) { |
175 |
if (isspace(c)) |
176 |
continue; |
177 |
if (c == '#') { /* comment/options */ |
178 |
buf[0] = c; |
179 |
fgets(buf+1, sizeof(buf)-1, infp); |
180 |
fputs(buf, stdout); |
181 |
getoptions(buf, name); |
182 |
} else if (c == '!') { /* command */ |
183 |
buf[0] = c; |
184 |
fgetline(buf+1, sizeof(buf)-1, infp); |
185 |
if ((pfp = popen(buf+1, "r")) == NULL) { |
186 |
sprintf(errmsg, "cannot execute \"%s\"", buf); |
187 |
error(SYSTEM, errmsg); |
188 |
} |
189 |
mkillum(pfp, buf); |
190 |
pclose(pfp); |
191 |
} else { /* object */ |
192 |
ungetc(c, infp); |
193 |
xobject(infp, name); |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
|
199 |
getoptions(s, nm) /* get options from string s */ |
200 |
char *s; |
201 |
char *nm; |
202 |
{ |
203 |
extern FILE *freopen(); |
204 |
char buf[64]; |
205 |
int nerrs = 0; |
206 |
register char *cp; |
207 |
|
208 |
if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) |
209 |
return; |
210 |
cp = s+10; |
211 |
while (*cp) { |
212 |
switch (*cp) { |
213 |
case ' ': |
214 |
case '\t': |
215 |
case '\n': |
216 |
cp++; |
217 |
continue; |
218 |
case 'm': /* material name */ |
219 |
cp++; |
220 |
if (*cp != '=') |
221 |
break; |
222 |
cp++; |
223 |
atos(thisillum.matname, MAXSTR, cp); |
224 |
cp = sskip(cp); |
225 |
if (!(thisillum.flags & IL_DATCLB)) { |
226 |
strcpy(thisillum.datafile, thisillum.matname); |
227 |
thisillum.dfnum = 0; |
228 |
} |
229 |
continue; |
230 |
case 'f': /* data file name */ |
231 |
cp++; |
232 |
if (*cp != '=') |
233 |
break; |
234 |
cp++; |
235 |
if (*cp == '\0') { |
236 |
thisillum.flags &= ~IL_DATCLB; |
237 |
continue; |
238 |
} |
239 |
atos(thisillum.datafile, MAXSTR, cp); |
240 |
cp = sskip(cp); |
241 |
thisillum.dfnum = 0; |
242 |
thisillum.flags |= IL_DATCLB; |
243 |
continue; |
244 |
case 'i': /* include material */ |
245 |
case 'e': /* exclude material */ |
246 |
matselect = (*cp++ == 'i') ? S_ELEM : S_COMPL; |
247 |
if (*cp != '=') |
248 |
break; |
249 |
cp++; |
250 |
atos(matcheck, MAXSTR, cp); |
251 |
cp = sskip(cp); |
252 |
continue; |
253 |
case 'a': /* use everything */ |
254 |
cp = sskip(cp); |
255 |
matselect = S_ALL; |
256 |
continue; |
257 |
case 'n': /* use nothing (passive) */ |
258 |
cp = sskip(cp); |
259 |
matselect = S_NONE; |
260 |
continue; |
261 |
case 'c': /* color calculation */ |
262 |
cp++; |
263 |
if (*cp != '=') |
264 |
break; |
265 |
cp++; |
266 |
switch (*cp) { |
267 |
case 'a': /* average */ |
268 |
thisillum.flags |= IL_COLAVG; |
269 |
thisillum.flags &= ~IL_COLDST; |
270 |
break; |
271 |
case 'd': /* distribution */ |
272 |
thisillum.flags |= IL_COLDST; |
273 |
thisillum.flags &= ~IL_COLAVG; |
274 |
break; |
275 |
case 'n': /* none */ |
276 |
thisillum.flags &= ~(IL_COLAVG|IL_COLDST); |
277 |
break; |
278 |
default: |
279 |
goto opterr; |
280 |
} |
281 |
cp = sskip(cp); |
282 |
continue; |
283 |
case 'd': /* point sample density */ |
284 |
cp++; |
285 |
if (*cp != '=') |
286 |
break; |
287 |
cp++; |
288 |
if (!isintd(cp, " \t\n")) |
289 |
break; |
290 |
sampdens = atoi(cp); |
291 |
cp = sskip(cp); |
292 |
continue; |
293 |
case 's': /* point super-samples */ |
294 |
cp++; |
295 |
if (*cp != '=') |
296 |
break; |
297 |
cp++; |
298 |
if (!isintd(cp, " \t\n")) |
299 |
break; |
300 |
thisillum.nsamps = atoi(cp); |
301 |
cp = sskip(cp); |
302 |
continue; |
303 |
case 'o': /* output file */ |
304 |
cp++; |
305 |
if (*cp != '=') |
306 |
break; |
307 |
cp++; |
308 |
atos(buf, sizeof(buf), cp); |
309 |
cp = sskip(cp); |
310 |
if (freopen(buf, "w", stdout) == NULL) { |
311 |
sprintf(errmsg, |
312 |
"cannot open output file \"%s\"", buf); |
313 |
error(SYSTEM, errmsg); |
314 |
} |
315 |
doneheader = 0; |
316 |
continue; |
317 |
} |
318 |
opterr: /* skip faulty option */ |
319 |
cp = sskip(cp); |
320 |
nerrs++; |
321 |
} |
322 |
if (nerrs) { |
323 |
sprintf(errmsg, "(%s): %d error(s) in option line:", |
324 |
nm, nerrs); |
325 |
error(WARNING, errmsg); |
326 |
eputs(s); |
327 |
} |
328 |
} |