1 / 61

Objectives

Objectives. Introduce basic implementation strategies Clipping Scan conversion. 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

azriel
Download Presentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 3D WORLD Modeling transformations Viewing transformation 3D World Projection transformation 3D camera Clipping 3D clip 3D clip “Normalized View Volume” Perspective division 3D NDC NDC to viewport (window coordinates) 2D window (with depth)

  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 y Z=-far +1 y Z=-near viewer z z -1 -1(near) +1 (far) Viewing frustum Normalized view volume

  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 y = ymax x = xmin x = xmax y = ymin

  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 y = ymax x = xmin x = xmax y = ymin

  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 y = ymax x = xmin x = xmax

  14. Defining Outcodes • For each endpoint, define an outcode • Outcodes divide space into 9 regions • Computation of outcode requires at most 4 subtractions b0b1b2b3 b0 = 1 if y > ymax, 0 otherwise b1 = 1 if y < ymin, 0 otherwise b2 = 1 if x > xmax, 0 otherwise b3 = 1 if x < xmin, 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 a where the line determined by the line segment crosses the lines that determine the window p(a) = (1-a)p1+ ap2 1  a  0 p2 p1

  22. Liang-Barsky Clipping • In (a): a4 > a3 > a2 > a1 • Intersect right, top, left, bottom: shorten • In (b): a4 > a2 > a3 > a1 • 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 a, 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. Q R Output P, Q Output R P Output S S (no output) 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

  35. 2 A B 5 C D 1 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 inside 1 D 2 5 C 7 B A 6 4 3

  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 = Dy/Dx = y2-y1/x2-x1 • Along scan line Dx = 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 candidates last pixel Note that line could have passed through any part of this pixel

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

  46. a b Incremental Form • To compute dk+1 • More efficient if we use dk, the value of the decision variable at x = xk

  47. a b Incremental Form Constant (c )

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

  49. Incremental Form dk+1= dk +2Dy, if dk < 0 dk+1= dk +2(Dy- Dx), 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 Dy, Dx, 2(Dy-Dx) • (4)calculate d1 = 2Dy-Dx • (5) At each xk, starting at k=1 do if dk < 0, plot (xk +1, yk) dk+1= dk +2Dy, Else plot(xk+1, yk+1) dk+1= dk +2(Dy- Dx)

More Related