1 |
– |
/* Copyright (c) 1995 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 |
+ |
#include "random.h" |
14 |
+ |
|
15 |
|
/* default parameters */ |
16 |
|
#define SAMPDENS 48 /* points per projected steradian */ |
17 |
|
#define NSAMPS 32 /* samples per point */ |
23 |
|
#define S_COMPL 2 /* select all but element */ |
24 |
|
#define S_ALL 3 /* select all */ |
25 |
|
|
28 |
– |
/* rtrace command and defaults */ |
29 |
– |
char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-", |
30 |
– |
"-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", }; |
31 |
– |
int rtargc = 14; |
32 |
– |
/* overriding rtrace options */ |
33 |
– |
char *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-fff", "-y", "0", NULL }; |
34 |
– |
|
35 |
– |
struct rtproc rt; /* our rtrace process */ |
36 |
– |
|
26 |
|
struct illum_args thisillum = { /* our illum and default values */ |
27 |
|
0, |
28 |
+ |
UDzpos, |
29 |
+ |
0., |
30 |
|
DFLMAT, |
31 |
|
DFLDAT, |
32 |
|
0, |
33 |
|
VOIDID, |
34 |
|
SAMPDENS, |
35 |
|
NSAMPS, |
36 |
+ |
NULL, |
37 |
|
0., |
38 |
|
}; |
39 |
|
|
40 |
|
char matcheck[MAXSTR]; /* current material to include or exclude */ |
41 |
|
int matselect = S_ALL; /* selection criterion */ |
42 |
|
|
51 |
– |
FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */ |
52 |
– |
|
43 |
|
int gargc; /* global argc */ |
44 |
|
char **gargv; /* global argv */ |
55 |
– |
#define progname gargv[0] |
45 |
|
|
46 |
|
int doneheader = 0; /* printed header yet? */ |
47 |
|
#define checkhead() if (!doneheader++) printhead(gargc,gargv) |
48 |
|
|
49 |
|
int warnings = 1; /* print warnings? */ |
50 |
|
|
51 |
< |
extern char *fgetline(), *fgetword(), *sskip(), |
52 |
< |
*atos(), *iskip(), *fskip(), *strcpy(); |
53 |
< |
extern FILE *popen(); |
51 |
> |
void init(char *octnm, int np); |
52 |
> |
void filter(register FILE *infp, char *name); |
53 |
> |
void xoptions(char *s, char *nm); |
54 |
> |
void printopts(void); |
55 |
> |
void printhead(register int ac, register char **av); |
56 |
> |
void xobject(FILE *fp, char *nm); |
57 |
|
|
58 |
|
|
59 |
< |
main(argc, argv) /* compute illum distributions using rtrace */ |
60 |
< |
int argc; |
61 |
< |
char *argv[]; |
59 |
> |
int |
60 |
> |
main( /* compute illum distributions using rtrace */ |
61 |
> |
int argc, |
62 |
> |
char *argv[] |
63 |
> |
) |
64 |
|
{ |
65 |
< |
extern char *getenv(), *getpath(); |
72 |
< |
char *rtpath; |
65 |
> |
int nprocs = 1; |
66 |
|
FILE *fp; |
67 |
+ |
int rval; |
68 |
|
register int i; |
69 |
|
/* set global arguments */ |
70 |
|
gargv = argv; |
71 |
< |
/* set up rtrace command */ |
71 |
> |
progname = gargv[0]; |
72 |
> |
/* set up rendering defaults */ |
73 |
> |
dstrsrc = 0.25; |
74 |
> |
directrelay = 3; |
75 |
> |
directvis = 0; |
76 |
> |
ambounce = 2; |
77 |
> |
/* get options from command line */ |
78 |
|
for (i = 1; i < argc; i++) { |
79 |
< |
if (argv[i][0] == '<' && argv[i][1] == '\0') |
79 |
> |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
80 |
> |
; |
81 |
> |
if (rval < 0) { |
82 |
> |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
83 |
> |
error(SYSTEM, errmsg); |
84 |
> |
} |
85 |
> |
if (argv[i][0] != '-') |
86 |
|
break; |
87 |
< |
rtargv[rtargc++] = argv[i]; |
88 |
< |
if (argv[i][0] == '-' && argv[i][1] == 'w') |
89 |
< |
switch (argv[i][2]) { |
84 |
< |
case '\0': |
85 |
< |
warnings = !warnings; |
86 |
< |
break; |
87 |
< |
case '+': |
88 |
< |
case 'T': case 't': |
89 |
< |
case 'Y': case 'y': |
90 |
< |
case '1': |
91 |
< |
warnings = 1; |
92 |
< |
break; |
93 |
< |
case '-': |
94 |
< |
case 'F': case 'f': |
95 |
< |
case 'N': case 'n': |
96 |
< |
case '0': |
97 |
< |
warnings = 0; |
98 |
< |
break; |
99 |
< |
} |
100 |
< |
} |
101 |
< |
gargc = i; |
102 |
< |
rtargc--; |
103 |
< |
for (i = 0; myrtopts[i] != NULL; i++) |
104 |
< |
rtargv[rtargc++] = myrtopts[i]; |
105 |
< |
rtargv[rtargc++] = argv[gargc-1]; |
106 |
< |
rtargv[rtargc] = NULL; |
107 |
< |
/* just asking for defaults? */ |
108 |
< |
if (!strcmp(argv[gargc-1], "-defaults")) { |
109 |
< |
printopts(); fflush(stdout); |
110 |
< |
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
111 |
< |
if (rtpath == NULL) { |
112 |
< |
eputs(rtargv[0]); |
113 |
< |
eputs(": command not found\n"); |
114 |
< |
exit(1); |
87 |
> |
if (!strcmp(argv[i], "-w")) { |
88 |
> |
warnings = 0; |
89 |
> |
continue; |
90 |
|
} |
91 |
< |
execv(rtpath, rtargv); |
92 |
< |
perror(rtpath); |
93 |
< |
exit(1); |
91 |
> |
if (!strcmp(argv[i], "-n")) { |
92 |
> |
nprocs = atoi(argv[++i]); |
93 |
> |
if (nprocs <= 0) |
94 |
> |
error(USER, "illegal number of processes"); |
95 |
> |
continue; |
96 |
> |
} |
97 |
> |
if (!strcmp(argv[i], "-defaults")) { |
98 |
> |
printopts(); |
99 |
> |
print_rdefaults(); |
100 |
> |
quit(0); |
101 |
> |
} |
102 |
> |
rval = getrenderopt(argc-i, argv+i); |
103 |
> |
if (rval < 0) { |
104 |
> |
sprintf(errmsg, "bad render option at '%s'", argv[i]); |
105 |
> |
error(USER, errmsg); |
106 |
> |
} |
107 |
> |
i += rval; |
108 |
|
} |
109 |
< |
if (gargc < 2 || argv[gargc-1][0] == '-') |
109 |
> |
gargc = ++i; |
110 |
> |
/* add "mandatory" render options */ |
111 |
> |
do_irrad = 0; |
112 |
> |
if (gargc > argc || argv[gargc-1][0] == '-') |
113 |
|
error(USER, "missing octree argument"); |
114 |
|
/* else initialize and run our calculation */ |
115 |
< |
init(); |
116 |
< |
if (gargc+1 < argc) |
115 |
> |
init(argv[gargc-1], nprocs); |
116 |
> |
if (gargc < argc) { |
117 |
> |
if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1]) |
118 |
> |
error(USER, "use '< file1 file2 ..' for multiple inputs"); |
119 |
|
for (i = gargc+1; i < argc; i++) { |
120 |
|
if ((fp = fopen(argv[i], "r")) == NULL) { |
121 |
|
sprintf(errmsg, |
125 |
|
filter(fp, argv[i]); |
126 |
|
fclose(fp); |
127 |
|
} |
128 |
< |
else |
128 |
> |
} else |
129 |
|
filter(stdin, "standard input"); |
130 |
|
quit(0); |
131 |
+ |
return 0; /* pro forma return */ |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
< |
quit(status) /* exit with status */ |
136 |
< |
int status; |
135 |
> |
void |
136 |
> |
init(char *octnm, int np) /* start rendering process(es) */ |
137 |
|
{ |
143 |
– |
int rtstat; |
144 |
– |
|
145 |
– |
rtstat = close_process(rt.pd); |
146 |
– |
if (status == 0) |
147 |
– |
if (rtstat < 0) |
148 |
– |
error(WARNING, |
149 |
– |
"unknown return status from rtrace process"); |
150 |
– |
else |
151 |
– |
status = rtstat; |
152 |
– |
exit(status); |
153 |
– |
} |
154 |
– |
|
155 |
– |
|
156 |
– |
init() /* start rtrace and set up buffers */ |
157 |
– |
{ |
158 |
– |
extern int o_face(), o_sphere(), o_ring(); |
159 |
– |
int maxbytes; |
160 |
– |
/* set up object functions */ |
161 |
– |
ofun[OBJ_FACE].funp = o_face; |
162 |
– |
ofun[OBJ_SPHERE].funp = o_sphere; |
163 |
– |
ofun[OBJ_RING].funp = o_ring; |
138 |
|
/* set up signal handling */ |
139 |
+ |
signal(SIGINT, quit); |
140 |
+ |
#ifdef SIGHUP |
141 |
+ |
signal(SIGHUP, quit); |
142 |
+ |
#endif |
143 |
+ |
#ifdef SIGTERM |
144 |
+ |
signal(SIGTERM, quit); |
145 |
+ |
#endif |
146 |
+ |
#ifdef SIGPIPE |
147 |
|
signal(SIGPIPE, quit); |
148 |
< |
/* start rtrace process */ |
149 |
< |
errno = 0; |
150 |
< |
maxbytes = open_process(rt.pd, rtargv); |
169 |
< |
if (maxbytes == 0) { |
170 |
< |
eputs(rtargv[0]); |
171 |
< |
eputs(": command not found\n"); |
172 |
< |
exit(1); |
173 |
< |
} |
174 |
< |
if (maxbytes < 0) |
175 |
< |
error(SYSTEM, "cannot start rtrace process"); |
176 |
< |
rt.bsiz = maxbytes/(6*sizeof(float)); |
177 |
< |
rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--); |
178 |
< |
rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz); |
179 |
< |
if (rt.buf == NULL || rt.dest == NULL) |
180 |
< |
error(SYSTEM, "out of memory in init"); |
181 |
< |
rt.nrays = 0; |
182 |
< |
/* set up urand */ |
183 |
< |
initurand(16384); |
148 |
> |
#endif |
149 |
> |
/* start rendering process(es) */ |
150 |
> |
ray_pinit(octnm, np); |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
< |
eputs(s) /* put string to stderr */ |
155 |
< |
register char *s; |
154 |
> |
void |
155 |
> |
eputs( /* put string to stderr */ |
156 |
> |
register char *s |
157 |
> |
) |
158 |
|
{ |
159 |
|
static int midline = 0; |
160 |
|
|
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
+ |
void |
172 |
|
wputs(s) /* print warning if enabled */ |
173 |
|
char *s; |
174 |
|
{ |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
< |
filter(infp, name) /* process stream */ |
181 |
< |
register FILE *infp; |
182 |
< |
char *name; |
180 |
> |
void |
181 |
> |
filter( /* process stream */ |
182 |
> |
register FILE *infp, |
183 |
> |
char *name |
184 |
> |
) |
185 |
|
{ |
186 |
|
char buf[512]; |
187 |
|
FILE *pfp; |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
< |
xoptions(s, nm) /* process options in string s */ |
215 |
< |
char *s; |
216 |
< |
char *nm; |
214 |
> |
void |
215 |
> |
xoptions( /* process options in string s */ |
216 |
> |
char *s, |
217 |
> |
char *nm |
218 |
> |
) |
219 |
|
{ |
220 |
|
extern FILE *freopen(); |
221 |
|
char buf[64]; |
222 |
+ |
int negax; |
223 |
|
int nerrs = 0; |
224 |
|
register char *cp; |
225 |
|
|
233 |
|
case ' ': |
234 |
|
case '\t': |
235 |
|
case '\n': |
236 |
+ |
case '\r': |
237 |
+ |
case '\f': |
238 |
|
cp++; |
239 |
|
continue; |
240 |
|
case 'm': /* material name */ |
299 |
|
} |
300 |
|
cp = sskip(cp); |
301 |
|
continue; |
302 |
< |
case 'd': /* point sample density */ |
302 |
> |
case 'd': /* sample density / BSDF data */ |
303 |
|
if (*++cp != '=') |
304 |
|
break; |
305 |
< |
if (!isintd(++cp, " \t\n")) |
306 |
< |
break; |
307 |
< |
thisillum.sampdens = atoi(cp); |
305 |
> |
if (thisillum.sd != NULL) { |
306 |
> |
free_BSDF(thisillum.sd); |
307 |
> |
thisillum.sd = NULL; |
308 |
> |
} |
309 |
> |
if (!*++cp || isspace(*cp)) |
310 |
> |
continue; |
311 |
> |
if (isintd(++cp, " \t\n\r")) { |
312 |
> |
thisillum.sampdens = atoi(cp); |
313 |
> |
} else { |
314 |
> |
atos(buf, sizeof(buf), cp); |
315 |
> |
thisillum.sd = load_BSDF(buf); |
316 |
> |
if (thisillum.sd == NULL) |
317 |
> |
break; |
318 |
> |
} |
319 |
|
cp = sskip(cp); |
320 |
|
continue; |
321 |
< |
case 's': /* point super-samples */ |
321 |
> |
case 's': /* surface super-samples */ |
322 |
|
if (*++cp != '=') |
323 |
|
break; |
324 |
< |
if (!isintd(++cp, " \t\n")) |
324 |
> |
if (!isintd(++cp, " \t\n\r")) |
325 |
|
break; |
326 |
|
thisillum.nsamps = atoi(cp); |
327 |
|
cp = sskip(cp); |
339 |
|
case 'b': /* brightness */ |
340 |
|
if (*++cp != '=') |
341 |
|
break; |
342 |
< |
if (!isfltd(++cp, " \t\n")) |
342 |
> |
if (!isfltd(++cp, " \t\n\r")) |
343 |
|
break; |
344 |
|
thisillum.minbrt = atof(cp); |
345 |
|
if (thisillum.minbrt < 0.) |
360 |
|
} |
361 |
|
doneheader = 0; |
362 |
|
continue; |
363 |
+ |
case 'u': /* up direction */ |
364 |
+ |
if (*++cp != '=') |
365 |
+ |
break; |
366 |
+ |
if (!*++cp || isspace(*cp)) { |
367 |
+ |
thisillum.udir = UDunknown; |
368 |
+ |
continue; |
369 |
+ |
} |
370 |
+ |
negax = 0; |
371 |
+ |
if (*cp == '+') |
372 |
+ |
cp++; |
373 |
+ |
else if (*cp == '-') { |
374 |
+ |
negax++; |
375 |
+ |
cp++; |
376 |
+ |
} |
377 |
+ |
switch (*cp++) { |
378 |
+ |
case 'x': |
379 |
+ |
case 'X': |
380 |
+ |
thisillum.udir = negax ? UDxneg : UDxpos; |
381 |
+ |
break; |
382 |
+ |
case 'y': |
383 |
+ |
case 'Y': |
384 |
+ |
thisillum.udir = negax ? UDyneg : UDypos; |
385 |
+ |
break; |
386 |
+ |
case 'z': |
387 |
+ |
case 'Z': |
388 |
+ |
thisillum.udir = negax ? UDxneg : UDxpos; |
389 |
+ |
break; |
390 |
+ |
default: |
391 |
+ |
thisillum.udir = UDunknown; |
392 |
+ |
break; |
393 |
+ |
} |
394 |
+ |
if (thisillum.udir == UDunknown || !isspace(*cp)) |
395 |
+ |
break; |
396 |
+ |
continue; |
397 |
+ |
case 't': /* object thickness */ |
398 |
+ |
if (*++cp != '=') |
399 |
+ |
break; |
400 |
+ |
if (!isfltd(++cp, " \t\n\r")) |
401 |
+ |
break; |
402 |
+ |
thisillum.thick = atof(cp); |
403 |
+ |
if (thisillum.thick < .0) |
404 |
+ |
thisillum.thick = .0; |
405 |
+ |
cp = sskip(cp); |
406 |
+ |
continue; |
407 |
|
case '!': /* processed file! */ |
408 |
|
sprintf(errmsg, "(%s): already processed!", nm); |
409 |
|
error(WARNING, errmsg); |
430 |
|
printf("# %s", s+2); |
431 |
|
} |
432 |
|
|
433 |
< |
|
434 |
< |
printopts() /* print out option default values */ |
433 |
> |
void |
434 |
> |
printopts(void) /* print out option default values */ |
435 |
|
{ |
436 |
|
printf("m=%-15s\t\t# material name\n", thisillum.matname); |
437 |
|
printf("f=%-15s\t\t# data file name\n", thisillum.datafile); |
449 |
|
printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens); |
450 |
|
printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps); |
451 |
|
printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt); |
452 |
+ |
switch (thisillum.udir) { |
453 |
+ |
case UDzneg: |
454 |
+ |
fputs("u=-Z\t\t\t\t# up is negative Z\n", stdout); |
455 |
+ |
break; |
456 |
+ |
case UDyneg: |
457 |
+ |
fputs("u=-Y\t\t\t\t# up is negative Y\n", stdout); |
458 |
+ |
break; |
459 |
+ |
case UDxneg: |
460 |
+ |
fputs("u=-X\t\t\t\t# up is negative X\n", stdout); |
461 |
+ |
break; |
462 |
+ |
case UDxpos: |
463 |
+ |
fputs("u=+X\t\t\t\t# up is positive X\n", stdout); |
464 |
+ |
break; |
465 |
+ |
case UDypos: |
466 |
+ |
fputs("u=+Y\t\t\t\t# up is positive Y\n", stdout); |
467 |
+ |
break; |
468 |
+ |
case UDzpos: |
469 |
+ |
fputs("u=+Z\t\t\t\t# up is positive Z\n", stdout); |
470 |
+ |
break; |
471 |
+ |
case UDunknown: |
472 |
+ |
break; |
473 |
+ |
} |
474 |
+ |
printf("t=%f\t\t\t# object thickness\n", thisillum.thick); |
475 |
|
} |
476 |
|
|
477 |
|
|
478 |
< |
printhead(ac, av) /* print out header */ |
479 |
< |
register int ac; |
480 |
< |
register char **av; |
478 |
> |
void |
479 |
> |
printhead( /* print out header */ |
480 |
> |
register int ac, |
481 |
> |
register char **av |
482 |
> |
) |
483 |
|
{ |
484 |
|
putchar('#'); |
485 |
|
while (ac-- > 0) { |
490 |
|
} |
491 |
|
|
492 |
|
|
493 |
< |
xobject(fp, nm) /* translate an object from fp */ |
494 |
< |
FILE *fp; |
495 |
< |
char *nm; |
493 |
> |
void |
494 |
> |
xobject( /* translate an object from fp */ |
495 |
> |
FILE *fp, |
496 |
> |
char *nm |
497 |
> |
) |
498 |
|
{ |
499 |
|
OBJREC thisobj; |
500 |
|
char str[MAXSTR]; |
505 |
|
if (fgetword(str, MAXSTR, fp) == NULL) |
506 |
|
goto readerr; |
507 |
|
/* is it an alias? */ |
508 |
< |
if (!strcmp(str, ALIASID)) { |
508 |
> |
if (!strcmp(str, ALIASKEY)) { |
509 |
|
if (fgetword(str, MAXSTR, fp) == NULL) |
510 |
|
goto readerr; |
511 |
< |
printf("\n%s %s %s", thisillum.altmat, ALIASID, str); |
511 |
> |
printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str); |
512 |
|
if (fgetword(str, MAXSTR, fp) == NULL) |
513 |
|
goto readerr; |
514 |
|
printf("\t%s\n", str); |
545 |
|
checkhead(); |
546 |
|
/* process object */ |
547 |
|
if (doit) |
548 |
< |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm); |
548 |
> |
switch (thisobj.otype) { |
549 |
> |
case OBJ_FACE: |
550 |
> |
my_face(&thisobj, &thisillum, nm); |
551 |
> |
break; |
552 |
> |
case OBJ_SPHERE: |
553 |
> |
my_sphere(&thisobj, &thisillum, nm); |
554 |
> |
break; |
555 |
> |
case OBJ_RING: |
556 |
> |
my_ring(&thisobj, &thisillum, nm); |
557 |
> |
break; |
558 |
> |
default: |
559 |
> |
my_default(&thisobj, &thisillum, nm); |
560 |
> |
break; |
561 |
> |
} |
562 |
|
else |
563 |
|
printobj(thisillum.altmat, &thisobj); |
564 |
|
/* free arguments */ |
565 |
|
freefargs(&thisobj.oargs); |
566 |
|
return; |
567 |
|
readerr: |
568 |
< |
sprintf(errmsg, "(%s): error reading scene", nm); |
568 |
> |
sprintf(errmsg, "(%s): error reading input", nm); |
569 |
|
error(USER, errmsg); |
570 |
|
} |