You are on page 1of 23

COMP 175 | COMPUTER GRAPHICS

Lecture 15:

Clipping
COMP 175: Computer Graphics November 1, 2011
Remco Chang 15 – Clipping 1/22

COMP 175 | COMPUTER GRAPHICS

Discussions

Library trip Camera assignment
 

problems debugging methods

Midterm Eval General discussion on how we can do better
15 – Clipping 2/22

Remco Chang

COMP 175 | COMPUTER GRAPHICS Clipping  Last week we discussed some simple examples of clipping in Camera  We said that OpenGL takes care of a lot of that for free  But clipping is an important part of computer graphics: Remco Chang 15 – Clipping 3/22 .

y) Remco Chang 15 – Clipping 4/22 . ymax) (x.COMP 175 | COMPUTER GRAPHICS Line Clipping  (xmin . ymin) (xmax .

COMP 175 | COMPUTER GRAPHICS Parametric Line Formulation for Clipping  Remco Chang 15 – Clipping 5/22 .

COMP 175 | COMPUTER GRAPHICS Cohen-Sutherland Line Clipping in 2D  Clip Rectangle Remco Chang 15 – Clipping 6/22 .

then we do divide-and-conquer Subdivide line into two segments and test again C B I A H G F Clip rectangle E     Use a clip edge to cut line Use outcodes to choose which edge is crossed  The bits that are different between outcodes will tell us which edge to examine Pick an order for checking edges: top – bottom – right – left Compute the intersection point   Clip edge will be axis-aligned.. E-I at H) 15 – Clipping 7/22 Remco Chang .g.COMP 175 | COMPUTER GRAPHICS Cohen-Sutherland Algorithm D   If we can neither trivially accept or reject. might need multiple passes (e. so we can fix either the x or the y Can substitute into the line equation  Iterate for the newly created line segment.

y0. y1 = y. ComputeOutCode(x1. repeat check for trivial reject or trivial accept pick the point that is outside the clip rectangle if TOP then x = x0 + (x1 – x0) * (ymax – y0)/(y1 – y0). y0 is the outer point) then x0 = x. y0. x = xmax. else if LEFT then y = y0 + (y1 – y0) * (xmin – x0)/(x1 – x0). y = ymax. outcode0). if (x0. y0. xmax. ComputeOutCode(x1. y1. else if BOTTOM then x = x0 + (x1 – x0) * (ymin – y0)/(y1 – y0). outcode1) until done DrawRectangle(xmin. y1. y0. x1. ComputeOutCode(x0. else if RIGHT then y = y0 + (y1 – y0) * (xmax – x0)/(x1 – x0). ymin. outcode1). y1) ComputeOutCode(x0. x1. y1) Remco Chang 15 – Clipping 8/22 . ymax) DrawLine (x0. x = xmin.COMP 175 | COMPUTER GRAPHICS Cohen-Sutherland Algorithm  Algorithm (float x0. outcode0) else x1 = x. y = ymin. y0 = y.

COMP 175 | COMPUTER GRAPHICS Cohen-Sutherland Algorithm in 3D    Similar to 2D Divide volume into 27 regions (picture a Rubik’s cube) 6-bit outcode records results of 6 bounds tests    1st / 2nd bits: back & front planes 3rd / 4th bits: top & bottom planes 5th / 6th bits: right & left planes  The same operation as the 2D case in using outcodes Back plane 000000 (in front) Front plane 010000 (in front) Top plane 001000 (above) 000000 (below) 100000 (behind) 000000 (behind) Bottom plane 000000 (above) 000100 (below) Right plane 000000 (to left of) 000010 (to right of) Left plane 000001 (to left of) 000000 (to right of) Remco Chang 15 – Clipping 9/22 .

COMP 175 | COMPUTER GRAPHICS Sutherland-Hodgman Polygon Clipping Remco Chang 15 – Clipping 10/22 .

COMP 175 | COMPUTER GRAPHICS Cyrus-Beck / Liang-Barsky Parametric Line Clipping  Remco Chang 15 – Clipping 11/22 .

COMP 175 | COMPUTER GRAPHICS Parametric Line Clipping  Remco Chang 15 – Clipping 12/22 .

Using Dot Product  Find t  Note that if t is less than 0 or greater than 1. then the intersection occurs outside of the line segment 15 – Clipping 13/22 Remco Chang .COMP 175 | COMPUTER GRAPHICS Parametric Line Clipping.

Solving t  Remco Chang 15 – Clipping 14/22 .COMP 175 | COMPUTER GRAPHICS Parametric Line Clipping.

the normal should not be 0. it must be that:    Ni  0 (that is.COMP 175 | COMPUTER GRAPHICS Parametric Line Clipping. if they are. Solving t  solve for t:  t = Ni • [P0 – PE ] / (-Ni • D) i  Caveats: for this equation to be true. P1  P0) Ni • D  0 (edge Ei and line D are not parallel. no intersection)  The algorithm needs to check for these 15 – Clipping 15/22 Remco Chang . this could occur only as a mistake) D  0 (that is.

how can you tell?? 15 – Clipping 16/22 Remco Chang . 1] Determine if the intersection is considered potentially entering (PE) or potentially leaving (PL)  Problem. Against A Polygon PL PE  When applied to line-clipping. there will be multiple t values (a t value for each clip edge test)   Eliminate t values outside of [0.COMP 175 | COMPUTER GRAPHICS Parametric Line Clipping.

Against A Polygon  Remco Chang 15 – Clipping 17/22 .COMP 175 | COMPUTER GRAPHICS Parametric Line Clipping.

if PE then tE = max(tE. tL = 1.t). if PL then tL = min(tL. end if tE > tL then return nil else return P(tE) and P(tL) as true clip intersections end Remco Chang 15 – Clipping 18/22 . else begin tE = 0. for each candidate intersection with a clip edge if Ni • D  0 then {Ignore edges parallel to line} begin calculate t. {of line and clip edge intersection} use sign of Ni • D to categorize as PE or PL. for each line segment to be clipped if P1 = P0 then line is degenerate so clip as a point.COMP 175 | COMPUTER GRAPHICS Pseudocode Pre-calculate Ni and select PEi for each edge.t).

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 15 – Clipping 19/22 .

and same or similar algorithms can be applied 15 – Clipping 20/22   Remco Chang .COMP 175 | COMPUTER GRAPHICS Other Types of Clipping  The intersection test can be extended to 3D Here we try to find the intersection point (I) between a line segment (AC) and a cube Problem is the same as the 2D one.

it is similar to the Cyrus-Beck algorithm. but in 3D 15 – Clipping 21/22   Remco Chang . points a and b) Since clipping works with line segments..COMP 175 | COMPUTER GRAPHICS Other Types of Clipping  Clipping works on edges of triangles (see right) New vertices might have to be created (e.g.

COMP 175 | COMPUTER GRAPHICS Sutherland-Hodgman Polygon Clipping  Keeps polygon info intact Remco Chang 15 – Clipping 22/22 .

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 15 – Clipping 23/22 .