Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives Introduce basic implementation strategies Clipping Scan conversion.

Similar presentations


Presentation on theme: "Objectives Introduce basic implementation strategies Clipping Scan conversion."— Presentation transcript:

1 Objectives Introduce basic implementation strategies Clipping Scan conversion

2 Major tasks (1) Modeling: results in sets of vertices that specify a group of geometric objects supported by the rest of the system ­E.g. GL_POLYGON in OpenGL ­Modeler is usually an application program that may be interactive

3 Major tasks (2) Geometry Processing: works with vertices involves a number of transformations

4 Modeling transformations Viewing transformation Projection transformation Clipping Perspective division NDC to viewport (window coordinates) 3D WORLD 3D World 3D camera 3D clip 3D NDC 2D window (with depth) “Normalized View Volume”

5 Projection Transformation OpenGL transforms vertices by a projection transformation to a normalized view volume (cube centered at origin, with side length 2). These coordinates are referred to as normalized device coordinates No matter how the viewing volume was specified by the user (e.g. glOrtho or gluPerspective)

6 Perspective Projection z y Z=-near Z=-far z y +1 +1 (far) -1(near) Viewing frustum Normalized view volume viewer

7 Normalized Device Coordinates (x,y) coordinates indicate the projected point on the final viewing window. ­Orthogonal projection of the distorted shape in the normalized volume is equivalent to perspective projection of the original shape z coordinate is used for depth ­There is a reversal of z coordinates: the points closest to the viewer have smaller z coordinates ­Z coordinates went through a nonlinear shortening, however relative order of z coordinates are maintained—that is sufficient for hidden surface removal

8 Why normalization? To simplify clipping ­Now primitives are clipped with respect to the same shape! ­In fact, clipping takes place before the perspective division (division by w): still an axis aligned rectangular box. To avoid division operation for parts that are not going to be visible in the window at the end! Clip coordinates

9 Clipping 2D against clipping window 3D against clipping volume Easy for line segments polygons Hard for curves and text ­Convert to lines and polygons first

10 Clipping 2D Line Segments Brute force approach: compute intersections with all sides of clipping window ­Inefficient: one division per intersection

11 Cohen-Sutherland Algorithm Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window x = x max x = x min y = y max y = y min

12 The Cases Case 1: both endpoints of line segment inside all four lines ­Draw (accept) line segment as is Case 2: both endpoints outside all lines and on same side of a line ­Discard (reject) the line segment x = x max x = x min y = y max y = y min

13 The Cases Case 3: One endpoint inside, one outside ­Must do at least one intersection Case 4: Both outside ­May have part inside ­Must do at least one intersection x = x max x = x min y = y max

14 Defining Outcodes For each endpoint, define an outcode Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions b0b1b2b3b0b1b2b3 b 0 = 1 if y > y max, 0 otherwise b 1 = 1 if y < y min, 0 otherwise b 2 = 1 if x > x max, 0 otherwise b 3 = 1 if x < x min, 0 otherwise

15 Using Outcodes Consider the 5 cases below AB: outcode(A) = outcode(B) = 0 ­Accept line segment

16 Using Outcodes CD: outcode (C) = 0, outcode(D)  0 ­Compute intersection ­Location of 1 in outcode(D) determines which edge to intersect with ­Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two intersections

17 Using Outcodes EF: outcode(E) logically ANDed with outcode(F) (bitwise)  0 ­Both outcodes have a 1 bit in the same place ­Line segment is outside of corresponding side of clipping window ­reject

18 Using Outcodes GH and IJ: same outcodes, neither zero but logical AND yields zero Shorten line segment by intersecting with one of sides of window Compute outcode of intersection (new endpoint of shortened line segment) Reexecute algorithm

19 Efficiency In many applications, the clipping window is small relative to the size of the whole data base ­Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step

20 Cohen Sutherland in 3D Use 6-bit outcodes When needed, clip line segment against planes

21 Liang-Barsky Clipping Consider the parametric form of a line segment (works in both 2- and 3-dimensions) We can distinguish between the cases by looking at the ordering of the values of  where the line determined by the line segment crosses the lines that determine the window p(  ) = (1-  )p 1 +  p 2 1  0 p1p1 p2p2

22 Liang-Barsky Clipping In (a):  4 >  3 >  2 >  1 ­Intersect right, top, left, bottom: shorten In (b):  4 >  2 >  3 >  1 ­Intersect right, left, top, bottom: reject

23 Liang-Barsky Clipping Efficient implementation requires avoiding computing intersections until they are needed. We also want to avoid divisions when possible.

24 Liang-Barsky Clipping The intersection with top line (y= ymax) is at Rewrite equation as It is possible to restate all the rules in terms of

25 Liang-Barsky Clipping Many lines can be rejected before all 4 intersections are known. ­E.g. if, reject

26 Liang-Barsky Clipping Can accept/reject as easily as with Cohen-Sutherland Using values of , we do not have to use algorithm recursively as with C-S Extends to 3D ­We are now clipping lines against planes (six of them)

27 Plane-Line Intersections

28 Plane-line intersection The above equation requires six multiplications and divisions! However, the normalized viewing volume has axis aligned planes ­Reduces to a single division just like in 2D. E.g.

29 Polygon Clipping Not as simple as line segment clipping ­Clipping a line segment yields at most one line segment ­Clipping a polygon can yield multiple polygons However, clipping a convex polygon can yield at most one other polygon

30 Tessellation and Convexity Either do not allow nonconvex polygons or replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation)

31 Clipping as a Black Box Can consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment

32 Pipeline Clipping of Line Segments Clipping against each side of window is independent of other sides ­Can use four independent clippers in a pipeline

33 Pipeline Clipping of Polygons Clip edges of polygons successively Three dimensions: add front and back clippers Strategy used in SGI Geometry Engine Small increase in latency

34 Therefore clipped polygon is P, Q, R, S. Clipping to One Boundary Consider each polygon edge in turn Four cases: ­ENTER: ­STAY IN: ­LEAVE: ­STAY OUT: inside P Q Output P, Q R Output R S Output S (no output)

35 Clipping Example Works for more nonconvex shapes. Input Case Output 1 start - 2 stay in 2 3 leave A 4 stay out - 5 enter B, 5 6 leave C 7 stay out - 1 enter D, 1 2 A B 5 C D 1 1 4 6 5 3 2 7 A D C B inside

36 Bounding Boxes Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent ­Smallest rectangle aligned with axes that encloses the polygon ­Simple to compute: max and min of x and y

37 Bounding boxes Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping

38 Rasterization Rasterization (scan conversion) ­Shade pixels that are inside an object specified by a set of vertices Line segments Polygons: scan conversion = fill Shades determined by color, texture, shading model Here we study algorithms for determining the correct pixels starting with the vertices

39 Scan Conversion of Line Segments Start with line segment in window coordinates with integer values for endpoints Assume implementation has a write_pixel function y = mx + h

40 DDA Algorithm Digital Differential Analyzer ­DDA was a mechanical device for numerical solution of differential equations ­Line y=mx+ h satisfies differential equation dy/dx = m =  y/  x = y 2 -y 1 /x 2 -x 1 Along scan line  x = 1 For(x=x1; x<=x2, x++) { y+=m; write_pixel(x, round(y), line_color) }

41 Problem DDA = for each x plot pixel at closest y ­Problems for steep lines

42 Using Symmetry Use for 1  m  0 For m > 1, swap role of x and y ­For each y, plot closest x

43 Bresenham’s Algorithm DDA requires one floating point addition per step We can eliminate all fp through Bresenham’s algorithm Consider only 1  m  0 ­Other cases by symmetry Assume pixel centers are at half integers If we start at a pixel that has been written, there are only two candidates for the next pixel to be written into the frame buffer

44 Candidate Pixels 1  m  0 last pixel candidates Note that line could have passed through any part of this pixel

45 Decision Variable - d =  x(b-a) d is an integer d > 0 use upper pixel d < 0 use lower pixel

46 Incremental Form To compute d k+1 ­More efficient if we use d k, the value of the decision variable at x = x k a b

47 Incremental Form a b Constant (c )

48 Incremental Form a b Either 0 or 1 depending on the sign of dk

49 Incremental Form d k+1 = d k +2  y, if d k < 0 d k+1 = d k +2(  y-  x), otherwise For each x, we need do only an integer addition and a test

50 Bresenham’s Algorithm (1) Input the two line endpoints and store the left endpoint in(x1, y1) (2)set the color for posiiton(x1,y2) and plot the first point (3) calculate constants  y,  x, 2(  y-  x) (4)calculate d1 = 2  y-  x (5) At each xk, starting at k=1 do if dk < 0, plot (xk +1, yk) d k+1 = d k +2  y, Else plot(xk+1, yk+1) d k+1 = d k +2(  y-  x)

51 Example Endpoints (20,10) and (30,18)  x=10,  y=8, d1=6 2  y-2  x=-4, 2  y=16

52 Example 20222530 10 15 18 Endpoints (20,10) and (30,18)  x=10,  y=8, d1=6 2  y-2  x=-4, 2  y=16

53 20222530 10 15 18 (x1,y1)=(20,10) P1=6 Next pixel (x2,y2): (21,11) P2=6-4=2

54 20222530 10 15 18 (x2,y2)=(21,11) P2=2 Next pixel (x3,y3): (22,12) P3=2-4=-2

55 20222530 10 15 18 (x3,y3)=(22,12) P3=-2 Next pixel (x4,y4): (23,12) P4=-2+16=14

56 20222530 10 15 18 (x4,y4)=(23,12) P4=14 Next pixel (x5,y5): (24,13) P5=14-4=10

57 20222530 10 15 18

58 20222530 10 15 18

59 20222530 10 15 18

60 Aliasing Ideal rasterized line should be 1 pixel wide Choosing best y for each x (or visa versa) produces aliased raster lines

61 Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line originalantialiased magnified


Download ppt "Objectives Introduce basic implementation strategies Clipping Scan conversion."

Similar presentations


Ads by Google