55
Recap Clipping Culling Hidden surface removal Graphics 2011/2012, 4th quarter Lecture 8 Graphics pipeline (clipping and culling) Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Lecture 08: graphics pipeline (clipping and culling)

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics 2011/2012, 4th quarter

Lecture 8

Graphics pipeline (clipping and culling)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 2: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Graphics pipeline - part 1 (recap)

Perspective projection by matrixmultiplication:

xpixelypixel

zcanonical1

= MvpMperMcam

xyz1

This is an important part of the graphicspipeline.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 3: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Graphics pipeline - part 1 (recap)

View frustumSpecify camera position and view frustum

Camera transformationMove from world space to camera space

Orthographic view volumeTransform view frustum to axis-parallel box

Canonical view volumnTransform orthographic view volumeto 2x2x2 cube around the origin

Windowing transformationOrthographic projection andtransformation to screen space

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 4: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Extending homogeneous coordinates

Linear transformations: Points represented by vectors (x, y, z)

x 7→ x′ = ax+ by + cz

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 5: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Extending homogeneous coordinates

Linear transformations: Points represented by vectors (x, y, z)

x 7→ x′ = ax+ by + cz

Affine transformations: (x, y, z, 1) represents point (x, y, z)

x 7→ x′ = ax+ by + cz + d

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 6: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Geometric interpretation (in 2D)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 7: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Extending homogeneous coordinates

Linear transformations: Points represented by vectors (x, y, z)

x 7→ x′ = ax+ by + cz

Affine transformations: (x, y, z, 1) represents point (x, y, z)

x 7→ x′ = ax+ by + cz + d

Projective transformations: (x, y, z, w) represents (x/w, y/w, z/w, 1)which in turn represents (x/w, y/w, z/w)which in turn represents point (x, y, z).

x 7→ x′ = ax+by+cz+dex+fy+gz+h

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 8: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Geometric interpretation (in 1D)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 9: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Extending homogeneous coordinates

Matrix multiplication:

xyzw

=

a1 b1 c1 d1

a2 b2 c2 d2

a3 b3 c3 d3

e f g h

xyz1

=

a1x+ b1y + c1z + d1

a2x+ b2y + c2z + d2

a3x+ b3y + c3z + d3

ex+ fy + gz + h

Homogenization:

a1x+ b1y + c1z + d1

a2x+ b2y + c2z + d2

a3x+ b3y + c3z + d3

ex+ fy + gz + h

homogenize−−−−−−−−→

a1x+b1y+c1z+d1

ex+fy+gz+ha2x+b2y+c2z+d2

ex+fy+gz+ha3x+b3y+c3z+d3

ex+fy+gz+h

1

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 10: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Extending homogeneous coordinates

Matrix multiplication:

xyzw

= c

a1 b1 c1 d1

a2 b2 c2 d2

a3 b3 c3 d3

e f g h

xyz1

=

c(a1x+ b1y + c1z + d1)c(a2x+ b2y + c2z + d2)c(a3x+ b3y + c3z + d3)c(ex+ fy + gz + h)

Homogenization:

c(a1x+ b1y + c1z + d1)c(a2x+ b2y + c2z + d2)c(a3x+ b3y + c3z + d3)c(ex+ fy + gz + h)

homogenize−−−−−−−−→

c(a1x+b1y+c1z+d1)

c(ex+fy+gz+h)c(a2x+b2y+c2z+d2)

c(ex+fy+gz+h)c(a3x+b3y+c3z+d3)

c(ex+fy+gz+h)

1

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 11: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Extending homogeneous coordinates

Hence, both of these two matrices can be used as perspectivematrix:

n 0 0 00 n 0 00 0 n+ f −fn0 0 1 0

and

1 0 0 00 1 0 00 0 n+f

n −f0 0 1

n 0

and . . .

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 12: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Graphics pipeline (part 1)Linear transformationsAffine transformationsProjective transformations

Other stages in the graphics pipeline (recap)

Triangles that lie (partly)outside the view frustumneed not be projected,and are clipped.

The remaining trianglesare projected if they arefront facing.

Projected triangles haveto be shaded and/ortextured.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 13: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping

In general, we cannot expectall triangles to lie within theview frustum. Triangles thatlie partly outside the viewfrustum must be clipped.

We must . . .

1 verify if a triangleintersects with ahyperplane

2 create new triangle(s)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 14: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Implicit equations revisited

Remember the implicit equationof a plane:

f(~p) = ~n(~p− ~p0) = 0

or

f(~p) = ~n · ~p+ d = 0

We already learned that f(~p)splits our space into a positive and anegative subspace for whichf(~p) > 0 and f(~p) < 0, respectively.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 15: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Finding candidates for clipping

In tutorial 1 and 2, we have proventhat the normal always points to thepositive side.

Hence, if the normals of the clippingplanes point outward:

~p is inside the planeif f(~p) < 0~p is outside the planeif f(~p) > 0

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 16: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Finding intersection points

If two points ~a and ~b are ondifferent sides of a hyperplane,we first determine theparametric equation of the linethrough the points:

~p(t) = ~a+ t(~b− ~a)

Substituting this into thehyperplane equation yields

t′ =~n · ~a+ d

~n · (~a−~b)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 17: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Creating clipped triangles

Given the intersection points,we can clip the triangle againstthe hyperplane as follows:

If two vertices are on thepositive side, we get onenew triangle.

If one vertex is on thepositive side of thehyperplane, we get twonew triangles.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 18: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping against a hyperplane

But what do we do if atriangle is intersected by twoclipping planes?

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 19: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping against a hyperplane

We have to deal with suchsituations one clipping plane ata time.

We first clip the initial triangleagainst one of the clippingplanes. . .

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 20: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping against a hyperplane

. . . and clip the remainingtriangle(s) against the otherclipping plane.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 21: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping against a hyperplane

Similarly, we can deal withsituations where a triangle isintersected by three clippingplanes.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 22: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Where should we do the clipping?

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 23: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping after the perspective divide

Advantage: equations for these hyperplanes are quite simple:

−x+ l = 0x− r = 0−y + b = 0y − t = 0−z + n = 0z − f = 0

But: there’s a problem at the XY -plane . . .

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 24: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping after the perspective divide

Clipping after the perspectivedivide may lead to incorrectresults if line segments crossthe XY -plane, since a divisionby z is involved:

z′ = n+ f − fn

z

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 25: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping before the perspective divide

It is possible to clip against thesix clipping planes right beforethe perspective divide.

The eight corners of the viewfrustum are easily found by theinverse transformation M−1

per.

From these, we can derive theplane equations for the viewfrustum.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 26: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping in homogeneous coordinates

Surprisingly, it turns out to be easiest to clip in homogeneouscoordinates, which means that we clip in four dimensions againstthree-dimensional clipping hyperplanes.

The equations for these hyperplanes are quite simple:

−x′ + lw′ = 0x′ − rw′ = 0−y′ + bw′ = 0y′ − tw′ = 0−z′ + nw′ = 0z′ − fw′ = 0

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 27: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping in homogeneous coordinates

Hyperplanes in homogeneouscoordinates:

−x′ + lw′ = 0x′ − rw′ = 0−y′ + bw′ = 0y′ − tw′ = 0−z′ + nw′ = 0z′ − fw′ = 0

(x, y, z, 1)

(x′, y′, z′, w′)

( x′

w′ ,y′

w′ ,z′

w′ , 1)

matrix multiplication

homogeneous divide

rasterize

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 28: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

Clipping arbitrary polygons

Some rendering engines dealwith arbitrary polygons ratherthan with triangles.

Also, in drawing programs weoften need to clip arbitrarypolygons.

p0

p1 p2

p3p4

p5

p6

i0 i1

i2

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 29: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Sutherland-Hodgman algorithm

The Sutherland-Hodgmanclipping algorithm isstraightforward:

Clip the polygon subsequentlyagainst every clippinghyperplane.

p0

p1 p2

p3p4

p5

p6

i0 i1

i2

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 30: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Sutherland-Hodgman algorithm

Extend upper side ofrectangle across 2D space

Start at one vertex of thepolygone and follow its path

Create new vertex wherepath crosses clipping line

Repeat till we are back atstarting vertex

Create new polygon fromsets over vertices on orbeneath clipping line

Repeat for every clipping line

p0

p1 p2

p3p4

p5

p6

i0 i1

i2

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 31: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Sutherland-Hodgman algorithm

However, sometimes theSutherland-Hodgmanalgorithm results in degeneratepolygons.

For example, the resultingpolygon on the right hasvertices p0, i0, r2, i3, p4, p5,i2, r2, and i1, consecutively.

p0

p1

p2

p3p4

p5

i0 i1

i2p6

p7p8

i3r0

r1r2

r3

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 32: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Weiler-Atherton algorithm

Basic idea: create a graph with nodes for all vertices of the polygone, cornersof the view frustum, and intersection points and edges that allow us to easilyextract the clipped polygones.

p0

p1

p2

p3

p4

p5

p6

p7

p8

i0

i1

i2

i3

r0

r1

r2

r3

p0

p1

p2

p3p4

p5

i0 i1

i2p6

p7p8

i3r0

r1r2

r3

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 33: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Weiler-Atherton algorithm

Building the graph:

Make a graph with three groups of vertices: polygon vertices,clipping region vertices, and intersection vertices.

Insert edges by walking along the boundary of the polygon,including the intersection vertices. Distinguish outgoingintersections (colored red in the image) and incomingintersections (colored pink).

Insert edges by walking along the boundary of the clippingregion, including the intersection vertices.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 34: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Weiler-Atherton algorithm

p0

p1

p2

p3p4

p5

i0 i1

i2p6

p7p8

i3r0

r1r2

r3

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 35: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Weiler-Atherton algorithm

Using the graph:

Start at an outgoing intersection vertex, and walk along theboundary of the clipping region (black edges in image),reporting every vertex along the way.

Upon encountering an incoming intersection vertex, continuealong the boundary of the polygon (red edges in image).

Continue, changing from polygon boundary to clipping regionboundary and the other way around at outgoing and incomingintersection vertices, respectively, until we reach the startingvertex.

Start at the next unvisited outgoing intersection vertex tooutput the next clipped polygon, etc, until no unvisitedoutgoing intersection vertices are left.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 36: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

How to clip trianglesWhere to clip?Clipping arbitrary polygons

The Weiler-Atherton algorithm

p0

p1

p2

p3

p4

p5

p6

p7

p8

i0

i1

i2

i3

r0

r1

r2

r3

p0

p1

p2

p3p4

p5

i0 i1

i2p6

p7p8

i3r0

r1r2

r3

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 37: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Culling triangles

When a triangle lies entirelyoutside the view frustum, itcan be culled, i.e., removedfrom the graphics pipeline.

Testing individual triangles isexpensive, however.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 38: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Culling bounding volumes

Culling bounding volumes ofcomplicated objects(consisting of many triangles)will generally improve theperformance of the graphicspipeline.

If a bounding volume isoutside of the frustum, then soare the enclosed triangles.

This is called a conservativetest. (Why?)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 39: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Culling bounding volumes

Often spheres are used asbounding volumes.

If the plane is defined by

(~p− ~a) · ~n = 0

and the bounding sphere hascenter ~c and radius r, then wehave to check if

(~c−~a)·~n‖~n‖ > r

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 40: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Culling bounding volumes

(~c−~a)·~n‖~n‖ = cos θ‖~c−~a‖‖~n‖

‖~n‖ = cos θ‖~c−~a‖

Remember that the projection p~v ofa vector ~v onto another vector iscos θ‖~v‖ with θ being the anglebetween both vectors.

Hence, cos θ = p~c−~a

‖~c−~a‖and thus

cos θ‖~c− ~a‖ = p~c−~a

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 41: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Other forms of culling

frustum cullingculling of triangles outsideof the view frustum

occlusion cullingculling of triangleswithing the frustum thatare occluded by others

backface cullingculling of triangles facingaway from the camera

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 42: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Backface culling

frustum cullingculling of triangles outsideof the view frustum

occlusion cullingculling of triangleswithing the frustum thatare occluded by others

backface cullingculling of triangles facingaway from the camera

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 43: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Backface culling

Surfaces of polyhedral objects areoften modeled with connected,outward facing triangles.

How many triangles do we need tomodel a cube?

What is the maximum number ofthese triangles that is visible fromany given viewing direction?

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 44: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Backface culling

Obviously, if a camera facesthe backside of a triangle,there is no need to draw it.

But how do we know whichside we are looking at?

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 45: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

View frustum cullingBackface culling

Backface culling

If our models have outward facingnormals, we can again use theimplicit representation to verify if thecamera ~e is on the positive ornegative side of the plane / triangle:

If f(~e) > 0, we draw the triangle

If f(~e) < 0, we apply backfaceculling

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 46: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Painter’s algorithm

If we draw triangles that arefurther away before trianglesthat are closer to the viewingpoint, we end up with acorrect image:

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 47: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Painter’s algorithm

If we draw triangles that arefurther away before trianglesthat are closer to the viewingpoint, we end up with acorrect image:

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 48: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Painter’s algorithm

If we draw triangles that arefurther away before trianglesthat are closer to the viewingpoint, we end up with acorrect image:

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 49: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Cyclic overlap

However, not everyarrangement of trianglesadmits a back-to-frontordering:

And how about intersectingtriangles?

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 50: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Z-Buffer

Most common approach for hiddensurface removal: Z-buffer (or depthbuffer)

Uses additional storage for depthinformation (mostly hardware, butimplementations in software exist aswell)

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 51: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Z-buffer algorithm: basic idea

Apart from the frame buffer, which contains the pixels of the image,also maintain a Z-buffer of the same width and height, to store depthinformation for the projected triangles.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 52: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Z-buffer algorithm: basic idea

Initialize all Z-buffer entries to zmax

If a pixel is to be drawn at position [i, j],first test if its corresponding z-value pz is smaller then Z-buffer[i, j]

If this is the case, draw the pixel, and update Z-buffer[i, j] to pz

Otherwise, do not draw the pixel

z-values for projected vertices: calculated; for remaining pixels: interpolated

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 53: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Precision issues

For speed: z-values stored asnon-negative integers, i.e.

B values {0, 1, . . . , B − 1}

Hence, the z-values are mapped tointervals of size

∆z = (f − n)/B

Points on near plane z = n→ mapped to 0Points on far plane z = f→ mapped to B − 1

Remember: z′ is reciprocal to z!

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 54: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Precision issues

This results in

higher precision close to the eye

lower precision in the distance

If we have b bits for our z-buffer, thenB = 2b and the size of our intervals is

∆z = (f − n)/2b

Hence, we can influence the precision by

increasing b

pushing n further away

moving f closer

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)

Page 55: Lecture 08: graphics pipeline (clipping and culling)

RecapClipping

CullingHidden surface removal

Painter’s algorithmZ-buffer

Other stages in the graphics pipeline (recap)

Triangles that lie (partly)outside the view frustumneed not be projected,and are clipped.

The remaining trianglesare projected if they arefront facing.

Projected triangles haveto be shaded and/ortextured.

Graphics 2011/2012, 4th quarter Lecture 08: graphics pipeline (clipping and culling)