15

Click here to load reader

Hidden Surface Removal using Z-buffer

Embed Size (px)

DESCRIPTION

In Computer Graphics, Hidden surface determination also known as Visible Surface determination or hidden surface removal is the process used to determine which surfaces of a particular object are not visible from a particular angle or particular viewpoint. In this scribe we will describe the object-space method and image space method. We will also discuss Algorithm based on Z-buffer method, A-buffer method, and Scan-Line Method.

Citation preview

Page 1: Hidden Surface Removal using Z-buffer

CS 362: Graphics Scribe

Hidden Surface Removal

By:

Rajkumar Singh (09010138)Akhilesh Shah (09010147)

Course Instructors:

Professor Pinaki MitraProfessor Samit Bhattacharya

Department of Computer Science and EngineeringIndian Institute of Technology, Guwahati

23rd April, 2012

1

Page 2: Hidden Surface Removal using Z-buffer

Abstract

In Computer Graphics, Hidden surface determination also known as Visible Surface

determination or hidden surface removal is the process used to determine which surfaces

of a perticular object are not visible from a perticular angle or perticular viewpoint.

In this scribe we will describe the object-space method and image space method. We

will also discuss Algorithm based on Z-buffer method, A-buffer method, and Scan-Line

Method.

Keywords: A buufer, Z-buffer, depth buffer, Refresh buffer, Hidden Surface Re-

moval(HSR).

1 Hidden Surface removal

Visible Surface detection or Hidden surface removal algorithms are mainly classified

according to whether they deal with object definations directly or the images that are

obtained after projections. Broadly there are two types of methods shown as follows.

1.1 Object-Space Method

In object-space method we took whole object under consideration. This method com-

pares objects and parts of objects to each other within the scene defination to determine

that which surfaces, as a whole are not visible from a certain viewpoint and so that can

mark the surfaces as visible which are visible.

1.2 Image-Space Method

In Image-Space algorithm, visibility is decided by point by point at each pixel position

on the projection plane. Here we consider the projected image of the object not the

object itself like object-space method. Visibility of unvisibility is decided by pixel by

pixel or point by point traversal.

Note: Most visible surface/ Hidden Surface removal algorithms use image-space

methods, although object-space methods can also be used effectively to locate visible

surfaces or to removing hidden surfaces. Eg. Line Diplaying algorithm uses object-space

methods for displaying lines in a wire-frame.

2 Hidden Surface Removal Algorithms

Algorithms are describes below.

2

Page 3: Hidden Surface Removal using Z-buffer

2.1 Back-Face Detection

A fast and simple object-space method for identifying the back face of poly-hedron is

based on the Inside-Outside (given a point is inside or outside of a plane/surface). Let

Plane Parameters are A,B,C,D then a point (x, y, z) will be inside to a plane or on

the plane if

Ax + By + Cz + D ≤ 0

When an inside point is along the line of sight to the surface the polygon must be a

back face (it means we are inside that face and can not see its front from our viewing

position).

We can simplify the test by taking a vector let say N which is normal to a polygon

surface. Let the components of N are (A, B, C). V is a vector in the viewing direction

from the camera or eye position. As shown in Figure 1. The polygon will be back face

if dot product of its normal vector and viewing vector is greater than zero.means

V ·N ≥ 0

If the object descriptions have been converted to projection coordinates and our viewing

direction is parallal direction is parallel to the viewing Zv axis, then V = (0, 0, Vz) and

V ·N = VzC

So that we only need to consider the sign of C, the z component of the normal vector

N .

Figure 1: Polyhedron

In a Right handed viewing system with viewing direction along the negative zv axis,

the polygon is a back face C < 0. Also viewer can not see any face whose normal has

z component C = 0, since the viewing direction in the given example is grazing that

polygon. This in general, we can label any face if its normal vector has a z-component

value C ≤ 0. Some Observation based on the z-componet value.

C < 0 Back face of the polyhedron

C = 0 Grazing the polygon, Not visible from Viewing position

C > 0 Face Visible from viewing position.

Similar methods can be used for Left handed viewing position systems. In Left

3

Page 4: Hidden Surface Removal using Z-buffer

Handed Viewing systems back faces are identified by C ≥ 0 when the viewing dorec-

tion is along the positive zv axis.

By examining parameter C for the different planes defining an object, all the back

faces can be identified. For a single convex polyhedron this test identifies all the hidden

surfaces because each surface in polyhedron is either completely visible or completely

hidden. And also if a scene contains only nonoverlapping convex polyhedra, then again

all hidden surfaces are identified with the back method.

For concave polyhedron more tests need to be carried out to determine whether

there are additional faces that are totally or partially obscured by other faces.

2.2 Depth-Buffer Method

This approach is based on image-Space method.In this method For detecting visible

surfaces we compares surface depth at each pixel position on the projection plane. This

procedure is also called as Z-buffer method, Since object depth is measured from the

view plane along the z axisof a viewing system. Here Each surface of the scene is pro-

cessed separately, one point time across the surface. The method is usually applied to

scenes containing only polygon surfaces, because depth values can be computed very

quickly and the method is easy to implement. But the method can be applied to non-

planar surfaces as well. Below is the figure in which comparison between actual scene

and z-buffer scene is shown.

Figure 2: Comparison between Z-buffer and Actual Scene

With the object descriptions converted to projection coordinates, each (x, y, z) position

on a polygon surface corresponds to the orthographic projection point (x, y) on the view

plane. Therefore, for each pixel position (x, y) on the view plane, object depths can be

compared by comparing z values. Figure 3 shows the comparison between depths. this

figure shows surfaces at varying distance along the orthographic projection line from

postion (x, y) in a view plane taken as the xvyv plane. Surface S1 is the closest at this

position, so its surface intensity at (x, y) is saved.

We can implement the depth-buffer algorithm in normalized coordinates, so that z val-

ues range from 0 at the black clipping plane to zmax at the front clipping plane. The

plane of zmax can be set either to 1 (for unit cube) or to the largest value that can be

4

Page 5: Hidden Surface Removal using Z-buffer

stored on the system.

As implied by the name of this method, two buffer areas are required. A depth buffer

is used to store depth values for each (x, y) position as surfaces are processed, and the

refresh buffer stores the intensity values for each position.Initially, all positions in the

depth buffer are set to 0 (minimum depth), and the refresh buffer is initialized to the

background intensity. Each surface listed in the polygon tables is then processed, one

scan line at a time, calculating the depth (z depth) at each (x, y) pixel. The calculated

depth is then compared with the value stored in the depth buffer. If it is greater than

the value in depth buffer then this new calculated value is stored in the depth buffer.

And the surfce intensity is calculated at the same position and stored in the same lo-

cation in the (x, y) in the refresh buffer.

Figure 3: Polygons at different depth

In short whatever algorithm we discussed above is discussed below in small points.

1. Initialize the Depth buffer and refresh buffer with minus inifinty and backgroundintensity respectively for all buffer positions (x, y).

depth(x, y) = −∞ refresh(x, y) = Ibackground

2. For each position on each polygon surface, compare depth values to previouslystoredvaluesin the depth buffer to determine visibility.

• Calculate the depth z for each position (x, y) on the polygon.

• if z > depth(x, y), then set

depth(x, y) = z, refresh(x, y) = Isurface(x, y)

where Ibackground is the value for the background intensity, and Isurface(x, y) is theprojectedd intensity value for the surface at pixel position (x, y).

5

Page 6: Hidden Surface Removal using Z-buffer

Equation of plane is:Ax + By + Cz + D = 0 (1)

Depth values for a surface position (x, y) are calculated from the plane equation for eachsurface:

z =−Ax−By −D

C(2)

For any scan line, adjacent hrizontal positions across the line differ by 1, and a verticaly value on an adjacent scan line differs by 1. if the depth of position (x, y) has beendetermined to be z, then the depth z′ of the next position (x + 1, y) along the scan line isobtained from given equation.

z′

=−A(x + 1)−By −D

C(3)

⇒ z′

= z − −AC

(4)

Figure 4: Depth at adjacent position

The Ratio −AC is constant for each surface, so succeding depth values across a scan line areobtained from preceding values with a single addition.We first determine the y-coordinate the y-coordinate extends of each polygon, and processthe surface from tha topmost scan line. Starting at a top vertex, we can recrsively calculatex position down a left edge of the polygon as x

′= x− 1

m , where m is the slope of the edgein the figure shown below.

Figure 5: scan line and a polygon

6

Page 7: Hidden Surface Removal using Z-buffer

Depth values of the edge are obtained recursively as

z′

= Ax′−B(y−1)−D

C

x = ym −

bm

y − 1 = mx′+ b

x′

= y−b−1m

x′

= x− 1m

⇒ z′

= −(Ax+By+D)C +

Am+B

C

⇒ z′

= z +Am + B

C(5)

For Vertical left edge since m→∞ hence we calculate z′ using given equation

z′ = z +B

C(6)

Note: This Depth-buffer alias Z-buffer method works only for Opague surfaces, but forTransparent surfaces we have to store the vertical scan line as well as modifying z-buffermethod and hence this method(modificationof z-buffer method) is known as A-buffer method.

2.3 A-Buffer Method

The A-Buffer method represents an anti-aliased, area-averaged, accumulation buffer method.It was developed for implementation in the surface-rendering system called REYES (”Ren-ders Everything You Ever Saw”). A drawback of Depth-buffer method is that it deals onlywith opaque surfaces and cannot accumulate intensity values for more than one surface asrequired for displaying transparent surfaces. In A-buffer method, each position in buffercan reference a linked list of surfaces. Thus, multiple surface intensity can be consideredat each pixel position and object edges can be anti-aliased. Each position in A-buffer hastwo fields:

• Depth field - stores positive or negative real number.

• Intensity field - stores surface intensity information or a pointer value.

Figure 6: surface overlap

7

Page 8: Hidden Surface Removal using Z-buffer

Positive depth field signifies that the number stored at that position is the depth of singlesurface overlapping the corresponding pixel area. The intensity field stores the RGB com-ponent of surface colour at that point and the percent of pixel coverage. Negative depthfield signifies multiple surface contribution to the intensity. The intensity field stores apointer to linked list of surface data.

Figure 7: Organization of A-buffer pixel position

Data for each surface in the linked list includes:

• RGB intensity component

• opacity parameter (percent of transparency)

• depth

• percent of area covered

• surface identifier

• other surface rendering parameters

• pointer to next surface

Scan lines are processed to determine surface overlaps of pixel across the individual scanlines. Surfaces are subdivided into polygon mesh and clipped against the pixel boundaries.Opacity factors and percent of surface overlaps can be used to calculate the intensity ofpixel as an average of the contributions from the overlapping surfaces.

2.4 Scan Line Method

This is image-space based method.This is used for removing hidden surface and this is anextension of the scan-line algorithm for filling polygon interiors and here in this algorithmwe deal with multiple surfaces, not just a single surface. To determine the visible surfaces,all polygon surfaces intersecting the scan line are examined. Across each scan line, surfacenearest to the view plane is determined by making depth calculations. After that, intensity

8

Page 9: Hidden Surface Removal using Z-buffer

value for that position is entered into refresh buffer. Edge Table and Polygon Table are setup for various surfaces.Edge table contains:-

• The x-coordinate of the end with the smaller y-coordinate.

• The y-coordinate of the edges other end.

• The x-increment .

• The polygon identification number indicating the polygon to which the edge belongs.

Figure 8: Edge Table Entry

Polygon table contains:-

• The coefficients of the plane equation.

• Shading or colour information for the polygon.

• An in-out boolean flag initialized to FALSE and used during the scan line processing.

Figure 9: Polygon Table Entry

In the below shown figure it is shown that how we actually locate the visible portions ofsurfaces for pixel position along the line. The active list for scan line 1 contains informationfrom the edge table for edges AB, BD, EH and FG. For positions along this scan line betweenedges AB and BD, only the flag for surface S2 is on. Therefore, no depth calculation isrequired, and intensity information for surfaces S2.

Figure 10: Scan Line Method for Hidden Surface Removal

9

Page 10: Hidden Surface Removal using Z-buffer

For the given example we will show the Active edge table entries. The example is as follows.

Figure 11: Scan Line Method for Hidden Surface Removal

Scan Line Entries

a AB AC [In out flag of 4ABCisonBetweenABandAC

b ABAC︸ ︷︷ ︸4ABCon

DEEF︸ ︷︷ ︸4DEFon

c ABD︸ ︷︷ ︸4ABCon

EBC︸ ︷︷ ︸4ABC,4DEFon

EF︸︷︷︸4DEFon

c+1 ABD︸ ︷︷ ︸4ABCon

EBC︸ ︷︷ ︸4ABC,4DEFon

EF︸︷︷︸4DEFon

c+2 ABBC︸ ︷︷ ︸4ABCon

DEEF︸ ︷︷ ︸4DEFon

Table 1: Active Edge Table for Scan line Method in Aove Example

In the above example Between Edge DF and BC since Flags for both 4ABC and 4DEFare On, So we do not have to perform the depth calculation for these two.The above described algorithms(Scan line method does not work for the scenes in whichthere are cyclically overlapping of surfaces. One of such case is shown below.

Figure 12: Cyclically Overlapping Surface

10

Page 11: Hidden Surface Removal using Z-buffer

2.5 Depth-Sorting Method or Painter’s Algorithm

This method uses both Image-space method and objects-space method. This method forsolving the hidden-surface removal is also known as Painter’s Algorithm because of thesimilarity between the Painting creation and this algorithm execution. In this Algorithmwe perform these given basic tasks.

1. All the surfaces are sorted in the order of decresing depths. This sorting is carriedout in both image-space and Object-space. For this we use the deepest point on eachsurface for comparison.

2. Surfaces are scan converted in order, First we start with the surface of largest depth.This scan conversion of the polygon surfaces is performed in image-space or the pro-jected space.

By refering the figure shown below. We perform certain tests that are as follows for eachsurface that overlaps with S (Shown in figure).

Figure 13: Cyclically Overlapping Surface

On performing following tests. if any of starting three tests is true no reasoning is necessaryfor that surface. The tests are listed in order of increasing difficulty.

Test 1: The bounding rectangle of the two surfaces on xy−plane do not overlap,

Test 2: Surface S us Completely behind the overlapping surface relative to the viewingposition.

11

Page 12: Hidden Surface Removal using Z-buffer

In the above shown figure it can be checked by the sign of the endpoints.

Test 3: The overlapping surface is completely in front of S relative to the viewing positions.

Test 4: The projection of the two surfaces on the view plane do not overlap.

3 Quetions

Question 1 In what case is scan line algorithm not applicable?

Scan line Algorithm is not applicable in case of Intersecting surfaces or the cases of cyclicoverlap as shown in figure below.

Figure 14: Cyclically Overlapping Surface

Question 2 Give an example of back face detection alogrithm.

Consider an example of a cube as shown in figure below. n and V are the unit normalvector to any surface and unit vector in viewing direction respectively. In this example,

12

Page 13: Hidden Surface Removal using Z-buffer

V = -k, nA = k, nC = -k

Figure 15: Example for Back Face Detection Algorithm

If dot product of n and V is greater than or equal to zero, the surface is a back face.

(~n ·~V) ≥ 0

In given example,

nA ·V = −1,nC ·V = 1,nx ·V = 0,for surfaces x = B,D,EandF . Hence, in the con-sidered example, only surface B is visible.

Question 3 Give an example of Z-buffer algorithm (show the calculated depth and color).

Consider the figure as shown below. Surface A (Red coloured) overlaps surface B (Bluecoloured). Consider that surface A is at depth 3 and surface B at depth 5. Now, we preparea table containing depth and intensity of any point for a surface determined by polygonEFGH.

Figure 16: Example for Z-buffer Algorithm

13

Page 14: Hidden Surface Removal using Z-buffer

Figure 17: Table showing Depth and Intensity of each point

Question 4 Give an example of A buffer Algorithm.

Consider the figure as shown below. Transparent surface A (Red coloured) overlaps trans-parent surface B (Blue coloured) and surface C (Green coloured). Consider that surface Ais at depth 3, surface B at depth 5 and surface C at depth 7. Now, we determine intensityat various points of considered polygon EFGH.

Figure 18: Example for Z-buffer Algorithm

For point A, +ve real number is stored in depth buffer field to signify one surfaceoverlap. Intensity stored in intensity buffer field. However, for point G that has multiplesurface overlaps, -ve real number is stored in depth field. The intensity field points to nextnode of the linked list that stores depth value and intensity of Surface A. Next pointer isto surface B and so forth.

14

Page 15: Hidden Surface Removal using Z-buffer

Figure 19: Example for A-buffer Algorithm

Question 5 Explain Depth sorting method.

• Both image and object space operations.

• Perform the basic functions.

• Surface are sorted in order of decreasing depth.

• Surface are scan converted in order, starting with the surface of greatest depth.

• Often referred as painters algorithm.

• Test listed in the order of increasing difficulty.

• Surfaces do not overlap.

• Projections of the two surfaces on to the view plane do nor overlap.

15