CS 112 -Clippinggraphics.ics.uci.edu/CS112/lectures/clipping.pdf · 2018-02-14 · Cohen-Sutherland...

22
Slide 1 CS 112 - Clipping

Transcript of CS 112 -Clippinggraphics.ics.uci.edu/CS112/lectures/clipping.pdf · 2018-02-14 · Cohen-Sutherland...

Slide 1

CS 112 - Clipping

Slide 2

Clipping

n Removing primitives (lines, polygons) that are not visible

n Can take place in different stagesn In image space

n OpenGL does only this clippingn In object space

n Several methods

Slide 3

Window Coordinates

n After projection we are in normalized device coordinates

n Convert to window coordinates

-1 +1

+1

-1

Screen

xmin xmax

ymin

ymax

Slide 4

Line Clipping

n Accept ABn Reject (Cull) CDn Clip EF

n One endpoint outside the window

n Clip GHn Both endpoints

outside the window

G

H

A

BE

F

C

DI

J

Slide 5

Cohen-Sutherland Clipping

n Divide the window in nine regions marked by binary codes

x = xmin x = xmax

y = ymax

y = ymin

b1 b2 b3 b4

y>ymax y<ymin x>xmax x<xmin

1000

0100

0000

1010

0110

0010

1001

0101

0001

Slide 6

Cohen-Sutherland Clipping

n Find binary codes of two endpoints (C1, C2)n C1= C2= 0

n Accept the line, both endpoints inside the windown C1=0, C2= 0

n One endpoint outside the windown Nonzero bits give the lines with which to intersectn Maximum two intersection to get the clipped line

Slide 7

Cohen-Sutherland Clippingn Find binary codes of two endpoints (C1, C2)

n C1& C2= 0n Both endpoints outside the same edge of the

windown Cull completely

n C1 & C2= 0n Both endpoints outside, but different edgesn Find the first intersection and find its binary coden Apply recursively on this culled line

n All these carried out in ordern Boolean operations, intersections if needed

Slide 8

Cohen-Sutherland Clipping

n Advantagen Most lines can be eliminated based on codesn Can be easily extended to 3Dn Plane-line intersection instead if line-line

intersectionn Disadvantage

n Has to be applied recursively

Slide 9

Extending it to 3D

n How many bits for codes? How many codes?n Plane-line intersection

n p(α) = p1+α(p2-p1)n n.(p(α)-p0) = 0n α = n. (p1-p0)

n. (p2-p1)p1

p2

p(α)

p0

n

Slide 10

Liang-Barsky Clipping

n Take the parametric equation of the linen Find intersection with four lines of

windowsn Order the alphas

Slide 11

Liang-Barsky Clipping

n 1 > α4 > α3 > α2 > α1 > 0n Line meets l before tn Line between α2 and α3 is

inside the windown 1 > α4 > α2 > α3 > α1 > 0

n Line meets t before ln Whole line is outsiden Reject completely

α1

α2

α3α4

α1

α2α3

α4

Slide 12

Efficiency Improvements

n Compute intersections one by onen May need less than four intersections to reject

n Compare without floating point divisionn If (α2 < α3) then (ymax-y1)(x2-x1) <(xmin-x1)(y2-y1)

Slide 13

Polygon Clipping

n Convex polygons clipped to a single polygon

n Concave polygonsn Clip and join to a

single polygonn Tessalate and clip

triangles

Slide 14

Clipping Convex Polygons

n Sutherland Hodgemann Subproblem

n Input: vertex list (polygon) and a clipping linen Output: vertex list (clipped polygon)

n In a pipeline for 4 clipping lines

Slide 15

Sutherland Hodgeman

n To clip vertex list against the linen Test first vertex, Output if inside else skipn Then loop through the list, testing transitions

n In-to-out: Output intersectionn In-to-in: Output vertexn Out-to-in: Output intersection and vertexn Out-to-out: Output nothing

n Can form a pipelinen Process vertex list concurrently

n Can be extended to 3D easily (line-plane intersection)

Slide 16

Bounding Boxes and Volumes

n Polygon clipping is overkill if entire polygon outside the window

n Maintain a bounding boxn Axis-aligned

n Can be a big savingsn Can be easily extended to

3Dn For volumes in object-space

Slide 17

View Frustum Cullingn Preprocessing: Spatial Subdivision

n Octree subdivision:Hierarchical Structuren Each box has a list of polygons inside itn An empty box is the leaf node

n If completely inside the view frustumn Accept

n If completely outside the view frustumn Reject

n If intersects the view frustumn Go through the children recursively

Slide 18

View Frustum Culling

n What happens when a triangle spans across a box?n Split the trianglen Include it in both boxes

n Screen space clipping takes care of it

n Octree is suboptimal divisionn Other methods control depth of the tree

Slide 19

Hidden Surface Removal

n Object Space Approachn Back Face Cullingn Painter’s Algorithm

n Image Space Approachn Z-buffer algorithm

Slide 20

Back Face Culling

n Do not want to render back facing polygons

n If the normal is pointed towards the viewern -90 ≤ θ ≤ 90n Cos (θ) ≥ 0n n.v ≥ 0

n Viewing in -z n Culled if normal has negative z

v

n

θ

Slide 21

Painter’s Algorithm

n Depth Sortn For back to front rendering

n Problems when polygons overlap in zn Can check overlap in x and y direction using

bounding boxesn Order can be found if no such overlap

n If also overlaps in x and yn Split polygons

Slide 22

Z-buffer

n Store z in screen space during projectionn Interpolate 1/z during rasterization n Find reciprocal to get correct themn Check less thann Then overwrite pixel if true