computer graphics - gunadarma university

32
Computer Graphics Inf4/MSc Computer Graphics Lecture 5 Hidden Surface Removal and Rasterization Taku Komura 1

Upload: others

Post on 22-Jan-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Computer Graphics Inf4/MSc

Computer Graphics

Lecture 5Hidden Surface Removal and Rasterization

Taku Komura

1

Computer Graphics Inf4/MSc

Hidden surface removal

• Drawing polygonal faces on screen consumes CPU cycles– Illumination

• We cannot see every surface in scenescene– We don’t want to waste time rendering

primitives which don’t contribute to primitives which don t contribute to the final image.

2

Computer Graphics Inf4/MScVisibility (hidden surface removal)

• A correct rendering requires correct visibility g q ycalculations

• Correct visibility• Correct visibility – when multiple opaque polygons cover the same

screen space only the closest one is visiblescreen space, only the closest one is visible (remove the other hidden surfaces)

3– wrong visibility correct visibility

Computer Graphics Inf4/MSc

Visibility of primitives• A scene primitive can be invisible for 3 reasons:

P i iti li t id fi ld f i– Primitive lies outside field of view– Primitive is back-facing

Primitive is occluded by one or more objects nearer the– Primitive is occluded by one or more objects nearer the viewer

4

Computer Graphics Inf4/MSc

Visible surface algorithms.Definitions:

•Object space techniques: applied before vertices are mapped to pixels•Back face culling, Painter’s algorithm, BSP trees

•Image space techniques: applied while the vertices are rasterized•Z buffering•Z-buffering

5

Computer Graphics Inf4/MSc

Back face culling.• The vertices of polyhedra are oriented

in an anticlockwise manner when i d f id f lviewed from outside – surface normal

N points out.• Project a polygon.

– Test z component of surface normal. If negative – cull, since normal points away from viewer.

– Or if N.V > 0 we are viewing the back face so polygon is obscured.

6

Computer Graphics Inf4/MSc

Painters algorithm (object space).• Draw surfaces in back

to front order nearerto front order – nearer polygons “paint” over farther onesfarther ones.

• Supports transparency.• Key issue is order• Key issue is order

determination.• Doesn’t always work• Doesn t always work –

see image at right.

7

Computer Graphics Inf4/MSc

BSP (Binary Space Partitioning) Tree.

•One of class of “list-priority” algorithms – returns ordered list of polygon fragments for specified view point (static pre-processing stage).

5p ( p p g g )

•Choose polygon arbitrarily

Di id i t f t ( l ti t l) d

2

•Divide scene into front (relative to normal) and back half-spaces.

•Split any polygon lying on both sides.

341

p y p yg y g

•Choose a polygon from each side – split scene again.

•Recursively divide each side until each node contains only 1 polygon.

View of scene from above

Computer Graphics Inf4/MSc

BSP Tree.5

•Choose polygon arbitrarily 2

5a5b

p yg y

•Divide scene into front (relative to normal) and back half-spaces.

341

•Split any polygon lying on both sides.

•Choose a polygon from each side – split scene again.

i l di id h id il h

3 backfront

•Recursively divide each side until each node contains only 1 polygon. 125a

45b

19/10/2007Lecture 9 9

Computer Graphics Inf4/MSc

BSP Tree.5

•Choose polygon arbitrarily 2

5a5b

p yg y

•Divide scene into front (relative to normal) and back half-spaces.

341

•Split any polygon lying on both sides.

•Choose a polygon from each side –split scene again.

i l di id h id il h

3 backfront

2•Recursively divide each side until each node contains only 1 polygon.

45b2

15a

front

Computer Graphics Inf4/MSc

BSP Tree.5

•Choose polygon arbitrarily 2

5a5b

p yg y

•Divide scene into front (relative to normal) and back half-spaces.

341

•Split any polygon lying on both sides.

•Choose a polygon from each side – split scene again.

i i i i i

3 backfront2•Recursively divide each side until

each node contains only 1 polygon.2

15a

front

5b

4

Computer Graphics Inf4/MSc

Displaying a BSP tree.

• Once we have the regions – need priority list• BSP tree can be traversed to yield a correct priority list for

an arbitrary viewpoint.S l• Start at root polygon.

– If viewer is in front half-space, draw polygons behind root first, then the root polygon then polygons in frontthen the root polygon, then polygons in front.

– If polygon is on edge – either can be used.– Recursively descend the tree. y

• If eye is in rear half-space for a polygon – then can back face cull.

19/10/2007Lecture 9 12

Computer Graphics Inf4/MSc

BSP Tree.• A lot of computation required at start.

– Try to split polygons along good dividing plane– Try to split polygons along good dividing plane– Intersecting polygon splitting may be costly

Ch t h k i ibilit t i t• Cheap to check visibility once tree is set up.• Can be used to generate correct visibility for g y

arbitrary views. Efficient when objects don’t change veryEfficient when objects don t change very

often in the scene.

19/10/2007Lecture 9 13

Computer Graphics Inf4/MSc

BSP performance measure

• Tree construction and traversal (object-space ordering algorithm good for relatively few staticordering algorithm – good for relatively few static primitives, precise)

• Overdraw: maximum• Overdraw: maximum• Front-to-back traversal is more efficient

• Record which region has been filled in already

• Terminate when all regions of the screen is filled gin

• S. Chen and D. Gordon. “Front-to-Back Display of BSP Trees.” IEEE

19/10/2007Lecture 9 14Computer Graphics & Algorithms, pp 79–85. September 1991.

Computer Graphics Inf4/MSc

Z-buffering : image space approach

Basic Z-buffer idea:i i l• rasterize every input polygon

• For every pixel in the polygon interior, calculate i di l (b i l i )its corresponding z value (by interpolation)

• Track depth values of closest polygon (smallest z) fso far

• Paint the pixel with the color of the polygon h l i h l hwhose z value is the closest to the eye.

15

Computer Graphics Inf4/MSc

16

Computer Graphics Inf4/MSc

17

Computer Graphics Inf4/MSc

18

Computer Graphics Inf4/MSc

19

Computer Graphics Inf4/MSc

Implementation.

• Initialise frame buffer to background colour.• Initialise depth buffer to z = max. value for

far clipping planefar clipping plane• For each triangle

– Calculate value for z for each pixel inside– Update both frame and depth bufferp p

20

Computer Graphics Inf4/MSc

Filling in Trianglesg g• Scan line algorithm

– Filling in the triangle by drawing horizontal lines from top to bottom

• Barycentric coordinates– Checking whether a pixel is inside / outside the

triangle

Computer Graphics Inf4/MSc

Triangle Rasterization

• Consider a 2D triangle with vertices p0 , p1 , p2. L b i i h l W l• Let p be any point in the plane. We can always find a, b, c such that

• We will have if and only if p is inside the triangle. g

• We call the barycentric coordinates of p.

22

Computer Graphics Inf4/MSc

C ti th b i t iComputing the baricentric coordinates of the interior pixelsp

• The triangle is composed of 3 points p0 (x0,y0), p1 (x1, y1), p2(x2,y2)

• (α,β,γ) : barycentric coordinates• Only if 0<α,β,γ<1, (x,y) is inside the triangle• Depth can be computed by αZ0 + βZ1 +γZ2

23

• Can do the same thing for color, normals, textures

Computer Graphics Inf4/MSc

Bounding box of the triangle• First, identify a rectangular region on the

canvas that contains all of the pixels in the ptriangle (excluding those that lie outside the canvas).

• Calculate a tight bounding box for a triangle: simply calculate pixel coordinates for each p y pvertex, and find the minimum/maximum for each axis

24

Computer Graphics Inf4/MSc

Scanning inside the triangle• Once we've identified the bounding box,

we loop over each pixel in the box.we loop over each pixel in the box.• For each pixel, we first compute the

corresponding (x, y) coordinates in thecorresponding (x, y) coordinates in the canonical view volume

• Next we convert these into barycentricNext we convert these into barycentric coordinates for the triangle being drawn.

• Only if the barycentric coordinatesOnly if the barycentric coordinatesare within the range of [0,1], we

l t it ( d t th d th)25

plot it (and compute the depth)

Computer Graphics Inf4/MSc

Why is z-buffering so popular ?Advantage• Simple to implement in hardware.

– Memory for z-buffer is now not expensive• Diversity of primitives – not just polygons.• Unlimited scene complexityUnlimited scene complexity• Don’t need to calculate object-object intersections.Disadvantage

d b d id h• Extra memory and bandwidth• Waste time drawing hidden objectsZ-precision errorsp• May have to use point sampling

26

Computer Graphics Inf4/MSc

Z-buffer performance• Brute-force image-space algorithm scores best for

l i l d icomplex scenes – easy to implement and is very general.

• Storage overhead: O(1)• Time to resolve visibility to screen precision: O(n)y p ( )

– n: number of polygons

27

Computer Graphics Inf4/MSc

Ex. Architectural scenesHere there can be an enormous amount of occlusion

19/10/2007Lecture 9 28

Computer Graphics Inf4/MSc

Occlusion at various levels

19/10/2007Lecture 9 29

Computer Graphics Inf4/MSc

Portal Culling (object-space)

Model scene as a graph:BC

D

E

F

Model scene as a graph:• Nodes: Cells (or rooms)• Edges: Portals (or doors)

A G

Graph gives us:P t ti ll i ibl t• Potentially visible set

1 Render the roomA

1.Render the room2.If portal to the next room is visible, render the connected room in the portal region

B

DC E

19/10/2007303.Repeat the process along the scene graph

Computer Graphics Inf4/MSc

Summary

•Z-buffer is easy to implement on hardware and is an important toolimportant tool

•We need to combine it with an object-based method jespecially when there are too many polygons

BSP trees, portal culling

Computer Graphics Inf4/MSc

References for hidden surface removal• Foley et al. Chapter 15, all of it.

d Ch 13 ll f i• Introductory text, Chapter 13, all of it• Baricentric coordinatesBaricentric coordinates

www.cs.caltech.edu/courses/cs171/barycentric pdfric.pdf

• Or equivalents in other texts, look out for:– (as well as the topics covered today) – Depth sort – Newell Newell & Sancha

32

Depth sort Newell, Newell & Sancha– Scan-line algorithms