1 / 83

From Vertices to Fragments

From Vertices to Fragments. Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com. Review. Shading in OpenGL; Lights & Material; From vertex to fragment: Cohen-Sutherland. Surface hidden Texture Transparency. Projection Fragments Clipping

andreapaul
Download Presentation

From Vertices to Fragments

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. From Vertices to Fragments Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com

  2. Review • Shading in OpenGL; • Lights & Material; • From vertex to fragment: • Cohen-Sutherland Surface hidden Texture Transparency Projection Fragments Clipping Shading Black box

  3. Objectives • Geometric Processing: Cyrus-Beck clipping algorithm Liang-Barsky clipping algorithm • Introduce clipping algorithm for polygons • Rasterization: DDA & Bresenham

  4. Cohen-Sutherland • In case IV: • o1 & o2 = 0 • Intersection: Clipping Lines by Solving Simultaneous Equations

  5. Solving Simultaneous Equations • Equation of a line: • Slope-intercept: y = mx + h difficult for vertical line • Implicit Equation: Ax + By + C = 0 • Parametric: Line defined by two points, P0 and P1 • P(t) = P0 + (P1 - P0) t • x(t) = x0 + (x1 - x0) t • y(t) = x0 + (y1 - y0) t

  6. Parametric Lines and Intersections • For L1 : • x=x0l1 + t(x1l1 – x0l1) • y=y0l1 + t(y1l1 – y0l1) • For L2 : • x=x0l2 + t(x1l2 – x0l2) • y=y0l2 + t(y1l2 – y0l2) • The Intersection Point: • x0l1 + t1 (x1l1 – x0l1)= x0l2 + t2 (x1l2 – x0l2) • y0l1 + t1(y1l1 – y0l1)= y0l2 + t2 (y1l2 – y0l2)

  7. 0≤t≤1 N P2 A is inside of R; ,then P1 Cyrus-Beck Algorithm • Cyrus-Beck algorithm (1978) for polygons Mike Cyrus, Jay Beck. "Generalized two- and three-dimensional clipping". Computers & Graphics, 1978: 23-28. • Given a convex polygon R: R is on R or extension; ,then is outside of R. ,then para te para ts How to get ts and te

  8. A Inside P(t) Outside NL Cyrus-Beck Algorithm P1 • Intersection: • NL● (P(t) – A) = 0 • Substitute line equation for P(t) P(t) = P0 + t(P1 - P0) • Solve for t t = NL● (P0 – A) / -NL●(P1 - P0) P0

  9. Cyrus-Beck Algorithm • Compute t for line intersection with all edges; • Discard all (t < 0) and (t > 1); • Classify each remaining intersection as • Potentially Entering Point (PE) • Potentially Leaving Point (PL) (How?) NL●(P1 - P0) < 0 implies PL NL●(P1 - P0) > 0 implies PE Note that we computed this term in when computing t

  10. L3 L2 P1 L1 L4 L5 P0 Cyrus-Beck Algorithm • For each edge: t3 t4 t1 t2 para te para ts t5 Compute PE with largest t Compute PL with smallest t Clip to these two points

  11. Cyrus-Beck Algorithm • When ; then • if • if Then P0P1 is invisible; Then go to next edge;

  12. Programming: for(kedges of clipping polygon) { solve Ni·(p1-p0); solve Ni·(p0-Ai); if (Ni·(p1-p0)= = 0 ) //parallel with the edge { if ( Ni·(p0-Ai) < 0 ) break; //invisible else go to next edge; } else // Ni·(p1-p0) != 0 { solve ti; if ( Ni·(p1-p0) < 0 ) else } } • Input: • If (P0 = P1 ) • Line is degenerate so clip as a point; • Output: • if ( ts > te ) return nil; • else • return P(ts) and P(te) as the true clip intersections;

  13. Liang-Barsky Algorithm (1984) The ONLY algorithm named for Chinese people in Computer Graphics course books Liang, Y.D., and Barsky, B., "A New Concept and Method for Line Clipping", ACM Transactions on Graphics, 3(1):1-22, January 1984.

  14. P1 PL PL PE PE P0 Liang-Barsky Algorithm (1984) • Because of horizontal and vertical clip lines: Many computations reduce • Normals • Pick constant points on edges • solution for t: tL=-(x0 - xleft) / (x1 - x0) tR=(x0 - xright) / -(x1 - x0) tB=-(y0 - ybottom) / (y1 - y0) tT=(y0 - ytop) / -(y1 - y0) (0, -1) (-1, 0) (1, 0) (0, 1)

  15. Liang-Barsky Algorithm (1984)

  16. Liang-Barsky Algorithm (1984) • When rk<0, tk is entering point; when rk>0, tk is leaving point. If rk=0 and sk<0, then the line is invisible; else process other edges Let ∆x=x2-x1,∆y=y2-y1:

  17. Comparison • Cohen-Sutherland: • Repeated clipping is expensive • Best used when trivial acceptance and rejection is possible for most lines • Cyrus-Beck: • Computation of t-intersections is cheap • Computation of (x,y) clip points is only done once • Algorithm doesn’t consider trivial accepts/rejects • Best when many lines must be clipped • Liang-Barsky: Optimized Cyrus-Beck • Nicholl et al.: Fastest, but doesn’t do 3D

  18. 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

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

  20. Clipping and Normalization • General clipping in 3D requires intersection of line segments against arbitrary plane • Example: oblique view

  21. Plane-Line Intersections

  22. Point-to-Plane Test • Dot product is relatively expensive • 3 multiplies • 5 additions • 1 comparison (to 0, in this case) • Think about how you might optimize or special-case this?

  23. Normalized Form top view before normalization after normalization Normalization is part of viewing (pre clipping) but after normalization, we clip against sides of right parallelepiped Typical intersection calculation now requires only a floating point subtraction, e.g. is x > xmax

  24. Clipping Polygons • Clipping polygons is more complex than clipping the individual lines • Input: polygon • Output: polygon, or nothing

  25. 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

  26. Tessellation and Convexity • One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation) • Also makes fill easier (we will study later) • Tessellation code in GLU library, the best is Constrained Delaunay Triangulation

  27. Pipeline Clipping of Polygons • Three dimensions: add front and back clippers • Strategy used in SGI Geometry Engine • Small increase in latency

  28. Sutherland-Hodgman Clipping Ivan Sutherland, Gary W. Hodgman: Reentrant Polygon Clipping. Communications of the ACM, vol. 17, pp. 32-42, 1974 • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation

  29. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  30. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  31. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  32. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  33. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  34. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  35. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  36. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped

  37. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped • Will this work for non-rectangular clip regions? • What would 3-D clipping involve?

  38. Sutherland-Hodgman Clipping • Input/output for algorithm: • Input: list of polygon vertices in order • Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe) • Note: this is exactly what we expect from the clipping operation against each edge

  39. Sutherland-Hodgman Clipping • Sutherland-Hodgman basic routine: • Go around polygon one vertex at a time • Current vertex has position p • Previous vertex had position s, and it has been added to the output if appropriate

  40. inside outside inside outside inside outside inside outside p s p s p s p s p output i output no output i outputp output Sutherland-Hodgman Clipping • Edge from s to ptakes one of four cases: (Gray line can be a line or a plane)

  41. Sutherland-Hodgman Clipping • Four cases: • s inside plane and p inside plane • Add p to output • Note: s has already been added • s inside plane and p outside plane • Find intersection point i • Add i to output • s outside plane and poutside plane • Add nothing • soutside plane and p inside plane • Find intersection point i • Add i to output, followed by p

  42. Sutherland-Hodgman Clipping

  43. q n p P Point-to-Plane test • A very general test to determine if a point p is “inside” a plane P, defined by q and n: (p - q) • n < 0: p inside P (p - q) • n = 0: p on P (p - q) • n > 0: p outside P q q n n p p P P

  44. 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

  45. Bounding Boxes Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping Ellipsoid collision detection

  46. Rasterization • Rasterization (scan conversion) • Determine which pixels that are inside primitive specified by a set of vertices • Produces a set of fragments • Fragments have a location (pixel location) and other attributes such color, depth and texture coordinates that are determined by interpolating values at vertices • Pixel colors determined later using color, texture, and other vertex properties.

  47. 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

  48. Scan Conversion of Line Segments One pixel

  49. DDA Algorithm • Digital Differential Analyzer (1964) • 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; //note:m is float number write_pixel(x, round(y), line_color); }

  50. Problem • DDA = for each x plot pixel at closest y • Problems for steep lines

More Related