1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Make illum sources for optimizing rendering process |
6 |
|
*/ |
7 |
|
|
11 |
– |
#include "mkillum.h" |
12 |
– |
|
8 |
|
#include <signal.h> |
14 |
– |
|
9 |
|
#include <ctype.h> |
10 |
|
|
11 |
+ |
#include "platform.h" |
12 |
+ |
#include "mkillum.h" |
13 |
+ |
|
14 |
|
/* default parameters */ |
15 |
< |
#define SAMPDENS 128 /* points per projected steradian */ |
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 */ |
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; |
26 |
> |
char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-", |
27 |
> |
"-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", }; |
28 |
> |
int rtargc = 14; |
29 |
|
/* overriding rtrace options */ |
30 |
< |
char *myrtopts[] = { "-I-", "-i-", "-di+", "-ov", "-h-", "-fff", NULL }; |
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 |
< |
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 */ |
57 |
|
|
58 |
|
int warnings = 1; /* print warnings? */ |
59 |
|
|
60 |
– |
extern char *fgetline(), *fgetword(), *sskip(), |
61 |
– |
*atos(), *iskip(), *fskip(); |
62 |
– |
extern FILE *popen(); |
60 |
|
|
64 |
– |
|
61 |
|
main(argc, argv) /* compute illum distributions using rtrace */ |
62 |
|
int argc; |
63 |
|
char *argv[]; |
64 |
|
{ |
69 |
– |
extern char *getenv(), *getpath(); |
65 |
|
char *rtpath; |
66 |
|
FILE *fp; |
67 |
|
register int i; |
69 |
|
gargv = argv; |
70 |
|
/* set up rtrace command */ |
71 |
|
for (i = 1; i < argc; i++) { |
72 |
< |
if (argv[i][0] == '<' && !argv[i][1]) |
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 |
< |
warnings = !warnings; |
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 |
< |
if ((gargc = i) < 2) |
84 |
< |
error(USER, "too few arguments"); |
94 |
> |
gargc = i; |
95 |
|
rtargc--; |
96 |
|
for (i = 0; myrtopts[i] != NULL; i++) |
97 |
|
rtargv[rtargc++] = myrtopts[i]; |
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]); |
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 { |
119 |
> |
if ((fp = fopen(argv[i], "r")) == NULL) { |
120 |
|
sprintf(errmsg, |
121 |
|
"cannot open scene file \"%s\"", argv[i]); |
122 |
|
error(SYSTEM, errmsg); |
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) |
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 |
+ |
} |
147 |
|
exit(status); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
init() /* start rtrace and set up buffers */ |
152 |
|
{ |
153 |
< |
extern int o_face(), o_ring(); |
153 |
> |
extern int o_face(), o_sphere(), o_ring(); |
154 |
|
int maxbytes; |
155 |
|
/* set up object functions */ |
156 |
|
ofun[OBJ_FACE].funp = o_face; |
157 |
+ |
ofun[OBJ_SPHERE].funp = o_sphere; |
158 |
|
ofun[OBJ_RING].funp = o_ring; |
159 |
|
/* set up signal handling */ |
160 |
+ |
#ifdef SIGPIPE /* not present on Windows */ |
161 |
|
signal(SIGPIPE, quit); |
162 |
+ |
#endif |
163 |
|
/* start rtrace process */ |
164 |
< |
maxbytes = open_process(rt.pd, rtargv); |
164 |
> |
errno = 0; |
165 |
> |
maxbytes = open_process(&(rt.pd), rtargv); |
166 |
|
if (maxbytes == 0) { |
167 |
|
eputs(rtargv[0]); |
168 |
|
eputs(": command not found\n"); |
171 |
|
if (maxbytes < 0) |
172 |
|
error(SYSTEM, "cannot start rtrace process"); |
173 |
|
rt.bsiz = maxbytes/(6*sizeof(float)); |
174 |
< |
rt.buf = (float *)malloc(rt.bsiz*(6*sizeof(float))); |
175 |
< |
if (rt.buf == NULL) |
174 |
> |
rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--); |
175 |
> |
rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz); |
176 |
> |
if (rt.buf == NULL || rt.dest == NULL) |
177 |
|
error(SYSTEM, "out of memory in init"); |
158 |
– |
rt.bsiz--; /* allow for flush ray */ |
178 |
|
rt.nrays = 0; |
179 |
+ |
/* set up urand */ |
180 |
+ |
initurand(16384); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
void |
185 |
|
eputs(s) /* put string to stderr */ |
186 |
|
register char *s; |
187 |
|
{ |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
+ |
void |
201 |
|
wputs(s) /* print warning if enabled */ |
202 |
|
char *s; |
203 |
|
{ |
257 |
|
case ' ': |
258 |
|
case '\t': |
259 |
|
case '\n': |
260 |
+ |
case '\r': |
261 |
+ |
case '\f': |
262 |
|
cp++; |
263 |
|
continue; |
264 |
|
case 'm': /* material name */ |
265 |
|
if (*++cp != '=') |
266 |
|
break; |
267 |
< |
if (!*++cp) |
267 |
> |
if (!*++cp || isspace(*cp)) |
268 |
|
break; |
269 |
|
atos(thisillum.matname, MAXSTR, cp); |
270 |
|
cp = sskip(cp); |
276 |
|
case 'f': /* data file name */ |
277 |
|
if (*++cp != '=') |
278 |
|
break; |
279 |
< |
if (!*++cp) { |
279 |
> |
if (!*++cp || isspace(*cp)) { |
280 |
> |
strcpy(thisillum.datafile,thisillum.matname); |
281 |
> |
thisillum.dfnum = 0; |
282 |
|
thisillum.flags &= ~IL_DATCLB; |
283 |
|
continue; |
284 |
|
} |
289 |
|
continue; |
290 |
|
case 'i': /* include material */ |
291 |
|
case 'e': /* exclude material */ |
292 |
< |
matselect = (*cp == 'i') ? S_ELEM : S_COMPL; |
266 |
< |
if (*++cp != '=') |
292 |
> |
if (cp[1] != '=') |
293 |
|
break; |
294 |
< |
atos(matcheck, MAXSTR, ++cp); |
294 |
> |
matselect = (*cp == 'i') ? S_ELEM : S_COMPL; |
295 |
> |
cp += 2; |
296 |
> |
atos(matcheck, MAXSTR, cp); |
297 |
|
cp = sskip(cp); |
298 |
|
continue; |
299 |
|
case 'a': /* use everything */ |
309 |
|
break; |
310 |
|
switch (*++cp) { |
311 |
|
case 'a': /* average */ |
312 |
< |
thisillum.flags |= IL_COLAVG; |
313 |
< |
thisillum.flags &= ~IL_COLDST; |
312 |
> |
thisillum.flags = (thisillum.flags|IL_COLAVG) |
313 |
> |
& ~IL_COLDST; |
314 |
|
break; |
315 |
|
case 'd': /* distribution */ |
316 |
< |
thisillum.flags |= IL_COLDST; |
289 |
< |
thisillum.flags &= ~IL_COLAVG; |
316 |
> |
thisillum.flags |= (IL_COLDST|IL_COLAVG); |
317 |
|
break; |
318 |
|
case 'n': /* none */ |
319 |
|
thisillum.flags &= ~(IL_COLAVG|IL_COLDST); |
326 |
|
case 'd': /* point sample density */ |
327 |
|
if (*++cp != '=') |
328 |
|
break; |
329 |
< |
if (!isintd(++cp, " \t\n")) |
329 |
> |
if (!isintd(++cp, " \t\n\r")) |
330 |
|
break; |
331 |
|
thisillum.sampdens = atoi(cp); |
332 |
|
cp = sskip(cp); |
334 |
|
case 's': /* point super-samples */ |
335 |
|
if (*++cp != '=') |
336 |
|
break; |
337 |
< |
if (!isintd(++cp, " \t\n")) |
337 |
> |
if (!isintd(++cp, " \t\n\r")) |
338 |
|
break; |
339 |
|
thisillum.nsamps = atoi(cp); |
340 |
|
cp = sskip(cp); |
341 |
|
continue; |
342 |
|
case 'l': /* light sources */ |
343 |
< |
if (*++cp != '+' && *cp != '-') |
344 |
< |
break; |
318 |
< |
if (*cp++ == '+') |
343 |
> |
cp++; |
344 |
> |
if (*cp == '+') |
345 |
|
thisillum.flags |= IL_LIGHT; |
346 |
< |
else |
346 |
> |
else if (*cp == '-') |
347 |
|
thisillum.flags &= ~IL_LIGHT; |
348 |
+ |
else |
349 |
+ |
break; |
350 |
+ |
cp++; |
351 |
|
continue; |
352 |
+ |
case 'b': /* brightness */ |
353 |
+ |
if (*++cp != '=') |
354 |
+ |
break; |
355 |
+ |
if (!isfltd(++cp, " \t\n\r")) |
356 |
+ |
break; |
357 |
+ |
thisillum.minbrt = atof(cp); |
358 |
+ |
if (thisillum.minbrt < 0.) |
359 |
+ |
thisillum.minbrt = 0.; |
360 |
+ |
cp = sskip(cp); |
361 |
+ |
continue; |
362 |
|
case 'o': /* output file */ |
363 |
|
if (*++cp != '=') |
364 |
|
break; |
365 |
< |
if (!*++cp) |
365 |
> |
if (!*++cp || isspace(*cp)) |
366 |
|
break; |
367 |
|
atos(buf, sizeof(buf), cp); |
368 |
|
cp = sskip(cp); |
373 |
|
} |
374 |
|
doneheader = 0; |
375 |
|
continue; |
376 |
+ |
case '!': /* processed file! */ |
377 |
+ |
sprintf(errmsg, "(%s): already processed!", nm); |
378 |
+ |
error(WARNING, errmsg); |
379 |
+ |
matselect = S_NONE; |
380 |
+ |
return; |
381 |
|
} |
382 |
|
opterr: /* skip faulty option */ |
383 |
< |
cp = sskip(cp); |
383 |
> |
while (*cp && !isspace(*cp)) |
384 |
> |
cp++; |
385 |
|
nerrs++; |
386 |
|
} |
387 |
|
/* print header? */ |
396 |
|
progname, nerrs); |
397 |
|
} |
398 |
|
/* print pure comment */ |
399 |
< |
putchar('#'); putchar(' '); fputs(s+2, stdout); |
399 |
> |
printf("# %s", s+2); |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
+ |
printopts() /* print out option default values */ |
404 |
+ |
{ |
405 |
+ |
printf("m=%-15s\t\t# material name\n", thisillum.matname); |
406 |
+ |
printf("f=%-15s\t\t# data file name\n", thisillum.datafile); |
407 |
+ |
if (thisillum.flags & IL_COLAVG) |
408 |
+ |
if (thisillum.flags & IL_COLDST) |
409 |
+ |
printf("c=d\t\t\t\t# color distribution\n"); |
410 |
+ |
else |
411 |
+ |
printf("c=a\t\t\t\t# color average\n"); |
412 |
+ |
else |
413 |
+ |
printf("c=n\t\t\t\t# color none\n"); |
414 |
+ |
if (thisillum.flags & IL_LIGHT) |
415 |
+ |
printf("l+\t\t\t\t# light type on\n"); |
416 |
+ |
else |
417 |
+ |
printf("l-\t\t\t\t# light type off\n"); |
418 |
+ |
printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens); |
419 |
+ |
printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps); |
420 |
+ |
printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt); |
421 |
+ |
} |
422 |
+ |
|
423 |
+ |
|
424 |
|
printhead(ac, av) /* print out header */ |
425 |
|
register int ac; |
426 |
|
register char **av; |
430 |
|
putchar(' '); |
431 |
|
fputs(*av++, stdout); |
432 |
|
} |
433 |
< |
putchar('\n'); |
433 |
> |
fputs("\n#@mkillum !\n", stdout); |
434 |
|
} |
435 |
|
|
436 |
|
|
447 |
|
if (fgetword(str, MAXSTR, fp) == NULL) |
448 |
|
goto readerr; |
449 |
|
/* is it an alias? */ |
450 |
< |
if (!strcmp(str, ALIASID)) { |
450 |
> |
if (!strcmp(str, ALIASKEY)) { |
451 |
|
if (fgetword(str, MAXSTR, fp) == NULL) |
452 |
|
goto readerr; |
453 |
< |
printf("\n%s %s %s", thisillum.altmat, ALIASID, str); |
453 |
> |
printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str); |
454 |
|
if (fgetword(str, MAXSTR, fp) == NULL) |
455 |
|
goto readerr; |
456 |
< |
printf(" %s\n", str); |
456 |
> |
printf("\t%s\n", str); |
457 |
|
return; |
458 |
|
} |
459 |
< |
thisobj.omod = OVOID; |
459 |
> |
thisobj.omod = OVOID; /* unused field */ |
460 |
|
if ((thisobj.otype = otype(str)) < 0) { |
461 |
|
sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str); |
462 |
|
error(USER, errmsg); |
473 |
|
doit = 0; |
474 |
|
break; |
475 |
|
case S_ALL: |
476 |
< |
doit = issurface(thisobj.otype); |
476 |
> |
doit = 1; |
477 |
|
break; |
478 |
|
case S_ELEM: |
479 |
|
doit = !strcmp(thisillum.altmat, matcheck); |
482 |
|
doit = strcmp(thisillum.altmat, matcheck); |
483 |
|
break; |
484 |
|
} |
485 |
< |
if (doit && ofun[thisobj.otype].funp == o_default) { |
420 |
< |
sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"", |
421 |
< |
nm, ofun[thisobj.otype].funame, thisobj.oname); |
422 |
< |
error(WARNING, errmsg); |
423 |
< |
doit = 0; |
424 |
< |
} |
485 |
> |
doit = doit && issurface(thisobj.otype); |
486 |
|
/* print header? */ |
487 |
|
checkhead(); |
488 |
|
/* process object */ |
489 |
|
if (doit) |
490 |
< |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt); |
490 |
> |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm); |
491 |
|
else |
492 |
|
printobj(thisillum.altmat, &thisobj); |
493 |
|
/* free arguments */ |
497 |
|
sprintf(errmsg, "(%s): error reading scene", nm); |
498 |
|
error(USER, errmsg); |
499 |
|
} |
439 |
– |
|
440 |
– |
|
441 |
– |
int |
442 |
– |
otype(ofname) /* get object function number from its name */ |
443 |
– |
char *ofname; |
444 |
– |
{ |
445 |
– |
register int i; |
446 |
– |
|
447 |
– |
for (i = 0; i < NUMOTYPE; i++) |
448 |
– |
if (!strcmp(ofun[i].funame, ofname)) |
449 |
– |
return(i); |
450 |
– |
|
451 |
– |
return(-1); /* not found */ |
452 |
– |
} |
453 |
– |
|
454 |
– |
|
455 |
– |
o_default() {} |