# CS 455 Z-Buffering by dffhrtcv3

VIEWS: 4 PAGES: 16

• pg 1
```									CS 455 – Computer Graphics

Z-buffering
Z-Buffering
• Image precision algorithm:
   Determine which object is visible at each pixel
   Order of polygons not critical
   Works for dynamic scenes

• Basic idea:
   Rasterize (scan-convert) each polygon, one at a time
   Keep track of a z value at each pixel
- Interpolate z value of polygon vertices during rasterization
   Replace pixel with new color if z value is greater
(i.e., if object is closer to eye)
Example

Goal is to figure out which color to make the pixels based
on what’s in front of what.
Z-buffering
• Need to maintain:
   Frame buffer
- contains color values for each pixel
   Z-buffer
- contains the current value of z for each pixel
• The two buffers are the same size
• No object/object intersections
• No sorting of objects required
• But, additional memory is required for the z-buffer
Z-Buffering: Algorithm
allocate z-buffer;              // Allocate depth buffer  Same size as viewport.
for each pixel (x,y)     // For each pixel in viewport.
writePixel(x,y,backgrnd);        // Initialize color.
writeDepth(x,y,farPlane);        // Initialize depth (z) buffer.
for each polygon          // Draw each polygon (in any order).
for each pixel (x,y) in polygon          // Rasterize polygon.
pz = polygon’s z-value at (x,y); // Interpolate z-value at (x, y).
if (pz > z-buffer(x,y))                // If new depth is closer:
writePixel(x,y,color);               // Write new (polygon) color.
writeDepth(x,y,pz);                  // Write new depth.
Z-Buffering: Example

Scan convert the following two polygons.
The number in the pixel represents the z- value
for that pixel

-1                                     -1      (3,3)
(0,3)                                               -3 -2
-2 -3
-3 -4 -5                         -5 -4 -3
-4 -5 -6 -7                   -7 -6 -5 -4

(0,0)          (3,0)           (0,0)           (3,0)

Does order matter?
Z-Buffering: Example

-1                -
-1 - - -                -1       -       -
-1 - - -1
-2 -3             - -
-2 -3 - -             -3 -2       - - - -
-2 -3 -3 -2
-3 -4 -5
=   - - -
-3 -4 -5 -
+      -5 -4 -3
=         -
- - -4 -
-3 -4 -5 -3
-4 -5 -6 -7       - - - -
-4 -5 -6 -7       -7 -6 -5 -4             - -
- - -5 -4
-4 -5 -6 -7
+
- - - -
- - - -
- - - -
- - - -

-1                -
- - - -1       -1                -       -
-1 - - -1
+          -3 -2             - -
- - -3 -2       -2 -3             - - - -
-2 -3 -3 -2
-5 -4 -3   =      - - -
- -5 -4 -3   +   -3 -4 -5      =      -
- -4 - -
-3 -5 -4 -3
-7 -6 -5 -4       - - - -
-7 -6 -5 -4       -4 -5 -6 -7       - -
-4 -5 - -
-7 -6 -5 -4
Z-Buffering: Computing Z
• How do you compute the z value at a given pixel?
   Interpolate between vertices
z1                                      y1 - y s
y1                                         z a  z1 - z1 - z 2 
y1 - y2
za             zb
ys                                                              y1 - y s
zp                  zb  z1 - z1 - z3 
y2                                                              y1 - y3
z2
xb - x p
z p  zb - zb - z a 
xb - xa
y3
z3
Z-buffer implementation
• Know how to draw 2d polygons.
• 3d polygon, when projected onto the “film plane” or screen
is just a 2d polygon.
• But, keep Z value to figure out what’s in front.
• Updates to polygon scan converter
   Need to keep track of z value in GET and AET
   Before you draw a pixel, check the z-buffer against the current z
value
   If you color the pixel, update the z-buffer also
   For optimization:
- Maintain a horizontal z-increment for each new pixel
- Maintain a vertical z-increment as you move from scanline to scanline
GET Entries Updated for Z-buffering
• Non Z-buffered GET Entries:

ymax        x @ ymin   1/m

• With Z-buffering:

ymax    x @ ymin       1/m   z @ymin   vert_zinc
Computing the vert_zinc
• This value is the increment in z each time we move to a
new scan line

z1 - z0
vert _ zinc   
y1 - y0
Horizontal_zinc
• For efficiency, we can also compute a horizontal_zinc
for the horizontal direction
• Each time we move to the next pixel in the x direction,
we can increment z by horizontal_zinc
• Given the current z values for the two edges of a span,
the horizontal_zinc is given by

zb - z a
horizontal _ zinc   
xb - xa
`

8
pa = (xa, ya, za)
7
6
5
pb = (xb, yb, zb)
4
3
2
edge a                           edge b
1
0
0   1   2   3   4    5   6   7   8
AET Entries Updated for Z-buffering
• Non Z-buffered AET Entries:

x@
ymax                    1/m
current y

• With Z-buffering:

x@                                  z@
ymax                     1/m     vert_zinc
current y                          current x,y

• Note: horizontal_zinc doesn’t need to be stored in the
AET – just computed each iteration.
Z-Buffering : Recap
• Create a z-buffer which is the same size as the frame-
buffer
• Initialize frame-buffer to background
• Initialize z-buffer to far plane
• Scan convert polygons one at a time, just as before
• Maintain z-increment values in the edge tables
• At each pixel, check the current z-value against the z-
value stored in the z-buffer at this (x, y) location
   If the current z-value is greater
- Color the pixel the color for this point in the polygon
- Update the z-buffer
Z-Buffering : Summary
   Easy to implement
   Fast with hardware support  Fast depth buffer memory
   On most hardware
   No sorting of objects