17 |
|
#define pscan(y) (ourpict+(y)*ourview.hresolu) |
18 |
|
#define zscan(y) (ourzbuf+(y)*ourview.hresolu) |
19 |
|
|
20 |
+ |
#define F_FORE 1 /* fill foreground */ |
21 |
+ |
#define F_BACK 2 /* fill background */ |
22 |
+ |
|
23 |
+ |
#define PACKSIZ 42 /* calculation packet size */ |
24 |
+ |
|
25 |
+ |
#define RTCOM "rtrace -h -ovl -fff -x %d %s" |
26 |
+ |
|
27 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
28 |
|
|
29 |
+ |
struct position {int x,y; float z;}; |
30 |
+ |
|
31 |
|
VIEW ourview = STDVIEW(512); /* desired view */ |
32 |
|
|
33 |
|
double zeps = .02; /* allowed z epsilon */ |
40 |
|
VIEW theirview = STDVIEW(512); /* input view */ |
41 |
|
int gotview; /* got input view? */ |
42 |
|
|
43 |
+ |
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
44 |
+ |
extern int backfill(), rcalfill(); /* fill functions */ |
45 |
+ |
int (*deffill)() = backfill; /* selected fill function */ |
46 |
+ |
COLR backcolr = BLKCOLR; /* background color */ |
47 |
+ |
double backz = 0.0; /* background z value */ |
48 |
+ |
|
49 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
50 |
< |
int regdist = 0; /* regular distance? */ |
50 |
> |
int normdist = 1; /* normalized distance? */ |
51 |
|
|
52 |
+ |
int childpid = -1; /* id of fill process */ |
53 |
+ |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
54 |
+ |
int queue[PACKSIZ][2]; /* pending pixels */ |
55 |
+ |
int queuesiz; /* number of pixels pending */ |
56 |
|
|
57 |
+ |
|
58 |
|
main(argc, argv) /* interpolate pictures */ |
59 |
|
int argc; |
60 |
|
char *argv[]; |
61 |
|
{ |
62 |
|
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
63 |
+ |
extern double atof(); |
64 |
|
int gotvfile = 0; |
65 |
+ |
char *zfile = NULL; |
66 |
|
char *err; |
67 |
|
int i; |
68 |
|
|
74 |
|
check(2,1); |
75 |
|
zeps = atof(argv[++i]); |
76 |
|
break; |
77 |
< |
case 'r': /* regular distance */ |
77 |
> |
case 'n': /* dist. normalized? */ |
78 |
|
check(2,0); |
79 |
< |
regdist = !regdist; |
79 |
> |
normdist = !normdist; |
80 |
|
break; |
81 |
+ |
case 'f': /* fill type */ |
82 |
+ |
switch (argv[i][2]) { |
83 |
+ |
case '0': /* none */ |
84 |
+ |
check(3,0); |
85 |
+ |
fill = 0; |
86 |
+ |
break; |
87 |
+ |
case 'f': /* foreground */ |
88 |
+ |
check(3,0); |
89 |
+ |
fill = F_FORE; |
90 |
+ |
break; |
91 |
+ |
case 'b': /* background */ |
92 |
+ |
check(3,0); |
93 |
+ |
fill = F_BACK; |
94 |
+ |
break; |
95 |
+ |
case 'a': /* all */ |
96 |
+ |
check(3,0); |
97 |
+ |
fill = F_FORE|F_BACK; |
98 |
+ |
break; |
99 |
+ |
case 'c': /* color */ |
100 |
+ |
check(3,3); |
101 |
+ |
deffill = backfill; |
102 |
+ |
setcolr(backcolr, atof(argv[i+1]), |
103 |
+ |
atof(argv[i+2]), atof(argv[i+3])); |
104 |
+ |
i += 3; |
105 |
+ |
break; |
106 |
+ |
case 'z': /* z value */ |
107 |
+ |
check(3,1); |
108 |
+ |
deffill = backfill; |
109 |
+ |
backz = atof(argv[++i]); |
110 |
+ |
break; |
111 |
+ |
case 'r': /* rtrace */ |
112 |
+ |
check(3,1); |
113 |
+ |
deffill = rcalfill; |
114 |
+ |
calstart(RTCOM, argv[++i]); |
115 |
+ |
break; |
116 |
+ |
default: |
117 |
+ |
goto badopt; |
118 |
+ |
} |
119 |
+ |
break; |
120 |
+ |
case 'z': /* z file */ |
121 |
+ |
check(2,1); |
122 |
+ |
zfile = argv[++i]; |
123 |
+ |
break; |
124 |
|
case 'x': /* x resolution */ |
125 |
|
check(2,1); |
126 |
|
ourview.hresolu = atoi(argv[++i]); |
184 |
|
goto userr; |
185 |
|
} |
186 |
|
/* check arguments */ |
187 |
< |
if (argc-i < 2 || (argc-i)%2) |
187 |
> |
if ((argc-i)%2) |
188 |
|
goto userr; |
189 |
|
/* set view */ |
190 |
|
if (err = setview(&ourview)) { |
192 |
|
exit(1); |
193 |
|
} |
194 |
|
/* allocate frame */ |
195 |
< |
ourpict = (COLR *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR)); |
195 |
> |
ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); |
196 |
|
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
197 |
< |
if (ourpict == NULL || ourzbuf == NULL) { |
198 |
< |
perror(progname); |
134 |
< |
exit(1); |
135 |
< |
} |
197 |
> |
if (ourpict == NULL || ourzbuf == NULL) |
198 |
> |
syserror(); |
199 |
|
/* get input */ |
200 |
|
for ( ; i < argc; i += 2) |
201 |
|
addpicture(argv[i], argv[i+1]); |
202 |
|
/* fill in spaces */ |
203 |
< |
fillpicture(); |
203 |
> |
if (fill&F_BACK) |
204 |
> |
backpicture(); |
205 |
> |
else |
206 |
> |
fillpicture(); |
207 |
> |
/* close calculation */ |
208 |
> |
caldone(); |
209 |
|
/* add to header */ |
210 |
|
printargs(argc, argv, stdout); |
211 |
|
if (gotvfile) { |
214 |
|
printf("\n"); |
215 |
|
} |
216 |
|
printf("\n"); |
217 |
< |
/* write output */ |
217 |
> |
/* write picture */ |
218 |
|
writepicture(); |
219 |
+ |
/* write z file */ |
220 |
+ |
if (zfile != NULL) |
221 |
+ |
writedistance(zfile); |
222 |
|
|
223 |
|
exit(0); |
224 |
|
userr: |
225 |
|
fprintf(stderr, |
226 |
< |
"Usage: %s [view opts][-t zthresh][-r] pfile zspec ..\n", |
226 |
> |
"Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", |
227 |
|
progname); |
228 |
|
exit(1); |
229 |
|
#undef check |
254 |
|
FILE *pfp, *zfp; |
255 |
|
char *err; |
256 |
|
COLR *scanin; |
257 |
< |
float *zin, *zlast; |
258 |
< |
int *plast; |
257 |
> |
float *zin; |
258 |
> |
struct position *plast; |
259 |
|
int y; |
260 |
|
/* open picture file */ |
261 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
280 |
|
/* allocate scanlines */ |
281 |
|
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
282 |
|
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
283 |
< |
plast = (int *)calloc(theirview.hresolu, sizeof(int)); |
284 |
< |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
285 |
< |
if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { |
286 |
< |
perror(progname); |
216 |
< |
exit(1); |
217 |
< |
} |
283 |
> |
plast = (struct position *)calloc(theirview.hresolu, |
284 |
> |
sizeof(struct position)); |
285 |
> |
if (scanin == NULL || zin == NULL || plast == NULL) |
286 |
> |
syserror(); |
287 |
|
/* get z specification or file */ |
288 |
|
if ((zfp = fopen(zspec, "r")) == NULL) { |
289 |
|
double zvalue; |
307 |
|
fprintf(stderr, "%s: read error\n", zspec); |
308 |
|
exit(1); |
309 |
|
} |
310 |
< |
addscanline(y, scanin, zin, plast, zlast); |
310 |
> |
addscanline(y, scanin, zin, plast); |
311 |
|
} |
312 |
|
/* clean up */ |
313 |
|
free((char *)scanin); |
314 |
|
free((char *)zin); |
315 |
|
free((char *)plast); |
247 |
– |
free((char *)zlast); |
316 |
|
fclose(pfp); |
317 |
|
if (zfp != NULL) |
318 |
|
fclose(zfp); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
< |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
358 |
> |
addscanline(y, pline, zline, lasty) /* add scanline to output */ |
359 |
|
int y; |
360 |
|
COLR *pline; |
361 |
|
float *zline; |
362 |
< |
int *lasty; /* input/output */ |
295 |
< |
float *lastyz; /* input/output */ |
362 |
> |
struct position *lasty; /* input/output */ |
363 |
|
{ |
364 |
< |
extern double sqrt(), fabs(); |
364 |
> |
extern double sqrt(); |
365 |
|
double pos[3]; |
366 |
< |
int lastx = 0; |
300 |
< |
double lastxz = 0; |
301 |
< |
double zt; |
302 |
< |
int xpos, ypos; |
366 |
> |
struct position lastx, newpos; |
367 |
|
register int x; |
368 |
|
|
369 |
|
for (x = theirview.hresolu-1; x >= 0; x--) { |
371 |
|
pos[1] = y - .5*(theirview.vresolu-1); |
372 |
|
pos[2] = zline[x]; |
373 |
|
if (theirview.type == VT_PER) { |
374 |
< |
if (!regdist) /* adjust for eye-ray distance */ |
374 |
> |
if (normdist) /* adjust for eye-ray distance */ |
375 |
|
pos[2] /= sqrt( 1. |
376 |
|
+ pos[0]*pos[0]*theirview.vhn2 |
377 |
|
+ pos[1]*pos[1]*theirview.vvn2 ); |
379 |
|
pos[1] *= pos[2]; |
380 |
|
} |
381 |
|
multp3(pos, pos, theirs2ours); |
382 |
< |
if (pos[2] <= 0) |
382 |
> |
if (pos[2] <= 0) { |
383 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
384 |
|
continue; |
385 |
+ |
} |
386 |
|
if (ourview.type == VT_PER) { |
387 |
|
pos[0] /= pos[2]; |
388 |
|
pos[1] /= pos[2]; |
389 |
|
} |
390 |
|
pos[0] += .5*ourview.hresolu; |
391 |
|
pos[1] += .5*ourview.vresolu; |
392 |
< |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
393 |
< |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
394 |
< |
continue; |
392 |
> |
newpos.x = pos[0]; |
393 |
> |
newpos.y = pos[1]; |
394 |
> |
newpos.z = zline[x]; |
395 |
|
/* add pixel to our image */ |
396 |
< |
zt = 2.*zeps*zline[x]; |
397 |
< |
addpixel(xpos, ypos, |
398 |
< |
(fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1, |
399 |
< |
(fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1, |
400 |
< |
pline[x], pos[2]); |
401 |
< |
lastx = xpos; |
402 |
< |
lasty[x] = ypos; |
403 |
< |
lastxz = lastyz[x] = zline[x]; |
396 |
> |
if (pos[0] >= 0 && newpos.x < ourview.hresolu |
397 |
> |
&& pos[1] >= 0 && newpos.y < ourview.vresolu) { |
398 |
> |
addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); |
399 |
> |
lasty[x].x = lastx.x = newpos.x; |
400 |
> |
lasty[x].y = lastx.y = newpos.y; |
401 |
> |
lasty[x].z = lastx.z = newpos.z; |
402 |
> |
} else |
403 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
< |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
409 |
< |
int xstart, ystart; |
344 |
< |
int width, height; |
408 |
> |
addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ |
409 |
> |
struct position *p0, *p1, *p2; |
410 |
|
COLR pix; |
411 |
|
double z; |
412 |
|
{ |
413 |
< |
register int x, y; |
414 |
< |
/* make width and height positive */ |
415 |
< |
if (width < 0) { |
416 |
< |
width = -width; |
417 |
< |
xstart = xstart-width+1; |
418 |
< |
} else if (width == 0) |
419 |
< |
width = 1; |
420 |
< |
if (height < 0) { |
421 |
< |
height = -height; |
422 |
< |
ystart = ystart-height+1; |
423 |
< |
} else if (height == 0) |
424 |
< |
height = 1; |
425 |
< |
/* fill pixel(s) within rectangle */ |
426 |
< |
for (y = ystart; y < ystart+height; y++) |
427 |
< |
for (x = xstart; x < xstart+width; x++) |
428 |
< |
if (zscan(y)[x] <= 0 |
429 |
< |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
413 |
> |
double zt = 2.*zeps*p0->z; /* threshold */ |
414 |
> |
int s1x, s1y, s2x, s2y; /* step sizes */ |
415 |
> |
int l1, l2, c1, c2; /* side lengths and counters */ |
416 |
> |
int p1isy; /* p0p1 along y? */ |
417 |
> |
int x1, y1; /* p1 position */ |
418 |
> |
register int x, y; /* final position */ |
419 |
> |
|
420 |
> |
/* compute vector p0p1 */ |
421 |
> |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
422 |
> |
s1x = p1->x - p0->x; |
423 |
> |
s1y = p1->y - p0->y; |
424 |
> |
l1 = ABS(s1x); |
425 |
> |
if (p1isy = (ABS(s1y) > l1)) |
426 |
> |
l1 = ABS(s1y); |
427 |
> |
} else { |
428 |
> |
l1 = s1x = s1y = 1; |
429 |
> |
p1isy = -1; |
430 |
> |
} |
431 |
> |
/* compute vector p0p2 */ |
432 |
> |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
433 |
> |
s2x = p2->x - p0->x; |
434 |
> |
s2y = p2->y - p0->y; |
435 |
> |
if (p1isy == 1) |
436 |
> |
l2 = ABS(s2x); |
437 |
> |
else { |
438 |
> |
l2 = ABS(s2y); |
439 |
> |
if (p1isy != 0 && ABS(s2x) > l2) |
440 |
> |
l2 = ABS(s2x); |
441 |
> |
} |
442 |
> |
} else |
443 |
> |
l2 = s2x = s2y = 1; |
444 |
> |
/* fill the parallelogram */ |
445 |
> |
for (c1 = l1; c1-- > 0; ) { |
446 |
> |
x1 = p0->x + c1*s1x/l1; |
447 |
> |
y1 = p0->y + c1*s1y/l1; |
448 |
> |
for (c2 = l2; c2-- > 0; ) { |
449 |
> |
x = x1 + c2*s2x/l2; |
450 |
> |
y = y1 + c2*s2y/l2; |
451 |
> |
if (zscan(y)[x] <= 0 || zscan(y)[x]-z |
452 |
> |
> zeps*zscan(y)[x]) { |
453 |
|
zscan(y)[x] = z; |
454 |
|
copycolr(pscan(y)[x], pix); |
455 |
|
} |
456 |
+ |
} |
457 |
+ |
} |
458 |
|
} |
459 |
|
|
460 |
|
|
461 |
< |
fillpicture() /* fill in empty spaces */ |
461 |
> |
backpicture() /* background fill algorithm */ |
462 |
|
{ |
463 |
|
int *yback, xback; |
464 |
|
int y; |
466 |
|
register int x, i; |
467 |
|
/* get back buffer */ |
468 |
|
yback = (int *)malloc(ourview.hresolu*sizeof(int)); |
469 |
< |
if (yback == NULL) { |
470 |
< |
perror(progname); |
381 |
< |
return; |
382 |
< |
} |
469 |
> |
if (yback == NULL) |
470 |
> |
syserror(); |
471 |
|
for (x = 0; x < ourview.hresolu; x++) |
472 |
|
yback[x] = -2; |
473 |
|
/* |
499 |
|
* Next, find background from left or right. |
500 |
|
*/ |
501 |
|
if (xback == -2) { |
502 |
< |
for (i = x+1; x < ourview.hresolu; i++) |
502 |
> |
for (i = x+1; i < ourview.hresolu; i++) |
503 |
|
if (zscan(y)[i] > 0) |
504 |
|
break; |
505 |
|
if (i < ourview.hresolu |
508 |
|
else |
509 |
|
xback = x-1; |
510 |
|
} |
423 |
– |
if (xback < 0 && yback[x] < 0) |
424 |
– |
continue; /* no background */ |
511 |
|
/* |
512 |
+ |
* Check to see if we have no background for |
513 |
+ |
* this pixel. If not, use background color. |
514 |
+ |
*/ |
515 |
+ |
if (xback < 0 && yback[x] < 0) { |
516 |
+ |
(*deffill)(x,y); |
517 |
+ |
continue; |
518 |
+ |
} |
519 |
+ |
/* |
520 |
|
* Compare, and use the background that is |
521 |
|
* farther, unless one of them is next to us. |
522 |
|
*/ |
523 |
< |
if (yback[x] < 0 || ABS(x-xback) <= 1 |
523 |
> |
if ( yback[x] < 0 |
524 |
> |
|| (xback >= 0 && ABS(x-xback) <= 1) |
525 |
|
|| ( ABS(y-yback[x]) > 1 |
526 |
< |
&& zscan(yback[x])[x] < zscan(y)[xback] )) |
526 |
> |
&& zscan(yback[x])[x] |
527 |
> |
< zscan(y)[xback] ) ) { |
528 |
|
copycolr(pscan(y)[x],pscan(y)[xback]); |
529 |
< |
else |
529 |
> |
zscan(y)[x] = zscan(y)[xback]; |
530 |
> |
} else { |
531 |
|
copycolr(pscan(y)[x],pscan(yback[x])[x]); |
532 |
+ |
zscan(y)[x] = zscan(yback[x])[x]; |
533 |
+ |
} |
534 |
|
} else { /* full pixel */ |
535 |
|
yback[x] = -2; |
536 |
|
xback = -2; |
540 |
|
} |
541 |
|
|
542 |
|
|
543 |
+ |
fillpicture() /* paint in empty pixels with default */ |
544 |
+ |
{ |
545 |
+ |
register int x, y; |
546 |
+ |
|
547 |
+ |
for (y = 0; y < ourview.vresolu; y++) |
548 |
+ |
for (x = 0; x < ourview.hresolu; x++) |
549 |
+ |
if (zscan(y)[x] <= 0) |
550 |
+ |
(*deffill)(x,y); |
551 |
+ |
} |
552 |
+ |
|
553 |
+ |
|
554 |
|
writepicture() /* write out picture */ |
555 |
|
{ |
556 |
|
int y; |
557 |
|
|
558 |
|
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
559 |
|
for (y = ourview.vresolu-1; y >= 0; y--) |
560 |
< |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { |
561 |
< |
perror(progname); |
560 |
> |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) |
561 |
> |
syserror(); |
562 |
> |
} |
563 |
> |
|
564 |
> |
|
565 |
> |
writedistance(fname) /* write out z file */ |
566 |
> |
char *fname; |
567 |
> |
{ |
568 |
> |
extern double sqrt(); |
569 |
> |
int donorm = normdist && ourview.type == VT_PER; |
570 |
> |
FILE *fp; |
571 |
> |
int y; |
572 |
> |
float *zout; |
573 |
> |
|
574 |
> |
if ((fp = fopen(fname, "w")) == NULL) { |
575 |
> |
perror(fname); |
576 |
> |
exit(1); |
577 |
> |
} |
578 |
> |
if (donorm |
579 |
> |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) |
580 |
> |
syserror(); |
581 |
> |
for (y = ourview.vresolu-1; y >= 0; y--) { |
582 |
> |
if (donorm) { |
583 |
> |
double vx, yzn2; |
584 |
> |
register int x; |
585 |
> |
yzn2 = y - .5*(ourview.vresolu-1); |
586 |
> |
yzn2 = 1. + yzn2*yzn2*ourview.vvn2; |
587 |
> |
for (x = 0; x < ourview.hresolu; x++) { |
588 |
> |
vx = x - .5*(ourview.hresolu-1); |
589 |
> |
zout[x] = zscan(y)[x] |
590 |
> |
* sqrt(vx*vx*ourview.vhn2 + yzn2); |
591 |
> |
} |
592 |
> |
} else |
593 |
> |
zout = zscan(y); |
594 |
> |
if (fwrite(zout, sizeof(float), ourview.hresolu, fp) |
595 |
> |
< ourview.hresolu) { |
596 |
> |
perror(fname); |
597 |
|
exit(1); |
598 |
|
} |
599 |
+ |
} |
600 |
+ |
if (donorm) |
601 |
+ |
free((char *)zout); |
602 |
+ |
fclose(fp); |
603 |
|
} |
604 |
|
|
605 |
|
|
611 |
|
&& *s != 'e' && *s != 'E' && *s != '+') |
612 |
|
return(0); |
613 |
|
return(1); |
614 |
+ |
} |
615 |
+ |
|
616 |
+ |
|
617 |
+ |
backfill(x, y) /* fill pixel with background */ |
618 |
+ |
int x, y; |
619 |
+ |
{ |
620 |
+ |
register BYTE *dest = pscan(y)[x]; |
621 |
+ |
|
622 |
+ |
copycolr(dest, backcolr); |
623 |
+ |
zscan(y)[x] = backz; |
624 |
+ |
} |
625 |
+ |
|
626 |
+ |
|
627 |
+ |
calstart(prog, args) /* start fill calculation */ |
628 |
+ |
char *prog, *args; |
629 |
+ |
{ |
630 |
+ |
char combuf[512]; |
631 |
+ |
int p0[2], p1[2]; |
632 |
+ |
|
633 |
+ |
if (childpid != -1) { |
634 |
+ |
fprintf(stderr, "%s: too many calculations\n", progname); |
635 |
+ |
exit(1); |
636 |
+ |
} |
637 |
+ |
sprintf(combuf, prog, PACKSIZ, args); |
638 |
+ |
if (pipe(p0) < 0 || pipe(p1) < 0) |
639 |
+ |
syserror(); |
640 |
+ |
if ((childpid = vfork()) == 0) { /* fork calculation */ |
641 |
+ |
close(p0[1]); |
642 |
+ |
close(p1[0]); |
643 |
+ |
if (p0[0] != 0) { |
644 |
+ |
dup2(p0[0], 0); |
645 |
+ |
close(p0[0]); |
646 |
+ |
} |
647 |
+ |
if (p1[1] != 1) { |
648 |
+ |
dup2(p1[1], 1); |
649 |
+ |
close(p1[1]); |
650 |
+ |
} |
651 |
+ |
execl("/bin/sh", "sh", "-c", combuf, 0); |
652 |
+ |
perror("/bin/sh"); |
653 |
+ |
_exit(127); |
654 |
+ |
} |
655 |
+ |
if (childpid == -1) |
656 |
+ |
syserror(); |
657 |
+ |
close(p0[0]); |
658 |
+ |
close(p1[1]); |
659 |
+ |
if ((psend = fdopen(p0[1], "w")) == NULL) |
660 |
+ |
syserror(); |
661 |
+ |
if ((precv = fdopen(p1[0], "r")) == NULL) |
662 |
+ |
syserror(); |
663 |
+ |
queuesiz = 0; |
664 |
+ |
} |
665 |
+ |
|
666 |
+ |
|
667 |
+ |
caldone() /* done with calculation */ |
668 |
+ |
{ |
669 |
+ |
int pid; |
670 |
+ |
|
671 |
+ |
if (childpid == -1) |
672 |
+ |
return; |
673 |
+ |
if (fclose(psend) == EOF) |
674 |
+ |
syserror(); |
675 |
+ |
clearqueue(); |
676 |
+ |
fclose(precv); |
677 |
+ |
while ((pid = wait(0)) != -1 && pid != childpid) |
678 |
+ |
; |
679 |
+ |
childpid = -1; |
680 |
+ |
} |
681 |
+ |
|
682 |
+ |
|
683 |
+ |
rcalfill(x, y) /* fill with ray-calculated pixel */ |
684 |
+ |
int x, y; |
685 |
+ |
{ |
686 |
+ |
FVECT orig, dir; |
687 |
+ |
float outbuf[6]; |
688 |
+ |
|
689 |
+ |
if (queuesiz >= PACKSIZ) { /* flush queue */ |
690 |
+ |
if (fflush(psend) == EOF) |
691 |
+ |
syserror(); |
692 |
+ |
clearqueue(); |
693 |
+ |
} |
694 |
+ |
/* send new ray */ |
695 |
+ |
rayview(orig, dir, &ourview, x+.5, y+.5); |
696 |
+ |
outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; |
697 |
+ |
outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; |
698 |
+ |
if (fwrite(outbuf, sizeof(float), 6, psend) < 6) |
699 |
+ |
syserror(); |
700 |
+ |
/* remember it */ |
701 |
+ |
queue[queuesiz][0] = x; |
702 |
+ |
queue[queuesiz][1] = y; |
703 |
+ |
queuesiz++; |
704 |
+ |
} |
705 |
+ |
|
706 |
+ |
|
707 |
+ |
clearqueue() /* get results from queue */ |
708 |
+ |
{ |
709 |
+ |
float inbuf[4]; |
710 |
+ |
register int i; |
711 |
+ |
|
712 |
+ |
for (i = 0; i < queuesiz; i++) { |
713 |
+ |
if (fread(inbuf, sizeof(float), 4, precv) < 4) { |
714 |
+ |
fprintf(stderr, "%s: read error in clearqueue\n", |
715 |
+ |
progname); |
716 |
+ |
exit(1); |
717 |
+ |
} |
718 |
+ |
setcolr(pscan(queue[i][1])[queue[i][0]], |
719 |
+ |
inbuf[0], inbuf[1], inbuf[2]); |
720 |
+ |
zscan(queue[i][1])[queue[i][0]] = inbuf[3]; |
721 |
+ |
} |
722 |
+ |
queuesiz = 0; |
723 |
+ |
} |
724 |
+ |
|
725 |
+ |
|
726 |
+ |
syserror() /* report error and exit */ |
727 |
+ |
{ |
728 |
+ |
perror(progname); |
729 |
+ |
exit(1); |
730 |
|
} |