Download presentation

Presentation is loading. Please wait.

Published byLouise Mills Modified over 6 years ago

1
3/2/04© University of Wisconsin, CS559 Spring 2004 Last Time General Perspective –Methods for specifying the view volume As a set of clip planes As a field of view and aspect ratio –Near and Far clip planes and depth resolution Sutherland-Hodgman Clipping –For clipping points, lines or polygons against convex clip regions –Clip against each clip plane in turn –Rewrite vertex lists

2
3/2/04© University of Wisconsin, CS559 Spring 2004 Today More clipping –Cohen-Sutherland –Liang-Barsky –Project 2 clipping –Weiler-Atherton –Weiler Drawing lines

3
3/2/04© University of Wisconsin, CS559 Spring 2004 Clipping Lines Lines can be clipped by Sutherland-Hodgman –Slower than necessary, unless you already have hardware Better algorithms exist –Cohen-Sutherland –Liang-Barsky –Nicholl-Lee-Nicholl (we won’t cover this one – only good for 2D)

4
3/2/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland (1) Works basically the same as Sutherland-Hodgman –Was developed earlier Clip line against each edge of clip region in turn –If both endpoints outside, discard line and stop –If both endpoints in, continue to next edge (or finish) –If one in, one out, chop line at crossing pt and continue Works in both 2D and 3D for convex clipping regions

5
3/2/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland (2) 12 3 4 12 3 4 3 4 12 3 4 12

6
3/2/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland (3) Some cases lead to premature acceptance or rejection –If both endpoints are inside all edges –If both endpoints are outside one edge General rule of clipping – if a fast test can cover many cases, do it first

7
3/2/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland - Details Only need to clip line against edges where one endpoint is out Use outcode to record endpoint in/out of each edge. One bit per edge, 1 if out, 0 if in –Codes can be computed very quickly – they are not independent for rectangular clip regions Trivial reject: –outcode(x1) & outcode(x2)!=0 Trivial accept: –outcode(x1) | outcode(x2)==0 Which edges to clip against? –outcode(x1) ^ outcode(x2) 12 3 4 0010 0101

8
3/2/04© University of Wisconsin, CS559 Spring 2004 Liang-Barsky Clipping Parametric clipping - view line in parametric form and reason about the parameter values –Parametric form: x = x 1 +(x 2 -x 1 )t –t [0,1] are points between x 1 and x 2 Liang-Barsky is more efficient than Cohen-Sutherland –Computing intersection vertices is most expensive part of clipping –Cohen-Sutherland may compute intersection vertices that are later clipped off, and hence don’t contribute to the final answer Works for convex clip regions in 2D or 3D

9
3/2/04© University of Wisconsin, CS559 Spring 2004 Paramteric Clipping Recall, points inside a convex region are inside all clip planes Parametric clipping finds the values of t, the parameter, that correspond to points inside the clip region Consider a rectangular clip region Line is inside clip region for values of t such that (for 2D): x min x max y min y max

10
3/2/04© University of Wisconsin, CS559 Spring 2004 Parametric Intersection Infinite line intersects clip region edges when: where

11
3/2/04© University of Wisconsin, CS559 Spring 2004 Parametric Intersection t bottom t left t top t right

12
3/2/04© University of Wisconsin, CS559 Spring 2004 Entering or Leaving? When p k <0, as t increases line goes from outside to inside - entering When p k >0, line goes from inside to outside – leaving When p k =0, line is parallel to an edge (clipping is easy) Major Insight: The infinite line must not move outside a clip plane (leave) before it moves inside another (enters) –If it does leave before it enters, it cannot be inside all the planes For rectangular, if there is a segment of the line inside the clip region, sequence of infinite line intersections must go: enter, enter, leave, leave

13
3/2/04© University of Wisconsin, CS559 Spring 2004 Entering and Leaving Enter Leave Enter Leave Enter Leave

14
3/2/04© University of Wisconsin, CS559 Spring 2004 Liang-Barsky - Algorithm Compute entering t values, which are q k /p k for each p k <0 –There will always be two for 2D rectangular clip region, three in 3D Compute leaving t values, which are q k /p k for each p k >0 Parameter value for small t end of line is: t small = max(0, entering t’s) Parameter value for large t end of line is: t large =min(1, leaving t’s) If t small <t large, there is a line segment - compute endpoints by substituting t values Improvement (and actual Liang-Barsky): –compute t’s for each edge in turn (some rejects occur earlier like this)

15
3/2/04© University of Wisconsin, CS559 Spring 2004 General Liang-Barsky Liang-Barsky works for any convex clip region –Compute intersection t for all clip lines/planes and label them as entering or exiting –Parameter value for small t end of line is:t small = max(0, entering t’s) –Parameter value for large t end of line is: t large =min(1, leaving t’s) –if t small <t large, there is a line segment - compute endpoints by substituting t values

16
3/2/04© University of Wisconsin, CS559 Spring 2004 In View Space For Project 2, you need to clip edges to a view frustum in world space Situation is: eye, e frustum x right x left x1x1 x2x2

17
3/2/04© University of Wisconsin, CS559 Spring 2004 First Step Determine which side of each clip plane the segment endpoints lie –Use the cross product –What do we know if (x 1 - e) (x left - e) > 0 ? –Other cross products give other information What can we say if both segment endpoints are outside one clip plane? –Stop here if we can, otherwise…

18
3/2/04© University of Wisconsin, CS559 Spring 2004 Finding Parametric Intersection Left clip edge: x = e + (x left - e) t Line: x = x 1 + (x 2 - x 1 ) s Solve simultaneous equations in t and s: Use endpoint inside/outside information to label as entering or leaving Now we have general Liang-Barsky case

19
3/2/04© University of Wisconsin, CS559 Spring 2004 Weiler Atherton Polygon Clipping Faster than Sutherland- Hodgman for complex polygons For clockwise polygon: –for out-to-in pair, follow usual rule –for in-to-out pair, follow clip edge Easiest to start outside

20
3/2/04© University of Wisconsin, CS559 Spring 2004 General Clipping Clipping general against general polygons is quite hard Outline of Weiler algorithm: –Replace crossing points with vertices –Double all edges and form linked lists of edges –Change links at vertices –Enumerate polygon patches

21
3/2/04© University of Wisconsin, CS559 Spring 2004 Weiler Algorithm (1)

22
3/2/04© University of Wisconsin, CS559 Spring 2004 Changes to Rearranging pointers makes it possible to enumerate all components of the intersection

23
3/2/04© University of Wisconsin, CS559 Spring 2004 Where We Stand At this point we know how to: –Convert points from local to screen coordinates –Clip polygons and lines to the view volume Next thing: –Determine which pixels are covered by any given point, line or polygon

24
3/2/04© University of Wisconsin, CS559 Spring 2004 Where We Stand At this point we know how to: –Convert points from local to screen coordinates –Clip polygons and lines to the view volume Next thing: –Determine which pixels are covered by any given point, line or polygon

25
3/2/04© University of Wisconsin, CS559 Spring 2004 Drawing Points When points are mapped into window coordinates, they could land anywhere – not just at a pixel center Solution is the simple, obvious one –Map to window space –Fill the closest pixel –Can also specify a radius – fill a square of that size, or fill a circle Square is faster What function are we sampling with?

26
3/2/04© University of Wisconsin, CS559 Spring 2004 Drawing Lines Task: Decide which pixels to fill (samples to use) to represent a line We know that all of the line lies inside the visible region (clipping gave us this!) Issues: –If slope between -1 and 1, one pixel per column. Otherwise, one pixel per row –Constant brightness? Lines of the same length should light the same number of pixels (we normally ignore this) –Anti-aliasing? (Getting rid of the “jaggies”) –Sampling theory?

27
3/2/04© University of Wisconsin, CS559 Spring 2004 Line Drawing Algorithms Consider lines of the form y=m x + c, where m= y/ x, 0<m<1, integer coordinates –All others follow by symmetry Variety of slow algorithms (Why slow?): –step x, compute new y at each step by equation, rounding: –step x, compute new y at each step by adding m to old y, rounding:

28
3/2/04© University of Wisconsin, CS559 Spring 2004 Bresenham’s Algorithm Overview Aim: For each x, plot the pixel whose y-value is closest to the line Given (x i,y i ), must choose from either (x i +1,y i +1) or (x i +1,y i ) Idea: compute a decision variable –Value that will determine which pixel to draw –Easy to update from one pixel to the next Bresenham’s algorithm is the midpoint algorithm for lines –Other midpoint algorithms for conic sections (circles, ellipses)

29
3/2/04© University of Wisconsin, CS559 Spring 2004 yiyi y i +1 x i +1 Midpoint Methods Consider the midpoint between (x i +1,y i +1) and (x i +1,y i ) If it’s above the line, we choose (x i +1,y i ), otherwise we choose (x i +1,y i +1) xixi Choose (x i +1,y i ) yiyi y i +1 x i +1xixi Choose (x i +1,y i +1)

30
3/2/04© University of Wisconsin, CS559 Spring 2004 Midpoint Decision Variable Write the line in implicit form: The value of F(x,y) tells us where points are with respect to the line –F(x,y)=0: the point is on the line –F(x,y)<0: The point is above the line –F(x,y)>0: The point is below the line The decision variable is the value of d i = 2F(x i +1,y i +0.5) –The factor of two makes the math easier

31
3/2/04© University of Wisconsin, CS559 Spring 2004 What Can We Decide? d i negative => next point at (x i +1,y i ) d i positive => next point at (x i +1,y i +1) At each point, we compute d i and decide which pixel to draw How do we update it? What is d i+1 ?

32
3/2/04© University of Wisconsin, CS559 Spring 2004 Updating The Decision Variable d k+1 is the old value, d k, plus an increment: If we chose y i+1 =y i +1: If we chose y i+1 =y i : What is d 1 (assuming integer endpoints)? Notice that we don’t need c any more

33
3/2/04© University of Wisconsin, CS559 Spring 2004 Bresenham’s Algorithm For integers, slope between 0 and 1: –x=x 1, y=y 1, d=2dy - dx, draw (x, y) –until x=x 2 x=x+1 If d>0 then { y=y+1, draw (x, y), d=d+2 y - 2 x } If d<0 then { y=y, draw (x, y), d=d+2 y } Compute the constants (2 y-2 x and 2 y ) once at the start –Inner loop does only adds and comparisons Floating point has slightly more difficult initialization, but is otherwise the same Care must be taken to ensure that it doesn’t matter which order the endpoints are specified in (make a uniform decision if d==0)

34
3/2/04© University of Wisconsin, CS559 Spring 2004 Midterm Info March 11 in class All material up to exam time –Topics list will be posted online –Look at summary at start of each lecture Allowed: –Pencil/pen, ruler –One sheet of letter (standard) sized paper, with anything on it, both sides Nothing to increase the surface area Not allowed: –Calculator, anything else

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google