21 |
|
|
22 |
|
VIEW ourview = STDVIEW(512); /* desired view */ |
23 |
|
|
24 |
< |
double zeps = 0.001; /* allowed z epsilon */ |
24 |
> |
double zeps = .02; /* allowed z epsilon */ |
25 |
|
|
26 |
|
COLR *ourpict; /* output picture */ |
27 |
|
float *ourzbuf; /* corresponding z-buffer */ |
173 |
|
char *pfile, *zfile; |
174 |
|
{ |
175 |
|
FILE *pfp, *zfp; |
176 |
– |
COLR *scanin; |
177 |
– |
float *zin; |
176 |
|
char *err; |
177 |
+ |
COLR *scanin; |
178 |
+ |
float *zin, *zout; |
179 |
+ |
int *pout; |
180 |
|
int xres, yres; |
181 |
|
int y; |
182 |
|
/* open input files */ |
207 |
|
/* allocate scanlines */ |
208 |
|
scanin = (COLR *)malloc(xres*sizeof(COLR)); |
209 |
|
zin = (float *)malloc(xres*sizeof(float)); |
210 |
< |
if (scanin == NULL || zin == NULL) { |
210 |
> |
zout = (float *)calloc(xres, sizeof(float)); |
211 |
> |
pout = (int *)calloc(xres, sizeof(int)); |
212 |
> |
if (scanin == NULL || zin == NULL || zout == NULL || pout == NULL) { |
213 |
|
perror(progname); |
214 |
|
exit(1); |
215 |
|
} |
223 |
|
fprintf(stderr, "%s: read error\n", zfile); |
224 |
|
exit(1); |
225 |
|
} |
226 |
< |
addscanline(y, scanin, zin); |
226 |
> |
addscanline(y, scanin, zin, pout, zout); |
227 |
|
} |
228 |
|
/* clean up */ |
229 |
|
free((char *)scanin); |
230 |
|
free((char *)zin); |
231 |
+ |
free((char *)pout); |
232 |
+ |
free((char *)zout); |
233 |
|
fclose(pfp); |
234 |
|
fclose(zfp); |
235 |
|
} |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
< |
addscanline(y, pline, zline) /* add scanline to output */ |
274 |
> |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
275 |
|
int y; |
276 |
|
COLR *pline; |
277 |
|
float *zline; |
278 |
+ |
int *lasty; |
279 |
+ |
float *lastyz; |
280 |
|
{ |
281 |
< |
extern double sqrt(); |
281 |
> |
extern double sqrt(), fabs(); |
282 |
|
double pos[3]; |
283 |
+ |
int lastx = 0; |
284 |
+ |
double lastxz = 0; |
285 |
+ |
double zt; |
286 |
+ |
int xpos, ypos; |
287 |
|
register int x; |
277 |
– |
register int xpos, ypos; |
288 |
|
|
289 |
< |
for (x = 0; x < theirview.hresolu; x++) { |
289 |
> |
for (x = theirview.hresolu-1; x >= 0; x--) { |
290 |
|
pos[0] = x - .5*(theirview.hresolu-1); |
291 |
|
pos[1] = y - .5*(theirview.vresolu-1); |
292 |
|
pos[2] = zline[x]; |
293 |
|
if (theirview.type == VT_PER) { |
294 |
+ |
/* |
295 |
+ |
* The following (single) statement can go |
296 |
+ |
* if z is along the view direction rather |
297 |
+ |
* than an eye ray. |
298 |
+ |
*/ |
299 |
|
pos[2] /= sqrt( 1. |
300 |
|
+ pos[0]*pos[0]*theirview.vhn2 |
301 |
|
+ pos[1]*pos[1]*theirview.vvn2 ); |
303 |
|
pos[1] *= pos[2]; |
304 |
|
} |
305 |
|
multp3(pos, pos, theirs2ours); |
306 |
< |
if (pos[2] <= 0.0) |
306 |
> |
if (pos[2] <= 0) |
307 |
|
continue; |
308 |
|
if (ourview.type == VT_PER) { |
309 |
|
pos[0] /= pos[2]; |
311 |
|
} |
312 |
|
pos[0] += .5*ourview.hresolu; |
313 |
|
pos[1] += .5*ourview.vresolu; |
314 |
< |
if (pos[0] < 0 || pos[0] >= ourview.hresolu |
315 |
< |
|| pos[1] < 0 || pos[1] >= ourview.vresolu) |
314 |
> |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
315 |
> |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
316 |
|
continue; |
317 |
< |
/* check current value at pos */ |
318 |
< |
xpos = pos[0]; |
319 |
< |
ypos = pos[1]; |
320 |
< |
if (zscan(ypos)[xpos] <= 0.0 |
321 |
< |
|| zscan(ypos)[xpos] - pos[2] |
322 |
< |
> zeps*zscan(ypos)[xpos]) { |
323 |
< |
zscan(ypos)[xpos] = pos[2]; |
324 |
< |
copycolr(pscan(ypos)[xpos], pline[x]); |
325 |
< |
} |
317 |
> |
/* add pixel to our image */ |
318 |
> |
zt = 2.*zeps*zline[x]; |
319 |
> |
addpixel(xpos, ypos, |
320 |
> |
(fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1, |
321 |
> |
(fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1, |
322 |
> |
pline[x], pos[2]); |
323 |
> |
lastx = xpos; |
324 |
> |
lasty[x] = ypos; |
325 |
> |
lastxz = lastyz[x] = zline[x]; |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
+ |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
331 |
+ |
int xstart, ystart; |
332 |
+ |
int width, height; |
333 |
+ |
COLR pix; |
334 |
+ |
double z; |
335 |
+ |
{ |
336 |
+ |
register int x, y; |
337 |
+ |
/* make width and height positive */ |
338 |
+ |
if (width < 0) { |
339 |
+ |
width = -width; |
340 |
+ |
xstart = xstart-width+1; |
341 |
+ |
} else if (width == 0) |
342 |
+ |
width = 1; |
343 |
+ |
if (height < 0) { |
344 |
+ |
height = -height; |
345 |
+ |
ystart = ystart-height+1; |
346 |
+ |
} else if (height == 0) |
347 |
+ |
height = 1; |
348 |
+ |
/* fill pixel(s) within rectangle */ |
349 |
+ |
for (y = ystart; y < ystart+height; y++) |
350 |
+ |
for (x = xstart; x < xstart+width; x++) |
351 |
+ |
if (zscan(y)[x] <= 0 |
352 |
+ |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
353 |
+ |
zscan(y)[x] = z; |
354 |
+ |
copycolr(pscan(y)[x], pix); |
355 |
+ |
} |
356 |
+ |
} |
357 |
+ |
|
358 |
+ |
|
359 |
|
fillpicture() /* fill in empty spaces */ |
360 |
|
{ |
361 |
|
int *yback, xback; |
380 |
|
for (y = 0; y < ourview.vresolu; y++) { |
381 |
|
xback = -2; |
382 |
|
for (x = 0; x < ourview.hresolu; x++) |
383 |
< |
if (zscan(y)[x] <= 0.0) { /* empty pixel */ |
383 |
> |
if (zscan(y)[x] <= 0) { /* empty pixel */ |
384 |
|
/* |
385 |
|
* First, find background from above or below. |
386 |
|
* (farthest assigned pixel) |
387 |
|
*/ |
388 |
|
if (yback[x] == -2) { |
389 |
|
for (i = y+1; i < ourview.vresolu; i++) |
390 |
< |
if (zscan(i)[x] > 0.0) |
390 |
> |
if (zscan(i)[x] > 0) |
391 |
|
break; |
392 |
|
if (i < ourview.vresolu |
393 |
|
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
400 |
|
*/ |
401 |
|
if (xback == -2) { |
402 |
|
for (i = x+1; x < ourview.hresolu; i++) |
403 |
< |
if (zscan(y)[i] > 0.0) |
403 |
> |
if (zscan(y)[i] > 0) |
404 |
|
break; |
405 |
|
if (i < ourview.hresolu |
406 |
|
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |