cs 445 / 645 introduction to computer graphics

43
CS 445 / 645 Introduction to Computer Graphics Lecture 21 Lecture 21 Visibility Visibility

Upload: carlos-jimenez

Post on 31-Dec-2015

41 views

Category:

Documents


5 download

DESCRIPTION

CS 445 / 645 Introduction to Computer Graphics. Lecture 21 Visibility. Recap: Rendering Pipeline. Modeling transformations Viewing transformations Projection transformations Clipping Scan conversion - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 445 / 645 Introduction to Computer Graphics

CS 445 / 645Introduction to Computer Graphics

Lecture 21Lecture 21

VisibilityVisibility

Lecture 21Lecture 21

VisibilityVisibility

Page 2: CS 445 / 645 Introduction to Computer Graphics

Recap: Rendering Pipeline

• Modeling transformationsModeling transformations

• Viewing transformationsViewing transformations

• Projection transformationsProjection transformations

• ClippingClipping

• Scan conversionScan conversion

We now know everything about how to draw a We now know everything about how to draw a polygon on the screen, except polygon on the screen, except visible surface visible surface determinationdetermination

• Modeling transformationsModeling transformations

• Viewing transformationsViewing transformations

• Projection transformationsProjection transformations

• ClippingClipping

• Scan conversionScan conversion

We now know everything about how to draw a We now know everything about how to draw a polygon on the screen, except polygon on the screen, except visible surface visible surface determinationdetermination

Page 3: CS 445 / 645 Introduction to Computer Graphics

Invisible Primitives

Why might a polygon be invisible?Why might a polygon be invisible?

• Polygon outside the Polygon outside the field of viewfield of view

• Polygon is Polygon is backfacingbackfacing

• Polygon is Polygon is occludedoccluded by object(s) nearer the viewpointby object(s) nearer the viewpoint

For efficiency reasons, we want to avoid spending work For efficiency reasons, we want to avoid spending work on polygons outside field of view or backfacingon polygons outside field of view or backfacing

For efficiency and correctness reasons, we need to know For efficiency and correctness reasons, we need to know when polygons are occludedwhen polygons are occluded

Why might a polygon be invisible?Why might a polygon be invisible?

• Polygon outside the Polygon outside the field of viewfield of view

• Polygon is Polygon is backfacingbackfacing

• Polygon is Polygon is occludedoccluded by object(s) nearer the viewpointby object(s) nearer the viewpoint

For efficiency reasons, we want to avoid spending work For efficiency reasons, we want to avoid spending work on polygons outside field of view or backfacingon polygons outside field of view or backfacing

For efficiency and correctness reasons, we need to know For efficiency and correctness reasons, we need to know when polygons are occludedwhen polygons are occluded

Page 4: CS 445 / 645 Introduction to Computer Graphics

View Frustum Clipping

Remove polygons entirely outside frustumRemove polygons entirely outside frustum

• Note that this includes polygons “behind” eye (actually Note that this includes polygons “behind” eye (actually behind near plane)behind near plane)

Pass through polygons Pass through polygons entirely inside frustumentirely inside frustum

Modify remaining polygonsModify remaining polygonsto include only portions to include only portions intersecting view frustumintersecting view frustum

Remove polygons entirely outside frustumRemove polygons entirely outside frustum

• Note that this includes polygons “behind” eye (actually Note that this includes polygons “behind” eye (actually behind near plane)behind near plane)

Pass through polygons Pass through polygons entirely inside frustumentirely inside frustum

Modify remaining polygonsModify remaining polygonsto include only portions to include only portions intersecting view frustumintersecting view frustum

Page 5: CS 445 / 645 Introduction to Computer Graphics

Back-Face CullingMost objects in scene are typically “solid”Most objects in scene are typically “solid”

More rigorously: More rigorously: closed, orientable manifoldsclosed, orientable manifolds• Must not cut through itselfMust not cut through itself

• Must have two distinct sidesMust have two distinct sides

– A sphere is orientable since it has A sphere is orientable since it has two sides, 'inside' and 'outside'.two sides, 'inside' and 'outside'.

– A Mobius strip or a Klein bottle is not A Mobius strip or a Klein bottle is not orientable orientable

• Cannot “walk” from one side to the otherCannot “walk” from one side to the other

– A sphere is a closed manifold A sphere is a closed manifold whereas a plane is not whereas a plane is not

Most objects in scene are typically “solid”Most objects in scene are typically “solid”

More rigorously: More rigorously: closed, orientable manifoldsclosed, orientable manifolds• Must not cut through itselfMust not cut through itself

• Must have two distinct sidesMust have two distinct sides

– A sphere is orientable since it has A sphere is orientable since it has two sides, 'inside' and 'outside'.two sides, 'inside' and 'outside'.

– A Mobius strip or a Klein bottle is not A Mobius strip or a Klein bottle is not orientable orientable

• Cannot “walk” from one side to the otherCannot “walk” from one side to the other

– A sphere is a closed manifold A sphere is a closed manifold whereas a plane is not whereas a plane is not

www.kleinbottle.comwww.kleinbottle.com

Page 6: CS 445 / 645 Introduction to Computer Graphics

Back-Face Culling

Most objects in scene are typically “solid”Most objects in scene are typically “solid”

More rigorously: More rigorously: closed, orientable manifoldsclosed, orientable manifolds

• Local neighborhood of all points isomorphic to discLocal neighborhood of all points isomorphic to disc

• Boundary partitions space into interior & exteriorBoundary partitions space into interior & exterior

Most objects in scene are typically “solid”Most objects in scene are typically “solid”

More rigorously: More rigorously: closed, orientable manifoldsclosed, orientable manifolds

• Local neighborhood of all points isomorphic to discLocal neighborhood of all points isomorphic to disc

• Boundary partitions space into interior & exteriorBoundary partitions space into interior & exterior

Yes No

Page 7: CS 445 / 645 Introduction to Computer Graphics

Manifold

Examples of Examples of manifoldmanifold objects: objects:

• SphereSphere

• TorusTorus

• Well-formedWell-formedCAD partCAD part

Examples of Examples of manifoldmanifold objects: objects:

• SphereSphere

• TorusTorus

• Well-formedWell-formedCAD partCAD part

Page 8: CS 445 / 645 Introduction to Computer Graphics

Back-Face Culling

Examples of non-manifold objects:Examples of non-manifold objects:

• A single polygonA single polygon

• A terrain or height fieldA terrain or height field

• polyhedron w/ missing facepolyhedron w/ missing face

• Anything with cracks or holes in boundaryAnything with cracks or holes in boundary

• one-polygon thick lampshadeone-polygon thick lampshade

Examples of non-manifold objects:Examples of non-manifold objects:

• A single polygonA single polygon

• A terrain or height fieldA terrain or height field

• polyhedron w/ missing facepolyhedron w/ missing face

• Anything with cracks or holes in boundaryAnything with cracks or holes in boundary

• one-polygon thick lampshadeone-polygon thick lampshade

Page 9: CS 445 / 645 Introduction to Computer Graphics

Back-Face Culling

On the surface of a closed manifold, polygons On the surface of a closed manifold, polygons whose normals point away from the camera are whose normals point away from the camera are always occluded:always occluded:

On the surface of a closed manifold, polygons On the surface of a closed manifold, polygons whose normals point away from the camera are whose normals point away from the camera are always occluded:always occluded:

Note: backface cullingalone doesn’t solve the

hidden-surface problem!

Page 10: CS 445 / 645 Introduction to Computer Graphics

Back-Face Culling

Not rendering backfacing polygons improves Not rendering backfacing polygons improves performanceperformance

• By how much?By how much?

– Reduces by about half the number of polygons to be Reduces by about half the number of polygons to be considered for each pixelconsidered for each pixel

Every front-facing polygon must have a corresponding Every front-facing polygon must have a corresponding rear-facing onerear-facing one

Not rendering backfacing polygons improves Not rendering backfacing polygons improves performanceperformance

• By how much?By how much?

– Reduces by about half the number of polygons to be Reduces by about half the number of polygons to be considered for each pixelconsidered for each pixel

Every front-facing polygon must have a corresponding Every front-facing polygon must have a corresponding rear-facing onerear-facing one

Page 11: CS 445 / 645 Introduction to Computer Graphics

Occlusion

For most interesting scenes, some polygons will overlap:For most interesting scenes, some polygons will overlap:

To render the correct image, we need to determine which To render the correct image, we need to determine which polygons polygons occlude occlude whichwhich

For most interesting scenes, some polygons will overlap:For most interesting scenes, some polygons will overlap:

To render the correct image, we need to determine which To render the correct image, we need to determine which polygons polygons occlude occlude whichwhich

Page 12: CS 445 / 645 Introduction to Computer Graphics

Painter’s Algorithm

Simple approach: render the polygons from back to front, Simple approach: render the polygons from back to front, “painting over” previous polygons:“painting over” previous polygons:

• Draw blue, then green, then orangeDraw blue, then green, then orange

Will this work in the general case?Will this work in the general case?

Simple approach: render the polygons from back to front, Simple approach: render the polygons from back to front, “painting over” previous polygons:“painting over” previous polygons:

• Draw blue, then green, then orangeDraw blue, then green, then orange

Will this work in the general case?Will this work in the general case?

Page 13: CS 445 / 645 Introduction to Computer Graphics

Painter’s Algorithm: Problems

Intersecting polygonsIntersecting polygons present a problem present a problem

Even non-intersecting polygons can form a cycle Even non-intersecting polygons can form a cycle with no valid visibility order:with no valid visibility order:

Intersecting polygonsIntersecting polygons present a problem present a problem

Even non-intersecting polygons can form a cycle Even non-intersecting polygons can form a cycle with no valid visibility order:with no valid visibility order:

Page 14: CS 445 / 645 Introduction to Computer Graphics

Analytic Visibility Algorithms

Early visibility algorithms computed the set of visible Early visibility algorithms computed the set of visible polygon polygon fragmentsfragments directly, then rendered the directly, then rendered the fragments to a display:fragments to a display:

Early visibility algorithms computed the set of visible Early visibility algorithms computed the set of visible polygon polygon fragmentsfragments directly, then rendered the directly, then rendered the fragments to a display:fragments to a display:

Page 15: CS 445 / 645 Introduction to Computer Graphics

Analytic Visibility Algorithms

What is the minimum worst-case cost of What is the minimum worst-case cost of computing the fragments for a scene composed computing the fragments for a scene composed of of nn polygons? polygons?

Answer: Answer: O(O(nn22))

What’s your opinionWhat’s your opinion

of O(nof O(n22)?)?

What is the minimum worst-case cost of What is the minimum worst-case cost of computing the fragments for a scene composed computing the fragments for a scene composed of of nn polygons? polygons?

Answer: Answer: O(O(nn22))

What’s your opinionWhat’s your opinion

of O(nof O(n22)?)?

Page 16: CS 445 / 645 Introduction to Computer Graphics

Analytic Visibility Algorithms

So, for about a decade (late 60s to late 70s) there So, for about a decade (late 60s to late 70s) there was intense interest in finding efficient was intense interest in finding efficient algorithms for algorithms for hidden surface removalhidden surface removal

We’ll talk about two: We’ll talk about two:

• Binary Space-Partition (BSP) TreesBinary Space-Partition (BSP) Trees

• Warnock’s AlgorithmWarnock’s Algorithm

So, for about a decade (late 60s to late 70s) there So, for about a decade (late 60s to late 70s) there was intense interest in finding efficient was intense interest in finding efficient algorithms for algorithms for hidden surface removalhidden surface removal

We’ll talk about two: We’ll talk about two:

• Binary Space-Partition (BSP) TreesBinary Space-Partition (BSP) Trees

• Warnock’s AlgorithmWarnock’s Algorithm

Page 17: CS 445 / 645 Introduction to Computer Graphics

Binary Space Partition Trees (1979)

BSP tree: organize all of space (hence BSP tree: organize all of space (hence partitionpartition)) into a binary treeinto a binary tree

• PreprocessPreprocess: overlay a binary tree on objects in the scene: overlay a binary tree on objects in the scene

• RuntimeRuntime: correctly traversing this tree enumerates objects : correctly traversing this tree enumerates objects from back to frontfrom back to front

• Idea: divide space recursively into half-spaces by choosing Idea: divide space recursively into half-spaces by choosing splitting planessplitting planes

– Splitting planes can be arbitrarily orientedSplitting planes can be arbitrarily oriented

BSP tree: organize all of space (hence BSP tree: organize all of space (hence partitionpartition)) into a binary treeinto a binary tree

• PreprocessPreprocess: overlay a binary tree on objects in the scene: overlay a binary tree on objects in the scene

• RuntimeRuntime: correctly traversing this tree enumerates objects : correctly traversing this tree enumerates objects from back to frontfrom back to front

• Idea: divide space recursively into half-spaces by choosing Idea: divide space recursively into half-spaces by choosing splitting planessplitting planes

– Splitting planes can be arbitrarily orientedSplitting planes can be arbitrarily oriented

Page 18: CS 445 / 645 Introduction to Computer Graphics

BSP Trees: Objects

Page 19: CS 445 / 645 Introduction to Computer Graphics

BSP Trees: Objects

Page 20: CS 445 / 645 Introduction to Computer Graphics

BSP Trees: Objects

Page 21: CS 445 / 645 Introduction to Computer Graphics

BSP Trees: Objects

Page 22: CS 445 / 645 Introduction to Computer Graphics

BSP Trees: Objects

Page 23: CS 445 / 645 Introduction to Computer Graphics

Rendering BSP TreesrenderBSP(BSPtree *T)renderBSP(BSPtree *T)

BSPtree *near, *far;BSPtree *near, *far;

if (if (eye on left side of T->planeeye on left side of T->plane))

near = T->left; far = T->right;near = T->left; far = T->right;

else else

near = T->right; far = T->left;near = T->right; far = T->left;

renderBSP(far);renderBSP(far);

if (if (T is a leaf nodeT is a leaf node))

renderObject(T)renderObject(T)

renderBSP(near);renderBSP(near);

renderBSP(BSPtree *T)renderBSP(BSPtree *T)

BSPtree *near, *far;BSPtree *near, *far;

if (if (eye on left side of T->planeeye on left side of T->plane))

near = T->left; far = T->right;near = T->left; far = T->right;

else else

near = T->right; far = T->left;near = T->right; far = T->left;

renderBSP(far);renderBSP(far);

if (if (T is a leaf nodeT is a leaf node))

renderObject(T)renderObject(T)

renderBSP(near);renderBSP(near);

Page 24: CS 445 / 645 Introduction to Computer Graphics

Rendering BSP Trees

Page 25: CS 445 / 645 Introduction to Computer Graphics

Rendering BSP Trees

Page 26: CS 445 / 645 Introduction to Computer Graphics

Polygons: BSP Tree Construction

Split along the plane defined by any polygon from Split along the plane defined by any polygon from scenescene

Classify all polygons into positive or negative Classify all polygons into positive or negative half-space of the planehalf-space of the plane• If a polygon intersects plane, split polygon into two and If a polygon intersects plane, split polygon into two and

classify them bothclassify them both

Recurse down the negative half-spaceRecurse down the negative half-space

Recurse down the positive half-spaceRecurse down the positive half-space

Split along the plane defined by any polygon from Split along the plane defined by any polygon from scenescene

Classify all polygons into positive or negative Classify all polygons into positive or negative half-space of the planehalf-space of the plane• If a polygon intersects plane, split polygon into two and If a polygon intersects plane, split polygon into two and

classify them bothclassify them both

Recurse down the negative half-spaceRecurse down the negative half-space

Recurse down the positive half-spaceRecurse down the positive half-space

Page 27: CS 445 / 645 Introduction to Computer Graphics

No bunnies were harmed in my exampleNo bunnies were harmed in my example

But what if a splitting plane passes through an But what if a splitting plane passes through an object?object?• Split the object; give half to each nodeSplit the object; give half to each node

No bunnies were harmed in my exampleNo bunnies were harmed in my example

But what if a splitting plane passes through an But what if a splitting plane passes through an object?object?• Split the object; give half to each nodeSplit the object; give half to each node

Discussion: BSP Tree Cons

Ouch

Page 28: CS 445 / 645 Introduction to Computer Graphics

BSP Demo

Nice demo:Nice demo:

http://http://symbolcraft.com/graphics/bspsymbolcraft.com/graphics/bsp

Page 29: CS 445 / 645 Introduction to Computer Graphics

Summary: BSP TreesPros:Pros:

• Simple, elegant schemeSimple, elegant scheme

• Only writes to framebuffer (no reads to see if current polygon is Only writes to framebuffer (no reads to see if current polygon is in front of previously rendered polygon, i.e., painters algorithm)in front of previously rendered polygon, i.e., painters algorithm)

– Thus very popular for video games (but getting less so)Thus very popular for video games (but getting less so)

Cons:Cons:• Computationally intense preprocess stage restricts algorithm to Computationally intense preprocess stage restricts algorithm to

static scenesstatic scenes

• Slow time to construct treeSlow time to construct tree

• Splitting increases polygon countSplitting increases polygon count

Pros:Pros:• Simple, elegant schemeSimple, elegant scheme

• Only writes to framebuffer (no reads to see if current polygon is Only writes to framebuffer (no reads to see if current polygon is in front of previously rendered polygon, i.e., painters algorithm)in front of previously rendered polygon, i.e., painters algorithm)

– Thus very popular for video games (but getting less so)Thus very popular for video games (but getting less so)

Cons:Cons:• Computationally intense preprocess stage restricts algorithm to Computationally intense preprocess stage restricts algorithm to

static scenesstatic scenes

• Slow time to construct treeSlow time to construct tree

• Splitting increases polygon countSplitting increases polygon count

Page 30: CS 445 / 645 Introduction to Computer Graphics

Octrees

Frequently used in modern video gamesFrequently used in modern video games

• A BSP tree subdivides space into a series of half-spaces A BSP tree subdivides space into a series of half-spaces using single planesusing single planes

• An octree subdivides space into eightAn octree subdivides space into eightvoxels using three axis-aligned planesvoxels using three axis-aligned planes

– A voxel is labeled as havingA voxel is labeled as havingpolygons inside it or notpolygons inside it or not

Frequently used in modern video gamesFrequently used in modern video games

• A BSP tree subdivides space into a series of half-spaces A BSP tree subdivides space into a series of half-spaces using single planesusing single planes

• An octree subdivides space into eightAn octree subdivides space into eightvoxels using three axis-aligned planesvoxels using three axis-aligned planes

– A voxel is labeled as havingA voxel is labeled as havingpolygons inside it or notpolygons inside it or not

www.gamasutra.com/features/19970801/octree.htmwww.gamasutra.com/features/19970801/octree.htmwww.gamasutra.com/features/19970801/octree.htmwww.gamasutra.com/features/19970801/octree.htm

Page 31: CS 445 / 645 Introduction to Computer Graphics

Octrees

• A voxel may have geometry inside it or subdivideA voxel may have geometry inside it or subdivide

– Can have as many as eight childrenCan have as many as eight children

• Thus we partition 3-D space into Thus we partition 3-D space into 3-D cells3-D cells

• Checking visibility with polygons isChecking visibility with polygons isnow faster due to only checkingnow faster due to only checkingparticular cellsparticular cells

• Quadtrees are a 2-D variantQuadtrees are a 2-D variant

• A voxel may have geometry inside it or subdivideA voxel may have geometry inside it or subdivide

– Can have as many as eight childrenCan have as many as eight children

• Thus we partition 3-D space into Thus we partition 3-D space into 3-D cells3-D cells

• Checking visibility with polygons isChecking visibility with polygons isnow faster due to only checkingnow faster due to only checkingparticular cellsparticular cells

• Quadtrees are a 2-D variantQuadtrees are a 2-D variant

Page 32: CS 445 / 645 Introduction to Computer Graphics

Warnock’s Algorithm (1969)

Elegant scheme based on a powerful general approach Elegant scheme based on a powerful general approach common in graphics: common in graphics: if the situation is too complex, if the situation is too complex, subdividesubdivide

• Start with a Start with a root viewportroot viewport and a list of all primitives (polygons) and a list of all primitives (polygons)

• Then recursively:Then recursively:

– Clip objects to viewportClip objects to viewport

– If number of objects incident to viewport is zero or one, visibility is If number of objects incident to viewport is zero or one, visibility is trivialtrivial

– Otherwise, subdivide into smaller viewports, distribute primitives Otherwise, subdivide into smaller viewports, distribute primitives among them, and recurseamong them, and recurse

Elegant scheme based on a powerful general approach Elegant scheme based on a powerful general approach common in graphics: common in graphics: if the situation is too complex, if the situation is too complex, subdividesubdivide

• Start with a Start with a root viewportroot viewport and a list of all primitives (polygons) and a list of all primitives (polygons)

• Then recursively:Then recursively:

– Clip objects to viewportClip objects to viewport

– If number of objects incident to viewport is zero or one, visibility is If number of objects incident to viewport is zero or one, visibility is trivialtrivial

– Otherwise, subdivide into smaller viewports, distribute primitives Otherwise, subdivide into smaller viewports, distribute primitives among them, and recurseamong them, and recurse

Page 33: CS 445 / 645 Introduction to Computer Graphics

Warnock’s Algorithm

What is the What is the terminating terminating condition?condition?

How to determine How to determine the correct visible the correct visible surface in this surface in this case?case?

What is the What is the terminating terminating condition?condition?

How to determine How to determine the correct visible the correct visible surface in this surface in this case?case?

Page 34: CS 445 / 645 Introduction to Computer Graphics

Warnock’s Algorithm

Pros:Pros:

• Very elegant schemeVery elegant scheme

• Extends to any primitive typeExtends to any primitive type

Cons:Cons:

• Hard to embed hierarchical schemes in hardwareHard to embed hierarchical schemes in hardware

• Complex scenes usually have small polygons and high Complex scenes usually have small polygons and high depth depth complexitycomplexity

– Thus most screen regions come down to the single-pixel caseThus most screen regions come down to the single-pixel case

Pros:Pros:

• Very elegant schemeVery elegant scheme

• Extends to any primitive typeExtends to any primitive type

Cons:Cons:

• Hard to embed hierarchical schemes in hardwareHard to embed hierarchical schemes in hardware

• Complex scenes usually have small polygons and high Complex scenes usually have small polygons and high depth depth complexitycomplexity

– Thus most screen regions come down to the single-pixel caseThus most screen regions come down to the single-pixel case

Page 35: CS 445 / 645 Introduction to Computer Graphics

The Z-Buffer Algorithm

Both BSP trees and Warnock’s algorithm were Both BSP trees and Warnock’s algorithm were proposed when memory was expensiveproposed when memory was expensive

• Example: first 512x512 framebuffer > $50,000!Example: first 512x512 framebuffer > $50,000!

Ed Catmull (mid-70s) proposed a radical new Ed Catmull (mid-70s) proposed a radical new approach called approach called z-bufferingz-buffering..

The big idea: resolve visibility The big idea: resolve visibility independently at independently at each pixeleach pixel

Both BSP trees and Warnock’s algorithm were Both BSP trees and Warnock’s algorithm were proposed when memory was expensiveproposed when memory was expensive

• Example: first 512x512 framebuffer > $50,000!Example: first 512x512 framebuffer > $50,000!

Ed Catmull (mid-70s) proposed a radical new Ed Catmull (mid-70s) proposed a radical new approach called approach called z-bufferingz-buffering..

The big idea: resolve visibility The big idea: resolve visibility independently at independently at each pixeleach pixel

Page 36: CS 445 / 645 Introduction to Computer Graphics

The Z-Buffer Algorithm

We know how to rasterize polygons into an image We know how to rasterize polygons into an image discretized into pixels:discretized into pixels:

We know how to rasterize polygons into an image We know how to rasterize polygons into an image discretized into pixels:discretized into pixels:

Page 37: CS 445 / 645 Introduction to Computer Graphics

The Z-Buffer Algorithm

What happens if multiple primitives occupy the What happens if multiple primitives occupy the same pixel on the screen? Which is allowed to same pixel on the screen? Which is allowed to paint the pixel?paint the pixel?

What happens if multiple primitives occupy the What happens if multiple primitives occupy the same pixel on the screen? Which is allowed to same pixel on the screen? Which is allowed to paint the pixel?paint the pixel?

Page 38: CS 445 / 645 Introduction to Computer Graphics

The Z-Buffer Algorithm

Idea: retain depth (Z in eye coordinates) through Idea: retain depth (Z in eye coordinates) through projection transformprojection transform

• Use canonical viewing volumesUse canonical viewing volumes

• Each vertex has z coordinate (relative to eye point) intactEach vertex has z coordinate (relative to eye point) intact

Idea: retain depth (Z in eye coordinates) through Idea: retain depth (Z in eye coordinates) through projection transformprojection transform

• Use canonical viewing volumesUse canonical viewing volumes

• Each vertex has z coordinate (relative to eye point) intactEach vertex has z coordinate (relative to eye point) intact

Page 39: CS 445 / 645 Introduction to Computer Graphics

The Z-Buffer Algorithm

Augment framebuffer with Augment framebuffer with Z-bufferZ-buffer or or depth bufferdepth buffer which stores Z value at each pixelwhich stores Z value at each pixel

• At frame beginning, initialize all pixel depths to At frame beginning, initialize all pixel depths to

• When rasterizing, interpolate depth (Z) across polygon and When rasterizing, interpolate depth (Z) across polygon and store in pixel of Z-bufferstore in pixel of Z-buffer

• Suppress writing to a pixel if its Z value is more distant than Suppress writing to a pixel if its Z value is more distant than the Z value already stored therethe Z value already stored there

Augment framebuffer with Augment framebuffer with Z-bufferZ-buffer or or depth bufferdepth buffer which stores Z value at each pixelwhich stores Z value at each pixel

• At frame beginning, initialize all pixel depths to At frame beginning, initialize all pixel depths to

• When rasterizing, interpolate depth (Z) across polygon and When rasterizing, interpolate depth (Z) across polygon and store in pixel of Z-bufferstore in pixel of Z-buffer

• Suppress writing to a pixel if its Z value is more distant than Suppress writing to a pixel if its Z value is more distant than the Z value already stored therethe Z value already stored there

Page 40: CS 445 / 645 Introduction to Computer Graphics

Interpolating ZEdge equations: Z is just another planar parameter:Edge equations: Z is just another planar parameter:

z = (-D - Ax – By) / Cz = (-D - Ax – By) / C

If walking across scanline by (If walking across scanline by (x) x)

zznewnew = z = zoldold – (A/C)( – (A/C)(x)x)

• Look familiar?Look familiar?

• Total cost:Total cost:

– 1 more parameter to 1 more parameter to increment in inner loopincrement in inner loop

– 3x3 matrix multiply for setup3x3 matrix multiply for setup

Edge walking: just interpolate Z along edges and across spansEdge walking: just interpolate Z along edges and across spans

Edge equations: Z is just another planar parameter:Edge equations: Z is just another planar parameter:

z = (-D - Ax – By) / Cz = (-D - Ax – By) / C

If walking across scanline by (If walking across scanline by (x) x)

zznewnew = z = zoldold – (A/C)( – (A/C)(x)x)

• Look familiar?Look familiar?

• Total cost:Total cost:

– 1 more parameter to 1 more parameter to increment in inner loopincrement in inner loop

– 3x3 matrix multiply for setup3x3 matrix multiply for setup

Edge walking: just interpolate Z along edges and across spansEdge walking: just interpolate Z along edges and across spans

Page 41: CS 445 / 645 Introduction to Computer Graphics

The Z-Buffer Algorithm

How much memory does the Z-buffer use?How much memory does the Z-buffer use?

Does the image rendered depend on the drawing Does the image rendered depend on the drawing order?order?

Does the time to render the image depend on the Does the time to render the image depend on the drawing order?drawing order?

How does Z-buffer load scale with visible How does Z-buffer load scale with visible polygons? With framebuffer resolution?polygons? With framebuffer resolution?

How much memory does the Z-buffer use?How much memory does the Z-buffer use?

Does the image rendered depend on the drawing Does the image rendered depend on the drawing order?order?

Does the time to render the image depend on the Does the time to render the image depend on the drawing order?drawing order?

How does Z-buffer load scale with visible How does Z-buffer load scale with visible polygons? With framebuffer resolution?polygons? With framebuffer resolution?

Page 42: CS 445 / 645 Introduction to Computer Graphics

Z-Buffer Pros

Simple!!!Simple!!!

Easy to implement in hardwareEasy to implement in hardware

Polygons can be processed in arbitrary orderPolygons can be processed in arbitrary order

Easily handles polygon interpenetrationEasily handles polygon interpenetration

EnablesEnables deferred shading deferred shading

• Rasterize shading parameters (e.g., surface normal) and Rasterize shading parameters (e.g., surface normal) and only shade final visible fragmentsonly shade final visible fragments

Simple!!!Simple!!!

Easy to implement in hardwareEasy to implement in hardware

Polygons can be processed in arbitrary orderPolygons can be processed in arbitrary order

Easily handles polygon interpenetrationEasily handles polygon interpenetration

EnablesEnables deferred shading deferred shading

• Rasterize shading parameters (e.g., surface normal) and Rasterize shading parameters (e.g., surface normal) and only shade final visible fragmentsonly shade final visible fragments

Page 43: CS 445 / 645 Introduction to Computer Graphics

Z-Buffer Cons

Lots of memory (e.g. 1280x1024x32 bits)Lots of memory (e.g. 1280x1024x32 bits)

• With 16 bits cannot discern millimeter differences in objects at 1 km distanceWith 16 bits cannot discern millimeter differences in objects at 1 km distance

Read-Modify-Write in inner loop requires fast memoryRead-Modify-Write in inner loop requires fast memory

Hard to do analytic antialiasingHard to do analytic antialiasing

• We don’t know which polygon to map pixel back toWe don’t know which polygon to map pixel back to

Shared edges are handled inconsistentlyShared edges are handled inconsistently

• Ordering dependentOrdering dependent

Hard to simulate translucent polygonsHard to simulate translucent polygons

• We throw away color of polygons behind closest oneWe throw away color of polygons behind closest one

Lots of memory (e.g. 1280x1024x32 bits)Lots of memory (e.g. 1280x1024x32 bits)

• With 16 bits cannot discern millimeter differences in objects at 1 km distanceWith 16 bits cannot discern millimeter differences in objects at 1 km distance

Read-Modify-Write in inner loop requires fast memoryRead-Modify-Write in inner loop requires fast memory

Hard to do analytic antialiasingHard to do analytic antialiasing

• We don’t know which polygon to map pixel back toWe don’t know which polygon to map pixel back to

Shared edges are handled inconsistentlyShared edges are handled inconsistently

• Ordering dependentOrdering dependent

Hard to simulate translucent polygonsHard to simulate translucent polygons

• We throw away color of polygons behind closest oneWe throw away color of polygons behind closest one