830 likes | 845 Views
Explore shading, lighting, surface texture, and geometric processing algorithms in OpenGL. Learn about clipping methods like Cyrus-Beck and Liang-Barsky, fall in vertices, compute t-intersections, and classify clipping points. Discover the efficiency of various algorithms and their applicability in 3D graphics. Cyrus-Beck and Liang-Barsky provide different advantages in terms of computation complexity and line segment clipping. Gain insights into pipeline clipping and normalization techniques. Improve your graphics processing skills with this comprehensive course.
E N D
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 Shading Black box
Objectives • Geometric Processing: Cyrus-Beck clipping algorithm Liang-Barsky clipping algorithm • Introduce clipping algorithm for polygons • Rasterization: DDA & Bresenham
Cohen-Sutherland • In case IV: • o1 & o2 = 0 • Intersection: Clipping Lines by Solving Simultaneous Equations
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
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)
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
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
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
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
Cyrus-Beck Algorithm • When ; then • if • if Then P0P1 is invisible; Then go to next edge;
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;
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.
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)
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:
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
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
Pipeline Clipping of Line Segments • Clipping against each side of window is independent of other sides • Can use four independent clippers in a pipeline
Clipping and Normalization • General clipping in 3D requires intersection of line segments against arbitrary plane • Example: oblique view
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?
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
Clipping Polygons • Clipping polygons is more complex than clipping the individual lines • Input: polygon • Output: polygon, or nothing
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
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
Pipeline Clipping of Polygons • Three dimensions: add front and back clippers • Strategy used in SGI Geometry Engine • Small increase in latency
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
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
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
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
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
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
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
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
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
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?
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
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
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)
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
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
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
Bounding Boxes Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping Ellipsoid collision detection
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.
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
Scan Conversion of Line Segments One pixel
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); }
Problem • DDA = for each x plot pixel at closest y • Problems for steep lines