10 |
|
|
11 |
|
#include "standard.h" |
12 |
|
|
13 |
+ |
#include <fcntl.h> |
14 |
+ |
|
15 |
|
#include "view.h" |
16 |
|
|
17 |
|
#include "color.h" |
18 |
|
|
19 |
+ |
#ifndef BSD |
20 |
+ |
#define vfork fork |
21 |
+ |
#endif |
22 |
+ |
|
23 |
|
#define pscan(y) (ourpict+(y)*hresolu) |
24 |
|
#define zscan(y) (ourzbuf+(y)*hresolu) |
25 |
|
|
28 |
|
|
29 |
|
#define PACKSIZ 42 /* calculation packet size */ |
30 |
|
|
31 |
< |
#define RTCOM "rtrace -h -ovl -fff -x %d %s" |
31 |
> |
#define RTCOM "rtrace -h -ovl -fff %s" |
32 |
|
|
33 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
34 |
|
|
37 |
|
VIEW ourview = STDVIEW; /* desired view */ |
38 |
|
int hresolu = 512; /* horizontal resolution */ |
39 |
|
int vresolu = 512; /* vertical resolution */ |
40 |
+ |
double pixaspect = 1.0; /* pixel aspect ratio */ |
41 |
|
|
42 |
|
double zeps = .02; /* allowed z epsilon */ |
43 |
|
|
46 |
|
|
47 |
|
char *progname; |
48 |
|
|
49 |
< |
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
49 |
> |
int fillo = F_FORE|F_BACK; /* selected fill options */ |
50 |
> |
int fillsamp = 0; /* sample separation (0 == inf) */ |
51 |
|
extern int backfill(), rcalfill(); /* fill functions */ |
52 |
< |
int (*deffill)() = backfill; /* selected fill function */ |
52 |
> |
int (*fillfunc)() = backfill; /* selected fill function */ |
53 |
|
COLR backcolr = BLKCOLR; /* background color */ |
54 |
|
double backz = 0.0; /* background z value */ |
55 |
|
int normdist = 1; /* normalized distance? */ |
99 |
|
switch (argv[i][2]) { |
100 |
|
case '0': /* none */ |
101 |
|
check(3,0); |
102 |
< |
fill = 0; |
102 |
> |
fillo = 0; |
103 |
|
break; |
104 |
|
case 'f': /* foreground */ |
105 |
|
check(3,0); |
106 |
< |
fill = F_FORE; |
106 |
> |
fillo = F_FORE; |
107 |
|
break; |
108 |
|
case 'b': /* background */ |
109 |
|
check(3,0); |
110 |
< |
fill = F_BACK; |
110 |
> |
fillo = F_BACK; |
111 |
|
break; |
112 |
|
case 'a': /* all */ |
113 |
|
check(3,0); |
114 |
< |
fill = F_FORE|F_BACK; |
114 |
> |
fillo = F_FORE|F_BACK; |
115 |
|
break; |
116 |
+ |
case 's': /* sample */ |
117 |
+ |
check(3,1); |
118 |
+ |
fillsamp = atoi(argv[++i]); |
119 |
+ |
break; |
120 |
|
case 'c': /* color */ |
121 |
|
check(3,3); |
122 |
< |
deffill = backfill; |
122 |
> |
fillfunc = backfill; |
123 |
|
setcolr(backcolr, atof(argv[i+1]), |
124 |
|
atof(argv[i+2]), atof(argv[i+3])); |
125 |
|
i += 3; |
126 |
|
break; |
127 |
|
case 'z': /* z value */ |
128 |
|
check(3,1); |
129 |
< |
deffill = backfill; |
129 |
> |
fillfunc = backfill; |
130 |
|
backz = atof(argv[++i]); |
131 |
|
break; |
132 |
|
case 'r': /* rtrace */ |
133 |
|
check(3,1); |
134 |
< |
deffill = rcalfill; |
134 |
> |
fillfunc = rcalfill; |
135 |
|
calstart(RTCOM, argv[++i]); |
136 |
|
break; |
137 |
|
default: |
150 |
|
check(2,1); |
151 |
|
vresolu = atoi(argv[++i]); |
152 |
|
break; |
153 |
+ |
case 'p': /* pixel aspect */ |
154 |
+ |
check(2,1); |
155 |
+ |
pixaspect = atof(argv[++i]); |
156 |
+ |
break; |
157 |
|
case 'v': /* view file */ |
158 |
|
if (argv[i][2] != 'f') |
159 |
|
goto badopt; |
160 |
|
check(3,1); |
161 |
< |
gotvfile = viewfile(argv[++i], &ourview); |
161 |
> |
gotvfile = viewfile(argv[++i], &ourview, 0, 0); |
162 |
|
if (gotvfile < 0) { |
163 |
|
perror(argv[i]); |
164 |
|
exit(1); |
183 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
184 |
|
exit(1); |
185 |
|
} |
186 |
+ |
normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); |
187 |
|
/* allocate frame */ |
188 |
|
ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR)); |
189 |
|
ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float)); |
193 |
|
for ( ; i < argc; i += 2) |
194 |
|
addpicture(argv[i], argv[i+1]); |
195 |
|
/* fill in spaces */ |
196 |
< |
if (fill&F_BACK) |
197 |
< |
backpicture(); |
196 |
> |
if (fillo&F_BACK) |
197 |
> |
backpicture(fillfunc, fillsamp); |
198 |
|
else |
199 |
< |
fillpicture(); |
199 |
> |
fillpicture(fillfunc); |
200 |
|
/* close calculation */ |
201 |
|
caldone(); |
202 |
|
/* add to header */ |
203 |
|
printargs(argc, argv, stdout); |
204 |
|
if (gotvfile) { |
205 |
< |
printf(VIEWSTR); |
205 |
> |
fputs(VIEWSTR, stdout); |
206 |
|
fprintview(&ourview, stdout); |
207 |
< |
printf("\n"); |
207 |
> |
putc('\n', stdout); |
208 |
|
} |
209 |
< |
if (ourexp > 0 && ourexp != 1.0) |
209 |
> |
if (pixaspect < .99 || pixaspect > 1.01) |
210 |
> |
fputaspect(pixaspect, stdout); |
211 |
> |
if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) |
212 |
|
fputexpos(ourexp, stdout); |
213 |
< |
printf("\n"); |
213 |
> |
putc('\n', stdout); |
214 |
|
/* write picture */ |
215 |
|
writepicture(); |
216 |
|
/* write z file */ |
230 |
|
headline(s) /* process header string */ |
231 |
|
char *s; |
232 |
|
{ |
233 |
< |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
233 |
> |
static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; |
234 |
|
register char **an; |
235 |
|
|
236 |
< |
printf("\t%s", s); |
236 |
> |
putc('\t', stdout); |
237 |
> |
fputs(s, stdout); |
238 |
|
|
239 |
|
if (isexpos(s)) { |
240 |
|
theirexp *= exposval(s); |
253 |
|
char *pfile, *zspec; |
254 |
|
{ |
255 |
|
extern double atof(); |
256 |
< |
FILE *pfp, *zfp; |
256 |
> |
FILE *pfp; |
257 |
> |
int zfd; |
258 |
|
char *err; |
259 |
|
COLR *scanin; |
260 |
|
float *zin; |
292 |
|
if (scanin == NULL || zin == NULL || plast == NULL) |
293 |
|
syserror(); |
294 |
|
/* get z specification or file */ |
295 |
< |
if ((zfp = fopen(zspec, "r")) == NULL) { |
295 |
> |
if ((zfd = open(zspec, O_RDONLY)) == -1) { |
296 |
|
double zvalue; |
297 |
|
register int x; |
298 |
|
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { |
308 |
|
fprintf(stderr, "%s: read error\n", pfile); |
309 |
|
exit(1); |
310 |
|
} |
311 |
< |
if (zfp != NULL |
312 |
< |
&& fread(zin,sizeof(float),thresolu,zfp) < thresolu) { |
311 |
> |
if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float)) |
312 |
> |
< thresolu*sizeof(float)) { |
313 |
|
fprintf(stderr, "%s: read error\n", zspec); |
314 |
|
exit(1); |
315 |
|
} |
320 |
|
free((char *)zin); |
321 |
|
free((char *)plast); |
322 |
|
fclose(pfp); |
323 |
< |
if (zfp != NULL) |
324 |
< |
fclose(zfp); |
323 |
> |
if (zfd != -1) |
324 |
> |
close(zfd); |
325 |
|
} |
326 |
|
|
327 |
|
|
372 |
|
struct position lastx, newpos; |
373 |
|
register int x; |
374 |
|
|
375 |
+ |
lastx.z = 0; |
376 |
|
for (x = thresolu-1; x >= 0; x--) { |
377 |
|
pos[0] = (x+.5)/thresolu + theirview.hoff - .5; |
378 |
|
pos[1] = (y+.5)/tvresolu + theirview.voff - .5; |
425 |
|
register int x, y; /* final position */ |
426 |
|
|
427 |
|
/* compute vector p0p1 */ |
428 |
< |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
428 |
> |
if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) { |
429 |
|
s1x = p1->x - p0->x; |
430 |
|
s1y = p1->y - p0->y; |
431 |
|
l1 = ABS(s1x); |
436 |
|
p1isy = -1; |
437 |
|
} |
438 |
|
/* compute vector p0p2 */ |
439 |
< |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
439 |
> |
if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) { |
440 |
|
s2x = p2->x - p0->x; |
441 |
|
s2y = p2->y - p0->y; |
442 |
|
if (p1isy == 1) |
465 |
|
} |
466 |
|
|
467 |
|
|
468 |
< |
backpicture() /* background fill algorithm */ |
468 |
> |
backpicture(fill, samp) /* background fill algorithm */ |
469 |
> |
int (*fill)(); |
470 |
> |
int samp; |
471 |
|
{ |
472 |
|
int *yback, xback; |
473 |
|
int y; |
518 |
|
xback = x-1; |
519 |
|
} |
520 |
|
/* |
521 |
< |
* Check to see if we have no background for |
522 |
< |
* this pixel. If not, use background color. |
521 |
> |
* If we have no background for this pixel, |
522 |
> |
* or if the background is too distant, |
523 |
> |
* use the given fill function. |
524 |
|
*/ |
525 |
< |
if (xback < 0 && yback[x] < 0) { |
526 |
< |
(*deffill)(x,y); |
525 |
> |
if ((xback < 0 && yback[x] < 0) |
526 |
> |
|| (samp > 0 |
527 |
> |
&& ABS(x-xback) >= samp |
528 |
> |
&& ABS(y-yback[y]) >= samp)) { |
529 |
> |
(*fill)(x,y); |
530 |
> |
if (fill != backfill) { /* reuse */ |
531 |
> |
yback[x] = -2; |
532 |
> |
xback = -2; |
533 |
> |
} |
534 |
|
continue; |
535 |
|
} |
536 |
|
/* |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
< |
fillpicture() /* paint in empty pixels with default */ |
560 |
> |
fillpicture(fill) /* paint in empty pixels using fill */ |
561 |
> |
int (*fill)(); |
562 |
|
{ |
563 |
|
register int x, y; |
564 |
|
|
565 |
|
for (y = 0; y < vresolu; y++) |
566 |
|
for (x = 0; x < hresolu; x++) |
567 |
|
if (zscan(y)[x] <= 0) |
568 |
< |
(*deffill)(x,y); |
568 |
> |
(*fill)(x,y); |
569 |
|
} |
570 |
|
|
571 |
|
|
585 |
|
{ |
586 |
|
extern double sqrt(); |
587 |
|
int donorm = normdist && ourview.type == VT_PER; |
588 |
< |
FILE *fp; |
588 |
> |
int fd; |
589 |
|
int y; |
590 |
|
float *zout; |
591 |
|
|
592 |
< |
if ((fp = fopen(fname, "w")) == NULL) { |
592 |
> |
if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) { |
593 |
|
perror(fname); |
594 |
|
exit(1); |
595 |
|
} |
609 |
|
} |
610 |
|
} else |
611 |
|
zout = zscan(y); |
612 |
< |
if (fwrite(zout, sizeof(float), hresolu, fp) < hresolu) { |
612 |
> |
if (write(fd, (char *)zout, hresolu*sizeof(float)) |
613 |
> |
< hresolu*sizeof(float)) { |
614 |
|
perror(fname); |
615 |
|
exit(1); |
616 |
|
} |
617 |
|
} |
618 |
|
if (donorm) |
619 |
|
free((char *)zout); |
620 |
< |
fclose(fp); |
620 |
> |
close(fd); |
621 |
|
} |
622 |
|
|
623 |
|
|
652 |
|
fprintf(stderr, "%s: too many calculations\n", progname); |
653 |
|
exit(1); |
654 |
|
} |
655 |
< |
sprintf(combuf, prog, PACKSIZ, args); |
655 |
> |
sprintf(combuf, prog, args); |
656 |
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
657 |
|
syserror(); |
658 |
|
if ((childpid = vfork()) == 0) { /* fork calculation */ |
688 |
|
|
689 |
|
if (childpid == -1) |
690 |
|
return; |
657 |
– |
if (fclose(psend) == EOF) |
658 |
– |
syserror(); |
691 |
|
clearqueue(); |
692 |
+ |
fclose(psend); |
693 |
|
fclose(precv); |
694 |
|
while ((pid = wait(0)) != -1 && pid != childpid) |
695 |
|
; |
700 |
|
rcalfill(x, y) /* fill with ray-calculated pixel */ |
701 |
|
int x, y; |
702 |
|
{ |
703 |
< |
FVECT orig, dir; |
671 |
< |
float outbuf[6]; |
672 |
< |
|
673 |
< |
if (queuesiz >= PACKSIZ) { /* flush queue */ |
674 |
< |
if (fflush(psend) == EOF) |
675 |
< |
syserror(); |
703 |
> |
if (queuesiz >= PACKSIZ) /* flush queue if needed */ |
704 |
|
clearqueue(); |
705 |
< |
} |
678 |
< |
/* send new ray */ |
679 |
< |
viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu); |
680 |
< |
outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; |
681 |
< |
outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; |
682 |
< |
if (fwrite(outbuf, sizeof(float), 6, psend) < 6) |
683 |
< |
syserror(); |
684 |
< |
/* remember it */ |
705 |
> |
/* add position to queue */ |
706 |
|
queue[queuesiz][0] = x; |
707 |
|
queue[queuesiz][1] = y; |
708 |
|
queuesiz++; |
709 |
|
} |
710 |
|
|
711 |
|
|
712 |
< |
clearqueue() /* get results from queue */ |
712 |
> |
clearqueue() /* process queue */ |
713 |
|
{ |
714 |
< |
float inbuf[4]; |
714 |
> |
FVECT orig, dir; |
715 |
> |
float fbuf[6]; |
716 |
|
register int i; |
717 |
|
|
718 |
|
for (i = 0; i < queuesiz; i++) { |
719 |
< |
if (fread(inbuf, sizeof(float), 4, precv) < 4) { |
719 |
> |
viewray(orig, dir, &ourview, |
720 |
> |
(queue[i][0]+.5)/hresolu, |
721 |
> |
(queue[i][1]+.5)/vresolu); |
722 |
> |
fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2]; |
723 |
> |
fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; |
724 |
> |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
725 |
> |
} |
726 |
> |
/* flush output and get results */ |
727 |
> |
fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ |
728 |
> |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
729 |
> |
if (fflush(psend) == EOF) |
730 |
> |
syserror(); |
731 |
> |
for (i = 0; i < queuesiz; i++) { |
732 |
> |
if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) { |
733 |
|
fprintf(stderr, "%s: read error in clearqueue\n", |
734 |
|
progname); |
735 |
|
exit(1); |
736 |
|
} |
737 |
|
if (ourexp > 0 && ourexp != 1.0) { |
738 |
< |
inbuf[0] *= ourexp; |
739 |
< |
inbuf[1] *= ourexp; |
740 |
< |
inbuf[2] *= ourexp; |
738 |
> |
fbuf[0] *= ourexp; |
739 |
> |
fbuf[1] *= ourexp; |
740 |
> |
fbuf[2] *= ourexp; |
741 |
|
} |
742 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
743 |
< |
inbuf[0], inbuf[1], inbuf[2]); |
744 |
< |
zscan(queue[i][1])[queue[i][0]] = inbuf[3]; |
743 |
> |
fbuf[0], fbuf[1], fbuf[2]); |
744 |
> |
zscan(queue[i][1])[queue[i][0]] = fbuf[3]; |
745 |
|
} |
746 |
|
queuesiz = 0; |
747 |
|
} |